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

跪求C++编程的计算器一个。

不需要太复杂,能做两位数加减乘除运算,如果输入格式错误有提示。
 
补充:

那个有可以用到stack -  pop, push的方法吗。

老师需要 = =#

答案:

上班时间抽空写的,有点乱,将就下用吧。

#include <stdio.h>
#include <stdlib.h>

#define ERRO printf("Program Erro\n");ERRO_NUM = 1
#define ERRO_NUM n
char buf[255];//缓存空间
int n;//错误代码

int empty_str(char* str)//清空缓存
{
 int i;
 for( i = 0; i<255; i++)
 {
  *str =0;
  str++;
 }
 return 1;
}

int process_lv1(char* str)//去掉空格
{
 char * p = str;
    if( !str ) return 0;

 while ( *str )
 {
  if( *str != 32 )
  {
   *p = *str;
   p++;
  }
  str++;
 }
 *p = 0;
 return 1;
}

double process_lv2(void)//解析表达式
{
 ERRO_NUM = 0;
 if( !process_lv1(buf) )
 {
  ERRO;
  empty_str(buf);
  return 0;
 }
 double a,b;
 char key,*pos;
 pos = 0;
 a = strtod(buf,&pos);
 key = *pos;
 pos++;
 b = strtod( pos,NULL);
 empty_str(buf);
printf("a = %f,b = %f , key = %c ",a,b,key);
 switch ( key )
 {
 case '+':
  printf("\nfuck fuck fuck !!!%f\n",a+b);
  return a+b;
 case '-':
  printf("\nfuck fuck fuck !!!%f\n",a-b);
  return a-b;
 case '*':
  printf("\nfuck fuck fuck !!!%f\n",a*b);
  return a*b;
 case '/':
  printf("\nfuck fuck fuck !!!%f\n",a/b);
  return a/b;
 default:
   ERRO;
   break;
 }
 return 0;
}

int go(void)//主人机界面
{
 double tmp;
 puts("Enter:\n");
 scanf("%s",buf);
 putchar('\n');
 puts( buf );
 tmp = process_lv2();
 if( !ERRO_NUM )
 {
  printf("\n Answer: %f\n",tmp);
  return 1;
 }
 return 0;
}


int main(void)//程序入口
{
 while( go() );
 return 0;
}

//这个是用if函数编出来的,有的草略,不过能用,其实只是我的水平只在这里,呵呵!满意就给点分吧!

#include<iostream.h>
void main()
{
 int a,a1,a2,b1,b2,c1,c2,d1,d2;
  cout<<"请先输入一个数!\n"<<"1:代表乘法,2:代表除法,3:代表加法,4:代表减法!"<<endl;
   cin>>a;

  if(a==1)
  {
   cout<<"请输入一个被乘数:";
    cin>>a1;
   cout<<"\n请输入一个乘数:";
    cin>>a2;

   cout<<a1<<'*'<<a2<<'='<<a1*a2;
  }
  if(a==2)
  {
   cout<<"请输入一个被除数:";
    cin>>b1;
   cout<<"\n请输入一个除数:";
    cin>>b2;
    if(b2==0)
    {
     cout<<"\n0不能当除数\n";
    }
    else
     cout<<b1<<'*'<<b2<<'='<<b1*b2;
  }
  if(a==3)
  {
   cout<<"请输入一个被加数:";
    cin>>c1;
   cout<<"\n请输入一个加数:";
    cin>>c2;

   cout<<c1<<'*'<<c2<<'='<<c1*c2;
  }
  if(a==4)
  {
   cout<<"请输入一个被减数:";
    cin>>d1;
   cout<<"\n请输入一个减数:";
    cin>>d2;
 
   cout<<d1<<'*'<<d2<<'='<<d1*d2;
  }

  if(a<1)
  {
   cout<<"请输入正确的数字!谢谢!"<<endl;
  }
  if(a>4)
  {
   cout<<"请输入正确的数字!谢谢!"<<endl;
  }
}

#include <iostream>

using namespace std;

int DoAdd(int a, int b)
{
    return a+b;
}

int DoSub(int a, int b)
{
    return a-b;
}

int DoMul(int a, int b)
{
    return a*b;
}

float DoDiv(float a, float b)
{
    return a/b;
}

int main()
{
    int a, b, choose = 0;
    while(1)
    {
        cout << "选择操作:" << endl;
        cout << "1.加" << endl;
        cout << "2.减" << endl;
        cout << "3.乘" << endl;
        cout << "4.除" << endl;
        cout << "5.退出" << endl;
        cin >> choose;
        if(choose == 5)
            break;

        cout << "输入两个数:";
        cin >> a;
        cin >> b;

        cout << "结果为:";
        switch(choose)
        {
        case 1:
            cout << DoAdd(a, b);
            break;
        case 2:
            cout << DoSub(a, b);
            break;
        case 3:
            cout << DoMul(a, b);
            break;
        case 4:
            if(b == 0)
                cout << "除数不能为0";
            else
            cout << DoDiv((float)a, (float)b);
            break;
        }
        cout << endl;
    }
    return 0;
}

上一个:C++编程二维数组做参数
下一个:c++编程题 类与对象

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