3iv.cpp

来自「C/C++程序设计导论(第二版)》程序源文件」· C++ 代码 · 共 49 行

CPP
49
字号
// Combcalc.cpp  Combinations calculations program
// Prompts the user for a group and committee size and then 
//   calculates the number of different committees that can be formed.

#include <iostream.h>

// Factorial()  Returns the factorial of the argument n.
// IN:  n is a positive integer
float Factorial (float n)
{	float product = 1.0;
	while (n > 1.0)
	{ 	product = product * n;
		n = n - 1.0;
	}
	return (product);
}

// Combi()  Returns the number of different of committees
//   that could be formed from group
//  IN: 	 group is a whole-number value greater than committee
//		committee is a positive whole-number value.
float Combi (float group, float committee)
{	float combinations;
	combinations = Factorial (group) / 
		(Factorial (group - committee) * Factorial (committee));
	return (combinations);
}

void main()
{	float group_size, committee_size, combinations;
	float done = 1.0;			
	while (done > 0.0)
	{	cout << "enter the size of the group and committee: ";
		cin >> group_size >> committee_size;
		if (group_size > committee_size)
		{	combinations = Combi (group_size, committee_size);
			cout << "for " << group_size << " people and " <<
				committee_size << " sized committees, the ";
			cout << "number of committees is " << 
				combinations << endl;
		}
		else  
			cout << " ** illegal values; group must be larger" << endl;
		cout << "Do you wish another calculation? ";
		cout << " enter 1.0 for yes, 0.0 for no: ";
		cin >> done;
	}
}

⌨️ 快捷键说明

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