多项式加减乘
描述: 多项式加减乘
有A,B,C…… n 个关于x的个多项式,求一个关于A,B,C……的多项式的结果
多项式的每一项之间用空格隔开,每一项的系数,x,指数之间也用空格隔开,系数为0的项不输出
输入: 基于多项式A,B,C……的运算表达式
关于x的多项式A
关于x的多项式B
关于x的多项式C
…
输出: 表达式的运算结果
含有A,B,C……表达式的运算结果,保留两位小数。
输出多项式表达式按指数降序排序
输入样例: (A+B)*(A-B)
A 1 x 2.1 3 x -3.09
B 2 x 1 2 x 0
输出样例: 1.00 x 4.20 -4.00 x 2.00 -8.00 x 1.00 -4.00 x 0.00 6.00 x -0.99 9.00 x -6.18
[解决办法]
- C/C++ code
//表达式求值#include <stdio.h>#include <malloc.h>#include <string.h>/**功能:根据运算符计算*参数:a, b参与运算的数, ch运算符*返回值:计算结果,操作符错误则返回0*/int cal(int a, char ch, int b){ switch(ch) { case '+': return a+b; break; case '-': return a-b; break; case '*': return a*b; break; case '/': return a/b; break; } return 0;}/**功能:计算表达式的值(用数组模拟栈)*参数:表达式字符串*返回值:计算结果*/int evaluateExpression(char *str){ int i = 0, result, numSub = 0, operSub = 0; int tmp, len = strlen(str); int *operand = (int*)malloc(sizeof(int)*len); char *operat = (char*)malloc(sizeof(char)*len); while(str[i] != '\0') { switch(str[i]) { case '+': while(operSub > 0 && operat[operSub-1] != '(') { printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]); operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]); printf("%d\n", operand[numSub-2]); --numSub; --operSub; } operat[operSub++] = '+'; break; case '-': while(operSub > 0 && operat[operSub-1] != '(') { printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]); operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]); printf("%d\n", operand[numSub-2]); --numSub; --operSub; } operat[operSub++] = '-'; break; case '*': if(str[i+1] >= '0' && str[i+1] <= '9') { tmp = 0; while(str[i+1] >= '0' && str[i+1] <= '9') { tmp = tmp * 10 + str[i+1] - '0'; ++i; } --i; printf("%d * %d = ", operand[numSub-1], tmp); operand[numSub-1] = cal(operand[numSub-1], '*', tmp); printf("%d\n", operand[numSub-1]); ++i; } else operat[operSub++] = '*'; break; case '/': if(str[i+1] >= '0' && str[i+1] <= '9') { tmp = 0; while(str[i+1] >= '0' && str[i+1] <= '9') { tmp = tmp * 10 + str[i+1] - '0'; ++i; } --i; printf("%d / %d = ", operand[numSub-1], tmp); operand[numSub-1] = cal(operand[numSub-1], '/', tmp); printf("%d\n", operand[numSub-1]); ++i; } else operat[operSub++] = '/'; break; case '(': operat[operSub++] = '('; break; case ')': while(operat[operSub-1] != '(') { printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]); operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]); printf("%d\n", operand[numSub-2]); --numSub; --operSub; } --operSub; break; default: tmp = 0; while(str[i] >= '0' && str[i] <= '9') { tmp = tmp * 10 + str[i] - '0'; ++i; } --i; operand[numSub++] = tmp; break; } ++i; } while(numSub > 1 && operSub >= 1) { printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]); operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]); printf("%d\n", operand[numSub-2]); --numSub; --operSub; } result = operand[numSub-1]; free(operand); free(operat); return result;}int main(){ char *str = "225/15-20+(4-3)*2"; int result; printf("计算过程:\n"); result = evaluateExpression(str); printf("计算结果:result = %d\n", result); return 0;}