|
楼主 |
发表于 2012-4-5 20:15:41
|
显示全部楼层
又忙了半天,终于写好了,现在这个程序可以实现四则运算,不过数字都是个位数,后面看看能不能改成支持多位数的
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 64
#define MAX_LEN 128
#define SUCCESS 0
#define FAILED 1
#define FULL 2
#define EMPTY 3
#define OVERFLOW -1
#define UNDERFLOW -2
typedef union
{
int num;
char op;
}ELEMENT;
typedef struct
{
ELEMENT data[MAX_SIZE];
int top;
}STACK;
STACK *NumStack, *OpStack;
int InitStack(STACK *stack);
int JudgeStack(STACK *stack);
int PopStack(STACK *stack, ELEMENT *data);
int PushStack(STACK *stack, ELEMENT *data);
int GetStackTop(STACK *stack, ELEMENT *data);
int Expr(char *str);
void CalcPlainExpr(void);
int CharToNum(char inputchar);
char NumToChar(int inputnum);
int StrToNum(char *inputstr);
char *NumToStr(int inputnum);
void ReverseStr(char *str);
int CalcNum(int num1, int num2, char op);
int main(int argc, char *argv[])
{
//char *s = "1+ ( 3-2)*5 + 4#";
//char *s = "1+ 3-2*5#";
//char *s = "1+ 3-2 + 4#";
//char *s;
int result;
ELEMENT first;
//s = (char *)malloc(MAX_LEN * sizeof(char));
//memset(s, 0, MAX_LEN * sizeof(char));
NumStack = (STACK *)malloc(sizeof(STACK));
OpStack = (STACK *)malloc(sizeof(STACK));
first.op = '#';
//printf("please input the expression:\n");
//gets(s);
InitStack(NumStack);
InitStack(OpStack);
PushStack(OpStack, &first);
if(argc != 2)
{
printf("wrong input!");
return 0;
}
else
{
result = Expr(argv[1]);
}
printf("the calc result is: %d\n", result);
return 1;
}
int InitStack(STACK *stack)
{
stack->top = -1;
return SUCCESS;
}
int JudgeStack(STACK *stack)
{
if(stack->top == -1)
{
return EMPTY;
}
else if(stack->top == MAX_SIZE - 1)
{
return FULL;
}
}
int PushStack(STACK *stack, ELEMENT *data)
{
if(stack->top == MAX_SIZE - 1)
{
return OVERFLOW;
}
else
{
stack->data[++(stack->top)] = *data;
return SUCCESS;
}
}
int PopStack(STACK *stack, ELEMENT *data)
{
if(stack->top == -1)
{
return UNDERFLOW;
}
else
{
*data = stack->data[(stack->top)--];
return SUCCESS;
}
}
int GetStackTop(STACK *stack, ELEMENT *data)
{
if(stack->top < 0 || stack->top > MAX_SIZE - 1)
{
return FAILED;
}
else
{
*data = stack->data[stack->top];
return SUCCESS;
}
}
int CharToNum(char inputchar)
{
int num;
num = (int)inputchar - 48;
return num;
}
char NumToChar(int inputnum)
{
char chr;
chr = (char)inputnum + 48;
return chr;
}
void ReverseStr(char *str)
{
char temp;
int len;
int i;
len = strlen(str);
for(i = 0; i <= (int)len/2 - 1; i++)
{
temp = *(str + i);
*(str + i) = *(str + len - 1 - i);
*(str + len - 1 - i) = temp;
}
}
int StrToNum(char *inputstr)
{
int len;
int num = 0;
char *p;
len = strlen(inputstr);
p = inputstr;
while(len > 0)
{
num += CharToNum(*p++) * pow(10, --len);
}
return num;
}
char *NumToStr(int inputnum)
{
char *str;
char *p;
int num = inputnum;
str = (char *)malloc(MAX_LEN * sizeof(char));
memset(str, 0, MAX_LEN);
p = str;
while(num > 0)
{
*p = NumToChar(num % 10);
num = (num - (num % 10)) / 10;
p++;
}
ReverseStr(str);
return str;
}
int CalcNum(int num1, int num2, char op)
{
int result;
switch (op)
{
case '+':
result = num2 + num1;
break;
case '-':
result = num2 - num1;
break;
case '*':
result = num2 * num1;
break;
case '/':
result = num2 / num1;
break;
}
return result;
}
void CalcPlainExpr(void)
{
ELEMENT num1;
ELEMENT num2;
ELEMENT op;
ELEMENT top;
ELEMENT temp;
while(1)
{
if(GetStackTop(OpStack, &top) == FAILED)
{
break;
}
else if(top.op == '(')
{
PopStack(OpStack, &temp);
break;
}
else
{
PopStack(NumStack, &num1);
PopStack(NumStack, &num2);
PopStack(OpStack, &op);
temp.num = CalcNum(num1.num, num2.num, op.op);
PushStack(NumStack, &temp);
}
}
}
int Expr(char *str)
{
char *p;
ELEMENT top;
ELEMENT result;
ELEMENT num1;
ELEMENT num2;
ELEMENT op;
ELEMENT temp;
p = str;
while (1)
{
if(*p == '\0')
{
CalcPlainExpr();
break;
}
else if(isspace(*p))
{
p++;
}
else if (isdigit(*p))
{
temp.num = CharToNum(*p++);
PushStack(NumStack, &temp);
}
else if(*p == '(')
{
temp.op = *p++;
PushStack(OpStack, &temp);
}
else if(*p == ')')
{
CalcPlainExpr();
p++;
}
else if(*p == '+' || *p == '-')
{
if(GetStackTop(OpStack, &top) == FAILED)
{
temp.op = *p++;
PushStack(OpStack, &temp);
}
else if(top.op == '+' || top.op == '-' || top.op == '*' || top.op == '/')
{
PopStack(NumStack, &num1);
PopStack(NumStack, &num2);
PopStack(OpStack, &op);
temp.num = CalcNum(num1.num, num2.num, op.op);
PushStack(NumStack, &temp);
}
else
{
temp.op = *p++;
PushStack(OpStack, &temp);
}
}
else if(*p == '*' || *p == '/')
{
if(GetStackTop(OpStack, &top) == FAILED)
{
temp.op = *p++;
PushStack(OpStack, &temp);
}
else if(top.op == '*' || top.op == '/')
{
PopStack(NumStack, &num1);
PopStack(NumStack, &num2);
PopStack(OpStack, &op);
temp.num = CalcNum(num1.num, num2.num, op.op);
PushStack(NumStack, &temp);
}
else
{
temp.op = *p++;
PushStack(OpStack, &temp);
}
}
else
{
printf("error!\n");
break;
}
}
GetStackTop(NumStack, &result);
return result.num;
} |
|