c语言表达式求值 只能算两个数的操作 超过了就有错误 麻烦大神看看怎么回事部分显示是我为了方便测试加的

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<malloc.h>
#define size 100
#define size_1 10
typedef char Elemtype;
void InitStack ();
void Push();void function();
void Pop();
char sign(char ch1,char ch2);
char firstvalue(char ch);
typedef struct {
Elemtype *top;
Elemtype *base;
Elemtype stackSize;
}sqStack;
void InitStack (sqStack *s){
s->base=(Elemtype *)malloc(size * sizeof(Elemtype));
if(!s->base){
exit(0);
}
s->top=s->base;
s->stackSize=size;
}
void Push(sqStack *s,Elemtype e){
if(s->top - s->base >= s->stackSize){
s->base=(Elemtype *)realloc(s->base,(s->stackSize+size_1)* sizeof(Elemtype));
if(!s->base) exit(0);
}
*(s->top)=e;
s->top++;
}
void Pop(sqStack *s,char *e){

if(s->top == s->base)
return;
*e=*--(s->top);
}

int count(char b,char h,char a)
{
int i=0;
switch(a)
{
case '+':i=(h-'0')+(b-'0'); printf("执行了加法\n");;break;
case '-':i=(b-'0')-(h-'0'); printf("执行了减法\n");break;
case '*':i=(b-'0')*(h-'0'); printf("执行了乘法\n");break;
case '/':i=(b-'0')/(h-'0'); printf("执行了除法\n");break;

default:printf("错误\n");break;}
return i;
}
char firstvalue(char ch)
{
int k=0;
switch(ch)
{
case '+':k=0;break;
case '-':k=1;break;
case '*':k=2;break;
case '/':k=3;break;
case '(':k=4;break;
case ')':k=5;break;
case '#':k=6;break;
}
return k;}
char sign(char ch1,char ch2)
{
int i,j;
char a[7][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}

};
i=firstvalue(ch1);
j=firstvalue(ch2);//调用函数两次,返回两个函数值
return a[i][j];
}
int main(){
char a,e;
char n,c,b,k,h;
sqStack s1,s2;
InitStack(&s1);
InitStack(&s2);
Push(&s1,'#');
printf("请输入表达式,以#结尾\n");
c=getchar();
while(c!='#'||*(s1.top-1)!='#') {
if(c<='9'&&c>='0'){
Push(&s2,c);
c=getchar();

printf("已经压栈数字\n\n");
}
else
switch(sign(*(s1.top-1),c))
{
case'<':printf("已经执行1\n\n");Push(&s1,c);c=getchar();break;
case'=':Pop(&s1,&e);printf("已经执行9\n\n");break;
case'>':Pop(&s1,&a);Pop(&s2,&b);printf("b为:%c\n",b);Pop(&s2,&h);printf("h为:%c\n",h);n=count(b,h,a);printf("n为:%d \n\n",n);Push(&s2,n);printf("已经执行2 \n\n");break;
}}

Pop(&s2,&k);
printf("结果为:%d \n",k);
return 0;
}

// 测试数据:
// 3+5-9+7=6
// 3*4+2*6=24
// 3*(4+2)*6=108
// 3+5*9-7=41
// (3+5)*(9-7)=16
// 2*6*8/4=24
// 3*(4+8-7)*6=90
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<malloc.h>
#define size 100
#define size_1 10
typedef char Elemtype;
//原代码void InitStack ();
//原代码void Push();void function();
//原代码void Pop();
char sign(char ch1,char ch2);
char firstvalue(char ch);
typedef struct
{
    Elemtype *top;
    Elemtype *base;
    Elemtype stackSize;
}sqStack;

void InitStack (sqStack *s)
{
    s->base=(Elemtype *)malloc(size * sizeof(Elemtype));
    if(!s->base)
    {
        exit(0);
    }
    s->top=s->base;
    s->stackSize=size;
}
void Push(sqStack *s,Elemtype e)
{
    if(s->top - s->base >= s->stackSize)
    {
        s->base=(Elemtype *)realloc(s->base,(s->stackSize+size_1)* sizeof(Elemtype));
        if(!s->base) exit(0);
    }
    *(s->top)=e;
    s->top++;
}
void Pop(sqStack *s,char *e)
{
    if(s->top == s->base)
        return;
    *e=*--(s->top);
}

//b是第1操作数,h是第2操作数
//例如,b - h = 7 - 1 = 6
//     b / h = 8 / 4 = 2
int count(char b,char h,char a)
{
    int i=0;
    switch(a)
    {
        case '+':
            i = b + h;
            break;
        case '-':
            i = b - h;
            break;
        case '*':
            i = b * h;
            break;
        case '/':
            if(h==0) //除数不能为0
            {
                i=0;
            }
            else
            {
               i = b / h;
            }
            break;
        default:
            printf("错误\n");
            break;
    }
    return i;
}
char firstvalue(char ch) //获得该运算符的优先级
{
    int k=0;
    switch(ch)
    {
        case '+':
            k=0;
            break;
        case '-':
            k=1;
            break;
        case '*':
            k=2;
            break;
        case '/':
            k=3;
            break;
        case '(':
            k=4;
            break;
        case ')':
            k=5;
            break;
        case '#':
            k=6;
            break;
        default:   //补充这个语句
            k=7;
            break;
    }
    return k;
}

char sign(char ch1,char ch2) //比较两个运算符的优先级
{
    int i,j;
    char a[7][7]={
    {'>','>','<','<','<','>','>'},
    {'>','>','<','<','<','>','>'},
    {'>','>','>','>','<','>','>'},
    {'>','>','>','>','<','>','>'},
    {'<','<','<','<','<','=','&'},
    {'>','>','>','>','&','>','>'},
    {'<','<','<','<','<','&','='}};
    //将数组a[][]里的空格' '改为'&"
    //原代码
    /*
    char a[7][7]={
    {'>','>','<','<','<','>','>'},
    {'>','>','<','<','<','>','>'},
    {'>','>','>','>','<','>','>'},
    {'>','>','>','>','<','>','>'},
    {'<','<','<','<','<','=',' '},
    {'>','>','>','>',' ','>','>'},
    {'<','<','<','<','<',' ','='}};
    */
    i=firstvalue(ch1); //栈顶[*(s1.top-1)]"运算符"的优先级
    j=firstvalue(ch2); //当前[c]"运算符"的优先级
    if(i>6 || j>6) //补充这个语句
    {
        return '&'; //输入的"运算符"有错误
    }
    return a[i][j];
}
int main()
{
    char a,e;
    char n,c,b,k,h;
    sqStack s1,s2;  //s1运算符栈  s2数字栈
    InitStack(&s1);
    InitStack(&s2);
    Push(&s1,'#');
    printf("请输入表达式,以#结尾\n");
    c=getchar();
    while(c!='#'||*(s1.top-1)!='#')
    {
        if(c<='9'&&c>='0')
        {
            //将ASCII转换为数字
            //这一步,一定要提前完成,不要让函数count来完成
            c=c-'0';

            Push(&s2,c);
            c=getchar();
        }
        else
        {
            char preOp;
            char whatSign;

            preOp=*(s1.top-1); //[运算符]栈顶的运算符
            whatSign=sign(preOp,c); //两个运算符的等级对比
            switch(whatSign)
            {
            case'<':
                Push(&s1,c);
                c=getchar();
                break;
            case'=':
                Pop(&s1,&e);
                c=getchar(); //补充这个语句
                break;
            case'>':
                Pop(&s1,&a); //运算符出栈:运算符=a
                //原代码Pop(&s2,&b);
                Pop(&s2,&h); //数字出栈:第2操作数=h
                //原代码Pop(&s2,&h);
                Pop(&s2,&b); //数字出栈:第1操作数=b
                n=count(b,h,a);
                Push(&s2,n);  //中间的计算结果,重新入数字栈
                break;
            case '&':   //补充这个语句
                printf("\n输入的表达式有错误.\n");
                return 0;
            } //switch结束
        } //if(c<='9'&&c>='0')else结果
    } //while循环结束

    Pop(&s2,&k);
    printf("结果为:%d \n",k);
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-05-01
数据结构啊,大一的吗