LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 900|回复: 3

duandeyou

[复制链接]
发表于 2004-4-1 18:59:13 | 显示全部楼层 |阅读模式

  1. #include "stdio.h"
  2. #include "math.h"
  3. #include "stdlib.h"

  4. struct OperationStack        {
  5.         char elem[100];
  6.         short top;
  7. } operation;

  8. struct DataStack{
  9.         float elem[100];
  10.         short top;
  11. } opnd;

  12. char  w,theta,choose;
  13. float result1,a,b,data;
  14. char  precede[17][17];
  15. FILE  *f;

  16. void Initialization(void);
  17. void Read(void);
  18. char GetOpStackTop(void);
  19. int  MyOrder(char);
  20. void PushOperationStack(char w);
  21. char PopOperationStack(void);

  22. int main() {
  23.         float result;
  24.         Initialization();
  25.     choose='Y';
  26.         do        {
  27.                 opnd.top=0 ;
  28.                 operation.top=0 ;
  29.                 PushOperationStack('=');/*INI_STACK*/
  30.                 Read();
  31.                 while (!((w=='=')&&(GetOpStackTop()=='='))) /*当运算符不只=时循环*/
  32.                 switch (precede[MyOrder(GetOpStackTop())][MyOrder(w)])
  33.                 {
  34.                         case  '<':        PushOperationStack(w);/*栈顶运算符比目前读入运算优先级小
  35.                                   当前读入的运算符入栈*/
  36.                                                 Read();/*读入下一个字符 */
  37.                                                 break;
  38.                         case  '=':        //char PopOperationStack(void);/*优先级相等时即'(',')'相遇时脱括号*/
  39.                                                 (operation.top)--;
  40.                                                 Read();
  41.                                                 break;
  42.                         case  '>':        theta=PopOperationStack();/*栈顶运算符比目前读入运算优先级高
  43.                                       则退栈并将结果入栈作下一运算的操作数*/
  44.                                                 float PopData(void);
  45.                                                 a=PopData();
  46.                                                 float PushData(float data);
  47.                                                 float operate(float,char);
  48.                                                 PushData(operate(a,theta));
  49.                                                 break;
  50.                         case  '!':        printf("Error!Check the input.\n");
  51.                                                 //goto a_b;
  52.                         default:break;
  53.                 }
  54.                 float PopData(void);
  55.                 printf("%f\n",PopData());                              
  56.                 printf("Continue?Y/N:");
  57.                 scanf("%c",&choose);
  58.         }while (choose=='Y');
  59. //switch (choose)
  60. //{case 'Y': goto a_a;break;
  61. // default : printf("Thank you!\n");
  62. // }
  63.     return;
  64. }

  65. void PushOperationStack(char w)        {
  66.         (operation.top)++;
  67.         operation.elem[operation.top]=w;
  68.         return;
  69. }

  70. void PushData(float data){
  71.         (opnd.top)++;
  72.         opnd.elem[opnd.top]=data;
  73.         return;
  74. }

  75. char PopOperationStack(void)        {
  76.         char op;
  77.         op=operation.elem[operation.top];
  78.         (operation.top)--;
  79.         return op;
  80. }

  81. float PopData(void)        {
  82.         float   num;
  83.         num=opnd.elem[opnd.top];
  84.         (opnd.top)--;
  85.         return num;
  86. }

  87. char GetOpStackTop(void)        {
  88.         return operation.elem[operation.top];
  89. }

  90. float gettop_data(void){
  91.         return opnd.elem[opnd.top];
  92. }

  93. void Read(void)        {
  94.         float data;
  95.         short scale;
  96.         BEGIN_1:scanf("%c",&w);
  97.         if ((w>='0')&&(w<='9'))        {
  98.                 data=0 ;
  99.             while ((w>='0')&&(w<='9')) {
  100.                         data=data*10+w-'0';
  101.                         scanf("%c",&w);
  102.                 }
  103.         }/*以上读入整数部分,下面读入小数部分*/
  104.         if (w=='.') {
  105.                 scale=1;   
  106.                 scanf("%c",&w);
  107.                 while (w>='0'&&w<='9'){
  108.                         data=data*10 +w-48;/*48为'0'的ASCII值*/
  109.                         (scale)*=10;
  110.                         scanf("%c",&w);
  111.                 }
  112.                 (data)/=scale;/*小数点左移得到数据的真值*/
  113.         }
  114.         PushData(data);
  115.         /*上面结束后W必定不在'0'..'9'之间*/
  116.         /*下面主要是对三角函数和max,min运算符的读入*/
  117.         switch (w)        {
  118.             case 'S':                     /*读入三角正弦w赋值为'B'*/
  119.                 case 's':scanf("%c",&w);
  120.                         switch (w) {
  121.                             case 'I':
  122.                                 case 'i':scanf("%c",&w);   
  123.                                                  if ( ( w=='n' ) || ( w=='N' ) ){
  124.                              w='B';
  125.                             }
  126.                          break;
  127.                 case 'E':
  128.                                 case 'e':scanf("%c",&w);
  129.                          if ( ( w=='c' ) || ( w=='C' ) ){
  130.                              w='F';
  131.                              }
  132.                          break;
  133.                         }
  134.                         break;
  135.                 case  'C':                        
  136.                 case  'c':scanf("%c",&w);
  137.             switch (w)        {
  138.                 case 'O':
  139.                                 case 'o':scanf("%c",&w);
  140.                                                  if ( ( w=='s' ) || ( w=='S' ) ){
  141.                                w='C';/*对应cos*/
  142.                                }
  143.                           else if ( ( w=='t' ) || ( w=='T') ){
  144.                                w='E';/*对应cot*/                        
  145.                                                        }
  146.                                break;
  147.                 case 'T':               
  148.                                 case 't':scanf("%c",&w);
  149.                                          if ( ( w=='g' ) || ( w=='G' ) ){
  150.                                                        w='E';/*对应ctg*/
  151.                             }
  152.                             break;
  153.                 case 'S':            
  154.                                 case 's':scanf("%c",&w);
  155.                                                  if ( ( w=='c' ) || ( w=='C' ) ){
  156.                                w='G';/*对应csc*/
  157.                                }
  158.                                break;      
  159.             }
  160.                         break;
  161.                 case 'T':       
  162.                 case 't':if ( ( getchar()=='a' ) || ( getchar()=='A' )){
  163.                               if ( ( getchar()=='n' ) || ( getchar()=='N' ) ){
  164.                                     w='D';/*对应tan*/
  165.                             }                          
  166.                                }
  167.                  else if ( ( getchar()=='g' ) || ( getchar()=='G' ) ){
  168.                             w='D';/*对应tg*/
  169.                             }                     
  170.                          break;
  171.                 case 'M':         
  172.                 case 'm':scanf("%c",&w);
  173.                         switch (w)        {
  174.                             case 'A':
  175.                                 case 'a':if ( ( getchar()=='a' ) || ( getchar()=='A' ) ) {
  176.                                                         if ( ( getchar()=='x' ) || ( getchar()=='X' ) ){  
  177.                                  w='H';/*对应max*/
  178.                                  }
  179.                                 }  
  180.                          break;
  181.                                 case 'I':                                                               
  182.                                 case 'i':if ( ( getchar()=='n' ) ||  ( getchar()=='N' ) ){
  183.                                                                 w='I';/*对应min*/
  184.                                 }
  185.                          break;
  186.             }
  187.                         break;
  188.                 case ',':goto BEGIN_1;/*跳转后为max和min读入下一个参数*/
  189.         default:;/*此处省略了读入+,-,*,/,%,^等符号时的空操作*/
  190.         }
  191.         return;
  192. }

  193. float operate(float a,char theta)        {
  194.         float b;
  195.         int c,d;
  196.         switch (theta)        {
  197.                 case '+':b=PopData();
  198.                                 return a+b;
  199.                 case '-':b=PopData();
  200.                                 return b-a;
  201.                 case '*':b=PopData();
  202.                                 return a*b;
  203.                 case '/':b=PopData();
  204.                                 if (b!=0)//I really don't
  205.                      //know why there is a error.
  206.                                 return b/a;
  207.                 case '%':c=floor(a);
  208.                                 b=PopData();
  209.                                 d=floor(b);
  210.                                 return d/c;/*库函数:取余 */
  211.                 case 'A':b=PopData();
  212.                                  return pow(a,b);/*POWER(a,b)*/
  213.         case 'B':return sin(a);
  214.                 case 'C':return cos(a);
  215.                 case 'D':return tan(a);
  216.                 case 'E':return 1/tan(a);
  217.                 case 'F':return 1/cos(a);
  218.                 case 'G':return 1/sin(a);
  219.                 case 'H':b=PopData();
  220.                                  return (a>b?a:b);
  221.                 case 'I':b=PopData();
  222.                                 return (a<b?a:b);
  223.                 default:break;
  224.         }
  225. }

  226. void Initialization()        {
  227.         short i=0,j=0;
  228.         char  tmp_c='\0';
  229.        
  230.         if ((f=fopen("for_ok.txt","r"))==NULL){
  231.                 printf("open file error\n");
  232.                 exit(-1);
  233.         };

  234.         while (!feof(f))        {
  235.                 j=0;               
  236.                 tmp_c=fgetc(f);
  237.                 //while (!(j==17))        {
  238.                 while ( tmp_c != '\n' ) {
  239.                           precede[i][j]=tmp_c;
  240.                           if (!feof(f)) tmp_c=fgetc(f);
  241.                           else break;
  242.                         j++;
  243.                 }
  244.                 i++;
  245.         }
  246.         fclose(f);
  247. }

  248. int MyOrder(char c){
  249.         switch (c){
  250.                 case '^':return 0;/*POW*/
  251.                 case 'B':return 1;/*SIN*/
  252.                 case 'C':return 2;/*COS*/
  253.                 case 'D':return 3;/*TAN*/
  254.                 case 'E':return 4;/*CTAN*/
  255.                 case 'F':return 5;/*SEC*/
  256.                 case 'G':return 6;/*CSC*/
  257.                 case 'H':return 7;/*MAX*/
  258.                 case 'I':return 8;/*MIN*/
  259.                 case '(':return 9;
  260.                 case ')':return 10;
  261.                 case '+':return 11;
  262.                 case '_':return 12;
  263.                 case '*':return 13;
  264.                 case '/':return 14;
  265.                 case '%':return 15;
  266.                 case '=':return 16;
  267.         }
  268. }
复制代码
发表于 2004-4-1 20:34:38 | 显示全部楼层
你的问题呢?:confused:
发表于 2004-4-4 22:18:55 | 显示全部楼层
收到。。。。
发表于 2004-4-5 15:24:54 | 显示全部楼层
从内容上看,好象是一个计算器的源代码。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表