C++计算器(数据结构作业)

C++计算器(数据结构作业)

(1)运算的准备

	//如下属性用于存放操作数以及运算符
	char* str;		//存放运算串
	stack<int>nums; //存放操作数
	stack<char>ops; //存放计算符(+,-,*,/)

(2)运算符优先级判断

//判断运算符的优先级
int Judge_Priority(char ch){
	if (ch == '(')return 1;
	else if (ch == '+' || ch == '-')return 2;
	else if (ch == '*' || ch == '/')return 3;
	else if (ch == ')')return 4;
	else throw("操作失败,运算符出现error!!");
}

(3)数值运算

int Math_Cal(int a, int b, char c) {
		switch (c) {
		case '+':
			return a + b;
		case '-':
			return a - b;
		case '*':
			return a * b;
		case '/':
			if (b == 0)throw("操作失败,除数不能为0 ! ! !");
			else return a / b;
		}
	}

(4)运算的整体部分

	//输入操作字符串并计算
	void Input_string()
	{
		//字符数组s用于存放大于10的数字
		char ch,s[100] = {0};
		int index = 0;
		//用于存放操作数和结果
		int tempa, tempb,sum;
		int t = 0;
		//输入计算式子
		while ((ch = getchar()) != '\n')str[index++] = ch;
		//结束符号:
		str[index] = '@';
		for ( index = 0; str[index] != '@'; index++) {
			//首先判断初始是否为负号--->处理开头就是负数导致的情况
			if (index == 0 && str[index] == '-')s[t++] = str[index++];
			//判断括号中第一个数字是否为负数
			else if(str[index]=='('&&str[index+1]=='-') {
				index++;
				s[t++] = str[index++];
				while (str[index] >= '0' && str[index] <= '9') {
					s[t++] = str[index++];
				}
				//atoi函数用于将字符串转换为数值
				this->nums.push(atoi(s));
				while (t > 0) {
					s[t--] = 0;
				}
				if (str[index] != ')')this->ops.push('(');
			}
			else if (str[index] >= '0' && str[index] <= '9') {
				while (str[index] >= '0' && str[index] <= '9')s[t++] = str[index++];
				this->nums.push(atoi(s));
				while (t > 0) {
					s[t--] = 0;
				}
				index--;
			}
			else {
				if (ops.empty())this->ops.push(str[index]);
				else if (Judge_Priority(str[index]) == 1)this->ops.push(str[index]);
				else if (Judge_Priority(str[index]) == 2) {
					if (Judge_Priority(ops.top()) == 1)ops.push(str[index]);
					else if (Judge_Priority(ops.top()) >= 2) {
						while (!ops.empty() && nums.size() >= 2) {
							tempa = nums.top(); nums.pop();
							tempb = nums.top(); nums.pop();
							sum = Math_Cal(tempb, tempa, ops.top());
							ops.pop();
							nums.push(sum);
						}
						ops.push(str[index]);
					}
				}
				else if (Judge_Priority(str[index]) == 3) {
					if (Judge_Priority(ops.top())<3)ops.push(str[index]);
					else {
						while (ops.size() != 0 && nums.size() >= 2)
						{
							tempa = nums.top(); nums.pop();
							tempb = nums.top(); nums.pop();
							sum = Math_Cal(tempb, tempa, ops.top());
							ops.pop();
							nums.push(sum);
						}
						ops.push(str[index]);
					}
				}
				else
				{
					do { //循环出栈直到遇到'('
						tempa = nums.top(); nums.pop();
						tempb = nums.top(); nums.pop();
						sum = Math_Cal(tempb, tempa, ops.top());
						ops.pop();
						nums.push(sum);
					} while (ops.top() != '(');
					ops.pop();
				}
			}
		}
		while (!ops.empty())
		{
			tempa = nums.top(); nums.pop();
			tempb = nums.top(); nums.pop();
			sum = Math_Cal(tempb, tempa, ops.top());
			ops.pop();
			nums.push(sum);
		}
		cout << "计算结果为:" << nums.top() << endl;
		//清空内存,初始化
		while (!nums.empty())nums.pop();
		while (!ops.empty())ops.pop();
		str = new char[MAXN];
		while (t > 0) {
			s[t--] = 0;
		}
	}

(0)完整代码部分

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <algorithm>
#include <cstring>
#include <stack>
#define MAXN 1010
using namespace std;

class Function_Calculator 
{
public:
	Function_Calculator() {
		while (!nums.empty())nums.pop();
		while (!ops.empty())ops.pop();
		str = new char[MAXN];
	}
	//判断运算符的优先级
	int Judge_Priority(char ch){
		if (ch == '(')return 1;
		else if (ch == '+' || ch == '-')return 2;
		else if (ch == '*' || ch == '/')return 3;
		else if (ch == ')')return 4;
		else throw("操作失败,运算符出现error!!");
	}
	int Math_Cal(int a, int b, char c) {
		switch (c) {
		case '+':
			return a + b;
		case '-':
			return a - b;
		case '*':
			return a * b;
		case '/':
			if (b == 0)throw("操作失败,除数不能为0 ! ! !");
			else return a / b;
		}
	}
	//输入操作字符串并计算
	void Input_string()
	{
		//字符数组s用于存放大于10的数字
		char ch,s[100] = {0};
		int index = 0;
		//用于存放操作数和结果
		int tempa, tempb,sum;
		int t = 0;
		//输入计算式子
		while ((ch = getchar()) != '\n')str[index++] = ch;
		//结束符号:
		str[index] = '@';
		for ( index = 0; str[index] != '@'; index++) {
			//首先判断初始是否为负号--->处理开头就是负数导致的情况
			if (index == 0 && str[index] == '-')s[t++] = str[index++];
			//判断括号中第一个数字是否为负数
			else if(str[index]=='('&&str[index+1]=='-') {
				index++;
				s[t++] = str[index++];
				while (str[index] >= '0' && str[index] <= '9') {
					s[t++] = str[index++];
				}
				//atoi函数用于将字符串转换为数值
				this->nums.push(atoi(s));
				while (t > 0) {
					s[t--] = 0;
				}
				if (str[index] != ')')this->ops.push('(');
			}
			else if (str[index] >= '0' && str[index] <= '9') {
				while (str[index] >= '0' && str[index] <= '9')s[t++] = str[index++];
				this->nums.push(atoi(s));
				while (t > 0) {
					s[t--] = 0;
				}
				index--;
			}
			else {
				if (ops.empty())this->ops.push(str[index]);
				else if (Judge_Priority(str[index]) == 1)this->ops.push(str[index]);
				else if (Judge_Priority(str[index]) == 2) {
					if (Judge_Priority(ops.top()) == 1)ops.push(str[index]);
					else if (Judge_Priority(ops.top()) >= 2) {
						while (!ops.empty() && nums.size() >= 2) {
							tempa = nums.top(); nums.pop();
							tempb = nums.top(); nums.pop();
							sum = Math_Cal(tempb, tempa, ops.top());
							ops.pop();
							nums.push(sum);
						}
						ops.push(str[index]);
					}
				}
				else if (Judge_Priority(str[index]) == 3) {
					if (Judge_Priority(ops.top())<3)ops.push(str[index]);
					else {
						while (ops.size() != 0 && nums.size() >= 2)
						{
							tempa = nums.top(); nums.pop();
							tempb = nums.top(); nums.pop();
							sum = Math_Cal(tempb, tempa, ops.top());
							ops.pop();
							nums.push(sum);
						}
						ops.push(str[index]);
					}
				}
				else
				{
					do { //循环出栈直到遇到'('
						tempa = nums.top(); nums.pop();
						tempb = nums.top(); nums.pop();
						sum = Math_Cal(tempb, tempa, ops.top());
						ops.pop();
						nums.push(sum);
					} while (ops.top() != '(');
					ops.pop();
				}
			}
		}
		while (!ops.empty())
		{
			tempa = nums.top(); nums.pop();
			tempb = nums.top(); nums.pop();
			sum = Math_Cal(tempb, tempa, ops.top());
			ops.pop();
			nums.push(sum);
		}
		cout << "计算结果为:" << nums.top() << endl;
		//清空内存,初始化
		while (!nums.empty())nums.pop();
		while (!ops.empty())ops.pop();
		str = new char[MAXN];
		while (t > 0) {
			s[t--] = 0;
		}
	}
protected:
	//如下属性用于存放操作数以及运算符
	char* str;
	stack<int>nums;
	stack<char>ops;
};

signed main()
{
	Function_Calculator cal;
	while (true)
	{
		cal.Input_string();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_橙留香

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值