大学学的是计算机专业,所以又拾起c++
写个计算器练练手
以后可能还会更新
/*
四则运算器1.0
只能进行两个数之间的运算
并且中间不能有空格。。。。
*/
#include <cstdio>
#include <iostream>
int main()
{
int a,b;
char c;
scanf("%d%c%d",&a,&c,&b);
switch(c)
{
case '+':a+=b;break;
case '-':a-=b;break;
case '*':a*=b;break;
case '/':a/=b;break;
}
printf("%d",a);
return 0;
}
——————————————————————————————分割线
9.30更新
因为1.0实在太过“淳朴”
所以写完后立即着手更新
/*
四则运算器2.0
只能进行两个数之间的运算
对空格没什么要求
*/
#include <cstdio>
#include <iostream>
char d;
int work(bool flag)
{
int emp=0;char c;
while(c>'9' || c<'0')c=getchar();
while(c>='0'&&c<='9'){emp=emp*10+c-'0';c=getchar();}
if(flag)d=c;
return emp;
}
int main()
{
int a,b;
a=work(true);b=work(false);
switch(d)
{
case '+':a+=b;break;
case '-':a-=b;break;
case '*':a*=b;break;
case '/':a/=b;break;
}
printf("%d",a);
return 0;
}
————————————————————分割线
肝了两天的原神。。。。。
10.3更新
/*
四则运算器2.0
可以进行多项式运算,不过只能从右向左依次进行(没错就是从右向左,后输入的算式先运行,因为是用递归实现的运算)
不识别括号
*/
#include <cstdio>
#include <iostream>
double work()
{
double a=0;char c=getchar();
while(c< '0' || c> '9')c=getchar();
while(c>='0' && c<='9'){a=a*10.0+c-'0';c=getchar();}
switch(c)
{
case '+':a+=work();break;
case '-':a-=work();break;
case '*':a*=work();break;
case '/':a/=work();break;
}
return a;
}
int main()
{
printf("%lf",work());
return 0;
}
--------------------------------分割线
10.13更新
这段时间忙的有点头疼
抽空来更新
/*
四则运算器2.2
从左向右依次计算
不支持括号
由getchar改为scanf
同样用=来解决最后的输入完成判定
感觉有上世纪的计算器那味了
*/
#include <cstdio>
#include <iostream>
double a=0,ans=0;
char c;
void work()
{
switch(c)
{
case '+':ans+=a;break;
case '-':ans-=a;break;
case '*':ans*=a;break;
case '/':ans/=a;break;
}
return ;
}
int main()
{
printf("Don't forget to input the '='");
scanf("%lf%c%lf",&ans,&c,&a);
work();
while(c=getchar())
{
if(c=='=')break;
scanf("%lf",&a);
work();
}
printf("%lf",ans);
return 0;
}