计算器程序

计算器程序,非递归实现

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <deque>
#include <vector>
#include <stack>
#include <sstream>
using namespace std;

void calculateAndOutput(char* str);
int isspace(int x)
{
	if(x == ' '|| x == '\t' || x == '\n' || x == '\f' || x == '\b' || x == '\r')
		return 1;
	else  
		return 0;
}

int isdigit(int x)
{
	if(x <= '9' && x >= '0')         
		return 1;
	else 
		return 0;
}

int toInteger(string str)
{
	int i = 0;
	int c;              /* current char */
	int total;         /* current total */
	int sign;           /* if '-', then negative, otherwise positive */
	
	/* skip whitespace */
	while(isspace((int)(unsigned char)str[i]) )
		++i;
	
	c = (int)(unsigned char)str[i++];
	sign = c;           /* save sign indication */
	if(c == '-' || c == '+')
		c = (int)(unsigned char)str[i++];    /* skip sign */
	
	total = 0;
	
	while(isdigit(c))
	{
		total = 10 * total + (c - '0');     /* accumulate digit */
		c = (int)(unsigned char)str[i++];    /* get next char */
	}
	
	if(sign == '-')
		return -total;
	else
		return total;   /* return result, negated if necessary */
}

string intToString(int v)
{
	string str;
	ostringstream oss;
	oss << v;
	return (oss.str());
}

char *getLine()
{
	static char str[1025];
	fgets(str, 1024, stdin);
	int len = strlen(str);
	while(--len >= 0)
	{
		if(str[len]=='\r' || str[len]=='\n')
			str[len] = '\0';
		else
			break;
	}
	return str;
}

int main(int argc, char ** argv)
{

    char *input = getLine();
    calculateAndOutput(input);
    return 0;
}

void calculateAndOutput(char* str)
{
	int len = strlen(str);
	int i = 0;
	stack<string> st;
	while(i < len)
	{
		if(str[i] == '(' || str[i] == ')')
		{
			string ss = "";
			ss += str[i];
			st.push(ss);
			++i;
		}
		else
		{
			string ss = "";
			int j = i;
			while(j < len && str[j] != ' ' && str[j] != '(' && str[j] != ')')
			{
				ss += str[j];
				++j;
			}
			st.push(ss);
			while(j < len && str[j] == ' ')
				++j;
			if(j - i > 2 || j - i == 0)
			{
				cout << "Syntax Error" << endl;
				return;
			}
			i = j;
		}
	}
	
	stack<string> backup;
	vector<int> nums;
	int total = 0;
	while(!st.empty())
	{
		if(st.top() == ")")
		{
			backup.push(")");
			st.pop();
		}
		else if(st.top() == "+" || st.top() == "-" || st.top() == "*" || st.top() == "/")
		{
			
			if(nums.size() > 0)
			{
				total = nums[nums.size() - 1];
				if(st.top() == "+")
				{
					for(int i = 0; i < nums.size() - 1; ++i)
						total += nums[i];
				}
				if(st.top() == "-")
				{
					for(int i = 0; i < nums.size() - 1; ++i)
						total -= (nums[i]);
				}
				if(st.top() == "*")
				{
					for(int i = 0; i < nums.size() - 1; ++i)
						total *= (nums[i]);
				}
				if(st.top() == "/")
				{
					for(int i = 0; i < nums.size() - 1; ++i)
					{
						if(nums[i] == 0)
						{
							cout << "Syntax Error" << endl;
							return;
						}
						total /= (nums[i]);
					}
				}
			}
			else
			{
				cout << "Syntax Error" << endl;
				return;
			}
			
			st.pop();
			if(st.top() == "(" && !backup.empty() && backup.top() == ")")
			{
				st.pop();
				backup.pop();
				if(!st.empty())
					st.push(intToString(total));
			}
			else if(st.top() == "(" && backup.empty())
			{
				cout << "Syntax Error" << endl;
				return;
			}
			else
			{
				cout << "Syntax Error" << endl;
				return;
			}
			nums.clear();
		}
		else
		{
			nums.push_back(toInteger(st.top()));
			st.pop();
		}
	}
	
	cout << total << endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值