《大兄弟的计算器》

#include"iostream"
#include"cmath"
using namespace std;
int n, **matrix;
#define N 100

double l(int m, double x, double a[N])
{
	int i, s = a[m - 1];
	for (i = m - 1; i >= 0; i--)
	{
		s = s * x + a[i];
	}
	return s;
}

int gcd(int x, int y)
{
	int r;
	do
	{
		r = x % y;
		x = y;
		y = r;
	} while (y != 0);
	return x;
}

int det(int **m, int size)				//求解行列式
{
	if (size == 2) return m[0][0] * m[1][1] - m[0][1] * m[1][0];
	int temp = 0;
	for (int i = 0; i < size; i++) {
		int **m0 = new int*[size - 1];
		for (int v = 0; v < size - 1; v++)
			m0[v] = new int[size - 1];
		for (int p = 0; p < size - 1; p++) {
			for (int q = 0; q < size - 1; q++)
				if (q < i) m0[p][q] = m[p + 1][q];
				else m0[p][q] = m[p + 1][q + 1];
		}
		if (i % 2 == 0) temp += m[0][i] * det(m0, size - 1);
		else if (i % 2 == 1) temp -= m[0][i] * det(m0, size - 1);
		for (int v = 0; v < size - 1; v++) delete[] m0[v];
		delete[] m0;
	}
	return temp;
}

int fun1(int x)				//素数判断
{
	
	int range = (int)sqrt(x);
	for (int i = 2; i <= range; i++)
	{
		if (x%i == 0)return false;
	}
	return true;

}

int fun2(int x, int y)   //最大公约数
{
	return x == 0 ? y : fun2(y%x, x);

}

void fun3(int a, int b, int c)
{
	if ((a*a == b * b + c * c || b * b == a * a + c * c || c * c == a * a + b * b) && a>0 && b>0 && c>0)
	{
		cout << "这三个数是勾股数" << endl<<endl<<endl;
	}
	else
	{
		cout << "这三个数不是勾股数" << endl<<endl<<endl;
	}

}

int fun4()
{
	int sum = 0;
	cout << "请输入行列式阶数" << endl;
	cin >> n;  // Matrix n * n ( n >= 2)
	cout << "请输入行列式元素" << endl;
	matrix = new int*[n];
	for (int i = 0; i < n; i++) matrix[i] = new int[n];
	for (int i = 0; i < n*n; i++) cin >> matrix[i / n][i%n];
	//cout << det(matrix, n) << endl;
	sum = det(matrix, n);
	for (int i = 0; i < n; i++) delete[] matrix[i];
	delete[] matrix;
	return sum;
}

void fun5()
{
	double a, b, c, d, x1, x2;
	cout << "输入方程的三个系数a,b,c:";
	cin >> a >> b >> c;
	if (a == 0)
	{
		if (b == 0)
		{
			if (c == 0)
			{
				cout << "解集是复数集" << endl;
			}
			else
			{
				cout << "无解" << endl;
			}
		}
		else
		{
			cout << "方程的解为:" << -c / b << endl;
		}
	}
	else
	{
		d = b * b - 4 * a*c;
		if (d >= 0)
		{
			if (b>0)
			{
				x1 = (-b - sqrt(d)) / (2 * a);
			}
			else
			{
				x1 = (-b + sqrt(d)) / (2 * a);
			}
			if (x1 == 0)
			{
				x2 = 0;
			}
			else
			{
				x2 = c / (a*x1);
			}
			cout << "方程的解为:" << x1 << "和" << x2 << endl;
		}
		else
		{
			cout << "x1=" << -b / (2 * a) <<" + "<<sqrt(-d) / (2 * a) << "i" << endl << "x2=" << -b / (2 * a) << " - "<<sqrt(-d) / (2 * a) << "i" << endl;
		}
	}

}
bool isrp(int a, int b) 
{
	if (a == 1 || b == 1)     
		return true;
	while (1) {          
		int t = a % b;
		if (t == 0) break;
		else {
			a = b;
			b = t;
		}
	}
	if (b > 1)	return false;
	else return true;	
}

void start()
{
	cout << "数学算法工具 " << endl;
	cout << "1.[在复数域中求解ax^2+bx+c=0的根] "<< endl;
	cout << "2.[素数判断] " << endl;
	cout << "3.[计算公约数 ]" << endl;
	cout << "4.[计算行列式] " << endl;
	cout << "5.[多项式的计算] " << endl;
	cout << "6.[有理数的四则运算]" << endl;
	cout << "7.[计算勾股数] " << endl;
	cout << "8.[判断三个数是否互素]" << endl;
	cout << "9.[退出系统]" << endl;
}
int main()
{
	again:int a = 8;
	start();
	cin >> a;
		switch (a)
		{
		case 1:
		{
				fun5();
				goto again;
		}break;
		case 2:
		{
			int x = 0;
			
			cout << "[请输入要判断的数]" << endl;
			cin >> x;
			if (!fun1(x))
			{
				cout << "该数非素数" << endl<<endl<<endl;
				goto again;
			}
			cout << "该数为素数" << endl<<endl<<endl;
			goto again;
		}break;
		case 3:
		{
			int n, m,z=0;
				cout << "[请输入两个数]" << endl;
				cin >> n >> m;
			     z=fun2(n,m);
				 cout<<"最大公约数为" << z << endl<<endl<<endl;
				 goto again;
		
		}break;
		case 4:
		{
			int sum = 0;
		
			sum=fun4();
			cout << "行列式的结果为" << sum << endl << endl<<endl;
			goto again;
		
		}break;
		case 5:
		{
				int i, m;
				double x, a[N], p;
				cout << "输入次数:";
				cin >> m;
				cout << "输入系数:";
				for (i = 0; i<m; i++)
				{

					cin >> a[i];
				}
				cout << "输入数x:";
				cin >> x;
				p = l(m, x, a);
				cout << "多项式的值为:" << p << endl << endl << endl;
				goto again;
		
			
			
		}break;
		case 6:
		{
			int you = 5;
			cout << "1.[有理数加法] " << endl;
			cout << "2.[有理数减法] " << endl;
			cout << "3.[有理数乘法 ]" << endl;
			cout << "4.[有理数除法] " << endl;
			cout << "5.[返回上一级菜单]" << endl;
			cin >> you;
			switch (you)
			{
			case 1:
			{
				int a, b, c, d, e, f, p;
				cout << "输入四个有理数a,b,c,d,表示两个分数a/b和c/d:";
				cin >> a >> b >> c >> d;
				f = a * d + b * c;
				e = b * d;
				if (f == 0)
				{
					e = 1;
				}
				else
				{
					p = gcd(abs(f), e);
					e /= p;
					f /= p;
				}
				cout << "两个分数的和为:" << f << "/" << e << endl << endl << endl;
				goto again;
			}break;
			case 2:
			{
				int a, b, c, d, e, f, p;
				cout << "输入四个有理数a,b,c,d,表示两个分数a/b和c/d:";
				cin >> a >> b >> c >> d;
				f = a * d - b * c;
				e = b * d;
				if (f == 0)
				{
					e = 1;
				}
				else
				{
					p = gcd(abs(f), e);
					e /= p;
					f /= p;
				}
				cout << "两个分数的差为:" << f << "/" << e << endl<<endl<<endl;
				goto again;
			
			}break;
			case 3:
			{
				int a, b, c, d, e, f, p;
				cout << "输入四个有理数a,b,c,d,表示两个分数a/b和c/d:";
				cin >> a >> b >> c >> d;
				f = a * c;
				e = b * d;
				if (f == 0)
				{
					e = 1;
				}
				else
				{
					p = gcd(abs(f), e);
					e /= p;
					f /= p;
				}
				cout << "两个分数的积为:" << f << "/" << e << endl << endl << endl;
				goto again;
			}break;
			case 4:
			{
				int a, b, c, d, e, f, p;
				cout << "输入四个有理数a,b,c,d,表示两个分数a/b和c/d:";
				cin >> a >> b >> c >> d;
				f = a * d;
				e = b * c;
				if (f == 0)
				{
					e = 1;
				}
				else
				{
					p = gcd(abs(f), e);
					e /= p;
					f /= p;
				}
				cout << "两个分数的商为:" << f << "/" << e << endl << endl << endl;
				goto again;
			
			}break;
			case 5:
			{
				goto again;
			}break;

			}

		}break;
		case 7:
		{
			int x, y, z;
			cout << "输入三个整数:";
			cin >> x >> y >> z;
			fun3(x, y, z);
			goto again;
		}break;
		case 8:
		{
			int a, b, c;
			cout << "输入三个整数:";
			cin >> a>> b>>c;
			if (gcd(a, b) == 1 || gcd(b, c) == 1)
			{
				cout << "这三个数互素" << endl << endl << endl;
			}
			else
			{
				cout << "这三个数不互素" << endl << endl << endl;
			}
			goto again;
		}break;
		case 9:
		{
			exit(0);
		}break;
	}

	system("pause");
	return 0;


}

 

  大兄弟的作业题,希望会有用,如有错误,欢迎指出,感谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值