calc

大学学的是计算机专业,所以又拾起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;
}

--------------------分割线
2021.10.9更新
经典年更
主要是我们C++作业的一道选做题是用栈实现这个玩意

/*
 * 四则运算器2.3
 * 栈实现四则运算
 * 从左向右进行
 * 不支持括号
 * 参与运算的数字不能为0
 * 输入的数字和运算符之间可以带空格
 */
#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;

const int Maxn = 1001;
template <typename T>
class Stack {
private:
	int top;
	T sta[Maxn];
public:
	Stack() {
		top = 0;
	}
	Stack(int n, T a[]) {
		top = n;
		for(int i=0; i<n; ++i)
			sta[i] = a[i];
	}
	void Push(T x) {
		if(top == Maxn) throw "Fulled!";
		sta[top++] = x;
	}
	T GetTop() {//只输出,不弹出
		if(Empty()) throw "Empty!";
		return sta[top-1];
	}
	T Drop() {//输出且弹出
		T x = GetTop();
		Pop();
		return x;
	}
	void Pop() {//弹出不输出
		if(Empty()) throw "Empty!";
		--top;
	}
	bool Empty() {
		return top == 0;
	}
};

int main(void) {
	Stack<double> num;
	Stack<char> op;
	char s[1000] = {};
	cin.getline(s, 1000, '\n');
	strcat(s, "#");
	//cout << s << endl;
	
	double x=0;
	for(int i=0; s[i] != '\0'; ++i) {
		if(isdigit(s[i]))
			x = x*10 + s[i]-'0';
		else {
			if(x != 0) {
				//cout << x << ' ' << s[i] << endl;
				num.Push(x);
				x = 0;
			}
			switch(s[i]) {
				case '+':
				case '-':
					while(!op.Empty()) {
						if(op.GetTop() == '*') {
							op.Pop();
							x = num.Drop();
							x *= num.Drop();
							num.Push(x);
							//cout << x << endl;
							x = 0;
						} else
							if(op.GetTop() == '/') {
								op.Pop();
								x = num.Drop();
								x = num.Drop() / x;
								num.Push(x);
								//cout << x << endl;
								x = 0;
							}
							else	break;
					}
					op.Push(s[i]);
					break;
				case '*':
				case '/':
					op.Push(s[i]);
					break;
			}
		}
	}
	while(!op.Empty()) {
		x = num.Drop();
		switch(op.GetTop()) {
			case '+':
				x += num.Drop();
				break;
			case '-':
				x = num.Drop() - x;//注意先后顺序
				break;
			case '*':
				x *= num.Drop();
				break;
			case '/':
				x = num.Drop() / x;
				break;
		}
		op.Pop();
		//cout << x << endl;
		num.Push(x);
	}
	cout << num.Drop();
	return 0;
}
posted @ 2020-09-30 17:15  快乐永恒  阅读(358)  评论(5编辑  收藏  举报