• 无符号数的词法分析程序

    #include<iostream> #include<cctype> #include<cstring> #include<cmath> using namespace std; int w=0; //尾数累加器 int p=0; //指数累加器 int j=0; //十进制小数位数计数器 int e=1; //用来记录十进制数的符号,当指数为正时为1,为负时为-1 int i=0; //用来标志元素位置 int d=0; //用来表示每个数值型元素对应的数值 const int N=40;//用来确定输入识别符的最大长度 char data[N];//存放输入的识别符 bool is_digit; //标志是否是数字 string CJ1;//确定是整形还是实型 double CJ2;//记数值 //函数声明 void check(char c);//检查首字母是否是数字的函数 void deal_integer(char c);//处理识别符的整数部分 void deal_point(char c);//用来处理小数部分 void deal_index(char c);//用来处理指数部分 void s_next();// 确定实型 void z_next();//确定整型 void last();// 计算 CJ2 void error();//程序中错误处理程序 void deal();//处理函数主体 int main(){ //主函数 cout<<"please input your data,and its maximum length is "<<N<<":"<<endl;//等待用户输入识别符 cin>>data; deal();//处理函数主体 last();// 计算 CJ2 system("pause"); return 0; } void check(char c) //判断输入的首字母是否是数字 { is_digit=isdigit(c); while(is_digit!=true){//输入的首字母不是数字时 cout<<"\nError! Try again.."<<endl;//要求重新输入 cin>>data; check(data[0]); } } void deal_integer(char c){//处理识别符的整数部分 d=(int)c-48; w=w*10+d; i++; if(isdigit(data[i])!=0)//下一个仍是数值时,调用程序本身 deal_integer(data[i]); } void deal_point(char c){//用来处理小数部分 int temp=i; if(isdigit(c)!=0)//是数值字符时 deal_integer(c); else { error(); //错误处理程序 deal();//处理函数主体 } j=i-temp;//记录十进制小数位数 } void deal_index(char c){//用来处理指数部分 if(c=='-') {e=-1;i++;}//是'-'号时 else {if(c=='+') i++;//是'+' 号时 else { if(isdigit(c)==false) //非数值字符时 { error();//错误处理程序 deal();//处理函数主体 } else { d=(int)c-48;//把输入字符转换为整型 goto pro2;} } } if(isdigit(data[i])!=0) pro1: d=(int)(data[i])-48; pro2: p=p*10+d; i++; if(isdigit(data[i])!=0)//是数值字符时 goto pro1; else if(data[i]!='\0'){//非结束标志 error();//错误处理程序 deal();//处理函数主体 } else s_next(); // 确定实型 } void s_next(){// 确定实型 i--;//退一个字符 CJ1="实型"; } void z_next(){//确定整型 i--;//退一个字符 CJ1="整型"; } void last(){// 计算 CJ2 CJ2=w*pow((double)10,e*p-j); cout<<CJ1<<": "<<CJ2<<endl;//输出 } void error(){//程序中错误处理程序 cout<<"\nError! Try again.."<<endl;//重新输入数据 cin>>data; p=0;w=0;j=0; //所有全局变量重新初始化 e=1;i=0;d=0; //exit(0); } void deal(){ check(data[0]);//判断输入的首字母是否是数字 deal_integer(data[i]);//处理识别符的整数部分 if(data[i]=='.') { deal_point(data[++i]);//用来处理小数部分 if(data[i]=='e'||data[i]=='E')//如果是e或E时 deal_index(data[++i]);//用来处理指数部分 else if(data[i]!='\0') { error();//错误处理程序 deal();//处理函数主体 } else s_next();// 确定实型 } else { if(data[i]=='e'||data[i]=='E')//如果是e或E时 { deal_index(data[++i]);//用来处理指数部分 //CJ1="整型"; } else if(data[i]!='\0'){ //非结束标志 error();//错误处理程序 deal();//处理函数主体 } else z_next();//确定整型 } }

    5
    626
    33KB
    2009-04-24
    10
  • 我的逆波兰式生成程序

    #define maxbuffer 64 void main() { char display_out(char out_ch[maxbuffer], char ch[32]); //int caculate_array(char out_ch[32]); static int i=0; static int j=0; char ch[maxbuffer],s[maxbuffer],out[maxbuffer]; cout<<"请输入中缀表达式: "; cin>>ch; for(i=0;i<maxbuffer;i++) {out[i]=ch[i];} cout<<"请确认您输入的表达式: "; while(out[j]!='#') { cout<<out[j]; j++; } cout<<'#'<<endl; display_out(s,out); //caculate_array; } char display_out(char out_ch[32],char ch[]) { int top=-1; int i=0,data[maxbuffer],n; int j=0; char sta[20]; while(ch[i]!='#') { if(isalnum(ch[i])) { while(isalnum(ch[i])) { out_ch[j]=ch[i]; j++; i++; }out_ch[j]=' ';j++; } else{ switch(ch[i]) { case '+': case '-': if(sta[top]=='('||top==-1) { top++; sta[top]=ch[i]; i++; } else { //j--; out_ch[j]=sta[top]; j++; top--; //i++; } break; //break; case '*': case '/':if(sta[top]=='*'&&sta[top]=='/') { out_ch[j]=sta[top]; j++; //i++; top--; } else { top++; sta[top]=ch[i]; i++; } break ; //break; case '(':top++; sta[top]=ch[i]; i++; break; case ')':if(sta[top]=='(') { top--; i++; } if(top==-1) { cout<<"错误: 第"<<j<<"个位置的“)”没有找到与之匹配的“(”"; ch[i]='#';j=0; } else { //while(sta[top]!='('){ out_ch[j]=sta[top]; top--; j++; //}break; } break; /*case '#': out_ch[j]='#'; j++; break;*/ default: cout<<" your input is error"<<endl; ch[i]='#'; j=0; break; } } }while(top!=-1) { out_ch[j]=sta[top];j++; top--; } out_ch[j]='#'; n=0; cout<<"逆波兰表达式为: "; while(out_ch[n]!='#') { cout<<out_ch[n]; n++; } cout<<endl; j=0; /*while(ch[j]!='#') { top++; data[top]=ch[j]; } cout<<data[top]; */ return out_ch[maxbuffer]; } 逆波兰

    0
    129
    27KB
    2009-04-24
    10
关注 私信
上传资源赚积分or赚钱