| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 451 人关注过本帖
标题:求教这段代码段错误的原因
只看楼主 加入收藏
linlin217
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2022-11-5
收藏
 问题点数:0 回复次数:0 
求教这段代码段错误的原因
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxsize 100
#define false 0;
#define true 1;
typedef struct{  //数字栈的定义
    int data[maxsize];
    int top;
}shustack;
typedef struct{  //符号栈的定义
    char data[maxsize];
    int top;
}fustack;
int Initsign(fustack *s){ //符号栈初始化
     s=(fustack*)malloc(100*sizeof(fustack));
     s->top=-1;
     if(s==NULL){
         return false;
     }else{
         return true;
     }
 }
int Initshu(shustack *s){ //数字栈初始化
    s=(shustack *)malloc(sizeof(shustack));
     s->top=-1;
     if(s==NULL){
         return false;
     }else{
         return true;
     }
}
int pushfu(fustack *s,char c){ //符号入栈
     if(s->top==maxsize-1){
         printf("栈满!");
         return false;
     }else{
        s->top++;
        s->data[s->top]=c;
        return true;
     }
 }
int pushshu(shustack *s,int x){ //数字入栈
     if(s->top==maxsize-1){
         printf("栈满!");
         return false;
     }else{
        s->top++;
        s->data[s->top]=x;
        return true;
     }
 }
char popfu(fustack *s,char a) //符号出栈
{
    if(s->top==-1){
          printf("栈空!");
        return false;   
    }else{
        a= s->data[s->top];
        s->data[s->top]=0;
        s->top--;
        return a;
    }
}
int popshu(shustack *s,int x){ //数字出栈
    if(s->top==-1){
        printf("栈空!");
        return false;
    }else{
        x=s->data[s->top];
        s->data[s->top]=0;
        s->top--;
        return x;
    }
}
char getfutop(fustack *s){ //取符号栈栈顶
     if(s->top==-1){
        printf("栈空fu!");
        return false;
    }else{
        return s->data[s->top];   
    }
 }
 int getshutop(shustack *s){ //取数字栈栈顶
     if(s->top==-1){
        printf("栈空shu!");
        return false;
    }else{
        return s->data[s->top];   
    }
 }
//计算表达式
 int operate(int a,char f,int b)   
{   
    if(f=='+'){
        return a+b;
    }else if(f=='-'){
        return a-b;   
    }else if(f=='*'){
        return a*b;
    }else if(f=='/'){
         return a/b;
    }
}
char compare(char a,char b)//比较符号优先级
{
   if(a=='+'||a=='-'){
        if(b=='*'||b=='/'||b=='('){
            return '<';
        }else{
            return '>';
        }
    }else if(a=='*'||a=='/'){
        if(b=='(') return '<';
        else return '>';
    }else if(a=='('){
        if(b=='#')printf("输入错误"); //缺少右括号
        if(b==')') return '=';
        else return '>';
    }else if(a==')'){
        if(b==')') return '>';
        else printf("输入错误");//缺少左括号
    }else if(a='#'){
        if(b=='#') return '=';
        else return '>';
    }
}
int compute(char *exp){
    shustack *s;  //数字栈
    fustack  *f;  //符号栈
    Initshu(s);
    Initsign(f);
    int a,b,data,num;
    char F,F1;
    pushfu(f,'#'); //符号栈以'#'压栈
    while(*exp!='#'||getfutop(f)!='#'){     //当输入的字符和符号栈栈顶元素不为'#'
        char ch=*exp;
        if(ch>='0'&&ch<='9')             //如果为数字符号
         {
             data=ch-'0';                  //将数字符号转换为十进制数字
             pushshu(s,data);              //数字进栈
             exp++;               //下一个字符
         }   
        else{                             //如果不为数字符号
            switch(compare(getfutop(f),ch))  //比较符号栈栈顶元素和取到的符号的优先级
            {
                case'<':pushfu(f,ch);     //如果小于,进符号栈
                 exp++;break;       //下一个字符
            
                 case'=':popfu(f,F);      //如果等于,符号栈出栈(代表有左右括号)
                 exp++;break;      //下一个字符
            
                 case'>':                 //如果大于
                popfu(f,F1);             //符号栈栈顶元素出栈赋值给F1
                popshu(s,b);             //数字栈栈顶元素出栈赋值给b
                popshu(s,a);            //数字栈栈顶元素出栈赋值给a
                int q=operate(a,F1,b);  //计算a,F1,b 的值赋给q
                 pushshu(s,q);            //q进操作数栈
                 break;
             }
             }
    return popshu(s,num);
    free(s);
    free(f);         
        }
}
int main(){      
    char str[100];  
    char *exp;
    scanf("%s",str); //输入字符串表达式
    exp=str;
    int l=strlen(str),num,i;
    if(str[0]!='#'||str[l-1]!='#'){
    printf("输入错误");
    }else{
        num=compute(exp++);
        printf("%d",num);    //输出结果
    }
}
搜索更多相关主题的帖子: top  int 符号 return 
2022-11-05 16:45
快速回复:求教这段代码段错误的原因
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014799 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved