zoukankan      html  css  js  c++  java
  • 科学计算器

      科学计算器

     

    C++编写计算器代码:

      1 #include<iostream>
      2 #include<stack>
      3 #include<sstream>
      4 #include<cstdio>
      5 #include<string>
      6 
      7 using namespace std;
      8 
      9 char ch;
     10 stringstream ss;
     11 stringstream ss2;
     12 stack<char> st;
     13 stack<double> st2;
     14 string str;
     15 string str2;
     16 double t;
     17 
     18 inline int diy(char ch)
     19 {
     20     if(ch=='*'||ch=='/')return 2;
     21     if(ch=='+'||ch=='-')return 1;
     22     else return 0;
     23 }
     24 
     25 void fun()//中缀式转后缀式
     26 {
     27     str2.clear ();
     28     while(!st.empty ())st.pop ();
     29     st.push ('#');
     30     ss.clear();
     31     ss<<str;
     32     while(ss>>ch)
     33     {
     34         if(ch=='=')
     35         {
     36             while(st.top ()!='#')
     37             {
     38                 
     39                 str2+=st.top ();
     40                 str2+=' ';
     41                 st.pop ();
     42             }
     43         }
     44         else if(ch=='.'||(ch<='9'&&ch>='0'))
     45         {
     46             str2+=ch;
     47             while(ss>>ch)
     48             {
     49                 if(ch=='.'||(ch<='9'&&ch>='0'))
     50                 {
     51                     str2+=ch;
     52                 }
     53                 else 
     54                 {
     55                     ss.unget ();
     56                     str2+=' ';break;
     57                 }
     58             }
     59         }
     60         else if(ch=='(') 
     61         {
     62             st.push ('(');
     63         }
     64         else if(ch==')')
     65         {
     66             while(st.top ()!='(')
     67             {
     68                 str2+=st.top ();
     69                 str2+=' ';
     70                 st.pop ();
     71             }
     72             st.pop ();//删除左括号
     73         }
     74         else //是运算符
     75         {
     76             while(diy(st.top ())>=diy(ch))
     77             {
     78                 str2+=st.top ();
     79                 str2+=' ';
     80                 st.pop ();
     81             }
     82             st.push (ch);
     83         }
     84     }
     85 }
     86 
     87 double fun2()//后缀式求值
     88 {
     89     ss2.clear ();
     90     ss2<<str2;
     91     while(!st2.empty ())st2.pop ();
     92     st2.push (0);
     93     while(ss2>>ch)
     94     {
     95         if(ch>='0'&&ch<='9')
     96         {
     97             ss2.unget ();
     98             ss2>>t;
     99             st2.push (t);
    100         }
    101         else 
    102         {
    103             switch (ch)
    104             {
    105                 case '+':t=st2.top ();st2.pop ();t+=st2.top ();st2.pop ();st2.push (t);break;
    106                 case '-':t=st2.top ();st2.pop ();t=st2.top ()-t;st2.pop ();st2.push (t);break;
    107                 case '*':t=st2.top ();st2.pop ();t*=st2.top ();st2.pop ();st2.push (t);break;
    108                 case '/':t=st2.top ();st2.pop ();t=st2.top ()/t;st2.pop ();st2.push (t);break;
    109             }
    110         }
    111     }
    112     return st2.top ();
    113 }
    114 
    115 int main()
    116 {
    117     cout << "欢迎使用科学计算器,仅适用于合法的四则运算.
    
    	输入只能是:
    
    		“+”  “-”   “*”  “/”  “()”   “.”   “0到9的数字” 
    
    			 结尾必须是“=”" << endl;
    118     cout << "
    输入结束快捷键:Ctrl + Z 
    
    计算开始
    " << endl;
    119     while(getline(cin,str))
    120     {    
    121         fun();            
    122         printf("%.4lf
    ",fun2());    
    123     }
    124     cout << "计算结束,欢迎再次使用!!!" << endl;
    125     return 0;
    126 }
  • 相关阅读:
    2018.09.25python学习第十天part3
    2018.09.25python学习第十天part2
    2018.09.25python学习第十天part1
    2018.09.21python学习第九天part3
    2018.09.21python学习第九天part2
    2018.09.21python学习第九天part1
    2018.09.20python作业
    Alpha 冲刺(3/10)
    Alpha 冲刺(2/10)
    Alpha 冲刺(1/10)
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4481534.html
Copyright © 2011-2022 走看看