拾三小白 2022-03-05 14:43 采纳率: 50%
浏览 40
已结题

C控制台程序,不让我输入就结束了这是咋回事(顺序栈求解算术表达式问题)

###### 问题遇到的现象和发生背景 

###### 问题相关代码,请勿粘贴截图 

###### 运行结果及报错内容 

###### 我的解答思路和尝试过的方法 

###### 我想要达到的结果
#include <iostream>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
#define MAX_CAPTURE 20
using namespace std;
typedef struct line_stack
{
    int cnt;//栈有效元素个数
    int cap;//栈最大容量
    char *pBottom;//栈底//指向数组第一个元素的指针
}STACK,*PSTACK;

void initial(PSTACK SL);
bool empty_stack(PSTACK SL);
void destory(PSTACK SL);
void clear_stack(PSTACK SL);
int length_of_stack(PSTACK SL);
void push_stack(PSTACK SL,char ch);
bool pop_stack(PSTACK SL,char *s);//弹出一个字符串,第一个元素即为需要的元素
bool GetTop(PSTACK SL,char *s);
void traverse(PSTACK SL);
bool in(char c);
char precede(char a,char b);
char EvalueateExpression();

int main()
{
    /*STACK SL;
    char s[5];
    initial(&SL);
    push_stack(&SL,'a');
    push_stack(&SL,'y');
    push_stack(&SL,'s');
    traverse(&SL);
    if(empty_stack(&SL))
        cout << "栈空" << endl;
    else
        cout <<"栈不空" << endl;
    cout << "栈长度为:" << length_of_stack(&SL) << endl;

    pop_stack(&SL,s);
    cout << "弹出的元素为:" << s <<endl;
    cout << "栈长度为:" << length_of_stack(&SL) << endl;
    traverse(&SL);*/
    cout << "输入一个表达式(结果在-127-128之间|中间数在0-9之间): ";
    cout << EvalueateExpression() <<endl;
    return 0;
}

void initial(PSTACK SL)
{
    SL->pBottom = (char*)malloc(sizeof(char)*SL->cap);
    if(!SL->pBottom)
    {
        cout << "动态分配内存失败!" << endl;
        exit(-1);
    }
    SL->cnt = 0;
    SL->cap = MAX_CAPTURE;
    //cout << "初始化成功!" << endl;
}
 bool empty_stack(PSTACK SL)
 {
     if(SL->cnt == 0)
        return true;
     else
        return false;
 }

 void destory(PSTACK SL)
 {
    free(SL->pBottom);
    SL->pBottom = NULL;
    SL->cnt = 0;
 }
 void clear_stack(PSTACK SL)
 {
     while(SL->cnt != 0)
     SL->pBottom[SL->cnt-1] = '\0';
 }

int length_of_stack(PSTACK SL)
{
    int i = 0;
    if(empty_stack(SL))
        return i;
    else
    {
        return SL->cnt;
    }
}

void push_stack(PSTACK SL,char ch)
{
    if(SL->cnt  == SL->cap)
    {
        SL->pBottom = (char*)realloc(SL->pBottom,(sizeof(char)*(SL->cap + 2)));
        if(!SL->pBottom)
        {
            cout << "追加内存失败" << endl;
            exit(-1);
        }
        else
        {
            SL->cnt += 2;
        }
    }
    SL->pBottom[SL->cnt] = ch;
    SL->cnt++;
    //SL->pBottom[SL->cnt] = '\0';
}

bool pop_stack(PSTACK SL,char *s)
{
    if(empty_stack(SL))
    {
        cout << "栈空,无元素可弹出" << endl;
        return false;
    }
    else
    {
        s[0] = SL->pBottom[(SL->cnt)-1];
        SL->cnt--;
        return true;
    }
}
bool GetTop(PSTACK SL,char *s)
{
    if(!empty_stack(SL))
    {
        s[0] = SL->pBottom[(SL->cnt)-1];
        return true;
    }
    else
    {
        cout << "栈空,无法GETTOP" << endl;
        return false;
    }
}
void traverse(PSTACK SL)
{
    if(empty_stack(SL))
        cout << "栈空,无法遍历" << endl;
    else
    {
        int i = SL->cnt;
        cout << "栈内元素有:";
        while(i > 0)
        {
            cout << SL->pBottom[i-1] << " ";
            --i;
        }
        cout << endl;
    }
}

bool in(char c)
{
    if(c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')'|| c == '\n')
        return true;
    else
        return false;
}

char operate(char c1,char tha, char c2)
//注意c1\c2顺序要与表达式一致
{
    switch(tha)
    {
        case '+':return c1+c2;
        case '-':return c1-c2;
        case '*':return c1*c2;
    }
    return c1/c2;
}//为啥÷要放在选择结构外面??

char precede(char a,char b)
{
    char f;
    switch(b)
    {
        case '+':
        case '-':if(a == '(' || a == '\n')
                        f = '<';
                    else
                        f = '>';
                    break;
        case '*':
        case '/':if(a == ')'|| a == '*' || a == '/')
                        f = '>';
                    else
                        f = '<';
                break;
        case '(':if(a == ')')
                    {
                        cout << "括号不符合规则!" << endl;
                        exit(-1);
                    }
                    else
                        f = '<';
                    break;
        case ')':switch(a)
                    {
                        case '(':f = '=';
                                break;
                        case '\n':
                            {
                                cout << "缺失左括号" << endl;
                                exit(-1);
                            }
                        default:f = '>';
                    }
                    //break;
        case '\n':switch(a)
        {
            case '(':
            {
                cout << "缺失右括号" << endl;
                exit(-1);
            }
            case '\n':f = '=';
                    break;
            default: f = '>';
        }

    }
    return f;
}
char EvalueateExpression()
{
    PSTACK OPND,OPTR;
    initial(OPND);
    initial(OPTR);
    push_stack(OPTR,'\n');
    char c,x,a,b;
    char s[5];
    c = getchar();
    GetTop(OPTR,s);
    x = s[0];
    while (c != '\n' || x != '\n')
    {
        if(in(c))
        {
            switch(precede(x,c))
            {
            case '>':
                    push_stack(OPTR,c);
                    c =getchar();
                    break;
            case '=':
                    pop_stack(OPTR,s);
                    c = getchar();
                    break;
            case '<':
                    pop_stack(OPTR,s);
                    x = s[0];
                    pop_stack(OPND,s);
                    b = s[0];
                    pop_stack(OPND,s);
                    a = s[0];
                    push_stack(OPND,operate(a,x,b));
            }
        }
        else if(c >= '0' && c <= '9')
        {
            push_stack(OPND,c- '0');
            c = getchar();
        }
        else
        {
            cout << "出现非法字符!" << endl;
            exit(-1);
        }
        GetTop(OPTR,s);
        x = s[0];
    }

    pop_stack(OPND,s);
    x = s[0];
    if(!empty_stack(OPND))
        {
            cout << "表达式有问题!" << endl;
            exit(-1);
        }
    return x;




}

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2022-03-05 14:50
    关注

    程序崩溃了啊,返回了一个异常码
    SL->pBottom = (char*)malloc(sizeof(char)*SL->cap);
    这行有错误,由于->的优先级高于 * ,所以你这个代码相当于把SL->cap当作地址,然后取值,自然就崩溃了,修改如下:

     SL->pBottom = (char*)malloc((*SL)->cap);
    
    
    

    后面所有分配函数处都有该问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月14日
  • 已采纳回答 3月6日
  • 修改了问题 3月5日
  • 创建了问题 3月5日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度