利用函数指针,编写一段简洁的代码,实现计算器的功能

#include<iostream>
using namespace std;
void function_addition();
void function_subtraction();
void function_multiplication();
void function_division();
int main()
{
	void (* point_function[4])();
	point_function[0]=function_addition;
	point_function[1]=function_subtraction;
	point_function[2]=function_multiplication;
	point_function[3]=function_division;
	int i;
	while(1)
	{
		cout<<"put the i"<<endl;
		cout<<"0---addition"<<endl;
		cout<<"1---subtraction"<<endl;
		cout<<"2---multiplication"<<endl;
		cout<<"3---division"<<endl;
		cout<<endl;
		cin>>i;
		if(i!=0&&i!=1&&i!=2&&i!=3)
		{
			cout<<"please input again";
			cin>>i;
		}
		point_function[i]();
	}
	return 0;
}
void function_addition()
{
	cout<<"this is addition:"<<endl<<"pleace put two num"<<endl;
	double a,b;
	cin>>a>>b;
	cout<<a+b<<endl<<endl;
}
void function_subtraction()
{
	cout<<"this is subtraction:"<<endl<<"pleace put two num"<<endl;
	double a,b;
	cin>>a>>b;
	cout<<a-b<<endl<<endl;
}
void function_multiplication()
{
	cout<<"this is multiplication:"<<endl<<"pleace put two num"<<endl;
	double a,b;
	cin>>a>>b;
	cout<<a*b<<endl<<endl;
}
void function_division()
{
	cout<<"this is division:"<<endl<<"pleace put two num"<<endl;
	double a,b;
	cin>>a>>b;
	cout<<a/b<<endl<<endl;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值