一个简单菜单设计

时间:2021-11-04 23:36:35
#include <iostream>
using namespace std;
int main()
{
	int n;
	int beishu(int, int);
	int  yueshu(int, int);
	int  jiecheng(int);
	void shuixianhua();
	for (;;)
	{
		cout << "主菜单:\n1、最大公约数\n2、最小公倍数\n3、阶乘\n4、水仙花数\n5、退出程序" << endl;
		cin >> n;
		if (n == 1)
		{
			cout << "最大公约数,请输入两个数:" << endl;
			int m, n, k;
			cin >> m >> n;
			k = yueshu(m, n);
			cout << k << endl;
		}
		if (n == 2)
		{
			cout << "最小公倍数,请输入两个数:" << endl;
			int m, n, k;
			cin >> m >> n;
			k = beishu(m, n);
			cout << k << endl;
		}
		if (n == 3)
		{
			cout << "阶乘,请输入一个数:" << endl;
			int n, k;
			cin >> n;
			k = jiecheng(n);
			cout << k << endl;

		}
		if (n == 4)
		{
			cout << "以下是水仙花数:" << endl;
			shuixianhua();
		}
		if (n == 5)
			break;
	}
	system("pause");
	return 0;
}
int  yueshu(int a, int b)
{
	int t;
	if (a < b)
	{
		t = a; a = b; b = t;
	}
	int r = a % b;
	while (r != 0)
	{
		a = b;
		b = r;
		r = a % b;
	}
	return b;
}
int beishu(int a, int b)
{
	int yueshu(int, int);
	int h = yueshu(a, b);
	return (a*b / h);
}
int jiecheng(int n)
{
	int i, m = 1;
	for (i = 1; i <= n; i++)
		m = m * i;
	return m;

}
void shuixianhua()
{
	int i, j, k, n;
	for (n = 100; n < 1000; n++)
	{
		i = n / 100;
		j = n / 10 - i * 10;
		k = n % 10;
		if (n == i * i*i + k * k*k + j * j*j)
			cout << n << " ";
	}
	cout << endl;
}