当前位置:编程学习 > C/C++ >>

c语言栈实现a+b*c

答案:一共3个文件,一个主程序源文件,两个头文件,分别为 CharStack.h,DoubleStack.h
将3个文件放在同一个文件夹中即可。。希望能帮上您。。

运行结果如下:


主程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include "CharStack.h"
#include "DoubleStack.h"
#define OP_NUM 7
char arrOP[OP_NUM] = {'+', '-', '*', '/', '(', ')', '='};
char priority[OP_NUM][OP_NUM] = 
{
{'>', '>', '<', '<', '<', '>', '>'},
{'>', '>', '<', '<', '<', '>', '>'},
{'>', '>', '>', '>', '<', '>', '>'},
{'>', '>', '>', '>', '<', '>', '>'},
{'<', '<', '<', '<', '<', '=', '?'},
{'>', '>', '>', '>', '?', '>', '>'},
{'<', '<', '<', '<', '<', '?', '='}
};
char Compare(char a, char b)
{
int i;
int aOrder, bOrder;
aOrder = -1;
bOrder = -1;
for (i = 0; i < OP_NUM; i++)
{
if (arrOP[i] == a)
{
aOrder = i;
break;
}
}
for (i = 0; i < OP_NUM; i++)
{
if (arrOP[i] == b)
{
bOrder = i;
break;
}
}
if ((aOrder == -1) || (bOrder == -1))
{
printf("Input illegal..\n");
getch();
exit(0);
}
else if (priority[aOrder][bOrder] == '?')
{
printf("Input illegal..\n");
getch();
exit(0);
}
else
{
return (priority[aOrder][bOrder]);
}
}
double Operate(double a, char op, double b)
{
double result;
result = 0;
switch (op)
{
case '+':
result = a + b;
break;
case '*':
result = a * b;
break;
case '/':
if (fabs(b) < 0.000001)
{
printf("Divisor cannot be zero..\n");
getch();
exit(0);
}
result = a / b;
break;
case '-':
result = a - b;
break;
default:
printf("\"%c\" is unfinished..\n", op);
getch();
exit(0);
break;
}
return (result);}
int main()
{
CharStack op;//operator set..
DoubleStack num;//num set..
char c[1000];
double num1, num2;
char tempOP;
int length;
int isNum;
int isFloat;
int floatBit;
int i;
printf("请输入一个合法的有理数范围内的四则运算表达式.(退出请输入q后按回车)\n");
printf("可以包含加\'+\'减\'-\'乘\'*\'除\'/\'和小括号\'()\',以等号\'=\'结尾.\n");
gets(c);
length = strlen(c);
while ((c[0] != 'q') || (length != 1))
{
InitCharStack(&op, length);
InitDoubleStack(&num, length);
PushChar(&op, '=');
i = 0;
isNum = 0;
isFloat = 0;
while ((c[i] != '=') || (GetTopChar(op) != '='))
{
if ((c[i] >= 48) && (c[i] <= 57))
{
if (isNum == 1)
{
if (isFloat == 1)
{
floatBit++;
PushDouble(&num, PopDouble(&num) + (c[i] - 48) / pow(10, floatBit));
}
else
{
PushDouble(&num, PopDouble(&num) * 10 + c[i] - 48);
}
}
else
{
PushDouble(&num, c[i] - 48);
isNum = 1;
}
i++;
}
else
{
if ((c[i] == '.') && (isNum == 1))
{
isFloat = 1;
floatBit = 0;
i++;
}
else
{
isNum = 0;
isFloat = 0;
switch (Compare(GetTopChar(op), c[i]))
{
case '<':
PushChar(&op, c[i]);
i++;
break;
case '=':
PopChar(&op);
i++;
break;
case '>':
tempOP = PopChar(&op);
num2 = PopDouble(&num);
num1 = PopDouble(&num);
PushDouble(&num, Operate(num1, tempOP, num2));
break;
default:
printf("Input illegal..\n");
getch();
exit(0);
break;
}
}
}
}
printf("%g\n", GetTopDouble(num));
DelCharStack(&op);
DelDoubleStack(&num);
gets(c);
length = strlen(c);
}
getch();
return (0);
}

CharStack.h 文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define CHAR_STACK_SIZE_ADD 1
typedef struct CharStack//stack define..
{
char *base;
char *top;
int size;
} CharStack;
void InitCharStack(CharStack *s, int stackSize)//stack initialization..
{
(*s).base = (char *)malloc(stackSize * sizeof(char));
if ((*s).base == NULL)
{
printf("Stack Initialization Failed..\n");
getch();
exit(0);
}
(*s).top = (*s).base;
(*s).size = stackSize;
}
void ClearCharStack(CharStack *s)
{
(*s).top = (*s).base;
}
void DelCharStack(CharStack *s)
{
free((*s).base);
}
char GetTopChar(CharStack s)//get top element of stack..
{
char e;
if (s.base == s.top)
{
printf("Stack is empty..\n");
getch();
exit(0);
}
e = *(s.top - 1);
return (e);
}
void PushChar(CharStack *s, char e)//insert an element as top..
{
if ((*s).top - (*s).base >= (*s).size)//if stack is full..
{
(*s).base = (char *)realloc((*s).base, ((*s).size + CHAR_STACK_SIZE_ADD) * sizeof(char));
if ((*s).base == NULL)
{
printf("Memory Reallocation Failed..\n");
getch();
exit(0);
}
(*s).top = (*s).base + (*s).size;
(*s).size += CHAR_STACK_SIZE_ADD;
}
*((*s).top) = e;
(*s).top++;
}
char PopChar(CharStack *s)//delete top element..
{
char e;
if ((*s).base == (*s).top)
{
printf("Stack is empty.. No element to delete..\n");
getch();
exit(0);
}
(*s).top--;
e = *((*s).top);
return (e);
}

DoubleStack.h 文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define DOUBLE_STACK_SIZE_ADD 1
typedef struct DoubleStack//stack define..
{
double *base;
double *top;
int size;
} DoubleStack;
void InitDoubleStack(DoubleStack *s, int stackSize)//stack initialization..
{
(*s).base = (double *)malloc(stackSize * sizeof(double));
if ((*s).base == NULL)
{
printf("Stack Initialization Failed..\n");
getch();
exit(0);
}
(*s).top = (*s).base;
(*s).size = stackSize;
}
void ClearDoubleStack(DoubleStack *s)
{
(*s).top = (*s).base;
}
void DelDoubleStack(DoubleStack *s)
{
free((*s).base);
}
double GetTopDouble(DoubleStack s)//get top element of stack..
{
double e;
if (s.base == s.top)
{
printf("Stack is empty..\n");
getch();
exit(0);
}
e = *(s.top - 1);
return (e);
}
void PushDouble(DoubleStack *s, double e)//insert an element as top..
{
if ((*s).top - (*s).base >= (*s).size)//if stack is full..
{
(*s).base = (double *)realloc((*s).base, ((*s).size + DOUBLE_STACK_SIZE_ADD) * sizeof(double));
if ((*s).base == NULL)
{
printf("Memory Reallocation Failed..\n");
getch();
exit(0);
}
(*s).top = (*s).base + (*s).size;
(*s).size += DOUBLE_STACK_SIZE_ADD;
}
*((*s).top) = e;
(*s).top++;
}
double PopDouble(DoubleStack *s)//delete top element..
{
double e;
if ((*s).base == (*s).top)
{
printf("Stack is empty.. No element to delete..\n");
getch();
exit(0);
}
(*s).top--;
e = *((*s).top);
return (e);
}

上一个:C语言题——汉诺塔问题
下一个:求C语言编写图书管理系统

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,