c++編寫簡單的計算器程序
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次
首先來看下本人的開發(fā)環(huán)境
系統:win7
電腦:dell
運行環(huán)境:vs2015
語言:c++
簡單計算器代碼
//四則運算
#include "stdafx.h"
#include<iostream>
#include<stdio.h>
using namespace std;
void add()
{
printf("輸入要計算的加數(例如a b)\n");
int adda=0, addb=0,addc=0;
cin >> adda;
cin >> addb;
addc = adda+addb;
cout <<adda<<"加"<<addb<< "等于" << addc << endl;
}
void substraction()
{
printf("輸入要計算的減數(例如a b)\n");
int suba = 0, subb = 0, subc = 0;
cin >> suba;
cin >> subb;
subc = suba-subb;
cout <<suba<<"減"<<subb<< "等于" << subc << endl;
}
void multiplication()
{
printf("輸入要計算的乘數(例如a b)\n");
int mula = 0, mulb = 0, mulc = 0;
cin >> mula;
cin >> mulb;
mulc = mula*mulb;
cout <<mula<<"乘"<<mulb<< "等于" << mulc << endl;
}
void division()
{
printf("輸入要計算的除數(例如a b)\n");
int dsa = 0, dsb = 0, dsc = 0,dsd=0;
cin >> dsa;
cin >> dsb;
dsc = dsa/dsb;
dsd = dsa%dsb;
cout <<dsa<<"除"<<dsb<< "等于" << dsc <<"余"<<dsd<<endl;
}
void operation()//運算函數
{
printf("輸入數據選擇做那種運算\n");
printf("輸入0選擇退出,1做加法,2做減法,3做乘法,4做除法(保留余數)\n");
int operatione = 0;
cin >> operatione;
cout << endl;
try
{
if (operatione == 1)
{
//加法
add();
}
else if (operatione == 2)
{
//減法
substraction();
}
else if (operatione == 3)
{
//乘法
multiplication();
}
else if (operatione == 4)
{
//出發(fā)
division();
}
else if (operatione == 0)
{
exit(0);
}
else
{
throw 1;
}
}
catch (int i)
{
cout << "輸入錯誤" << endl;
}
operation();
}
int main()
{
printf("歡迎使用本計算器");
operation();
return 0;
}
代碼比較簡單,希望大家能夠喜歡
您可能感興趣的文章
- 04-02c語言編寫函數冒泡排序 c語言冒泡排序法函數
- 04-02c語言沒有round函數 round c語言
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現與遞歸實現的方法(C++)
- 01-10C++大數模板(推薦)
- 01-10淺談C/C++中的static與extern關鍵字的使用詳解
- 01-10深入C/C++浮點數在內存中的存儲方式詳解


閱讀排行
本欄相關
- 04-02c語言函數調用后清空內存 c語言調用
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言的正則匹配函數 c語言正則表達
- 04-02c語言用函數寫分段 用c語言表示分段
- 04-02c語言中對數函數的表達式 c語言中對
- 04-02c語言編寫函數冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數 c語言中怎
- 04-02c語言調用函數求fibo C語言調用函數求
隨機閱讀
- 01-10C#中split用法實例總結
- 01-11ajax實現頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?


