基于栈的中缀算术表达式求值加强版(将小数,括号,多位数等情况包含在内),可同时运算多条计算式并将结果一同输出。

用循环解决小数和多位数问题,未使用atof函数。采用将运算式保存在二维数组的方式进行数据的输入,最后整齐输出。

#include
#include
#include
#include
//#pragma warning(disable:)
using namespace std;
#define MAXSIZE 1000
typedef struct OPND{
	double *base;
	double *top;
	int stacksize;
}stack1;
typedef struct OPTR {
	char *top;
	char *base;
	int stacksize;
}stack2;
double operate(double a,stack2 &t, double b) {
	if (*t.top == '+') {
		return a+b;
	}
	if (*t.top == '-') {
		return a-b;
	}
	if (*t.top == '*') {
		return a*b;
	}
	if (*t.top == '/') {
		return a / b;
	}
}
void compare(char ch,stack2 &p,stack1&s) {
	switch (ch) {
	case '+':if (*p.top == '*' || *p.top == '/') {
		*(s.top - 2) = operate(*(s.top - 2), p, *(s.top - 1));
		*(s.top - 1) = 0;
		s.top--;
		*p.top = '+';
		break;
	}
			 else {
		p.top++;
		*p.top = '+';
		break;
	}
	case'-':if (*p.top == '*' || *p.top == '/') {
		*(s.top - 2) = operate(*(s.top - 2), p, *(s.top - 1));
		*(s.top - 1) = 0;
		s.top--;
		*p.top = '-';
		break;
	}
			else {
		p.top++;
		*p.top = '-';
		break;
	}
	case')':while (*p.top != '(') {
		*(s.top - 2) = operate(*(s.top - 2), p, *(s.top - 1));
		*(s.top - 1) = 0;
		s.top--;
		p.top--;
		if (*p.top == '(') {
			p.top--;
			break;
		}
	}
			break;
	case'*':if (*p.top == ')' || *p.top == '/' || *p.top == '*') {
		*(s.top - 2) = operate(*(s.top - 2), p, *(s.top - 1));
		*(s.top - 1) = 0;
		s.top--;
		*p.top = '*';
		break;
	}
			else {
		p.top++;
		*p.top = '*';
		break;
	}
	case'/':if (*p.top == ')' || *p.top == '/' || *p.top == '*') {
		*(s.top - 2) = operate(*(s.top - 2), p, *(s.top - 1));
		*(s.top - 1) = 0;
		s.top--;
		*p.top = '/';
		break;
	}
			else {
		p.top++;
		*p.top = '/';
		break;
	}
	case '(': {
		p.top++;
		*p.top = '(';
		break;
	}
	case'=':while (*p.top != 0) {
		*(s.top - 2) = operate(*(s.top - 2), p, *(s.top - 1));
		*(s.top - 1) = 0;
		s.top--;
		p.top--;
	}
	}
}
void function(stack1 &s, stack2 &r, char str[][20], int i) {
	int m = 0, k = 0,j=0,n=0;
	double result[10] = { 0 };
	double multnum[10] = { 0 };
	double minenum[10] = { 0 };
	s.base = new double[MAXSIZE];
	r.base = new char[MAXSIZE];
	*r.base = NULL;
	while(i){
		*s.base = NULL;
		r.top = r.base;
		s.top = s.base;
		while (str[k][m] != 0) {
			if (isdigit(str[k][m])) {
				while (isdigit(str[k][m])) {
					multnum[n] = str[k][m] - 48;
					n++;
					m++;
				}
				while (n >= 2) {
					int t = 1;
					multnum[n - 2] = pow(10, t)*multnum[n - 2] + multnum[n - 1];
					multnum[n - 1] = 0;
					n--;
					t++;
				}
				n--;
				*s.top = multnum[n];
				s.top++;
			if (str[k][m] == '.') {
				m++;
				while (isdigit(str[k][m])) {
					minenum[n] = str[k][m] - 48;
					m++;
					n++;
				}
				if (n >= 2) {
					while (n >= 2) {
						minenum[n - 2] = minenum[n - 2] / pow(10, n - 1) + minenum[n - 1] / pow(10, n);
						minenum[n - 1] = 0;
						n--;
					}
					n--;
					*(s.top - 1) = *(s.top - 1) + minenum[n];
				}
				else {
					n--;
					*(s.top - 1) = *(s.top - 1) + minenum[n] / 10;
				}
			}
		}
			else {
				compare(str[k][m], r, s);
				m++;
			}
		}
		k++;
		result[j] = *s.base;
		j++;
		i--;
		m = 0;
	}
	while (j) {
		cout << result[i] << endl;
		j--;
		i++;
	}
}
int main() {
	char str[10][20] = { {0} ,{0} },ch=0;
	stack1 s; stack2 r;
	int i = 0;
	cout << "请输入算术表达式,并以‘=’结尾" << endl;
	while (1) {
		cin >> str[i];
		if (!strcmp(str[i], "=")) {
			break;
		}
		i++;
	}
	function(s, r,str,i);
	system("pause");
	return 0;
}

循环语句较多,看不懂需要帮助的可以私聊我,我给你注释。

你可能感兴趣的:(日常写程序)