今天闲的没事,用函数开发了一个计算器,先上源码:
#include<bits/stdc++.h>
#include<windows.h>
#define stoptimeshort 40
#define stoptimelong 100
using std::cout;
using std::cin;
using std::endl;
int a,b,c,d,e,f,g,h,j,k,t,s1,s2,s3,x=1;
int ch;
double s4;
void add()
{
cout<<"Please input two numbers to add"<<" ";
cin>>a>>b;
s1=a+b;
cout<<a<<"+"<<b<<"="<<s1<<endl;
}
void minus()
{
cout<<"Please input two numbers to minus"<<" ";
cin>>c>>d;
s2=c-d;
cout<<c<<"-"<<d<<"="<<s2<<endl;
}
void multiply()
{
cout<<"Please input two numbers to multiply"<<" ";
cin>>e>>f;
s3=e*f;
cout<<e<<"*"<<f<<"="<<s3<<endl;
}
void divide()
{
cout<<"Please input two numbers to devide"<<" ";
cin>>g>>h;
s4=(double) g/h;
cout<<g<<"/"<<h<<"="<<s4<<endl;
}
void power()
{
cout<<"Please input the number and its power"<<" ";
cin>>j>>k;
for(int i=1;i<=k;i++)
{
x=x*j;
}
//pow(j,k);
cout<<j<<"^"<<k<<"="<<x<<endl;
}
int main()
{
system("title calculator");
Sleep(stoptimelong);
printf("%s", "W");
Sleep(stoptimeshort);
printf("%s", "e");
Sleep(stoptimeshort);
printf("%s", "l");
system("color a1");
Sleep(stoptimeshort);
printf("%s", "c");
system("color b2");
printf("%s", "o");
printf("%s", "m");
printf("%s", "e");
system("color c4");
Sleep(stoptimelong);
printf("%s", " t");
Sleep(stoptimeshort);
printf("%s", "o");
system("color d4");
Sleep(stoptimeshort);
printf("%s", " o");
Sleep(stoptimeshort);
printf("%s", "u");
system("color a1");
Sleep(stoptimeshort);
printf("%s", "r");
Sleep(stoptimeshort);
system("color e3");
printf("%s", " c");
Sleep(stoptimeshort);
printf("%s", "a");
Sleep(stoptimeshort);
printf("%s", "l");
Sleep(stoptimeshort);
printf("%s", "c");
system("color a4");
Sleep(stoptimeshort);
printf("%s", "u");
Sleep(stoptimeshort);
system("color d2");
printf("%s", "l");
Sleep(stoptimeshort);
printf("%s", "a");
system("color a6");
Sleep(stoptimeshort);
printf("%s", "t");
Sleep(stoptimeshort);
printf("%s", "o");
system("color c5");
Sleep(stoptimeshort);
printf("%s", "r\n");
Sleep(stoptimeshort);
system("color c4");
system("color e2");
system("color d6");
///
system("color c3");
Sleep(stoptimelong);
printf("%s", "P");
Sleep(stoptimeshort);
printf("%s", "l");
system("color e2");
Sleep(stoptimeshort);
printf("%s", "e");
Sleep(stoptimeshort);
printf("%s", "a");
Sleep(stoptimeshort);
printf("%s", "s");
Sleep(stoptimeshort);
system("color f2");
printf("%s", "e");
Sleep(stoptimeshort);
printf("%s", " i");
Sleep(stoptimeshort);
printf("%s", "n");
system("color b5");
Sleep(stoptimeshort);
printf("%s", "p");
Sleep(stoptimeshort);
printf("%s", "u");
system("color a6");
Sleep(stoptimeshort);
printf("%s", "t");
system("color e1");
Sleep(stoptimeshort);
printf("%s", " t");
Sleep(stoptimeshort);
printf("%s", "h");
system("color c3");
Sleep(stoptimeshort);
printf("%s", "e");
Sleep(stoptimeshort);
printf("%s", " p");
Sleep(stoptimeshort);
printf("%s", "a");
system("color b4");
Sleep(stoptimeshort);
printf("%s", "s");
Sleep(stoptimeshort);
printf("%s", "s");
system("color b9");
Sleep(stoptimeshort);
printf("%s", "w");
system("color d2");
Sleep(stoptimeshort);
printf("%s", "o");
Sleep(stoptimeshort);
printf("%s", "r");
system("color e9");
Sleep(stoptimeshort);
printf("%s", "d ");
Sleep(stoptimelong);
int password;
cin>>password;
if(password!=123456)
{
cout<<endl;
cout<<"The password is wrong"<<endl;
system("pause");
return 0;
}
cout<<endl;
cout<<endl;
cout<<endl;
system("color b1");
cout<<"***************************"<<endl;
cout<<"*** 1.addition ***"<<endl;
cout<<"*** 2.subtraction ***"<<endl;
cout<<"*** 3.multiplication ***"<<endl;
cout<<"*** 4.devision ***"<<endl;
cout<<"*** 5.power ***"<<endl;
cout<<"*** 6.quit ***"<<endl;
cout<<"***************************"<<endl;
do
{
cin>>ch;
if(ch==1) add();
if(ch==2) minus();
if(ch==3) multiply();
if(ch==4) divide();
if(ch==5) power();
if(ch<1||ch>6) cout<<"Please input the right number"<<endl;
}while(ch!=6);
cout<<"The program has quited"<<endl;
system("pause");
return 0;
}
乍看有200多行程序,实际上原理十分简单,一开始,定义了5个函数,分别为加,减,乘,除和乘方,需要注意的是除法函数(devide)中要使用double才能计算出确切结果,要不然是整除,在乘方函数(power)中,int型只能算到2^32-1,long long也只能算到2^64-1,所以结果是有限的,高精度计算可以选择用python,是可以无限算下去的。中间的那100行代码可以忽略,只是为了带有速度和颜色打印出welcome to our calculator,please input the password,注意,只需一个if语句就可以判断密码是否正确,初始密码123456,接着打印出一个菜单可以选择,通过while循环实现想输入多少次都ok。
今天就分享到这啦。
未完待续…