#include<stdio.h>
#include<string>
char str[10]; //記錄要分析的字元串
int x=0; //記錄第一個字元
void E();
void X();
void T();
void Y();
void F();
int main()
{
int len;
printf("請輸入算數表達式:");
scanf("%s",str);
len=strlen(str);
str[len]='#';
str[len+1]='\0';
E();
printf("\n是正确的表達式!");
strcpy(str,"");
x=0;
return 0;
}
void E()
{
T();
X();
}
void X()
{
if(str[x]=='+'||str[x]=='-')
{
x++;
T();
X();
}
}
void T()
{
F();
Y();
}
void Y()
{
if(str[x]=='*'||str[x]=='/')
{
x++;
F();
Y();
}
}
void F()
{
if(str[x]>='a'&&str[x]<='z')
{
x++;
}
else if(str[x]>=0&&str[x]<=9)
{
x++;
}
else if (str[x]=='(')
{
x++;
E();
if(str[x]==')')
{
x++;
}
else
{
printf("\n有文法錯誤!");
exit(0);
}
}
else
{
printf("\n有文法錯誤!");
exit(0);
}
}