第一题:我刚才修改后的程序,这次应该没问题了。
#include <stdio.h>
#define N 20
main()
{
char bracket[N];
int sequence[N]={'\0'};
int i,j,len,sum=0;
printf("Please input the bracket string:");
gets(bracket);
for(i=0;bracket[i]!='\0';i++)
{
switch(bracket[i])
{
case '{': sequence[i]=1;break;
case '}': sequence[i]=8;break;
case '[': sequence[i]=2;break;
case ']': sequence[i]=7;break;
case '(': sequence[i]=3;break;
case ')': sequence[i]=6;break;
case '<': sequence[i]=4;break;
case '>': sequence[i]=5;break;
default: sum=1;break;
}
if(sum==1)
break;
}
if(sum)
printf("Input error!\n");
else
{
len=i-1;
if(i%2!=0)
printf("false\n");
else
{
for(i=1;sequence[i]!='\0';i++)
if(sequence[i-1]>sequence[i])
{
sum=1;
break;
}
if(sum==1)
printf("false\n");
else
{
for(i=0,j=len;i<j;i++,j--)
if(sequence[i]+sequence[j]!=9)
{
sum=1;
break;
}
if(sum==1)
printf("false\n");
else
printf("true\n");
}
}
}
}
[此贴子已经被作者于2007-3-19 14:23:05编辑过]
[此贴子已经被作者于2007-3-19 15:15:51编辑过]
int panduan(char achar[], int n);
int duiying(char achar[], int n);
int main()
{
char achar[NUMBER], ch;
int i;
while(EOF != scanf("%c", &ch))/*判断结束*/
{
i = 0;
achar[0] = ch;
for(i = 1;;i ++)
{
scanf("%c", &achar[i]);
if(achar[i] == '\n')
break;
}
if(panduan(achar, i - 1) && duiying(achar, i - 1))
printf("true\n");
else
printf("false\n");
}
return 0;
}
int panduan(char achar[], int n)/*判断顺序是否正确*/
{
int i = (n + 1) / 2, j;
for(j = 0;j < i - 1;j ++)
if(((achar[j] - achar[j + 1] < 0) && (achar[j] == '(' && achar[j + 1] == '<')) || (achar[j] == '<' && achar[j + 1] == '('))
return 0;
return 1;
}
int duiying(char achar[], int n)/*判断是否对应*/
{
int i = (n + 1) / 2, j;
for(j = 0;j < i;j ++)
if(achar[n - j] - achar[j] == 1 || achar[n - j] - achar[j] == 2)
return 1;
return 0;
}