利用函式指標,編寫一段簡潔的程式碼,實現計算器的功能

遠飛夢發表於2018-11-29
#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;
}

相關文章