c++四则混合计算器

#include <iostream>
#include <string>
#include <cmath>
#include <deque>
using namespace std;

#define LENGTH 61440

//判断是否为数字
bool isNo(char c)
{
	return c >= '0' && c <= '9' || c == '.';
}
//是否为运算符
bool isSyb(const char* c)
{
	return (*c == '+' || *c == '-' || *c == '*' || *c == '/') && strlen(c) == 1; //ver1.2
}
bool isSyb(char c)
{
	return c == '+' || c == '-' || c == '*' || c == '/';
}

//是否为括号
bool isIlg(char c)
{
	return !isNo(c) && !isSyb(c) && !(c == '(' || c == ')' || c == ' ');
}

const char* GetSubtext(const char* S, size_t bgn, size_t end) 
{
	char* subtext = new char[end - bgn + 1];
	subtext[end - bgn] = '\0';
	for (size_t i = bgn; i != end; i++)
		subtext[i - bgn] = S[i];
	return subtext;
}
const char* calc(const char* S, size_t len)
{
	deque<string> words;
	bool inNo = false;
	size_t NoPos = 0;
	size_t ParPos = 0, ParDep = 0;
	for (size_t i = 0; i <= len; i++)
	{

		if (isIlg(S[i]) && i != len)
		{
			return "不正确的公式!";
		}

		if (isNo(S[i]) && !inNo)
		{
			inNo = true;
			NoPos = i;
		}
		if (!isNo(S[i]) && inNo)
		{
			inNo = false;
			words.push_back(GetSubtext(S, NoPos, i));
		}

		if (isSyb(S[i]))
		{
			words.push_back(GetSubtext(S, i, i + 1));
		}

		if (S[i] == '(')
		{
			ParDep++;
			ParPos = i + 1;
			size_t j;
			for (j = i + 1; ParDep && j <= len; j++)
			{
				if (S[j] == '(') ParDep++;
				if (S[j] == ')') ParDep--;
			}
			if (j <= len)
			{
				string recResult(calc(GetSubtext(S, ParPos, j - 1), strlen(GetSubtext(S, ParPos, j - 1))));
				if (recResult == "不正确的公式!")
					return "不正确的公式!";
				else if (recResult == "括号不匹配!")
					return "括号不匹配!";
				else
					words.push_back(recResult);
			}
			else
			{
				return "括号不匹配!";
			}
			i = j;
		}
		if (S[i] == ')')
			return "括号不匹配!";
	}

	//检查公式
	long double tempResult = 0;
	static char CtmpResult[50];
	if (words.size() == 0) return "0.";
	if (words.size() == 1)
		if (!isSyb(words.front().c_str()))
		{
			strcpy_s(CtmpResult, words.front().c_str());
			return CtmpResult;
		}
		else
			return "不正确的公式!";
	if (words.front() == "*" || words.front() == "/")
		return "不正确的公式!";
	if (words.front() == "+" || words.front() == "-")//前面是符号
	{
		if (!isSyb((words.begin() + 1)->c_str()))
		{
			words.front() += *(words.begin() + 1);
			words.erase(words.begin() + 1); //删除第二个
		}
		else
			return "不正确的公式!";
	}
	if (isSyb(words.back().c_str()))
		return "不正确的公式!";
	for (deque<string>::iterator i = words.begin() + 1; i < words.end(); i++)
	{
		if ((*i == "*" || *i == "/") && (isSyb((i - 1)->c_str())) && strlen((i - 1)->c_str()))
			return "The formula is illegal!";
		if ((*i == "+" || *i == "-") && (isSyb((i - 1)->c_str())) && strlen((i - 1)->c_str()))
			if (!isSyb(*((i + 1)->c_str())))
			{
				*(i + 1) = *i + *(i + 1);
				i = words.erase(i);
			}
			else
				return "不正确的公式!";
	}

	for (deque<string>::iterator i = words.begin(); i != words.end(); i++)
	{
		if (*i == "*")
		{
			tempResult = atof((i - 1)->c_str()) * atof((i + 1)->c_str());
			_gcvt_s(CtmpResult, tempResult, 47);
			i = words.erase(i - 1);
			i = words.erase(i);
			*i = CtmpResult;
		}
		if (*i == "/")
		{
			tempResult = atof((i - 1)->c_str()) / atof((i + 1)->c_str());
			_gcvt_s(CtmpResult, tempResult, 47);
			i = words.erase(i - 1);
			i = words.erase(i);
			*i = CtmpResult;
		}
	}
	for (deque<string>::iterator i = words.begin(); i != words.end(); i++)
	{
		if (*i == "+")
		{
			tempResult = atof((i - 1)->c_str()) + atof((i + 1)->c_str());
			_gcvt_s(CtmpResult, tempResult, 47);
			i = words.erase(i - 1);
			i = words.erase(i);
			*i = CtmpResult;
		}
		if (*i == "-")
		{
			tempResult = atof((i - 1)->c_str()) - atof((i + 1)->c_str());
			_gcvt_s(CtmpResult, tempResult, 47);
			i = words.erase(i - 1);
			i = words.erase(i);
			*i = CtmpResult;
		}
	}

	strcpy_s(CtmpResult, words.front().c_str());
	return CtmpResult;
}

int main()
{
	string s;
	char S[LENGTH];
	while (1)
	{
		cout << "请输入算式:";
		cin.getline(S, LENGTH);
		cout << "结果为" << calc(S, strlen(S)) << endl;
	}
	return 0;
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值