【问题描述】
编写一程序检查C源程序文件中{}、()等括号是否匹配,并输出第一个检测到的不匹配的括号及所对应括号所在的行号(程序中只有一个括号不匹配)。
注意:
1.除了括号可能不匹配外,输入的C源程序无其它语法错误。
2.字符常量、字符串常量及注释中括号不应被处理,注释包括单行注释//和多行/* */注释
3.字符常量和字符串常量中不包含转义字符’和";
4.程序中出现有意义括号的个数不超过200个;
不匹配判断规则:
1.当检测的程序括号为’{‘时,若其前序尚未匹配的括号为’(‘时,输出该’('左括号及所在行号;
2.当遇到一个不匹配的右括号’)‘或’}'时,输出该右括号及所在行号;
3.当程序处理完毕时,还存在不匹配的左括号时,输出该左括号及所在行号。
【输入形式】
打开当前目录下文件example.c,查询其括号是否匹配。该文件中每行字符数不超过200。
【输出形式】
若存在括号不匹配时,应输出首先能判断出现不匹配的括号及其所在的行号。当出现括号不匹配时,按下面要求输出相关信息:
without maching at line
其中为‘{’, ‘}’, ‘(’, ‘)’等符号,为该符号所在的行号。
若整个程序括号匹配,则按下面所示顺序输出括号匹配情况,中间没有空格。
(){(()){}}
【样例输入1】
若当前目录下输入文件example.c中内容如下:
#include<stdio.h>
int main(){
printf(“{ hello world }\n”); // }
)
【样例输出1】
without maching ‘)’ at line 4
【样例输入2】
若当前目录下输入文件example.c中内容如下:
#include<stdio.h>
int main(){
printf(“{ hello world }d\n”); /* }*/
【样例输出2】
without maching ‘{’ at line 2
【样例输入3】
若当前目录下输入文件example.c中内容如下:
#include<stdio.h>
int main(){
printf(“{ hello world }d\n”); /* }*/
}
【样例输出3】
(){()}
【样例说明】
样例1:在注释部分和字符串中的括号不考虑,在将程序处理之后得到的括号序列是(){()),遇到右括号时与最近的左括号匹配,发现最后一个小括号和大括号不匹配。
样例2:处理之后的括号序列是(){(),在最后缺少了右大括号,那么应该输出与之相对应的左括号不匹配。
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
char a[210], b[210], temp[300];
int main()
{
FILE *fp;
fp = fopen("example.c", "r");
int i = 0, j = 0, len = 0, flag = 0, n, w, fuck = 0; //len是行数
while (fgets(temp, 205, fp) != NULL)
{
if (fuck == 1)
{
break;
}
len++;
flag = 0;
n = strlen(temp);
for (int k = 0; k < n; k++)
{
if (temp[k] == '(' || temp[k] == '{')
{
a[i++] = temp[k];
b[j++] = temp[k];
}
else if (temp[k] == '/' && temp[k + 1] == '/')
{
break;
}
else if (temp[k] == '/' && temp[k + 1] == '*')
{
for (w = k + 2; w < n; w++)
{
if (temp[w] == '*' && temp[w + 1] == '/')
{
flag = 1;
break;
}
}
while (flag == 0)
{
strcpy(temp, "\0");
fgets(temp, 205, fp);
len++;
flag = 0;
n = strlen(temp);
for (w = 0; w < n; w++)
{
if (temp[w] == '*' && temp[w + 1] == '/')
{
flag = 1;
break;
}
}
}
k = w;
// 后面接着判断括号
}
if (temp[k] == ')')
{
b[j++] = temp[k];
if (a[i - 1] == '(')
{
a[--i] = '\0';
}
else
{
printf("without maching '%c' at line %d", temp[k], len);
fuck = 1;
break;
}
}
if (temp[k] == '}')
{
b[j++] = temp[k];
if (a[i - 1] == '{')
{
a[--i] = '\0';
}
else
{
printf("without maching '%c' at line %d", temp[k], len);
fuck = 1;
break;
}
}
strcpy(temp, "\0");
}
}
/*if (fuck == 0 || strlen(a) != 0)
{
printf("%c", a[strlen(a) - 1]);
}*/
if (fuck == 0 && strlen(a) != 0)
{
printf("%s", b);
}
fclose(fp);
return 0;
}
方法二
#include <stdio.h>
#include <string.h>
#define MAX 202
struct one
{
char a;
int position;
} pos[202];
struct opstack
{
struct one pos[202];
int top;
} s;
int i, j, line;
char buffer[202], all[202];
typedef struct opstack stack;
void initstack(stack *s) { s->top = -1; }
void push_stack(stack *s, char c, int line)
{
s->pos[++s->top].a = c;
s->pos[s->top].position = line;
}
void pop_stack(stack *s)
{
s->pos[s->top].a = '\0';
s->pos[s->top].position = 0;
s->top--;
}
int main(void)
{
FILE *open = fopen("example.c", "r");
initstack(&s);
while (fgets(buffer, 201, open) != NULL)
{
line++;
int length = strlen(buffer), type = 0;
for (i = 0; i < length; i++)
{
if (type == 4)
continue;
if (buffer[i] != '(' && buffer[i] != ')' && buffer[i] != '{' &&
buffer[i] != '}' && buffer[i] != '"' && buffer[i] != '*' &&
buffer[i] != '/')
continue;
if (type != 3 && type != 5 &&
(buffer[i] == '(' || buffer[i] == '{'))
type = 1;
if (type != 3 && type != 5 &&
(buffer[i] == ')' || buffer[i] == '}'))
type = 2;
if (type != 3 && type != 5 &&
(buffer[i] == '/' && buffer[i + 1] == '*'))
type = 3;
if (type != 3 && type != 5 &&
(buffer[i] == '/' && buffer[i + 1] == '/'))
type = 4;
if (type == 5 && buffer[i] != '"')
continue;
if (type != 5 && buffer[i] == '"')
{
type = 5;
continue;
}
if (type == 5 && buffer[i] == '"')
{
type = 0;
continue;
}
if (type == 3 && buffer[i] == '*' && buffer[i + 1] == '/')
type = 0;
if (buffer[i] == '{' && s.pos[s.top].a == '(')
{
type = -1;
break;
}
if (buffer[i] == ')' && s.pos[s.top].a == '{')
{
push_stack(&s, buffer[i], line);
type = -1;
break;
}
if (type == 3)
continue;
if (type == 1 || type == 2)
{
all[j++] = buffer[i];
if (buffer[i] == ')' && s.pos[s.top].a == '(')
{
pop_stack(&s);
}
else if (buffer[i] == '}' && s.pos[s.top].a == '{')
{
pop_stack(&s);
}
else
push_stack(&s, buffer[i], line);
type = 0;
}
}
if (type == -1)
break;
for (i = 0; i < length; i++)
buffer[i] = '\0';
}
if (s.top == -1)
printf("%s", all);
if (s.top != -1)
printf("without maching '%c' at line %d", s.pos[s.top].a,
s.pos[s.top].position);
}
方法三
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define isbracket buff[i]=='('||buff[i]==')'||buff[i]=='{'||buff[i]=='}'
char buff[210];
struct stack
{
int line;
char b;
}sbracket[300];
char brackets[250];
int main()
{
FILE*fp;
int i,j,k,cnt=0,cnt1=1,n=1;
fp=fopen("example.c","r");
while(fgets(buff,202,fp)!=NULL)
{
cnt++;
for(i=0;buff[i]!='\0';i++)
{
int flag=0;
if(buff[i]=='/'&&buff[i+1]=='/')
break;
if(buff[i]=='"')
{
do
{
i++;
}while(buff[i]!='"');
}
if(buff[i]=='\'')
{
do
{
i++;
}while(buff[i]!='\'');
}
if(buff[i]=='/'&&buff[i+1]=='*')
{
while(!flag)
{
while(!(buff[i]=='*'&&buff[i+1]=='/'))
{
i++;
if(buff[i]=='\0')
break;
}
if(buff[i]!='\0')
flag=1;
else
{
memset(buff,0,210);
fgets(buff,202,fp);
i=0;
}
}
}
char match;
if(isbracket)
{
brackets[n++]=buff[i];
switch(buff[i])
{
case('('):
match=')';
break;
case(')'):
match='(';
break;
case('{'):
match='}';
break;
case('}'):
match='{';
break;
}
for(j=cnt1-1;j>0;j--)
{
if(sbracket[j].b=='('&&match=='}')
{
printf("without maching '(' at line %d",sbracket[j].line);
return 0;
}
if(sbracket[j].b==match)
{
for(k=j+1;k<=cnt1;k++,j++)
sbracket[j]=sbracket[k];
cnt1--;
break;
}
}
if(j==0)
{
if(buff[i]==')'||buff[i]=='}')
{
printf("without maching '%c' at line %d",buff[i],cnt);
return 0;
}
sbracket[cnt1].b=buff[i];
sbracket[cnt1].line=cnt;
cnt1++;
}
}
}
memset(buff,0,210);
}
if(cnt1==2)
printf("without maching '%c' at line %d",sbracket[1].b,sbracket[1].line);
else
{
for(i=1;i<n;i++)
printf("%c",brackets[i]);
}
return 0;
}
方法四
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 250
#define item 10
int STACK_0[max][item] , STACK[max][item] , top , TOP , count_line , flag_note , judge;
char line[max];
void func();
void SEEK();
void push(int element);
int isnote_1(int x);
int isnote_2(int x);
int isnote_2_end(int x);
int main()
{
FILE *fp = fopen( "example.c" , "r" );
count_line = 0 ;
flag_note = 0;
while(fgets(line, max-1, fp) != NULL)
{
count_line++;
SEEK();
}
func();
fclose(fp);
return 0;
}
void push(int element){
STACK_0[++top][0] = element;
STACK_0[top][1] = count_line;
}
int isnote_1(int x){
if(line[x] == '/' && line[x+1] == '/') return 1;
return 0;
}
int isnote_2(int x){
if( line[x+1] != '\0');
if(line[x] == '/' && line[x+1] == '*') return 1;
return 0;
}
int isnote_2_end(int x){
if( line[x+1] != '\0');
if(line[x] == '*' && line[x+1] == '/') return 1;
return 0;
}
void SEEK()
{
int x;
for (x = 0 ; line[x] != '\0' ; x++)
{
if( isnote_1(x) ) break;
if( isnote_2(x) ) flag_note = 1;
if( line[x] == '\"' ) flag_note = 2;
if( line[x] == '\'' ) flag_note = 3;
if(flag_note == 1)
{
int j = 0 ;
for(j = x ; line[j] != '\0' ; j++){
if( isnote_2_end(j) ){
x = j+1 ;
flag_note = 0;
}
}
if(flag_note == 1) break;
}
if(flag_note == 2)
{
int j = 0 ;
for(j = x ; line[j] != '\0' ; j++){
if( line[j] == '\"' ){
x = j ;
flag_note = 0;
}
}
if(flag_note == 2) break;
}
if(flag_note == 3)
{
int j = 0 ;
for(j = x ; line[j] != '\0' ; j++){
if( line[j] == '\'' ){
x = j ;
flag_note = 0;
}
}
if(flag_note == 3) break;
}
if(flag_note == 0)
{
if(line[x] == '(') push(1);
else if(line[x] == ')') push(2);
else if(line[x] == '{') push(-1);
else if(line[x] == '}') push(-2);
}
}
}
void func()
{
int i , flag_1 = 0;
char c = '\0';
for(i = 1 ; i <= top ; i++ )
{
if(STACK_0[i][0] == 1)
{
STACK[++TOP][0] = STACK_0[i][0];
STACK[TOP][1] = STACK_0[i][1];
}
else if(STACK_0[i][0] == -1)
{
if(STACK[TOP][0] == 1 )
{
printf("without maching \'(\' at line %d\n" , STACK[TOP][1]);
flag_1 = 1;
break;
}
else
{
STACK[++TOP][0] = STACK_0[i][0];
STACK[TOP][1] = STACK_0[i][1];
}
}
else if(STACK_0[i][0] == 2)
{
if(STACK[TOP][0] == 1 )
{
STACK[TOP][0] = 0;
STACK[TOP][1] = 0;
TOP--;
}
else
{
if(STACK_0[i][0] == 1) c = '(';
else if(STACK_0[i][0] == 2) c = ')';
else if(STACK_0[i][0] == -1) c = '{';
else if(STACK_0[i][0] == -2) c = '}';
printf("without maching \'%c\' at line %d\n" ,c , STACK_0[i][1]);
flag_1 = 1;
break;
}
}
else if(STACK_0[i][0] == -2)
{
if(STACK[TOP][0] == -1 )
{
STACK[TOP][0] = 0;
STACK[TOP][1] = 0;
TOP--;
}
else
{
if(STACK_0[i][0] == 1) c = '(';
else if(STACK_0[i][0] == 2) c = ')';
else if(STACK_0[i][0] == -1) c = '{';
else if(STACK_0[i][0] == -2) c = '}';
printf("without maching \'%c\' at line %d\n" , c , STACK_0[i][1]);
flag_1 = 1;
break;
}
}
}
if(flag_1 == 0)
{
if(TOP == 0)
{
for(i = 1 ; i<= top ; i++)
{
if(STACK_0[i][0] == 1) c = '(';
else if(STACK_0[i][0] == 2) c = ')';
else if(STACK_0[i][0] == -1) c = '{';
else if(STACK_0[i][0] == -2) c = '}';
printf("%c" , c);
}
}
else for(i = 1 ; i <= TOP ; i++ )
{
if(STACK[i][0] == 1 || STACK[i][0] == -1 )
{
if(STACK[i][0] == 1) c = '(';
else if(STACK[i][0] == 2) c = ')';
else if(STACK[i][0] == -1) c = '{';
else if(STACK[i][0] == -2) c = '}';
printf("without maching \'%c\' at line %d\n" ,c, STACK[i][1]);
break;
}
}
}
}