找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2526|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

C++計(jì)算器程序代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:105323 發(fā)表于 2016-2-22 20:51 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式


  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. #include<iostream>
  5. /*****************************************/
  6. /*  將數(shù)字字符轉(zhuǎn)化成浮點(diǎn)型實(shí)數(shù)進(jìn)行計(jì)算   */
  7. /*                                       */
  8. /*****************************************/
  9. double readnum(char f[],int*i)
  10. {
  11. double x=0.0;
  12. int k=0;
  13. while(f[*i]>='0'&&f[*i]<='9')
  14. {
  15.   x=x*10+(f[*i]-'0');
  16.   (*i)++;
  17. }
  18. if(f[*i]=='.')
  19. {
  20.   (*i)++;
  21.   while(f[*i]>='0'&&f[*i]<='9')
  22.   {
  23.    x=x*10+(f[*i]-'0');
  24.    (*i)++;
  25.    k++;
  26.   }
  27. }
  28. while(k-->0)
  29. {
  30.   x=x/10.0;
  31.   }
  32. return (x);
  33. }
  34. /*******************************/
  35. /*  計(jì)算后綴表達(dá)式的值         */
  36. /*******************************/
  37. double evalpost(char f[])
  38. {
  39. double obst[10];
  40. int top=0;
  41. int i=0;
  42. double x1,x2;
  43. while(f[i]!='=')
  44. {
  45.   if(f[i]>='0'&&f[i]<='9')
  46.   { obst[top]=readnum(f,&i);top++;}
  47.   else if(f[i]==' ')
  48.    i++;
  49.   else if(f[i]=='+')
  50.   {
  51.    x1=obst[--top];
  52.    x2=obst[--top];
  53.    obst[top]=x1+x2;
  54.    i++;
  55.    top++;
  56.   }
  57.   else if(f[i]=='-')
  58.   {
  59.    x1=obst[--top];
  60.    x2=obst[--top];
  61.    obst[top]=x2-x1;
  62.    i++;
  63.    top++;
  64.   }
  65.   else if(f[i]=='*')
  66.   {
  67.    x1=obst[--top];
  68.    x2=obst[--top];
  69.    obst[top]=x1*x2;
  70.    i++;
  71.    top++;
  72.   }
  73.   else if(f[i]=='/')
  74.   {
  75.    x1=obst[--top];
  76.    x2=obst[--top];
  77.    obst[top]=x2/x1;
  78.    i++;
  79.    top++;
  80.   }
  81.    }
  82. return obst[0];
  83. }
  84. /***********************************/
  85. /*       判斷字符是否為操作字符    */
  86. /***********************************/
  87. int is_operation(char op)
  88. {
  89. switch(op)
  90. {
  91. case'^':
  92. case'K':
  93. case'+':
  94. case'-':
  95. case'*':
  96. case'/': return 1;
  97. default: return 0;
  98. }
  99. }
  100. /*****************************/
  101. /*    判斷字符的優(yōu)先級       */
  102. /*****************************/
  103. int priority(char op)
  104. {
  105. switch(op)
  106. {
  107. case'=': return -1;
  108. case'(': return 0;
  109. case'+':
  110. case'-': return 1;
  111. case'*':
  112. case'/': return 2;
  113. default: return -1;
  114. }
  115. }
  116. /******************************/
  117. /*  中綴表達(dá)式轉(zhuǎn)化成后綴表達(dá)式*/
  118. /******************************/
  119. void postfix(char e[],char f[])
  120. {
  121. int i=0,j=0,k=0;
  122. char opst[100];
  123. int top=0;
  124. opst[0]='=';top++;
  125. while(e[i]!='=')
  126. {
  127.   if((e[i]>='0'&&e[i]<='9')||e[i]=='.')
  128.    f[j++]=e[i];
  129.   else if(e[i]=='(')
  130.   { opst[top]=e[i];top++;}
  131.   else if(e[i]==')')
  132.   {
  133.    k=top-1;
  134.    while(opst[k]!='(') {f[j++]=opst[--top];k=top-1;}
  135.    top--;
  136.   }
  137.   else if(is_operation(e[i]))
  138.   {
  139.    f[j++]=' ';
  140.    while(priority(opst[top-1])>=priority(e[i]))
  141.    f[j++]=opst[--top];
  142.    opst[top]=e[i];
  143.    top++;
  144.   }
  145.   i++;
  146. }
  147. while(top) f[j++]=opst[--top];f[j]='\0';
  148. }
  149. void print_1()
  150. {  printf("|_______________________________________ |                            \n");
  151.        printf("||                                     | |                            \n");
  152.        printf("||        歡迎使用多功能計(jì)算器         | |         本計(jì)算器能夠進(jìn)行   \n");
  153.        printf("||_____________________________________| |      +,-,×,÷,\n");
  154.        printf("||                    圖案僅供參考     | |        ()             \n");
  155.        printf("||_____________________________________| |              \n");
  156.        printf("|                                        |                         \n");
  157.        printf("|___  ___  ___  ___  ___  ___  ___  ___  |                            \n");
  158.        printf("|________  ________  ________  ________  |                            \n");
  159.        printf("||  ⑨  |  |  ⑧  |  |  ⑦  |  |  ×  |  |                            \n");
  160.        printf("||______|  |______|  |______|  |______|  |                            \n");
  161.        printf("|________  ________  ________  ________  |                            \n");
  162.        printf("||  ⑥  |  |  ⑤  |  |  ④  |  |  -  |  |                            \n");
  163.        printf("||______|  |______|  |______|  |______|  |                            \n");
  164.        printf("| _______  ________  ________  ________  |                            \n");
  165.        printf("||  ③  |  |  ②  |  |  ①  |  |  +  |  |                            \n");
  166.        printf("||______|  |______|  |______|  |______|  |                            \n");
  167.        printf("|________  ________  ________  ________  |                            \n");
  168.        printf("||  〇  |  |  =  |  |  AC  |  |  ÷  |  |                            \n");
  169.        printf("||______|  |______|  |______|  |______|  |                            \n");
  170.    getch();
  171.    system("cls");
  172. }
  173. void printf_2()
  174. {system("cls");
  175. printf("\n\n\n\n\n\n\n\n\t\t\t ##############################\n");
  176.     printf("\t\t\t #                            #\n");
  177.     printf("\t\t\t #----------謝謝使用----------#\n");
  178.    printf("\t\t\t #                            #\n");
  179.    printf("\t\t\t ##############################\n");
  180.    printf("\t\t\t                      --XXXXXX制作\n                                 ");
  181. }
  182. /****************/
  183. /*   轉(zhuǎn)化   */
  184. /****************/
  185. void zhuanhuan(char g[],char e[])
  186. {
  187. int k,i,j=0;
  188. for(i=0;g[i]!='=';i++)
  189. {
  190.   k=i+1;
  191.   if(g[i]=='('&&g[k]=='-')
  192.   {
  193.    e[j++]=g[i];
  194.    e[j++]='0';
  195.   }
  196.   else e[j++]=g[i];
  197. }
  198.   e[j]='=';
  199. }
  200. int main()
  201. {
  202.     int wei;
  203.          char e[100],f[100],g[100];
  204.    int sign;int flag;
  205.   print_1();
  206.    do
  207.    {
  208.      printf("輸入所要經(jīng)計(jì)算的表達(dá)式(如:a*b/(c-d)=):\n");
  209.      scanf("%s",g);
  210.      zhuanhuan(g,e);
  211.      postfix(e,f);
  212.      printf("輸出保留幾位小數(shù):\n");
  213.      scanf("%d",&wei);
  214.      printf("%.*lf\n",wei,evalpost(f));
  215.      while(1)
  216.      { flag=3 ;
  217.      printf("繼續(xù)計(jì)算/退出?1/0?");
  218.            sign=getch();
  219.      printf("%c\n",sign);
  220.      switch(sign)
  221.      {
  222.        case '1':flag=1;getch();break;
  223.     case '0':flag=0;getch();break;
  224.     default: printf("非法輸入,請重新輸入:\n");
  225.      }
  226.      if(flag==1||flag==0)break;
  227.      }
  228.    }while(flag==1);
  229.   printf_2();
  230. return 0;
  231. }
復(fù)制代碼






分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表