⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 4elist1509.cpp

📁 《21天学通C++》附盘的原代码。书上的每个例子在这里都有相应的C语言程序。
💻 CPP
字号:
// Listing 15.9. 
// Using typedef to make pointers to functions more readable

#include <iostream>
using namespace std;

void Square (int&,int&);
void Cube (int&, int&);
void Swap (int&, int &);
void GetVals(int&, int&);
typedef  void (*VPF) (int&, int&) ;
void PrintVals(VPF,int&, int&);

int main()
{
	int valOne=1, valTwo=2;
	int choice;
	bool fQuit = false;

	VPF pFunc;

	while (fQuit == false)
	{
		cout << "(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: ";
		cin >> choice;
		switch (choice)
		{
		case 1:	pFunc = GetVals; break;
		case 2:	pFunc = Square; break;
		case 3:	pFunc = Cube; break;
		case 4:	pFunc = Swap; break;
		default:fQuit = true; break;
		}
		if (fQuit == true)
			break;
		PrintVals ( pFunc, valOne, valTwo);
	}
	return 0;
}

void PrintVals( VPF pFunc,int& x, int& y)
{
	cout << "x: " << x << " y: " << y << endl;
	pFunc(x,y);
	cout << "x: " << x << " y: " << y << endl;
}

void Square (int & rX, int & rY)
{
	rX *= rX;
	rY *= rY;
}

void Cube (int & rX, int & rY)
{
	int tmp;

	tmp = rX;
	rX *= rX;
	rX = rX * tmp;

	tmp = rY;
	rY *= rY;
	rY = rY * tmp;
}

void Swap(int & rX, int & rY)
{
	int temp;
	temp = rX;
	rX = rY;
	rY = temp;
}

void GetVals (int & rValOne, int & rValTwo)
{
	cout << "New value for ValOne: ";
	cin >> rValOne;
	cout << "New value for ValTwo: ";
	cin >> rValTwo;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -