简单整数四则运算(C++实现)

简单整数四则运算(C++实现)

题目内容

你的程序要读入一个整数四则运算的式子,其中的整数均为非负整数且小于 10000,其中的运算符只有加减乘除(± */)四种。你的程序要计算这个式子,并输出结果。计算过程必须遵守优先级,即乘除法要在加减法之前先计算。当遇到除法时,按照 C++ 语言的整数除法的方式来计算,即只留下整数的结果。

提示:一种可能的做法是,将整个式子作为字符串读入,然后找出其中最后一个+或-,在此位置将其截成两段,分别计算后再做+或-,以此类推。
另,用atoi(str.c_str())可以从一个字符串得到整数。

输入格式:
输入时,所有的成分均没有有空格隔开,即每个运算符前后都没有空格。注意,单个数字也是可能的一种输入。

输出格式:
输出结果的整数。

输入样例:
2+3*4-6/3

输出样例:
12

解题思路

这道题本来要求用Java做的,怎奈刚学Java,很多东西还不是很熟悉,另外感觉用题目给的思路不太好做,就决定用C++实现,也有大佬给出了Java的解决方案link

思路就是先根据加减号对字符串进行分段,对每一个子串进行乘除号分段得到仅含乘除号的字符串片段,分段的同时用vector数组把字符串中的运算符和位置记录下来。分完以后每一段就是纯数字组成的字符串了,用C++自带的atoi(str.c_str())函数转成int型整数,然后一个for循环计算片段的
结果。最后再把一系列结果进行加减运算,得到最终结果。

第一次写CSDN博客,希望对大家有用!

源代码

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct oper
{
	int pos;
	char ch;
};

void ExprSep(const string & expr, vector<string>& SubExprs, vector<oper>& PandS, char c);
int PandS(const string & expr);
int MandD(const string & SubExprs);

int main()
{
	string expr;
	getline(cin, expr);
	
	cout << PandS(expr) << endl;
	cin.get();
	return 0;
}

void ExprSep(const string & expr, vector<string>& SubExprs, vector<oper>& op, char c)
{
	int frag = 1;
	op = { { -1, '\0' } };
	if (c == '+' || c == '-')
	{
		for (int i = 0; i < expr.length(); ++i)
			{
				if (expr[i] == '+')
				{
					op.push_back({ i, '+' });
					++frag;
				}	
				else if (expr[i] == '-')
				{
					op.push_back({ i, '-' });
					++frag;
				}
			
			}
	}
	else if (c == '*' || c == '/')
	{
		for (int i = 0; i < expr.length(); ++i)
		{
			if (expr[i] == '*')
			{
				op.push_back({ i, '*' });
				++frag;
			}
			else if (expr[i] == '/')
			{
				op.push_back({ i, '/' });
				++frag;
			}

		}
	}
	
	op.push_back({ expr.length(), '\0' });
	for (int i = 0; i < frag; i++)
	{
		SubExprs.push_back(expr.substr(op[i].pos + 1, op[i + 1].pos - op[i].pos - 1));
	}
}

int PandS(const string & expr)
{
	vector<oper> PandS;
	vector<string> SubExprs;
	ExprSep(expr, SubExprs, PandS, '+');
	/*for (int i = 0; i < PandS.size(); i++)
	{
	cout << PandS[i].ch << ' ' << PandS[i].pos << endl;
	}*/
	vector<int> operand(SubExprs.size());
	for (int i = 0; i < SubExprs.size(); i++)
	{
		operand[i] = MandD(SubExprs[i]);
		//cout << operand[i] << ' ';
	}
	//cout << endl;

	int result = operand[0];
	for (int i = 1; i < operand.size(); i++)
	{
		if (PandS[i].ch == '+')
			result += operand[i];
		else
			result -= operand[i];
	}

	return result;
}

int MandD(const string & SubExprs)
{
	//cout << SubExprs << endl;
	int frag = 1;
	vector<string> operand;
	vector<oper> MandD;
	ExprSep(SubExprs, operand, MandD, '*');
	int result = atoi(operand[0].c_str());
	//cout << result;
	for (int i = 1; i < operand.size(); i++)
	{
		if (MandD[i].ch == '*')
			result *= atoi(operand[i].c_str());
		else
			result /= atoi(operand[i].c_str());
		//cout << result;
	}

	return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值