gpt4 book ai didi

从另一个程序调用/实现函数

转载 作者:行者123 更新时间:2023-11-30 19:20:39 24 4
gpt4 key购买 nike

我长期以来一直试图解决这个问题,我发现我最困惑的部分是我应该使用已经在另一个程序中定义和创建的函数的部分,但是我应该将一个程序原封不动地附加到我的新程序中(抱歉,如果这没有意义)。但这里的程序已经定义了我们应该在程序中使用的函数,未更改:

#ifndef COMMON_H
#define COMMON_H
#include <stdlib.h>

/* constants: number of different characters, and
first and last printable characters */
#define NUM 128
#define FIRST '!'
#define LAST '~'

/* symbols for special characters, corresponding to codes 0 through FIRST-1 */ char *symbols[] = {"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK",
"BEL", "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", "DLE", "DC1",
"DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC",
"FS", "GS", "RS", "US", "SPC" };

/* symbol for DEL character, code LAST+1 (same as NUM-1) */ char
*symbolDel = "DEL";

/* the following four functions must be used to print results */

/* use prHeader at the start to print header row (titles) */ void prHeader(FILE *out) {
fprintf(out, "Code\tChar\tCount\n----\t----\t-----\n"); }

/* use prCountStr to print count for one of the special symbols */ void prCountStr(FILE *out, int code, char *str, int count) {
fprintf(out, "%3d\t%s\t%5d\n", code, str, count); }

/* use prCountChr to print count for one of the printable characters
*/ void prCountChr(FILE *out, int code, char chr, int count) {
fprintf(out, "%3d\t%c\t%5d\n", code, chr, count); }

/* use prTotal at the end to print total character count */ void prTotal(FILE *out, int count) {
fprintf(out, "\t\t-----\nTotal\t\t%5d\n", count); }

/* use the following three macros to print error messages for part 2 {
Beware: each macro executes two statements. } ignore these macros for part 1 */

/* use BADFILE(name) to exit if a file (name) cannot be opened */
#define BADFILE(name) fprintf(stderr, "bad file: %s\n", (name)); \
exit(1);

/* use BADOPTION(op) if an invalid option (not '-o') is on command line */
#define BADOPTION(op) fprintf(stderr, "bad option: %s\n", (op)); \
exit(2);

/* use MISSING (without parens) if output filename is missing */
#define MISSING fprintf(stderr, "missing output file\n"); \
exit(3);


#endif

使用这些定义,我应该在收到输入后打印一个表格,显示字符、ASCII 表示以及该字符出现的次数,如下例所示:

Code    Char    Count
---- ---- -----
10 LF 3
32 SPC 35
40 ( 1
41 ) 1
45 - 1
46 . 4
58 : 1
68 D 1
72 H 2
73 I 4
79 O 1
84 T 1

编辑代码:

#include <stdio.h>
#include "common.h"
int main(int argc, char *argv[]) {
FILE *in = stdin;
FILE *out = stdout;
int x;
int count1 = 0;
int count2 = 0;
int asciicount[256] = {0};

do
{
x = getchar();
asciicount[x]++;
count1++;
} while (x != EOF);

for (count2; count2<=255; count2++)
{
if (count2<33)
{
prCountStr(out, count2, symbols[count2], asciicount[count2]);
}
else
{
prCountChr(out,count2,x,asciicount[count2]);
}
}
prTotal(out, count1);
prHeader(out);
return 0;
}
/* do
{
if (count2 < '!')
{
prCountStr(out, count2, symbols[count2], asciicount[count2]);
}
else{
prCountChr(out,count2,x,asciicount[count2]);
}
count2++;
} while (count2 <= 255);
prTotal(out, count1);

prHeader(out);
return 0;
}*/

第二部分是我之前的内容,但我正在注释掉并尝试 for 循环以查看是否可以解决问题。这是新的输出,它一直打印到 255,而不计算每个出现的次数或打印 ascii 代码。

 29 GS      0
30 RS 0
31 US 0
32 SPC 0
33 ? 0
34 ? 0
35 ? 0
36 ? 0
37 ? 0
38 ? 0
39 ? 0
40 ? 0
41 ? 0
42 ? 0

最佳答案

你的想法是对的。但是您的代码中有几个语法错误。

  • 您需要#include <stdio.h>因为FILE、printf等的定义都在那里。

  • x应该是 char ,所以你应该这样定义它。我看到你评论了int x; 。你应该有char x;相反。

  • do-while 的正确语法循环是(注意 while 末尾的分号):

    do {
    //statements
    } while (condition);
  • r没有定义。

  • 因为你的if语句以 ; 结尾,prCountStr语句将始终被执行,并且您将在 else 上收到语法错误。就在之后。

  • 您只需在函数的定义中包含变量的类型( intchar 等),而不是在函数的调用中包含变量的类型。例如,调用 prCountStr 的正确方法将会是

    prCountStr(out, someInt, somePointerToChar, someInt);
  • 您需要为函数提供其定义所需的尽可能多的参数。这适用于您调用prCountChr的电话和prTotal ,您经过的地方void作为参数,但它们需要 FILE *out, int code, char chr, int countFILE *out, int count分别。

我在我原来的答案中包含了对您使用的算法的快速分析和建议的解决方案,但意识到这不是您所要求的。如果您需要任何进一步的说明,请告诉我

关于从另一个程序调用/实现函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21875040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com