|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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;
} |
|