错误计算行数和运算符的 C 程序

标签 c

这个程序应该是读取行和计数运算符,但是计数错误,无法找出错误所在。

帮我找出错误并修复程序,以便能够正确地计算行数和运算符。我已经尝试了几种方法来修复它,但它仍然计数错误。

当前输出:Broqt na operatorite e:1 Broqt na redovete e:1119

预期输出:Broqt na operatorite e:11 Broqt na redovete e:221

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
 #include<stdio.h>
 #include<stdlib.h>
 #include<string.h>
 #include<ctype.h>

void cycleOperatorsCounter(FILE* inputStream, FILE* outputStream);
void counter(FILE* inputStream, FILE* outputStream);
int fileToFile(void);
int fileToScreen(void);
int screenToFile(void);
int screenToScreen(void);
void getFileName(char* fileName, int mode);

int menu() {
int i;


printf("----1. FILE TO FILE                  \n");
printf("----2. FILE TO SCREEN                \n");
printf("----3. KBRD TO FILE                  \n");
printf("----4. KBRD TO SCREEN                \n");
printf("----0. EXIT                          \n");
    do {
    printf("SELECT OPTION: ");
    fflush(stdin);
    scanf("%i", &i);
} while (i < 0 || i> 4);

return i;
}

int main(void)  {

while (1) {
    system("cls");
    switch (menu()) {
    case 1: fileToFile();
        break;
    case 2: fileToScreen();
        break;
    case 3: screenToFile();
        break;
    case 4: screenToScreen();
        break;
    default:
        return 0;
    }
    system("pause");
}
 }

 void getFileName(char* fileName, int mode) {
  while (1) {
    fflush(stdin);
    if (mode == 1) {
        printf("Input file name(.C): ");
        gets(fileName);

        if (fileName[strlen(fileName) - 2] == '.' && toupper(fileName[strlen(fileName) - 1]) == 'C') {
            return;
        }
    }
    else {
        printf("Output File: ");
        gets(fileName);
        return;
    }
}
 }

int fileToFile(void) {
char inputFileName[256], outputFileName[256];
FILE *inputStream, *outputStream;

getFileName(inputFileName, 1);
if (!(inputStream = fopen(inputFileName, "r"))) {
    fprintf(stderr, "Error opening file!\n");
    return -1;
     }

getFileName(outputFileName, 2);
if (!(outputStream = fopen(outputFileName, "w"))) {
    fprintf(stderr, "Error opening file!\a\n");
    return -1;
 }

cycleOperatorsCounter(inputStream, outputStream);
rewind(inputStream);
counter(inputStream, outputStream);
fclose(inputStream);
fclose(outputStream);

printf("Results saved to \"%s\".\n", outputFileName);
return 0;
}

int fileToScreen(void) {
char inputFileName[256];
FILE* inputStream;

getFileName(inputFileName, 1);
if (!(inputStream = fopen(inputFileName, "r"))) {
    fprintf(stderr, "Error opening file!\n");
    return -1;
}

cycleOperatorsCounter(inputStream, stdout);
rewind(inputStream);
counter(inputStream, stdout);
fclose(inputStream);

return 0;
   }

int screenToFile(void) {
char outputFileName[256];
FILE *outputStream, *tempStream;
char str[999];

tempStream = fopen("temp.tmp", "w");
fflush(stdin);
printf("Napishete \"KRAI\" na nov red, kogato vuvedete teksta\n");
while (1) {
    gets(str);
    if (!strcmp(str, "KRAI")) {
        fclose(tempStream);
        tempStream = fopen("temp.tmp", "r");
        break;
    }
    fprintf(tempStream, "%s\n", str);
}

getFileName(outputFileName, 2);
if (!(outputStream = fopen(outputFileName, "w"))) {
    fprintf(stderr, "Error opening file!\a\n");
    return -1;
}

cycleOperatorsCounter(tempStream, outputStream);
rewind(tempStream);
counter(tempStream, outputStream);
fclose(tempStream);
fclose(outputStream);

printf("Results saved to \"%s\".\n", outputFileName);
return 0;
}

int screenToScreen(void) {
FILE *tempStream;
char str[999];

tempStream = fopen("temp.tmp", "w");
fflush(stdin);
printf("Napishete \"KRAI\" na nov red, kogato vuvedete teksta\n");
while (1) {
    gets(str);
    if (!strcmp(str, "KRAI")) {
        fclose(tempStream);
        tempStream = fopen("temp.tmp", "r");
        break;
    }
    fprintf(tempStream, "%s\n", str);
}

cycleOperatorsCounter(tempStream, stdout);
rewind(tempStream);
counter(tempStream, stdout);
fclose(tempStream);

return 0;
 }

 void cycleOperatorsCounter(FILE* inputStream, FILE* outputStream) {
char str[1000];
int cycleCounter = 0;
unsigned i;

while (fgets(str, sizeof(str), inputStream) != NULL) {
    for (i = 0; i < strlen(str); i++) {
if ((str[i-1] == ' ' || str[i-1] == '\n' || str[i-1] == '\t' || i==0) && 
(str[i] == 'i') && (str[i + 1] == 'f') && (str[i+3] == ' ' || str[i+3] == 
'\n' || str[i+3] == '\t')) {
            cycleCounter++;
        }
        if ((str[i-1] == ' ' || str[i-1] == '\n' || str[i-1] == '\t' || 
     i==0)
            && (str[i] == 'e') && (str[i + 1] == 'l') && (str[i + 2] == 's') 
&& (str[i + 3] == 'e')
                && (str[i+5] == ' ' || str[i+6] == '\n' || str[i+7] == 
'\t')) {
            cycleCounter++;
        }
    }
}
  fprintf(outputStream, "Broqt na operatorite za cikul e: %d\n", 
 cycleCounter);

 }

void counter(FILE* inputStream, FILE* outputStream) {
char str[1000];
int Counter = 0;
unsigned i;

while (fgets(str, sizeof(str), inputStream) != NULL) {
        for (i = 0; i < strlen(str); i++) {
     {
if ((str[i-1] == ' ' || str[i-1] == '\n' || str[i-1] == '\t' || i==0))
{
Counter++;
}
        }

    }
 }
fprintf(outputStream, "Broqt na redovete e: %d\n", Counter);
}

最佳答案

这是我的热门评论。

也就是找到ifelse使用 strtok 更容易完成和 strcmp .

并且,要计算行数,只需执行 fgets和计数或做fgetc并计算 \n将工作。

希望这能让你走得更远:

void
cycleOperatorsCounter(FILE *inputStream, FILE *outputStream)
{
    char *bp;
    char *tok;
    char str[1000];
    int cycleCounter = 0;

    while (fgets(str, sizeof(str), inputStream) != NULL) {
        bp = str;
        while (1) {
            tok = strtok(bp," \t\n");
            if (tok == NULL)
                break;
            bp = NULL;

            if (strcmp(tok,"if") == 0) {
                cycleCounter++;
                continue;
            }

            if (strcmp(tok,"else") == 0) {
                cycleCounter++;
                continue;
            }
        }
    }

    fprintf(outputStream, "Broqt na operatorite za cikul e: %d\n",
        cycleCounter);

}

void
counter(FILE *inputStream, FILE *outputStream)
{
    int Counter = 0;

    // count number of lines
    // NOTE: either of these should work:

#if 1
    char str[1000];
    while (fgets(str, sizeof(str), inputStream) != NULL)
        ++Counter;
#else
    while (1) {
        int chr = fgetc(inputStream);
        if (chr == EOF)
            break;
        if (chr == '\n')
            ++Counter;
    }
#endif

    fprintf(outputStream, "Broqt na redovete e: %d\n", Counter);
}

关于错误计算行数和运算符的 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54065187/

相关文章:

C 位的数字总和的位置次方

c - 声明一个 C 函数以返回一个数组

c - 访问结构或 union 中的填充是否安全?

-std=c99 可以阻止我的#includes 正常工作吗?

c - 如何在 C 中创建到空格字符的 #define 映射?

c - 通常 gets() 将输入视为\n,位于其上方 printf() 的末尾

c - 通过函数分配矩阵内存

c - 将两个不同的动态分配数组传递给 C 中的函数

C 结构体分配给结构体数组

c - 对 _kill、_getpid 和 _sbrk 的 undefined reference