7-2 符号配对 (20分)
请编写程序检查C语言源程序中下列符号是否配对:/*与*/、(与)、[与]、{与}。
输入格式:
输入为一个C语言源程序。当读到某一行中只有一个句点.和一个回车的时候,标志着输入结束。程序中需要检查配对的符号不超过100个。
输出格式:
首先,如果所有符号配对正确,则在第一行中输出YES,否则输出NO。然后在第二行中指出第一个不配对的符号:如果缺少左符号,则输出?-右符号;如果缺少右符号,则输出左符号-?。
输入样例1:
void test()
{
int i, A[10];
for (i=0; i<10; i++) /*/
A[i] = i;
}
.
输出样例1:
NO
/*-?
输入样例2:
void test()
{
int i, A[10];
for (i=0; i<10; i++) /**/
A[i] = i;
}]
.
输出样例2:
NO
?-]
输入样例3:
void test()
{
int i
double A[10];
for (i=0; i<10; i++) /**/
A[i] = 0.1*i;
}
.
输出样例3:
YES
作者
DS课程组
单位
浙江大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
Accepted Code
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
//函数状态码定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MaxSize 100
typedef int Position;
typedef char ElementType;
struct stack {
char elem[MaxSize];
int top;
};
typedef struct stack seqstack;
int push(seqstack *s,char c);
int pop(seqstack *s,char *x);
int gettop(seqstack *s,char *x);
void InitStack(seqstack *s);
int match(char c1,char c2);
void InitStack(seqstack *s) {
s->top=-1;
}
int push(seqstack *s, char c) {
if (s->top == MaxSize - 1)
return 0;
else {
s->elem[++s->top] = c;
return 1;
}
}
int pop(seqstack *s,char *x) {
if(s->top==-1)
return 0;
else{
*x=s->elem[s->top--];//这里用的*x
return 1;
}
}
int gettop(seqstack *s, char *x) {
if (s->top == -1)
return 0;
else {
*x = s->elem[s->top];
return 1;
}
}
int main() {
seqstack s;
char ch;
int i;
InitStack(&s);
char str[1000];
while (scanf("%s", str) != NULL) {
if (str[0] == '.' && str[1] == 0)//这里有个跳出循环
break;
for (i = 0; str[i] != '\0'; i++) {
switch (str[i]) {
case '(':
case '{':
case '[':
push(&s, str[i]);
break;
case '/':
if (str[i + 1] == '*') {
push(&s, str[i]);
push(&s, str[i + 1]);
}
i += 1;
break;
case ')':
case '}':
case ']':
case '*':
if (s.top == -1) {
printf("NO\n");
switch (str[i]) {
case ')':
printf("?-)");
break;
case '}':
printf("?-}");
break;
case ']':
printf("?-]");
break;
case '*':
if (str[i] == '*' && str[i + 1] == '/')
printf("?-*/");
break;
}
return 0;
} else {
gettop(&s, &ch);
if (match(ch, str[i])) {
pop(&s, &ch);
} else if (str[i] == '*')// * 字符 做单独处理
{
if (ch == '*' && str[i + 1] == '/')// 是*/的符号
{
pop(&s, &ch);
pop(&s, &ch);
i++;
} else {
//只是单一的 * 号
continue;
}
} else {
printf("NO\n");
switch (ch) {
case '{':
printf("{-?");
break;
case '(':
printf("(-?");
break;
case '[':
printf("[-?");
break;
case '*':
printf("/*-?");
break;
}
return 0;
}
}
}
}
}
if (s.top == -1)
printf("YES\n");
else {
printf("NO\n");
switch (ch) {
case '{':
printf("{-?");
break;
case '(':
printf("(-?");
break;
case '[':
printf("[-?");
break;
case '*':
printf("/*-?");
break;
}
return 0;
}
}
int match(char c1, char c2) {
switch (c1) {
case '(':
if (c2 == ')')
return 1;
else
return 0;
case '[':
if (c2 == ']')
return 1;
else
return 0;
case '{':
if (c2 == '}')
return 1;
else
return 0;
default:
return 0;
}
}
仅供参考
该博客介绍了一个C语言程序,用于检查输入的C语言源代码中符号是否正确配对,如/*与*/、(与)、[与]、{与}
1万+

被折叠的 条评论
为什么被折叠?



