C语言中判断对错的符号符号

编写一个程序,查找C语言中的基本语法错误,如圆括号、方括号、花括号不配对等,正确处理引号与注释 - 博客频道 - CSDN.NET
分类:C语言
#include&stdio.h&
int brace, brack,
void in_quote(int c);
void in_comment(void);
void search(int c);
&& &extern int brace, brack,&& //声明全局变量
&& &while((c = getchar()) != EOF){
&& &&& &if(c == '/'){
&& &&& &&& &if((c = getchar()) == '*')//遇到注释
&& &&& &&& &&& &in_comment();&&& //去掉注释
&& &&& &&& &else
&& &&& &&& &&& &search(c);&& // &
&& &&& &}else if(c == '\'' || c == '&')//遇到单引号或双引号
&& &&& &&& &in_quote(c);
&& &&& &else
&& &&& &&& &search(c);
&& &&& &if(brace & 0){&&&& //先有右括号的情况
&& &&& &&& &printf(&Unbalanced braces\n&);
&& &&& &&& &brace = 0;
&& &&& &}else if(brack & 0){
&& &&& &&& &printf(&Unbalanced brackets\n&);
&& &&& &&& &brack = 0;
&& &&& &}else if(paren & 0){
&& &&& &&& &printf(&Unbalanced parentheses\n&);
&& &&& &&& &paren = 0;
&& &if(brace & 0)&& //输出完少了右括号的情况
&& &&& &printf(&Unbalanced braces\n&);
&& &if(brack & 0)
&& &&& &printf(&Unbalanced brackets\n&);
&& &if(paren & 0)
&& &&& &printf(&Unbalanced parentheses\n&);
void search(int c)
&& &extern int brace, brack,
&& &if(c == '{')
&& &&& &++
&& &else if(c == '}')
&& &&& &--
&& &else if(c == '[')
&& &&& &++
&& &else if(c == ']')
&& &&& &--
&& &else if(c == '(')
&& &&& &++
&& &else if(c == ')')
&& &&& &--
void in_comment(void)
&& &int c,
&& &c = getchar();
&& &d = getchar();
&& &while(c != '*' || d != '/'){
&& &&& &c =
&& &&& &d = getchar();
void in_quote(int c)&&&&& //对引号之间的字符不进行左右括号的判断
&& &while((d = getchar()) != c)
&& &&& &if(d == '\\')
&& &&& &&& &getchar();&&&&&& //将\后的一个字符忽略掉& 不进行上面的while里面的判断 防止是引号继而跳出这个while循环
排名:千里之外
(13)(2)(3)(2)3470人阅读
C/C++(66)
/******************************************************
&& KnR 1-24
&& --------
&& Write a program to check the syntax of a C program
&& for matching {} () && '' []
******************************************************/
#include &stdio.h&
#define MAXLINE 1000 /* max input line size */
char line[MAXLINE]; /*current input line*/
int getline(void);& /* taken from the KnR book. */
int main()
& int len=0;//输入的长度
& int t=0;
& int brace=0;//方括号的个数,'['加1,']'减1
& int bracket=0;//大括号的个数,'['加1,']'减1
& int parenthesis=0;//小括号的个数,'['加1,']'减1
& int s_quote=1;//单引号奇偶标志
& int d_quote=1;//双引号奇偶标志
& while ((len = getline()) & 0 )
&&&&& t=0;
&&&&& while(t & len)
&&&&&&&&& if( line[t] == '[')
&&&&&&&&&&& {
&&&&&&&&&&&&& brace++;
&&&&&&&&&&& }
&&&&&&&&& if( line[t] == ']')
&&&&&&&&&&& {
&&&&&&&&&&&&& brace--;
&&&&&&&&&&& }
&&&&& if( line[t] == '{')
&&&&&&&&&&& {
&&&&&&&&&&&&& bracket++;
&&&&&&&&&&& }
&&&if( line[t] == '}')
&&&&&&&&&&& {
&&&&&&&&&&&&& bracket--;
&&&&&&&&&&& }
&&&&&&&&& if( line[t] == '(')
&&&&&&&&&&& {
&&&&&&&&&&&&& parenthesis++;
&&&&&&&&&&& }
&&&&&&&&& if( line[t] == ')')
&&&&&&&&&&& {
&&&&&&&&&&&&& parenthesis--;
&&&&&&&&&&& }
&&&&&&&&& if( line[t] == '\'')
&&&&&&&&&&& {
&&&&&&&&&&&&& s_quote *= -1;
&&&&&&&&&&& }
&&&&&&&&& if( line[t] == '&')
&&&&&&&&&&& {
&&&&&&&&&&&&& d_quote *= -1;
&&&&&&&&&&& }
&&&&&&&&& t++;
& if(d_quote !=1)
&&& printf (&Mismatching double quote mark\n&);
& if(s_quote !=1)
&&& printf (&Mismatching single quote mark\n&);
& if(parenthesis != 0)
&&& printf (&Mismatching parenthesis\n&);
& if(brace != 0)
&&& printf (&Mismatching brace mark\n&);
& if(bracket != 0)
&&& printf (&Mismatching bracket mark\n&);
& if( bracket==0 && brace==0 && parenthesis==0 && s_quote == 1 && d_quote == 1)
&&&&&&& printf (&Syntax appears to be correct.\n&);
& return 0;
int getline(void)
& extern char line[];
& for ( i=0;i&MAXLINE-1 && ( c=getchar()) != EOF && c != '\n'; ++i)
&&& line[i] =
& if(c == '\n')
&&&&& line[i++] =
&& line[i] = '\0';
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:297125次
积分:3344
积分:3344
排名:第9871名
原创:68篇
转载:23篇
评论:17条
(1)(1)(1)(7)(3)(5)(10)(2)(16)(6)(1)(38)

我要回帖

更多关于 判断题对错符号 的文章

 

随机推荐