Я пытаюсь написать программу, которая сканирует текстовый файл и подсчитывает количество открытых и закрытых скобок для сравнения. Однако до сих пор ive удалось вычислить первое появление скобки в строке.Подсчет количества скобок в файле
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fileName;
fileName = fopen("/home/vagrant/Desktop/tests/t5", "r");
if (fileName != NULL)
{
int noOfOpenPar=0;
int noOfClosePar=0;
int noOfOpenSqr=0;
int noOfCloseSqr=0;
int noOfOpenCurl=0;
int noOfCloseCurl=0;
char lineOfText[80];
while (fgets(lineOfText, 80, fileName) != NULL)
{
if (strstr(lineOfText, "(") != NULL)
noOfOpenPar++;
if (strstr(lineOfText, ")") != NULL)
noOfClosePar++;
if (strstr(lineOfText, "[") != NULL)
noOfOpenSqr++;
if (strstr(lineOfText, "]") != NULL)
noOfCloseSqr++;
if (strstr(lineOfText, "{") != NULL)
noOfOpenCurl++;
if (strstr(lineOfText, "}") != NULL)
noOfCloseCurl++;
}
printf ("\nThe number of open parenthesies is %d and closed parenthesies %d\n", noOfOpenPar, noOfClosePar);
if (noOfOpenPar!=noOfClosePar)
printf("Number of open and closed parenthesies doesn't match\n");
printf ("\nThe number of open square brackets is %d and closed square brackets is %d\n", noOfOpenSqr, noOfCloseSqr);
if (noOfOpenSqr!=noOfCloseSqr)
printf("Number of open and closed square brackets doesn't match\n");
printf ("\nThe number of open curly brackets is %d and closed curly brackets is %d\n", noOfOpenCurl, noOfCloseCurl);
if (noOfOpenCurl!=noOfCloseCurl)
printf("Number of open and closed curly brackets doesn't match\n");
}
}
Вам нужно больше петель. Или прочитать символ файла по символу. –
Вы не знаете, совпадает ли он с подсчетом. Например, ') (', '([)]' и т. Д. – BLUEPIXY