C++四则运算计算器

p.s. 代码中的List是自己写的头文件,也可以用std的list

#pragma once
#include"List.h"
using std::string;
//字符串分割
class Arithmetic{
private:
	static List<string> segement_string(const string& expression) {
		List<string> res;
		string temp_num;
		for (int j = 0; j < expression.size(); j++) {
			char temp = expression[j];
			if (temp == '(' || temp == ')' ||
				temp == '+' || temp == '-' ||
				temp == '*' || temp == '/') {//如果是运算或者是括号就检查前面是否有数字,如果有就先压入数字,清空,最后压入当前的符号
				if (!temp_num.empty()) {
					res.push_back(temp_num);
					temp_num.clear();
				}
				res.push_back(string(1, temp));
			}
			else {//如果是数字,压入temp_num
				temp_num.push_back(temp);
			}
		}
		if (!temp_num.empty()) {//如果结尾不是符号,就要考虑还有没有数字没有压入res
			res.push_back(temp_num);
			return res;
		}
	}
	//比较运算符的优先级,a是栈外运算符,b是栈外运算符
	static bool isprior(const char& a, const char& b) {
		if (b == '(')
			if (a != ')') return true;
		if (b == '+' || b == '-') {
			if (a == '(' || a == '*' || a == '/') return true;
		}
		if (b == '*' || b == '/') {
			if (a == '(') return true;
		}
		return false;
	}
	//将中缀表达式改为后缀表达式
	static List<string> infix2postfix(const List<string>& infix) {
		List<string> postfix;
		List<string> temp;
		List<string>::const_iterator iter;
		for (iter = infix.begin(); iter != infix.end(); iter++) {
			if (*iter == "(" || *iter == ")" ||//如果是符号的话,在temp为空或优先级大于temp栈顶的优先级时,对temp进行入栈操作,否则出栈
				*iter == "+" || *iter == "-" ||
				*iter == "*" || *iter == "/") {
				while (!(temp.empty() || isprior((*iter)[0], temp.back()[0]))) {
					if (temp.back() == "(") {//"("是不进入post,并且能让"("出栈的只能是")",一个")"只能让一个"("出栈,所以需要break
						temp.pop_back();
						break;
					}
					else {
						postfix.push_back(temp.back());
						temp.pop_back();
					}
				}
				if (*iter != ")")//")"是不进入post的
					temp.push_back(*iter);
			}
			else//是数值的话直接压入post
				postfix.push_back(*iter);
		}
		while (!temp.empty()) {//最后将栈中的符号都压入post
			postfix.push_back(temp.back());
			temp.pop_back();
		}
		return postfix;
	}
	//将字符串转为double类型的量
	static double string2num(const string& str) {
		double res = 0;
		double isInt = 0;
		for (int i = 0; i < str.size(); i++) {
			unsigned int temp;
			if (str[i] != '.') {
				temp = str[i] - '0';
				if (isInt == 0) {
					res = res * 10 + temp;
				}
				else {
					res += temp * isInt;
					isInt *= 0.1;
				}
			}
			else
				isInt = 0.1;
		}
		return res;
	}
	//后缀表达式的计算
	static double postfixCompute(const List<string>& postfix_expression) {
		double temp1;
		double temp2;
		List<double> temp;
		List<string>::const_iterator iter;
		for (iter = postfix_expression.begin(); iter != postfix_expression.end(); iter++) {
			if (*iter == "+") {
				temp2 = temp.back();
				temp.pop_back();
				temp1 = temp.back();
				temp.pop_back();
				temp.push_back(temp1 + temp2);
			}
			else if (*iter == "-") {
				temp2 = temp.back();
				temp.pop_back();
				temp1 = temp.back();
				temp.pop_back();
				temp.push_back(temp1 - temp2);
			}
			else if (*iter == "*") {
				temp2 = temp.back();
				temp.pop_back();
				temp1 = temp.back();
				temp.pop_back();
				temp.push_back(temp1 * temp2);
			}
			else if (*iter == "/") {
				temp2 = temp.back();
				temp.pop_back();
				temp1 = temp.back();
				temp.pop_back();
				temp.push_back(temp1 / temp2);
			}
			else
				temp.push_back(string2num(*iter));
		}
		return temp.back();
	}
public:
	//计算四则运算
	static double arithmetic(const string& expression) {
		List<string> segments = segement_string(expression);
		List<string> postfix_expression = infix2postfix(segments);
		return postfixCompute(postfix_expression);
	}
};

  

原文链接: https://www.cnblogs.com/airfy/p/12662113.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++四则运算计算器

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/195574

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午7:01
下一篇 2023年2月12日 下午7:01

相关推荐