我IS ~F 2021-10-24 17:33 采纳率: 50%
浏览 57

为什么我的这串代码在Visual Studio 2017里调试的时候报错的位置是char *str_1=malloc这,且在C-free里能正常运行出相应的结果(忽略scanf与scanf_s的问题)


#include <stdio.h>


#include<Windows.h>




#define MaxSize 10

typedef struct {
    char data[MaxSize];
    int top;//栈顶指针

}SqStack;

void InitStack(SqStack *S) {
    S->top = -1;

}

BOOL isEmpty(SqStack *s) {
    if (s->top == -1) {
        return TRUE;
    }
}
void Push(SqStack *S, char x)
{
    if (S->top == MaxSize - 1) {
        printf("顺序栈已满,不允许再添加\n");
        return -1;
    }
    S->data[++S->top] = x;


}

char  Pop(SqStack *S)
{
    if (S->top == -1) {
        printf("栈已空");
        return -1;
    }
    char x = S->data[S->top--];
    return  x;

}



//中缀表达式转后缀表达式
void ConcluseExpression(char str[], int length) {
    SqStack S;
    InitStack(&S);
    int i = 0;
    char *str_1 = (char*)malloc(sizeof(char));
    int m = 0;
    int count = 0;
    int tag = 0;
    int tag_2 = 0;
    int tag_3 = 0;
    for (i = 0; i < length; i++) {

        if (str[i] != '*' && str[i] != '/' && str[i] != '+' && str[i] != '-'&&str[i] != '('&&str[i] != ')') {
            str_1[m++] = str[i];

        }
        if (str[i] == '*' || str[i] == '/' || str[i] == '+' || str[i] == '-') {

            Push(&S, str[i]);
        }
        if (str[i] == '(') {
            tag_2++;
            if (tag_2 == 1) tag_3 = i;
            if (str[i + 1] == '(') {
                tag = 1;
                continue;
            }
            else {
                if (str[i + 1] == '*' || str[i + 1] == '/' || str[i + 1] == '+' || str[i + 1] == '-' || str[i + 1] == ')') {
                    printf("错误:中缀表达式输入错误");
                    return -1;
                }
                int k = 0;
                count = i + 1;
                for (k = i + 1; str[i + 1] != ')'&&str[i + 1] != '('; k++, i++) {
                    if (str[k] != '*' && str[k] != '/' && str[k] != '+' && str[k] != '-')
                        str_1[m++] = str[k];
                    else
                        Push(&S, str[k]);


                }

            }
        }

        if (str[i] == ')'&&tag == 1) {
            if (str[i + 1] == ')') {
                continue;
            }
            else {
                if (str[i + 1] == '*' || str[i + 1] == '/' || str[i + 1] == '+' || str[i + 1] == '-') {
                    int n;
                    for (n = 0; str[count] != '+'&&str[count] != '-'
                        &&str[count] != '*'&&str[count] != '/'; n++, count++)
                        str_1[m++] = Pop(&S);

                    count = i;
                    if (str[i + 2] != '*'&&str[i + 2] != '/'&&str[i + 2] != '+'&&str[i + 2] != '-'&&str[i + 2] != '\0'&&
                        str[i + 2] != ')'&&str[i + 2] != '('&&str[i + 3] != '\0') {
                        str_1[m++] = str[i + 2];
                        Push(&S, str[i + 1]);
                        str_1[m++] = Pop(&S);
                        i = i + 2;
                        continue;

                    }
                    tag = 0;
                    continue;
                    //Push(&S,str[i+1]);


                }
                if (str[i + 1] == '\0') {
                    break;

                }
                else {
                    printf("错误:输入的c语句不合法!\n");
                    return -1;
                }
            }
        }
        if (str[i] == ')'&&tag == 0) {
            if (str[i + 1] == ')') {
                continue;
            }
            else {
                if (str[i + 1] == '*' || str[i + 1] == '/' || str[i + 1] == '+' || str[i + 1] == '-') {
                    int n;
                    if (str[tag_3 - 1] == '+' || str[tag_3 - 1] == '-') {
                        for (n = 0; str[count] != ')'; n++, count++)
                            str_1[m++] = Pop(&S);

                        continue;
                    }
                    if (str[tag_3 - 1] == '*' || str[tag_3 - 1] == '/') {


                        for (n = 0; str[count - 1] != ')'; n++, count++)
                            str_1[m++] = Pop(&S);

                        continue;
                    }

                }
                if (str[i + 1] == '\0') {
                    break;

                }
                else {
                    printf("错误:输入的c语句不合法!\n");
                    return -1;

                }

            }
        }
    }
    int lengthofStr_1 = m;
    while (isEmpty(&S) != TRUE) {


        str_1[m++] = Pop(&S);

    }
    int j;
    for (j = m - 1; j > lengthofStr_1; j--) {

        if ((str_1[j] == '*'&&str[j - 1] == '+') || (str_1[j] == '*'&&str[j - 1] == '-')
            || (str_1[j] == '/'&&str[j - 1] == '+') || (str_1[j] == '/'&&str[j - 1] == '-'))
        {
            char temp = str_1[j];
            str_1[j] = str_1[j - 1];
            str_1[j - 1] = temp;
        }
    }
    int k;
    printf("其后缀表达式为:\n");
    for (k = 0; k < m; k++) {
        printf("%c ", str_1[k]);
    }
    printf("\n");
}

int main() {
    char *str = (char*)malloc(sizeof(char));
    printf("输入中缀表达式:\n");
    scanf("%s", str);
    int length = 0;
    while (str[length] != '\0')
        length++;
    ConcluseExpression(str, length);

}

  • 写回答

2条回答 默认 最新

  • CSDN专家-Time 2021-10-24 17:35
    关注

    你贴一下报错内容, 我看看。

    评论

报告相同问题?

问题事件

  • 创建了问题 10月24日

悬赏问题

  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题
  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画