栈的中缀计算器 编译器没有报错,但是计算结果很奇怪,注释可以不用管,来个大佬..
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT10
#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 = 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 + 1) % MAXSIZE;
//}
//#define MAX 10
////顺序队列,用数组存储
////初始条件 front =rear=0
////满:rear=m容量m
////空front=rear
//typedef struct
//{
// int data;
// 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++;
// //}
//}
//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;
char strMax = {0};
int i = 0;
InitStack(&s);
strMax = '0';
//InitQueue(&q);
printf("请输入中缀表达式,以#作为结束标志:");
scanf("%c", &c);
while (c != '#')
{
while (c >= '0' && c <= '9')
{
printf("%c", c);
strMax = c;
i++;
//InsertQueue(&q, c);
scanf("%c", &c);
if (c < '0' || c>'9')
{
printf(" ");
strMax = " ";
i++;
//InsertQueue(&q, '\');
}
}
if (')' == c)
{
Pop(&s, &e);
while ('(' != e)
{
printf("%c ", e);
strMax = 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++;
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 = e;
strMax = ' ';
}
strMax = '#';
strMax = '0';
//strMax = '\0';
//puts(strMax);
/* while (queuelength(q)) {
deletequeue(&q, &c);
printf("%c ", c);
}*/
//for (int j = 0; j < 50; j++) {
// //putchar(strMax);
// if (strMax == NULL)
// break;
//}
//后缀表达式
puts(strMax);
/*for (int j = 0; j < 50;j++) {
c = strMax;*/
//int purchar(c);
/*if (c == NULL)
break;*/
//deletequeue(&q, &c);
c = strMax;
i = 0;
int j = 1;
while (c != '#')
{
//while (isdigit(c) || c == '.')// 用于过滤数字
while (c >= '0' && c <= '9' || c == '.')
{
//putchar(1);
str = c;
i++;
str = '\0';
if (i >= 10)
{
printf("出错:输入的单个数据过大!\n");
return -1;
}
c = strMax;
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++;
}
Pop(&s, &d);
printf("\n最终的计算结果为:%f\n", d);
//}
return 0;
} 中缀先转换成后缀,后缀在进行计算,但是那个结果我快吐了,我用的.c文件,C语言编译的{:10_249:} 你的编译器什么提示也没有吗?
$ gcc -g -Wall -o main main.c
main.c:122:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
InitStack(sqStack* s)
^~~~~~~~~
main.c:132:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
Push(sqStack* s, ElemType e)
^~~~
main.c:149:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
Pop(sqStack* s, ElemType* e)
^~~
main.c: In function ‘main’:
main.c:194:27: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
strMax = " ";
^
main.c:231:31: warning: statement with no effect [-Wunused-value]
strMax;
~~~~~~^~~
main.c:302:37: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
while (c >= '0' && c <= '9' || c == '.')
~~~~~~~~~^~~~~~~~~~~
main.c:334:33: warning: passing argument 2 of ‘Pop’ from incompatible pointer type [-Wincompatible-pointer-types]
Pop(&s, &d);
^
main.c:149:1: note: expected ‘ElemType * {aka char *}’ but argument is of type ‘double *’
Pop(sqStack* s, ElemType* e)
^~~
main.c:339:33: warning: passing argument 2 of ‘Pop’ from incompatible pointer type [-Wincompatible-pointer-types]
Pop(&s, &d);
^
main.c:149:1: note: expected ‘ElemType * {aka char *}’ but argument is of type ‘double *’
Pop(sqStack* s, ElemType* e)
^~~
main.c:344:33: warning: passing argument 2 of ‘Pop’ from incompatible pointer type [-Wincompatible-pointer-types]
Pop(&s, &d);
^
main.c:149:1: note: expected ‘ElemType * {aka char *}’ but argument is of type ‘double *’
Pop(sqStack* s, ElemType* e)
^~~
main.c:349:33: warning: passing argument 2 of ‘Pop’ from incompatible pointer type [-Wincompatible-pointer-types]
Pop(&s, &d);
^
main.c:149:1: note: expected ‘ElemType * {aka char *}’ but argument is of type ‘double *’
Pop(sqStack* s, ElemType* e)
^~~
main.c:368:21: warning: passing argument 2 of ‘Pop’ from incompatible pointer type [-Wincompatible-pointer-types]
Pop(&s, &d);
^
main.c:149:1: note: expected ‘ElemType * {aka char *}’ but argument is of type ‘double *’
Pop(sqStack* s, ElemType* e)
^~~
main.c: In function ‘InitStack’:
main.c:130:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
main.c: In function ‘Push’:
main.c:147:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
main.c: In function ‘Pop’:
main.c:155:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^ 人造人 发表于 2021-7-3 14:54
你的编译器什么提示也没有吗?
我的是VS2019可以运行出来 我想把中缀转后缀和后缀计算器整合在一起,输入中缀,输出后缀,同时输出结果。一定是中缀转后缀的结果录入数组进入后缀计算器的时候,有问题,但是我把数组打印一遍打印一遍看不出来,很多代码都和小甲鱼的一样 952740799 发表于 2021-7-3 15:23
我的是VS2019可以运行出来
编译器给出了那么多的警告,你都不管吗?
编译器帮你检查出了这么多的问题,这个程序能正常运行就奇怪了,先把编译器报告的所有警告解决了
问题好像有点多,学习一下~ 学习一个 帮顶 顶顶顶 #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;
} 大家参考一下,定义了两个不同类型的栈,回车结束输入,不能输入小数,你可以拿除号用分数表示 哇{:10_257:}
页:
[1]