#include <stdio.h>
#include <ctype.h>
#define FALSE 0
#define TRUE 1
char getSymbol(char *string,int length,int *pos);
int isExpr(char * string,int length,int *pos);
int isTerm(char *string,int length,int * pos);
int isFactor(char *string,int length,int * pos);
char getSymbol(char *string,int length,int *pos){
char c;
if(*pos < length){
c = string[*pos];
}
else{
//printf("Overflow");
return ' ';
}
(*pos)++;
return c;
}
int isExpr(char * string,int length,int *pos){
char symbol ;
if(isTerm(string,length,pos) == FALSE){
return FALSE;
}
if(*pos == length){
if(isalpha(string[(*pos)-1]) || string[(*pos)-1] == ')'){
return TRUE;
}
else{
return FALSE;
}
}
symbol = getSymbol(string,length,pos);
if( symbol != '+' && symbol != '-'){
(*pos) --;
return TRUE;
}
return(isExpr(string,length,pos));
}
int isTerm(char *string,int length,int * pos){
char symbol;
if(isFactor(string,length,pos) == FALSE){
return FALSE;
}
if(*pos == length){
if(isalpha(string[(*pos)-1]) || string[(*pos)-1] == ')'){
return TRUE;
}
else{
return FALSE;
}
}
symbol = getSymbol(string,length,pos);
if( symbol != '*' && symbol != '/'){
(*pos) --;
return TRUE;
}
return(isTerm(string,length,pos));
}
int isFactor(char *string,int length,int * pos){
int c;
if(*pos == length){
if(isalpha(string[(*pos)-1]) || string[(*pos)-1] == ')'){
return TRUE;
}
else{
return FALSE;
}
}
c = getSymbol(string,length,pos);
if(c != '('){
return isalpha(c);
}
return(isExpr(string,length,pos) && getSymbol(string,length,pos) == ')');
}
int strLen(char * string){
int len=0;
while(string[len])
len++;
return len;
}
int main(void){
char *string = "A/aaaaaa";
int pos = 0;
int length = strLen(string);
if(isExpr(string,length,&pos) && pos >= length){
printf("Valid");
}
else{
printf("Invalid");
}
getchar();
}
[C]判断一个字符串是不是合格的表达式
最新推荐文章于 2022-07-02 14:41:04 发布