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

时间:2019-01-06 19:35:58

标签: 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);
}

1 个答案:

答案 0 :(得分:0)

这是我的主要评论开头。

也就是说,使用ifelse更容易找到strtokstrcmp

要计数行/行,只需执行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);
}