函数开发

今天闲的没事,用函数开发了一个计算器,先上源码:

#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。
今天就分享到这啦。
未完待续…

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值