(1)使用简单的C语句实现:要求不含左右括号,但是不限制10以内数据
#include "stdio.h"
#include "stdlib.h"
char newstr[100];
int calculate1(char str[],int len)
{
int i=0,j=0,k=0,l=0,front=0,behind=0;
int number1 = 0, number3 = 0,result = 0,result1 = 0, temp = 0, temp1 = 0;
float number2 = 0.0;
char add[100]={0};
int res[100]={0};
for(i = 0; i < len; i++)
{
if(str[i] == '*' || str[i] == '/')
{
front=i;behind=i; //记录当前位置
for(front=(i-1);front>=0;front--) //取出当前符号的前一个数据
{
if((str[front] == '+') || (str[front] == '-')) break;
//temp = ((int)(str[front]-'0'));
//printf("%d\n",temp);
number1 = number1 + ((int)(str[front]-'0')) * pow(10,i-front-1); //变成10进制数
}
//printf("%d\n",number1);
for(behind=(i+1);behind=0;i--)
{
//printf("%d\t",res[i]);
newstr[j++] = (char)(res[i] + '0'); //添加中间结果
//printf("%c\t",newstr[j-1]);
}
newstr[j] = '\0';
for(j=behind;j=0;front--) //取出当前符号的前一个数据
{
if((str[front] == '+') || (str[front] == '-')) break;
//temp = ((int)(str[front]-'0'));
//printf("%d\n",temp);
number1 = number1 + ((int)(str[front]-'0')) * pow(10,i-front-1); //变成10进制数
}
//printf("\n%d\n",number1);
for(behind=(i+1);behind=0;i--)
{
//printf("%d\t",res[i]);
newstr[j++] = (char)(res[i] + '0'); //添加中间结果
//printf("%c\t",newstr[j-1]);
}
newstr[j] = '\0';
//newstr[j++] = (char)(result + '0'); //添加中间结果
//newstr[j] = '\0';
//puts(newstr);
for(j=behind;j=0;front--) //取出当前符号的前一个数据
{
//temp = ((int)(str[front]-'0'));
//printf("%d\n",temp);
num1 = num1 + ((int)(newstr[front]-'0')) * pow(10,length-front-1); //变成10进制数
}
}
else if(j == 0 && k != 0)//如果只有加减没有乘除
{
num1 = calculate2(newstr,length);
}
else//既有加减也有乘除
{
num1 = calculate1(newstr,length);
if(num1 == -1)//乘除结束标志
{
length = strlen(newstr);
num1 = calculate2(newstr,length);//计算加减
}
}
printf("%d",num1);
return 0;
}
(2)使用栈结构:将中序表达式转为后序表达式,然后在栈中计算
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "stdlib.h"
#define MaxSize 50
typedef struct Stack {
char *data;
int Top;
int TopMaxSize;
}StackNode,*Stacklink;
Stacklink CreatStack(void) {
Stacklink stack = (Stacklink)malloc(MaxSize * sizeof(StackNode));
stack -> data = (char *)malloc(MaxSize * sizeof(char));
stack -> Top = -1;
stack -> TopMaxSize = MaxSize;
return stack;
}
void Push(Stacklink S,char ch) {
if(S -> Top + 1 == S -> TopMaxSize) {
printf("堆栈满");
return;
} else {
S -> data[++(S -> Top)] = ch;
return;
}
}
char Pop(Stacklink S) {
if(S -> Top == -1) {
printf("堆栈空");
return -1;
} else {
return S -> data[S -> Top --];
}
}
void MidtoFinish(char *a,char *b) { //3+((5*6)-(7/1*7))*9
int i = 0,j = 0;
char temp;
Stacklink S = CreatStack();
while(*a != '\0') {
if(*a >= '0' && *a <= '9') { //数字直接输出
*(b++) = *a;
} else {
if(S -> Top == -1 || '(' == *a) { //空栈或者左括号直接入栈
Push(S,*a);
}
else if(')' == *a) { //遇到右括号时将遇到左括号之前的运算符全部出栈,并丢弃左右括号
temp = Pop(S);
while('(' != temp) {
*(b++) = temp;
temp = Pop(S);
}
}
else {
temp = Pop(S); //其他情况,主要是分为加减遇到乘除,乘除遇到加减,加减遇到加减,乘除遇到乘除
if('+' == *a || '-' == *a) {
if('(' == temp) {//还没有遇到右括号之前,防止把左括号弹出
Push(S,temp);
Push(S,*a);
}
else {
*(b++) = temp; //加减遇到乘除,加减遇到加减
Push(S,*a);
}
}
else if('*' == *a || '/' == *a) {
if('*' == temp || '/' == temp) {//乘除遇到乘除
*(b++) = temp;
Push(S,*a);
}
else {
Push(S,temp);//乘除遇到加减
Push(S,*a);
}
}
}
}
a++;
}
while(S -> Top != -1) { //字符串a结束,将栈中所有的符号放入b
*(b++) = Pop(S);
}
*b = '\0';
}
void Operator(Stacklink S,char op) { //后序计算程序,计算相邻两个数据的值
int temp1,temp2;
temp2 = Pop(S) - '0'; //弹出最近的两个数据
temp1 = Pop(S) - '0';
switch (op) { //根据运算符进行计算
case '+': Push(S,(char)(temp1+temp2+'0')); break;
case '-': Push(S,(char)(temp1-temp2+'0')); break;
case '*': Push(S,(char)(temp1*temp2+'0')); break;
case '/': Push(S,(char)(temp1/temp2+'0')); break;
}
}
int main()
{
Stacklink S = CreatStack();
char a[50],b[50],result;
int i = 0;
gets(a); //输入中序算术表达式 //(1*2+4)/3-1 (2*5+2)/3-1 931-3*+102/+ 3+((5*6)-(7/1*7))*9 3+(1*4-2/1*1)*2
MidtoFinish(a,b); //使用栈将中序变成后序表达式
puts(b); //输出后序表达式 //12*4+3/1- 25*2+3/1- 931-3*+102/+ 356*71/7*-9*+ 314*21/1*-2*+
while(b[i] != '\0') {
if(b[i] == '+' || b[i] == '-' || b[i] == '*' || b[i] == '/' )
Operator(S,b[i]); //调用后序计算程序,计算相邻两个数据的值
else if(b[i] >= '0' && b[i] <= '9')
Push(S,b[i]); //数据直接压栈
i++;
}
result = Pop(S);
printf("结果为:%d\n",result-'0');
return 0;
}