大家帮帮忙,帮我用c++ 6.0编写一个简单的计算器.
- 提问者网友:王者佥
- 2021-05-13 06:55
- 五星知识达人网友:白昼之月
- 2021-05-13 08:33
#include<stack>
#include<math.h>
using namespace std;
#include<ctype.h>
stack<float> s;
stack<char> cs;
int isp(char optr)
{
switch (optr)
{
case '=': return 0;
case '(': return 1;
case '^': return 7;
case '*': return 5;
case '/': return 5;
case '%': return 5;
case '+': return 3;
case '-': return 3;
case ')': return 8;
default: return 0;
}
}
int icp(char optr)
{
switch (optr)
{
case '=': return 0;
case '(': return 8;
case '^': return 6;
case '*': return 4;
case '/': return 4;
case '%': return 4;
case '+': return 2;
case '-': return 2;
case ')': return 1;
default: return 0;
}
}
void DoOperator(char oper)
{
float oper1,oper2;
oper1=s.top();
s.pop();
oper2=s.top();
s.pop();
switch(oper)
{
case '+':s.push(oper2+oper1);break;
case '-':s.push(oper2-oper1);break;
case '*':s.push(oper2*oper1);break;
case '/':s.push(oper2/oper1);break;
case '^':s.push(pow(oper2,oper1));break;
}
}
void Run()
{
char c;float newop;char y;
cs.push('=');
while(cin>>c,c!='='){
//if(c!='+'&&c!='-'&&c!='*'&&c!='/')
if(isdigit(c)||isalpha(c))
{
cin.putback(c);cin>>newop;
s.push(newop);
}
else if(c==')')
for(y=cs.top(),cs.pop();y!='(';y=cs.top(),cs.pop())
DoOperator(y);
else
{
for(y=cs.top(),cs.pop();icp(c)<=isp(y);y=cs.top(),cs.pop())
DoOperator(y);
cs.push(y);cs.push(c);
}
}
while(!cs.empty()){
y=cs.top();
cs.pop();
if(y!='=')
DoOperator(y);
}
cout<<s.top();
}
int main()
{
Run();
}
http://www.cppblog.com/cxf20090520biancheng/archive/2010/04/14/112579.aspx我的博客里还有自己自定义栈写的,自己看看看
- 1楼网友:话散在刀尖上
- 2021-05-13 09:44
int chengfa(SqList &L,FILE *fp) { int a,b,d; int c,i,m=35,x,count=0; double fen; printf(" 该题库共分成三大等级:\n"); printf(" 1: 易\n 2: 中\n 3: 难\n 0: 退出 \n"); printf(" 请选择难易程度:\n"); scanf("%d",&c); if(c==0) {qingkong();return 0;} if(c==1) m=10; if(c==2) m=100; if(c==3) m=1000; printf("如果你想在中途退出请在每次提交答案时输入12345678\n"); srand(time(0)); for(i=0;i<=10;i++){ a=random(m); b=random(m); d=a*b; printf("%d*%d= ",a,b); scanf("%d",&x); if(x==12345678) break; if(x==d) count++; if(x!=d) printf("答案错误!!\n正确答案是:%d\n",d); } fen=count*100.0/i; printf("本次成绩:%0.2lf\n",fen); L.elem[L.length]=fen; fprintf(fp,"%lf",fen); L.length++; printf("本次成绩保存在:%d\n",L.length); printf("查询时只需输入%d\n",L.length); qingkong(); return 0; } int chufa(SqList &L,FILE *fp) { int a,b; double d; int c,i,m=20,count=0; double fen,x; printf(" 该题库共分成三大等级:\n"); printf(" 1: 易\n 2: 中\n 3: 难\n 0: 退出 \n"); printf(" 请选择难易程度:\n"); scanf("%d",&c); if(c==0){qingkong();return 0;} if(c==1) m=10; if(c==2) m=100; if(c==3) m=1000; printf("如果你想在中途退出请在每次提交答案时输入12345678\n"); srand(time(0)); for(i=0;i<=10;i++){ a=random(m); b=0; while(b==0) b=random(m); d=(a*100/b)*1.00/100; printf("%d/%d= ",a,b); scanf("%lf",&x); if(x==12345678) break; if(x==d) count++; if(x!=d) printf("答案错误!!\n正确答案是:%0.2lf\n",d); } fen=count*100.0/i; printf("本次成绩:%0.2lf\n",fen); L.elem[L.length]=fen; fprintf(fp,"%lf",fen); L.length++; printf("本次成绩保存在:%d\n",L.length); printf("查询时只需输入%d\n",L.length); qingkong(); return 0; } int zonghe(SqList &L,FILE *fp) { int a,b,g,y; double d,x; int i,m=20,t,count=0,c; double fen; printf(" 该题库共分成三大等级:\n"); printf(" 1: 易\n 2: 中\n 3: 难\n 0: 退出 \n"); printf(" 请选择难易程度:\n"); scanf("%d",&c); if(c==0){qingkong();return 0;} if(c==1) m=10; if(c==2) m=100; if(c==3) m=1000; printf("如果你想在中途退出请在每次提交答案时输入12345678\n"); srand((int)time(0)); for(i=0;i<=10;i++){ a=random(m); b=0; while(b==0) b=random(m); g=random(m); t=random(m); if(t%3==1) { if(a%3==2){ d=a*1.0+b*g*1.0; printf("%d+%d*%d=",a,b,g); scanf("%lf",&x); } if(a%3==1){ d=(a*100/b)*1.00/100-g; printf("%d/%d-%d=",a,b,g); scanf("%lf",&x); } if(a%3==0){ d=a*(b-g); printf("%d*(%d-%d)=",a,b,g); scanf("%lf",&x); } } if(t%3==1){ if(a%3==2){ d=a*b*g; printf("%d*%d*%d=",a,b,g); scanf("%lf",&x); } if(a%3==1){ d=(a*100/b)*1.00/100+g; printf("%d/%d+%d=",a,b,g); scanf("%lf",&x); } if(a%3==0){ d=(a*100/(b-g))*1.00/100; printf("%d/(%d+%d)=",a,b,g); scanf("%lf",&x); } } if(t%3==0){ if(a%2==0){ d=a*b*g; printf("%d*%d*%d=",a,b,g); scanf("%d",&x); } if(a%2==1){ d=a-b-g; printf("%d-%d-%d=",a,b,g); scanf("%d",&x); } } if(x==12345678){ if(d==12345678){ printf("是否退出\n1:是 2:否"); scanf("%d",&y); if(y==1) break; } else break; } if(x==d) count++; if(x!=d) printf("答案错误!!\n正确答案是:%0.2lf\n",d); } fen=count*100.0/i; printf("本次成绩:%0.2lf\n",fen); L.elem[L.length]=fen; fprintf(fp,"%lf",fen); L.length++; printf("本次成绩保存在:%d\n",L.length); printf("查询时只需输入%d\n",L.length); qingkong(); return 0; } int chaxun(SqList &L,FILE *fp) { int i=0; int e; printf("输入密码:"); scanf("%d",&e); if(e<0||e>L.length) { printf(" 输入的号码有错误或者不存在!!!!请查证后再输入:\n"); qingkong(); return 0; } else{ if((fp=fopen("成绩查询.txt","r"))==NULL){ printf("文件为空\n");qingkong();return 0;} while(! feof(fp)){ if(i==e){ fscanf(fp,"%lf\n",&L.elem[i]); printf("你的分数为:%0.2lf\n",L.elem[i]); if(L.elem[i]>=0&&L.elem[i]<=59){ printf("评分为:☆☆☆☆★(E)\n同志啊!!革命尚未成功你得仍好好需努力啊·····:"); system("cls"); } if(L.elem[i]>=60&&L.elem[i]<=69){ printf("评分为:☆☆☆★★(D)\n同志啊!!前途光明但你得仍需努力啊·····:"); system("cls"); } if(L.elem[i]>=70&&L.elem[i]<=79) { printf("评分为:☆☆★★★(C)\n同志啊!!前途光明但你得仍需努力啊·····:"); system("cls"); } if(L.elem[i]>=80&&L.elem[i]<=89) { printf("评分为:☆★★★★(B)\n同志啊!!整体较好但望更进一层楼·····:"); system("cls"); } if(L.elem[i]>=90&&L.elem[i]<=100) { printf("评分为:★★★★★(A)\n向高难度的挑战!!Go! Go! Go! Go! Go! Go!·····:"); } break; } i++; } }fclose(fp); qingkong(); return 0; }
这是我以前的课程设计 看看吧···········