给出语言无符号整数的文法

< num > —> < num >< dight > | < dight >
< dight > —> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
现在要求做出修改,修改之后无符号整数的首位不能为0

< num > —> < num >< dight1 > | < dight2 >
< dight1 > —> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
< dight2 > —> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
(< dight2 > —> 0 | < dight1 >)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
#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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值