C/C++計算器(棧的應用)
阿新 • • 發佈:2018-12-11
實現一個計算器涉及到了字首表示式轉字尾表示式。
分別定義兩個棧,一個存數字,一個存操作符。
C++ 版本
#include "iostream" #include <stack> using namespace std; int Priority(char ch) { switch(ch) { case '(': return 3; case '*': case '/': return 2; case '+': case '-': return 1; default: return 0; } } int main() { stack<char> s_opt; stack<int> s_num; char opt[1024] = {0}; int i = 0, tmp = 0, num1 = 0, num2 = 0; cout << "Please input: " << endl; cin >> opt; while(opt[i] != '\0' || s_opt.empty() != true) { if(opt[i] >= '0' && opt[i] <= '9') { tmp = tmp * 10 + opt[i] - '0'; i++; if(opt[i] > '9' || opt[i] < '0') { s_num.push(tmp); tmp = 0; } } else { if((opt[i] == ')') && (s_opt.top() == '(')) { s_opt.pop(); i++; continue; } if((s_opt.empty()) == true || (Priority(opt[i]) > Priority(s_opt.top())) || (s_opt.top() == '(' && (opt[i] != ')'))) { s_opt.push( opt[i]); i++; continue; } if( ((opt[i] == '\0') &&( s_opt.empty() != true)) || ( (opt[i] == ')') && (s_opt.top() != '(') )|| (Priority(opt[i]) <= Priority(s_opt.top())) ) { char ch = s_opt.top(); s_opt.pop(); int num1,num2; switch(ch) { case'+': num1 = s_num.top(); s_num.pop(); num2 = s_num.top(); s_num.pop(); s_num.push(num1 + num2); break; case'-': num1 = s_num.top(); s_num.pop(); num2 = s_num.top(); s_num.pop(); s_num.push(num1 - num2); break; case'*': num1 = s_num.top(); s_num.pop(); num2 = s_num.top(); s_num.pop(); s_num.push(num1 * num2); break; case'/': num1 = s_num.top(); s_num.pop(); num2 = s_num.top(); s_num.pop(); s_num.push(num1 / num2); break; } } } } cout << s_num.top() << endl; return 0; }
C版本: .h
#ifndef _LINKSTACK_H #define _LINKSTACK_H #define SUCCESS 10000 #define FAILURE 10001 #define TRUE 10002 #define FALSE 10003 #include <stdio.h> #include <stdlib.h> struct node { int date; struct node *next; }; typedef struct node Node; struct stack { Node *top; int count; }; typedef struct stack Stack; int LinkStackInit(Stack **s); int LinkStackEmpty(Stack *s); int Push(Stack **s,int e); int GetTop(Stack *s); int Pop(Stack **s); int LinkStackClear(Stack **s); int LinkStackDestory(Stack **s); #endif
.c
#include "linkstack.h" int LinkStackInit(Stack **s) { if(s == NULL) { return FAILURE; } (*s) = (Stack *)malloc(sizeof(Stack) * 1); if(*s == NULL) { return FAILURE; } (*s) -> top = NULL; (*s) -> count = 0; return SUCCESS; } int LinkStackEmpty(Stack *s) { if(s == NULL) { return FAILURE; } return (s->top == NULL)? TRUE : FALSE; } int Push(Stack **s, int e) { if(s == NULL) { return FAILURE; } if((*s) == NULL) { return FAILURE; } Node *p = (Node *)malloc(sizeof(Node)); p -> date = e; p -> next = (*s) -> top; (*s) -> top = p; (*s) -> count++; return SUCCESS; } int GetTop(Stack *s) { if(s == NULL || s->top == NULL) { return FAILURE; } return s->top->date; } int Pop(Stack **s) { if(s == NULL || (*s) == NULL) { return FAILURE; } Node *p = (*s) -> top; int e = (*s) -> top -> date; (*s) -> top = (*s) -> top -> next; (*s) -> count--; free(p); return e; } int LinkStackClear(Stack **s) { if(s == NULL || (*s) == NULL) { return FAILURE; } Node *temp = (*s) -> top; while(temp != NULL) { (*s) -> top = temp -> next; free(temp); temp = (*s) -> top; (*s)->count--; } return SUCCESS; } int LinkStackDestory(Stack **s) { if(s == NULL || (*s) == NULL) { return FAILURE; } free(*s); (*s) = NULL; return SUCCESS; }
main.c
#include "linkstack.h"
int Priority(char ch)
{
switch(ch)
{
case '(':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
int main()
{
Stack *s_opt, *s_num;
char opt[1024] = {0};
int i = 0, tmp = 0, num1 = 0, num2 = 0;
if(LinkStackInit(&s_opt) != SUCCESS || LinkStackInit(&s_num) != SUCCESS)
{
printf("Init Failure \n");
}
printf("Please input: \n");
scanf("%s",opt);
while(opt[i] != '\0' || LinkStackEmpty(s_opt) != TRUE)
{
if(opt[i] >= '0' && opt[i] <= '9')
{
tmp = tmp * 10 + opt[i] - '0';
i++;
if(opt[i] > '9' || opt[i] < '0')
{
Push(&s_num, tmp);
tmp = 0;
}
}
else
{
if((opt[i] == ')') && (GetTop(s_opt) == '('))
{
Pop(&s_opt);
i++;
continue;
}
if((LinkStackEmpty(s_opt) == TRUE) || (Priority(opt[i]) > Priority(GetTop(s_opt))) ||
( (GetTop(s_opt) == '(') && (opt[i] != ')')))
{
Push(&s_opt, opt[i]);
i++;
continue;
}
if( ((opt[i] == '\0') &&( LinkStackEmpty(s_opt) != TRUE)) || ( (opt[i] == ')') && (GetTop(s_opt) != '(') )||
(Priority(opt[i]) <= Priority(GetTop(s_opt))) )
{
switch(Pop(&s_opt))
{
case'+':
num1 = Pop(&s_num);
num2 = Pop(&s_num);
Push(&s_num, (num2 + num1));
break;
case'-':
num1 = Pop(&s_num);
num2 = Pop(&s_num);
Push(&s_num, (num2 - num1));
break;
case'*':
num1 = Pop(&s_num);
num2 = Pop(&s_num);
Push(&s_num, (num2 * num1));
break;
case'/':
num1 = Pop(&s_num);
num2 = Pop(&s_num);
Push(&s_num, (num2 / num1));
break;
}
}
}
}
printf("%d \n",GetTop(s_num));
return 0;
}