简单计算器(用数组模拟栈写的)

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 const int maxn=1000+5;
  4 typedef struct{
  5     int top;
  6     int a[maxn];
  7 }opn;
  8 typedef struct{
  9     int top;
 10     char a[maxn];
 11 }opt;
 12 void opn_init(opn *s){
 13     s->top=-1;
 14 }
 15 void opt_init(opt *s){
 16     s->top=-1;
 17 }
 18 void opn_push(int x,opn *s){
 19     s->top++;
 20     s->a[s->top]=x;
 21 }
 22 void opt_push(char x,opt *s){
 23     s->top++;
 24     s->a[s->top]=x;
 25 }
 26 void opn_pop(opn *s){
 27     s->top--;
 28 }
 29 void opt_pop(opt *s){
 30     s->top--;
 31 }
 32 int opn_top(opn *s){
 33     return s->a[s->top];
 34 }
 35 char opt_top(opt *s){
 36     return s->a[s->top];
 37 }
 38 int isop(char c)
 39 {
 40     if (c == ‘+‘ || c == ‘-‘ || c == ‘*‘ || c == ‘/‘ || c == ‘(‘ || c == ‘)‘||c==‘#‘)
 41         return 1;
 42     else return 0;
 43 }
 44 char pro(char s,char c)
 45 {
 46     switch (s){
 47     case(‘+‘):
 48     case(‘-‘):
 49         if (c == ‘+‘ || c == ‘-‘)return ‘>‘;
 50         else if (c == ‘*‘ || c == ‘/‘)return ‘<‘;
 51         else if (c == ‘(‘)return ‘<‘;
 52         else if (c == ‘)‘)return ‘>‘;
 53         else return ‘>‘;
 54         break;
 55
 56     case(‘*‘):
 57     case(‘/‘):
 58         if (c == ‘+‘ || c == ‘-‘)return ‘>‘;
 59         else if (c == ‘*‘ || c == ‘/‘)return ‘>‘;
 60         else if (c == ‘(‘)return ‘<‘;
 61         else if (c == ‘)‘)return ‘>‘;
 62         else return ‘>‘;
 63         break;
 64     case(‘(‘):
 65         if (c == ‘)‘)return ‘=‘;
 66         else return ‘<‘;
 67     case(‘)‘) :
 68         return ‘>‘;
 69     case(‘#‘):
 70         if (c == ‘#‘)return ‘=‘;
 71         else return ‘<‘;
 72     }
 73 }
 74 int oper(int a, char op, int b)
 75 {
 76     if (op == ‘+‘)return a + b;
 77     else if (op == ‘-‘)return a - b;
 78     else if (op == ‘*‘)return a*b;
 79     else if (op == ‘/‘)return a / b;
 80 }
 81 int main()
 82 {
 83     char ch,op;
 84     int i,a,b,res;
 85     opn p1;
 86     opt p2;
 87     opn_init(&p1);
 88     opt_init(&p2);
 89     ch = getchar();
 90     opt_push(‘#‘,&p2);
 91     while (ch != ‘#‘||opt_top(&p2) != ‘#‘)
 92     {
 93         if (!isop(ch))
 94         {
 95             i = atoi(&ch);
 96             ch = getchar();
 97             while (!isop(ch))
 98             {
 99                 i = i * 10 + atoi(&ch);
100                 ch = getchar();
101             }
102             opn_push(i,&p1);
103         }
104         else
105         {
106             switch (pro(opt_top(&p2), ch))
107             {
108             case(‘<‘) :
109                 opt_push(ch,&p2);
110                 ch = getchar();
111                 break;
112
113             case(‘=‘) :
114                 opt_pop(&p2);
115                 ch = getchar();
116                 break;
117
118             case(‘>‘) :
119                 op = opt_top(&p2);
120                 opt_pop(&p2);
121                 b = opn_top(&p1);
122                 opn_pop(&p1);
123                 a = opn_top(&p1);
124                 opn_pop(&p1);
125                 res=oper(a, op, b);
126                 opn_push(res,&p1);
127                 break;
128             }
129         }
130     }
131     printf("%d\n", opn_top(&p1));
132     system("pause");
133     return 0;
134 }
时间: 2024-12-28 08:27:58

简单计算器(用数组模拟栈写的)的相关文章

java、C语言实现数组模拟栈

java: public class ArrayStack { private int[] data; private int top; private int size; public ArrayStack(int size) { this.data = new int[size]; this.size = size; this.top = -1; } public boolean isEmpty() { if (this.top == -1) { return true; } return

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr

用数组模拟栈的结构

package datastruct; import java.util.Arrays; /** * 用数组模拟栈的结构:后进先出(LIFO) 线性表结构 * @author stone * 2014-07-29 06:34:49 */ public class SimulateStack<E> { public static void main(String[] args) { SimulateStack<String> ss = new SimulateStack<Str

数组模拟栈

数组模拟栈(Stack) package main import ( "fmt" ) //使用数组模拟stack type Stack struct { Max int //表示最大储存个数 Maxtop int arr [5]int } func (s *Stack) IsStackFulll()(bool){ if s.Maxtop == s.Max - 1 { return true } return false } func (s *Stack) IsStackEmpty()(

HDU 1237 简单计算器(后缀式+栈)

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16351    Accepted Submission(s): 5604 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,

JavaScript数组模拟栈和队列

*栈和队列:js中没有真正的栈和队列的类型              一切都是用数组对象模拟的 栈:只能从一端进出的数组,另一端封闭       FILO   何时使用:今后只要仅希望数组只能从一端进出时   如何使用:2种情况:     1. 末尾出入栈:已入栈元素的下标不再改变        入栈: arr.push(新值1,...)        出站: var last=arr.pop() 2. 开头出入栈:每次入栈新元素时,已入栈元素的位置都会向后顺移.        入栈:arr.u

ACM/ICPC 之 用双向链表 or 模拟栈 解“栈混洗”问题-火车调度(Tshing Hua OJ - Train)

本篇用双向链表和模拟栈混洗过程两种解答方式具体解答“栈混洗”的应用问题 有关栈混洗的定义和解释在此篇:手记-栈与队列相关 列车调度(Train) Description Figure 1 shows the structure of a station for train dispatching. Figure 1 In this station, A is the entrance for each train and B is the exit. S is the transfer end.

hdu1237 简单计算器 (模拟+栈)

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15075    Accepted Submission(s): 5132 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,

HDU1237 简单计算器 【栈】+【逆波兰式】

版本:1.0 日期:2014.5.17 2014.6.1 版权:© 2014 kince 转载注明出处 在介绍SwitchButton之前,先来看一下系统Button是如何实现的.源码如下: @RemoteView public class Button extends TextView { public Button(Context context) { this(context, null); } public Button(Context context, AttributeSet att