用于检查文件内容的程序。有更好的办法吗?
我创建了一个非常基本的“调试”程序,用于检查 ac 源文件是否具有相同数量的左右大括号、方括号和圆括号。我有一个相当简单的代码并且它可以工作,但代码似乎不必要地长。我正在考虑使用数组代替。一个数组用于存储每个 {,[,( 另一个用于存储 },],),然后计算每个实例并比较数量。但我认为代码几乎一样长。你们觉得怎么样?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char fname[20];
char c;
int curlybracket = 0;
int curlybracketr = 0;
int squarebracket = 0;
int squarebracketr = 0;
int parentheses = 0;
int parenthesesr = 0;
printf("Please enter the destination of the file: \n");
scanf("%s", fname);
fp = fopen(fname, "r");
if (fp == NULL)
{
printf("Problem opening file!\n");
exit(0);
}
else
{
printf("File opened correctly\n");
}
while (c != EOF)
{
c = getc(fp);
if (c == '{')
{
curlybracket++;
}
if (c == '[')
{
squarebracket++;
}
if (c == '(')
{
parentheses++;
}
if (c == '}')
{
curlybracketr++;
}
if (c == ']')
{
squarebracketr++;
}
if (c == ')')
{
parenthesesr++;
}
}
if (curlybracket == curlybracketr)
{
printf("There are an equal number of curlybrackets\n");
}
else
{
printf("There is an unequal number of curlybrackets\n");
return 0;
}
if (squarebracket == squarebracketr)
{
printf("There are an equal number of squarebrackets\n");
}
else
{
printf("There are an unequal number of squarebrackets\n");
}
if (parentheses == parenthesesr)
{
printf("There are an equal number of parentheses\n");
}
else
{
printf("There are an unequal number of parentheses\n");
}
return 0;
}
I've created a very basic 'debugging' program that checks if a c source file has the same number of opening and closing curly brackets, square brackets and parentheses. I have a code that's fairly simple and it works but the code seems unnecessarily long. I was considering using arrays instead. An array to store each {,[,( and another to store },],) then counting the instance of each and comparing the amounts. But I think that code would be almost as long. What do you guys think?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char fname[20];
char c;
int curlybracket = 0;
int curlybracketr = 0;
int squarebracket = 0;
int squarebracketr = 0;
int parentheses = 0;
int parenthesesr = 0;
printf("Please enter the destination of the file: \n");
scanf("%s", fname);
fp = fopen(fname, "r");
if (fp == NULL)
{
printf("Problem opening file!\n");
exit(0);
}
else
{
printf("File opened correctly\n");
}
while (c != EOF)
{
c = getc(fp);
if (c == '{')
{
curlybracket++;
}
if (c == '[')
{
squarebracket++;
}
if (c == '(')
{
parentheses++;
}
if (c == '}')
{
curlybracketr++;
}
if (c == ']')
{
squarebracketr++;
}
if (c == ')')
{
parenthesesr++;
}
}
if (curlybracket == curlybracketr)
{
printf("There are an equal number of curlybrackets\n");
}
else
{
printf("There is an unequal number of curlybrackets\n");
return 0;
}
if (squarebracket == squarebracketr)
{
printf("There are an equal number of squarebrackets\n");
}
else
{
printf("There are an unequal number of squarebrackets\n");
}
if (parentheses == parenthesesr)
{
printf("There are an equal number of parentheses\n");
}
else
{
printf("There are an unequal number of parentheses\n");
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果源文件是类似“([)]”的,你的程序不会报告任何错误,这实际上是非法的。
更好的解决方案是使用 堆栈,这是后进先出的-out 数据结构。维基百科页面的此部分说明了用法。
当您从文件中读取打开符号时,将其压入堆栈。如果它是结束符号,则弹出堆栈。如果弹出的符号不是对应的开盘符号,则报不平衡错误。
在文件末尾,如果堆栈为空,则文件中的符号是平衡的。
这是我所知道的测试符号是否平衡的最常见方法。
Your program will report no error if the source file is like "([)]", which is actually illegal.
A better solution is to use a stack, which is a last-in-first-out data structure. This section from the wikipedia page illustrates the usage.
When you read an opening symbol from the file, push it onto the stack. If it's a closing symbol, pop the stack. If the symbol popped is not the corresponding opening symbol, report unbalanced error.
At the end of file, if the stack is empty, the symbols in the file are balanced.
This is the most common way that I know to test whether the symbols are balanced.
使用
switch
语句获取与c
的比较列表。如果您希望代码更加简洁,请使用包含 256 个int
值的单个数组来存储每个字符的出现次数,并比较{
和处的数组值>}
。Use the
switch
statement for the list of comparisons withc
. If you want your code to be even more concise, use a single array of 256int
values to store the occurrence of each character and compare the array values at{
and}
.确实,可以使用数组以更短的方式重写程序。它可能看起来像:
但正如 @lbs 提到的那样,它很容易出错,使用 @lbs 方法要好得多!
True, program can be re-written in more shorter way by using arrays. It could look something like:
But it is error-prone as @lbs mentioned, it's far more better to use @lbs approach !
计算字符串中字符的出现次数
这是解决问题的另一种方法
Count character occurrences in a string
Yust another way to solve the problem