当前位置:编程学习 > C/C++ >>

用C++编写计算器

用C++编写计算器能够实现加减乘出、三角函数、指数函数、对数函数、倒数
追问:来自手机问问你好,不需要图形界面,做一个简单一点的程序,最好有详细解释。谢谢
答案:// arithmeticAnalysis.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<math.h>
#include<stdlib.h>

using namespace std;
#define MAX 100

class Cstack  
{   private:  
  char arr[MAX];// Contains all the Data
  int top;
  int c;
//Contains location of Topmost Data pushed onto Stack  
public: 
 Cstack() //Constructor 
    {  
   top = -1; //Sets the Top Location to -1 indicating an empty stack  
    }  
 void push(char a) // Push ie. Add Value Function 
    {   top++; // increment to by 1  
   if(top < MAX)  
     {   arr[top] = a; //If Stack is Vacant store Value in Array  
     }  
   else 
     {   cout<<"STACK FULL!!"<<endl;
      top--; 
     }
  }
 char pop() // Push ie. Add Value Function 
    {  
   top--; // increment to by 1  
   if(top >= -1)  
     { 
    c = top + 1;
    return arr[c]; //If Stack is Vacant store Value in Array
   }
   else
    return NULL;
   /*
   else 
     {  
      cout<<"STACK EMPTY!!"<<endl;
      top++;
      return NULL;
     }
     */
  }
  bool isEmpty()
    {
   if (top <= -1) 
    return true;
   else
    return false;

    }
}stack1;


class Nstack  
{   private:  
  float arr[MAX];// Contains all the Data
  int top;
  int c;
//Contains location of Topmost Data pushed onto Stack  
public: 
 Nstack()
     //Constructor 
    {   top=-1; //Sets the Top Location to -1 indicating an empty stack  
    }  
 void push(float a) // Push ie. Add Value Function 
    {   top++; // increment to by 1  
   if(top < MAX)  
     {   arr[top]=a; //If Stack is Vacant store Value in Array  
     }  
   else 
     {   cout<<"STACK FULL!!"<<endl;
      top--; 
     }
  }
 float pop() // Push ie. Add Value Function 
    {  
   top--; // increment to by 1
   if(top >= -1)  
     { 
    c = top + 1;
    return arr[c]; //If Stack is Vacant store Value in Array  
     
     }
   
  
   /*
   else 
     {  
      cout<<"STACK EMPTY!!"<<endl;
      top++;
      return NULL;
     }
     */
 
  }
   bool isEmpty()
    {
   if (top <= -1) 
    return true;
   else
    return false;

    }
}stack2;


int compute(char Opr)
 {

  if(Opr == '=')
  {
   cout<<stack2.pop()<<endl; 
   return 0;
        }//输出结果  
  float Num1 = stack2.pop() ,Num2 = stack2.pop();
  
  switch(Opr)
  {
   case '+':stack2.push(Num2+Num1);break;  //结果压栈
   case '-':stack2.push(Num2-Num1);break;
   case '*':stack2.push(Num2*Num1);break;
   case '/':stack2.push(Num2/Num1);break;
   
  }
  return 0;
 }


/*
bool isDigit(char ch)
{
 if(ch > '0' && ch < '10')
  return true;
 else
  false;
}
*/

int main(int argc, char* argv[])
{
 cout<<"输入算术表达式:"<<endl;
 char str[50];
 bool b1 =true,b2 = true,b3 = true;
 char ch,ch1;
 int k = -1;
 
 while(b1)
 {
  cin>>ch;
  if((ch >= '0' && ch <= '9')  || ch == '.')
  {
   k++;
   str[k] = ch;
   //cout<<ch;
  }
  else if(ch != '(' && b3)//第一个')'后面紧跟的那个个操作符在此会出问题
  {
   k++;
   str[k] = '\0';
   float a = atof(str);
   stack2.push(a);
   k = -1;
  }
  switch(ch)
  {
   case '(':b3 = true;stack1.push(ch);break;
   case '+':
   case '-':
    b3 = true;
    while(!stack1.isEmpty())
    {
     ch1 = stack1.pop();
     if(ch1 == '(')
     {
      stack1.push(ch1);
      break;
     }
     else
     {
      compute(ch1);
     }

    }
    stack1.push(ch);
    break;
   case '*':
   case '/':
    b3 = true;
    while(!stack1.isEmpty() && b2)
    {
     ch1 = stack1.pop();
     if(ch1 == '(')
     {
      stack1.push(ch1);
      break;
     }
     else if(ch1 == '*' || ch1 == '/')
     {
      compute(ch1);
     }
     else
     {
      stack1.push(ch1);
      b2 = false;
     }
    }
    stack1.push(ch);
    b2 = true;
    break;
   case ')':
    b3 =

上一个:C++问题 急求
下一个:C/C++/VB有什么关系?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,