鱼C论坛

 找回密码
 立即注册
查看: 7482|回复: 12

栈的中缀计算器 编译器没有报错,但是计算结果很奇怪,注释可以不用管,来个大佬..

[复制链接]
发表于 2021-7-3 14:40:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#define _CRT_SECURE_NO_WARNINGS 1


#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

#define STACK_INIT_SIZE 20
#define STACKINCREMENT  10
#define MAXBUFFER       10
#define MAX             50

////定义队列
//
////循环队列
//#define MAXSIZE 100
//typedef int QElemType;
//typedef struct{
//    QElemType *base;
//    int front;
//    int rear;
//}SqQueue;
//
//int InitQueue(SqQueue *q)//初始化队列
//{
//    q->base = (QElemType*)malloc(MAXSIZE * sizeof(QElemType));
//    if (!q->base)
//        exit(0);
//    q->front = q->rear = 0;
//}
//
//InsertQueue(SqQueue*q, QElemType e)//入队列
//{
//    if ((q->rear + 1) % MAXSIZE == q->front)
//    {
//        printf("队满");
//        exit(-1);
//    }
//    q->base[q->rear] = e;
//    q->rear = (q->rear + 1) % MAXSIZE;
//}
//
//
//DeleteQueue(SqQueue* q, QElemType* e)//出队列
//{
//    if (q->front == q->rear)
//    {
//        printf("队空");
//        exit(-1);
//    }
//        
//    *e = q->base[q->front];
//    q->front = (q->front + 1) % MAXSIZE;
//}

//#define MAX 10
////顺序队列,用数组存储
////初始条件 front =rear=0
////满:rear=m  容量m
////空  front=rear
//typedef struct
//{
//    int data[MAX];
//    int front, rear;
//}Queue;
//
////初始化队列
//Queue InitQueue()
//{
//    Queue p;
//    p.front = p.rear = 0;
//    return p;
//}
//
////插入
//int InQueue(Queue* p, int e)
//{
//    p->data[++p->rear] = e;
//    if (p->rear > MAX)
//        return 0;
//    else
//    {
//        printf("插入成功\n");
//        return 1;
//    }
//}
//
////删除
//int DeQueue(Queue* p, int* e)
//{
//    if (p->front == p->rear)
//        return 0;
//    else
//    {
//        p->front++;                 //将队头指针后移即可删除
//        printf("删除成功\n");
//        return 0;
//    }
//}
//
////打印
//void print(Queue* p)
//{
//    //while (p->front < (p->rear + 1))
//    //{
//        printf("%d", p->data[p->front]);
//        p->front++;
//    //}
//}

//int QueueLength(SqQueue q)
//{
//    return(q.rear - q.front + MAXSIZE) % MAXSIZE;
//}

//定义栈
typedef char ElemType;
typedef struct
{
    ElemType* base;
    ElemType* top;
    int stackSize;
}sqStack;

InitStack(sqStack* s)
{
    s->base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));
    if (!s->base)
        exit(0);

    s->top = s->base;
    s->stackSize = STACK_INIT_SIZE;
}

Push(sqStack* s, ElemType e)
{
    // 栈满,追加空间!
    if (s->top - s->base >= s->stackSize)
    {
        s->base = (ElemType*)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
        if (!s->base)
            exit(0);

        s->top = s->base + s->stackSize;
        s->stackSize = s->stackSize + STACKINCREMENT;
    }

    *(s->top) = e;      // 存放数据
    s->top++;
}

Pop(sqStack* s, ElemType* e)
{
    if (s->top == s->base)
        return 0;

    *e = *--(s->top);   // 将栈顶元素弹出并修改栈顶指针
}

int StackLen(sqStack s)
{
    return (s.top - s.base);
}

////中缀转后缀
int main()
{
    sqStack s;
    //SqQueue q;
    char c,e;
    double d;
    char str[MAXBUFFER];
    char strMax[MAX] = {0};
    int i = 0;

    InitStack(&s);

    strMax[MAX - 1] = '0';

    //InitQueue(&q);

    printf("请输入中缀表达式,以#作为结束标志:");
    scanf("%c", &c);

    while (c != '#')
    {
        while (c >= '0' && c <= '9')
        {
            printf("%c", c);
            strMax[i] = c;
            i++;
            //InsertQueue(&q, c);
            scanf("%c", &c);
            if (c < '0' || c>'9')
            {
                printf(" ");
                strMax[i] = " ";
                i++;
                //InsertQueue(&q, '\');
            }
        }

        if (')' == c)
        {
            Pop(&s, &e);
            while ('(' != e)
            {
                printf("%c ", e);
                strMax[i] = e;
                i++;
                printf("%c ", e);
                //InsertQueue(&q, e);
                Pop(&s, &e);
            }
        }
        else if ('+' == c || '-' == c)
        {
            if (!StackLen(s))
            {
                Push(&s, c);
            }
            else
            {
                do
                {
                    Pop(&s, &e);
                    if ('(' == e)
                    {
                        Push(&s, e);
                    }
                    else
                    {
                        printf("%c ", e);
                        strMax[i];
                        i++;
                        printf("%c ", e);
                        //InsertQueue(&q, e);
                    }
                } while (StackLen(s) && '(' != e);
                Push(&s, c);
            }
        }
        else if ('*' == c || '/' == c || '(' == c)
        {
            Push(&s, c);
        }
        else if ('#' == c)
        {
            break;
        }
        else if (' ' == c)
        {
            continue;
        }
        else
        {
            printf("\n出错:输入格式错误!\n");
            return -1;
        }

        scanf("%c", &c);
    }

    while (StackLen(s))
    {
        Pop(&s, &e);
        printf("%c ", e);
        strMax[i++] = e;
        strMax[i++] = ' ';

    }

    strMax[i-1] = '#';
    strMax[i] = '0';
    //strMax[i] = '\0';

    //puts(strMax);

   /* while (queuelength(q)) {
        deletequeue(&q, &c);
        printf("%c ", c);
    }*/

    //for (int j = 0; j < 50; j++) {
    //   //putchar(strMax[1]);
    //   if (strMax[j] == NULL)
    //       break;
    //}
        //后缀表达式
    puts(strMax);
    /*for (int j = 0; j < 50;j++) {
        c = strMax[j];*/
   
    //int purchar(c);
        /*if (c == NULL)
            break;*/
            //deletequeue(&q, &c);
    c = strMax[0];
    i = 0;
    int j = 1;
                while (c != '#')
                {
                    
                    //while (isdigit(c) || c == '.')  // 用于过滤数字
                    while (c >= '0' && c <= '9' || c == '.')
                    {
                        //putchar(1);
                        str[i] = c;
                        i++;
                        str[i] = '\0';
                        if (i >= 10)
                        {
                            printf("出错:输入的单个数据过大!\n");
                            return -1;
                        }
                        
                        c = strMax[j];
                        j++;
                      /*  if (j == 20)
                            break;*/
                        //putchar(c);
                        //deletequeue(&q, &c);
                        //scanf("%c", &e);
                        if (c == ' ')
                        {
                            d = atof(str);
                            Push(&s, d);
                            i = 0;
                            break;
                        }
                    }

                    switch (c)
                    {
                    case '+':
                        Pop(&s, &e);
                        Pop(&s, &d);
                        Push(&s, d + e);
                        break;
                    case '-':
                        Pop(&s, &e);
                        Pop(&s, &d);
                        Push(&s, d - e);
                        break;
                    case '*':
                        Pop(&s, &e);
                        Pop(&s, &d);
                        Push(&s, d * e);
                        break;
                    case '/':
                        Pop(&s, &e);
                        Pop(&s, &d);
                        if (e != 0)
                        {
                            Push(&s, d / e);
                        }
                        else
                        {
                            printf("\n出错:除数为零!\n");
                            return -1;
                        }
                        break;
                    }

                    //scanf("%c", &c);
                    //deletequeue(&q, &c);
                    c = strMax[j];
                    j++;
                }

            Pop(&s, &d);
            printf("\n最终的计算结果为:%f\n", d);
        //}
        
        

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-3 14:41:58 | 显示全部楼层
中缀先转换成后缀,后缀在进行计算,但是那个结果我快吐了,我用的.c文件,C语言编译的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-3 14:54:49 | 显示全部楼层

回帖奖励 +2 鱼币

你的编译器什么提示也没有吗?
  1. $ gcc -g -Wall -o main main.c
  2. main.c:122:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
  3. InitStack(sqStack* s)
  4. ^~~~~~~~~
  5. main.c:132:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
  6. Push(sqStack* s, ElemType e)
  7. ^~~~
  8. main.c:149:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
  9. Pop(sqStack* s, ElemType* e)
  10. ^~~
  11. main.c: In function ‘main’:
  12. main.c:194:27: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
  13.                  strMax[i] = " ";
  14.                            ^
  15. main.c:231:31: warning: statement with no effect [-Wunused-value]
  16.                          strMax[i];
  17.                          ~~~~~~^~~
  18. main.c:302:37: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
  19.                      while (c >= '0' && c <= '9' || c == '.')
  20.                             ~~~~~~~~~^~~~~~~~~~~
  21. main.c:334:33: warning: passing argument 2 of ‘Pop’ from incompatible pointer type [-Wincompatible-pointer-types]
  22.                          Pop(&s, &d);
  23.                                  ^
  24. main.c:149:1: note: expected ‘ElemType * {aka char *}’ but argument is of type ‘double *’
  25. Pop(sqStack* s, ElemType* e)
  26. ^~~
  27. main.c:339:33: warning: passing argument 2 of ‘Pop’ from incompatible pointer type [-Wincompatible-pointer-types]
  28.                          Pop(&s, &d);
  29.                                  ^
  30. main.c:149:1: note: expected ‘ElemType * {aka char *}’ but argument is of type ‘double *’
  31. Pop(sqStack* s, ElemType* e)
  32. ^~~
  33. main.c:344:33: warning: passing argument 2 of ‘Pop’ from incompatible pointer type [-Wincompatible-pointer-types]
  34.                          Pop(&s, &d);
  35.                                  ^
  36. main.c:149:1: note: expected ‘ElemType * {aka char *}’ but argument is of type ‘double *’
  37. Pop(sqStack* s, ElemType* e)
  38. ^~~
  39. main.c:349:33: warning: passing argument 2 of ‘Pop’ from incompatible pointer type [-Wincompatible-pointer-types]
  40.                          Pop(&s, &d);
  41.                                  ^
  42. main.c:149:1: note: expected ‘ElemType * {aka char *}’ but argument is of type ‘double *’
  43. Pop(sqStack* s, ElemType* e)
  44. ^~~
  45. main.c:368:21: warning: passing argument 2 of ‘Pop’ from incompatible pointer type [-Wincompatible-pointer-types]
  46.              Pop(&s, &d);
  47.                      ^
  48. main.c:149:1: note: expected ‘ElemType * {aka char *}’ but argument is of type ‘double *’
  49. Pop(sqStack* s, ElemType* e)
  50. ^~~
  51. main.c: In function ‘InitStack’:
  52. main.c:130:1: warning: control reaches end of non-void function [-Wreturn-type]
  53. }
  54. ^
  55. main.c: In function ‘Push’:
  56. main.c:147:1: warning: control reaches end of non-void function [-Wreturn-type]
  57. }
  58. ^
  59. main.c: In function ‘Pop’:
  60. main.c:155:1: warning: control reaches end of non-void function [-Wreturn-type]
  61. }
  62. ^
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-3 15:23:42 | 显示全部楼层
人造人 发表于 2021-7-3 14:54
你的编译器什么提示也没有吗?

我的是VS2019  可以运行出来
运行截图.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-3 15:28:19 | 显示全部楼层
我想把中缀转后缀和后缀计算器整合在一起,输入中缀,输出后缀,同时输出结果。一定是中缀转后缀的结果录入数组进入后缀计算器的时候,有问题,但是我把数组打印一遍打印一遍看不出来,很多代码都和小甲鱼的一样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-3 15:41:13 | 显示全部楼层
952740799 发表于 2021-7-3 15:23
我的是VS2019  可以运行出来

编译器给出了那么多的警告,你都不管吗?
编译器帮你检查出了这么多的问题,这个程序能正常运行就奇怪了,先把编译器报告的所有警告解决了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-3 16:21:20 | 显示全部楼层

回帖奖励 +2 鱼币

问题好像有点多,学习一下~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-5 20:41:32 | 显示全部楼层

回帖奖励 +2 鱼币

学习一个
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-6 10:10:57 | 显示全部楼层

回帖奖励 +2 鱼币

帮顶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-6 11:16:54 | 显示全部楼层

回帖奖励 +2 鱼币

顶顶顶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-10 21:05:27 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>    //两个栈,一个储存结果,一个储存运算符
#define newpc (stype *)malloc(sizeof(stype))
#define newpi (inttype *)malloc(sizeof(inttype))  //定义两个申请地址的宏
typedef struct char_stack
{
    char dat;
    struct _stack* next;
} stype;
typedef struct int_stack
{
    double dat;
    struct _stack* next;     //建立两个栈类型
} inttype;
void charpush(stype** stacktop, char c)  //运算符压栈函数,传进栈顶指针本身的地址
{
    stype* ss = newpc;
    ss->dat = c;
    ss->next = *stacktop;
    *stacktop = ss;
}
char charpop(stype** stacktop)  //运算符出栈函数
{
    char c = (*stacktop)->dat;
    stype* ss = (*stacktop);
    (*stacktop) = (*stacktop)->next;
    free(ss);
    return c;
}
void intpush(inttype** stacktop, double c)  //数字压栈函数
{
    inttype* ss = newpi;
    ss->dat = c;
    ss->next = *stacktop;
    *stacktop = ss;
}
double intpop(inttype** stacktop)  //数字出栈函数
{
    double c = (*stacktop)->dat;
    inttype* ss = (*stacktop);
    (*stacktop) = (*stacktop)->next;
    free(ss);
    return c;
}
void tanchu(char c, inttype** stacktop) //弹出字符,然后运算,然后进栈
{
    double a = intpop(stacktop);
    double b = intpop(stacktop);
    if (c == '+') intpush(stacktop, b + a);
    else if (c == '-') intpush(stacktop, b - a);
    else if (c == '*') intpush(stacktop, b * a);
    else if (c == '/') intpush(stacktop, b / a);
    /*else if (c == '/')
    {
        if (a != '0')
        {
            intpush(stacktop, b / a);
        }else {
            printf("\n出错:除数为零!\n");
            return -1;
        }

    }*/
}
int tance(char c)  //探测优先级的函数
{
    if (c == '+' || c == '-') return 0;
    else if (c == '*' || c == '/') return 1;
    else if (c == '@' || c == '(' || c == ')') return -1;
}
int midCalc()  //主函数 功能:处理输入的中缀表达式,输出结果(过程中用到了后缀的转化)
{
    //getchar();

    printf("请输入中缀表达式:");

    stype* sig, * sigtop;
    inttype* num, * numtop;  //每个栈需要两个指针进行操作
    char c = getchar();
    sig = newpc;
    sig->dat = '@';
    sig->next = NULL;
    sigtop = sig;
    num = newpi;
    num->next = NULL;
    numtop = num;   //初始化两个栈


    while (c != '\n')  //一直读入,直至读到回车符结束
    {
        //接下来要对读入的当前字符进行处理
        if (c >= '0' && c <= '9')
        {
            int a = c - 48;
            c = getchar();
            while (c >= '0' && c <= '9')
            {
                a = a * 10 + (c - 48);
                c = getchar();
            }
            intpush(&numtop, a);      //如果是个数字字符,就把这个数字一直读进去(因为不一定是几位数),然后压栈到num里
        }
        else if (c == '(')  //如果是左括号,直接压栈到sig里
        {
            charpush(&sigtop, '(');
            c = getchar();
        }
        else if (c == ')') //如果是右括号,就边弹栈边处理结果,直到遇到左括号
        {
            while (sigtop->dat != '(')
            {
                tanchu(charpop(&sigtop), &numtop);
            }
            c = getchar();
            charpop(&sigtop);
        }
        else if (c == '+' || c == '-' || c == '*' || c == '/') //如果是+-*/就比较与栈顶的优先级,如果高,直接压入,否则(等于也不能压入),边弹边处理结果,直到可以压入(和上面有点像)
        {
            int j = tance(c);
            int k = tance(sigtop->dat);
            if (j > k)
            {
                charpush(&sigtop, c);
                c = getchar();
            }
            else {
                while (j <= k)
                {
                    tanchu(charpop(&sigtop), &numtop);
                    k = tance(sigtop->dat);
                }
                charpush(&sigtop, c);
                c = getchar();
            }
        }
        else  //否则,忽略这个符号 ,并且显示输入有误
        {
            c = '\n';
            printf("输入有误\n");
        }

    }
    while (sigtop->dat != '@')  //收尾,把没有弹出的都处理一下
    {
        tanchu(charpop(&sigtop), &numtop);
    }
    printf("\n最终的计算结果为:%.2lf\n", numtop->dat);
    //c = getchar();
    //return 0;

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-10 21:07:03 | 显示全部楼层
大家参考一下,定义了两个不同类型的栈,回车结束输入,不能输入小数,你可以拿除号用分数表示
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-11 10:42:26 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-4 08:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表