• 重读The C programming Lanuage 笔记三:简单计算器程序


      1 //简单计算器
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <ctype.h>
      6 #include <string.h>
      7 #include <math.h>
      8 
      9 #define MAXOP 100       //max size of operand or operator
     10 #define NUMBER '0'      //sign of a number was found
     11 #define NAME 'n'        //sign of a mathfunc was found
     12 #define MAXVAL 100      //max size of the stack
     13 #define BUFSIZE 100     //buf io
     14 
     15 int sp = 0;              //the postion of the stack
     16 double val[MAXVAL];     //  the stack
     17 double variable[26];     //26 a~z
     18 void clear(void);
     19 
     20 int getop(char[]);     //get operand or operator
     21 void push(double);
     22 double pop(void);
     23 void mathfnc(char[]);  //math function
     24 
     25 int main()
     26 {
     27     int type,var = 0;
     28     double op1, op2, v;
     29     char s[MAXOP];
     30 
     31     for (int i = 0; i<26; i++)
     32     {
     33         variable[i] = 0.0;
     34 
     35         while ((type = getop(s)) != EOF)
     36             switch (type)
     37         {
     38 
     39             case NUMBER:
     40                 push(atof(s));
     41                 break;
     42             case NAME:
     43                 mathfnc(s);
     44                 break;
     45             case '+':
     46                 push(pop() + pop());
     47                 break;
     48             case '-':
     49                 op2 = pop(); push(pop() - op2);
     50                 break;
     51             case '*':
     52                 push(pop()*pop());
     53                 break;
     54             case '/':
     55                 op2 = pop();
     56                 if (op2 != 0.0)
     57                     push(pop() / op2);
     58                 else printf("error:zero divisor");
     59                 break;
     60             case '%':
     61                 op2 = pop();
     62                 if (op2 != 0.0)
     63                     push(fmod(pop(), op2));
     64                 else printf("error:zero divisor");
     65                 break;
     66                 //对栈操作 打印栈顶元素,交换栈值,清空栈
     67             case '?': // printf top of element of the stack
     68                 op2 = pop();
     69                 printf("	%.8g", op2);
     70                 push(op2);
     71                 break;
     72             case 'c':    //clear the stack
     73                 clear();
     74                 break;
     75             case 's':    //swap the top of the stack
     76                 op1 = pop();
     77                 op2 = pop();
     78                 push(op2);
     79                 push(op1);
     80                 break;
     81             case '
    ':
     82                 v = pop();
     83                 printf("	%8f
    ", v);
     84                 break;
     85             case '=':
     86                 pop();
     87                 if (var >= 'A' && var <= 'Z')
     88                     variable[var - 'A'] = pop();
     89                 else
     90                     printf("error: no variable name");
     91                 break;
     92             default:
     93                 if (type >= 'A' || type <= 'Z')
     94                     push(variable[type - 'A']);
     95                 else if (type == 'v')
     96                     push(v);
     97                 else
     98                     printf("error:unknown command %s
    ", s);
     99                 break;
    100 
    101         }
    102         var = type;
    103     }
    104     return 0;
    105 }
    106 
    107 //出入栈函数
    108 void push(double f)
    109 {
    110     if (sp<MAXVAL)
    111         val[sp++] = f;
    112     else
    113         printf("error :stack full,can push %s
    ", f);
    114 }
    115 
    116 double pop(void)
    117 {
    118     if (sp > 0)
    119         return val[--sp];
    120     else
    121         printf("error:stack empty");
    122     return 0.0;
    123 }
    124 
    125 
    126 //getop()函数
    127 int getch(void);
    128 void ungetch(int);
    129 
    130 int getop(char s[])
    131 {
    132     int c, i;
    133 
    134     while ((s[0] = c = getch()) == ' ' || c == '	')
    135         ;
    136     s[1] = '';
    137     i = 0;
    138 
    139     if (islower(c))      //commend or NAME
    140     {
    141         while (islower(s[++i] = c = getch()))
    142             ;
    143         s[i] = '';
    144         if (c != EOF)
    145             ungetch(c);
    146         if (strlen(s)>1)
    147             return NAME;
    148         else
    149             return c;
    150     }
    151     if (!isdigit(c) && c != '.' && c != '-')
    152         return c;           //not a number
    153     if (c == '-')
    154         if (isdigit(c = getch()) || c == '.')
    155             s[++i] = c;     //negetive number
    156         else
    157         {
    158             if (c != EOF)
    159                 ungetch(c);
    160             return '-';     //minus sign
    161         }
    162     if (isdigit(c))
    163         while (isdigit(s[++i] = c = getch()))
    164             ;
    165     if (c == '.')
    166         while (isdigit(s[++i] = c = getch()))
    167             ;
    168     s[i] = '';
    169     if (c != EOF)
    170         ungetch(c);
    171     return NUMBER;
    172 }
    173 
    174 //getch().ungetch()
    175 int  buf[BUFSIZE];      //不是char *buf 以能正确处理 EOF
    176 int bufp = 0;           //buf中下一个空闲位置
    177 
    178 int getch(void)         //取一个字符(可能是压回的字符)
    179 {
    180     return (buf > 0) ? buf[--bufp] : getchar();
    181 }
    182 
    183 void ungetchar(int c)   //把字符压回输入中
    184 {
    185     if (bufp >= BUFSIZE)
    186         printf("error :too many characters");
    187     else
    188         buf[bufp++] = c;
    189 }
    190 
    191 
    192 void clear(void)
    193 //reverse polish calculator
    194 {
    195     sp = 0;
    196 }
    197 
    198 void mathfnc(char s[])
    199 {
    200     double op2;
    201     if (strcmp(s, "sin") == 0)
    202         push(sin(pop()));
    203     else if (strcmp(s, "cos") == 0)
    204         push(cos(pop()));
    205     else if (strcmp(s, "exp") == 0)
    206         push(exp(pop()));
    207     else if (strcmp(s, "pow") == 0)
    208     {
    209         op2 = pop();
    210         push(pow(pop(), op2));
    211     }
    212     else printf("error:%s not supported
    ", s);
    213 }
    214 
    215 
    216 //push string back onto the input
    217 void ungets(char s[])
    218 {
    219     int len = strlen(s);
    220     void ungetch(int);
    221 
    222     while (len > 0)
    223         ungetch(s[--len]);
    224 }
    
     1 int getline(char line[], int lim)
     2 {
     3     int c, i;
     4     for (i = 0; i < MAXLINE - 1 && c != '
    '; ++i)
     5     {
     6         line[i] = c;
     7         if (c == '
    ')
     8         {
     9             line[i] = c;
    10             ++i;
    11         }
    12         line[i] = '';
    13     }
    14         return 0;
    15 
    16 }

    逆波兰表示法计算器(vs2013)

    可以完成简单运算(+ - * / %等)以及sin,cos,幂运算和对数运算

    以及例如:

         3 A =   将3的值复制给A

    此后 2 A +   则A的值为5

    计算器的换行操作符将输出数值5,同时把5赋值给变量v

    如下一个操作是 v 1 +  则结果将是 6

  • 相关阅读:
    NET 连接池救生员(转载:M$)
    gitflow学习总结
    Apache Zookeeper
    ZZ: C++实现读取整行文本和每次只读入一个单词
    ZZ: C C++ string转char*
    memset
    prinf
    ZZ: C++ int转string from http://pppboy.blog.163.com/blog/static/302037962010378296766/
    XHTML下,JS浮动代码失效的问题 心境
    13个超酷的js显示时间效果 心境
  • 原文地址:https://www.cnblogs.com/Yiaos/p/5396826.html
Copyright © 2020-2023  润新知