gpt4 book ai didi

c - 如何在c语言的文本输入文件中找到匹配括号或大括号的位置?

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

我正在用 C 编写一个程序..该程序打开一个包含类似 C 源代码的纯文本文件,读取它,并输出一个与第一个内容相同的文件,但所有注释都被删除。该程序必须检查所有括号是否匹配,如果不匹配,程序应该显示一条错误消息,显示错误类型和遇到此错误的行号。(我显示了一条错误消息,但如何找到错误位置。 .?) 输入和输出文件被传递给程序a####nd行参数,如下所示:./your_executable 输入文件.txt 输出文件.txt

这是我编写的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Functions */
void check_comment (char) ; // checks for both types of comments, then passes on control to below comments
void block_comment () ; // handles block or multiline comments
void single_comment () ; // handles single line comments

/* 2 file pointers - 1st is for the file in which we check for comments,
and 2nd is the file in which we copy the code after removing comments */
FILE *fp , *fp2;

int main(void)
{
char c;

fp = fopen ("inputfile.txt","r") ; // open the first file in read mode
fp2 = fopen ("outputfile.txt","w") ; // open the second file in write mode
while((c=fgetc(fp))!=EOF) // read the file character by character
check_comment(c); // check for each character if it seems like the beginning of a comment

// close both the files at the end of the program
fclose(fp);
fclose(fp2);

FILE *fp;
char fname[20];
char brackets[20] = "{}[]()";
int bracketCounts[6] = {0};
char * found;
int i;

printf("Please enter the destination of the file: \n");
scanf("%s", fname);

if ((fp = fopen(fname, "r")) == NULL){
printf("Problem opening file!\n");
return 0x00;
}

printf("File opened correctly\n");

// counting various parentheses
while ((c = getc(fp)) != EOF){
found = strchr(brackets, c);
if (found != NULL) {
bracketCounts[found - brackets]++;
}
}

// dont't forget to close file after reading is done
fclose(fp);

// checking parentheses counters
for (i=0; i < 6; i+=2) {
if (bracketCounts[i] != bracketCounts[i+1]) {
printf("Unbalanced parentheses !\n");
return 0x00;
}
}

printf("All parentheses are OK!\n");

return 0;
}

// function that handles both types of comments
void check_comment(char c)
{
char d;

if( c == '/') // if the character starts with '/', it 'could' be a comment
{
if((d=fgetc(fp))=='*') // if the next character we read is '*', it is the beginning of multiblock comment
block_comment(); // pass control to function that handles multiblock comments

else if( d == '/') // else if the next character we read is '/', it is the beginning of single line comment
{
single_comment();// pass control to function that handles single line comment

}
else
{
// if both the cases fail, it is not a comment, so we add the character as it is in the new file.
fputc(c,fp2);
fputc(d,fp2);
}
}

// again, if all above fails, we add the character as it is in the new file.
else
fputc(c,fp2);
}


// function that handles block comments
void block_comment()
{

char d,e;

while((d=fgetc(fp))!=EOF) // the block comment has started, read the character by character
{
/* keep reading the characters and do nothing,
as they do not have to be copied into the new file (we are removing the comments)
*/
if(d=='*') // if the comment 'seems' like ending
{
e=fgetc(fp); // check if it actually ends (block comments end with '*/')

if(e=='/') // if the comment 'has' ended, return from the function
return;
}
}

}

// function that handles single line comments
void single_comment()
{
char d,e;

while((d=fgetc(fp))!=EOF) // the single line comment has started, read the character by character
{
/* keep reading the characters and do nothing,
as they do not have to be copied into the new file (we are removing the comments)
*/
if(d=='\n') // check if the comment ends (single comments end with '\n', or newline)
return; // if the comment 'has' ended, return from the function

}

}

最佳答案

您可以在读取输入文件时实现或使用堆栈数据结构,如果您得到 { , ( 或 [ 压入堆栈,如果您得到 }, ) 或 ] 弹出堆栈,输入文件堆栈末尾应该为空,然后您将获得正确的匹配,否则会发生一些不匹配。

与括号一起,您可以保留行号(或位置等)

例如:1 (
2(某事)
3 东西)

push (, line1,然后push (, line2,当你得到 ) pop (, line2 依此类推,在这种情况下,如果您没有得到第二个结束 ),您可以说 (, line1 缺少关闭。

关于c - 如何在c语言的文本输入文件中找到匹配括号或大括号的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53819928/

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