3.2.4——随机生成一元二次方程,计算返回其解的值

源代码如下:

#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<iomanip>
#include<math.h>

using namespace std;

void function(int x , int y,int z,float d) {
	int junge;					//定义junge通过Derta的的结果进行对应赋值,方便后续switch语句进行判断。
	float x1, x2;				//x1,x2分别存储方程的两个解
	float y1, y2;				//y1,y2分别临时存储复数的值
	float genHao = sqrt(d);		//使用sqrt()函数进行根号计算

	if (d == 0)
		junge = 0;
	if (d > 0)
		junge = 1;
	if (d < 0)
		junge = 2;

	switch (junge) {
	case 0:
		x1 = (0 - y) / (2 * x);

		if (z > 0) {
			if (y > 0)
				cout << x << "x^2 + " << y << "x + " << z << " = 0的解为:x = " << setprecision(2) << x1 << "\n" << endl;
			if (y == 0)
				cout << x << "x^2 + " << z << " = 0的解为:x = " << setprecision(2) << x1 << "\n" << endl;
			if (y < 0)
				cout << x << "x^2 - " << 0 - y << "x + " << z << " = 0的解为:x = " << setprecision(2) << x1 << "\n" << endl;
		}
		if (z == 0) {
			if (y > 0)
				cout << x << "x^2 + " << y << "x " << " = 0的解为:x = " << setprecision(2) << x1 << "\n" << endl;
			if (y == 0)
				cout << x << "x^2 " << " = 0的解为:x = " << setprecision(2) << x1 << endl;
			if (y < 0)
				cout << x << "x^2 - " << 0 - y << "x " << " = 0的解为:x = " << setprecision(2) << x1 << "\n" << endl;
		}
		if (z < 0) {
			if (y > 0)
				cout << x << "x^2 + " << y << "x - " << 0 - z << " = 0的解为:x = " << setprecision(2) << x1 << "\n" << endl;
			if (y == 0)
				cout << x << "x^2 - " << 0 - z << " = 0的解为:x = " << setprecision(2) << x1 << "\n" << endl;
			if (y < 0)
				cout << x << "x^2 - " << 0 - y << "x - " << 0 - z << " = 0的解为:x = " << setprecision(2) << x1 << "\n" << endl;
		}
		break;


	case 1:
		x1 = ((0 - y) + (genHao)) / (2 * x);
		x2 = ((0 - y) + (0 - genHao)) / (2 * x);

		if (z > 0) {
			if (y > 0)
				cout << x << "x^2 + " << y << "x + " << z << " = 0的解为:x1 = " << setprecision(2) << x1 << " ; x2 = " << setprecision(2) << x2 << "\n" << endl;
			if (y == 0)
				cout << x << "x^2 + " << z << " = 0的解为:x1 = " << setprecision(2) << x1 << " ; x2 = " << setprecision(2) << x2 << "\n" << endl;
			if (y < 0)
				cout << x << "x^2 - " << 0 - y << "x + " << z << " = 0的解为:x1 = " << setprecision(2) << x1 << " ; x2 = " << setprecision(2) << x2 << "\n" << endl;
		}
		if (z == 0) {
			if (y > 0)
				cout << x << "x^2 + " << y << "x " << " = 0的解为:x1 = " << setprecision(2) << x1 << " ; x2 = " << setprecision(2) << x2 << "\n" << endl;
			if (y == 0)
				cout << x << "x^2 " << " = 0的解为:x1 = " << setprecision(2) << x1 << " ; x2 = " << setprecision(2) << x2 << "\n" << endl;
			if (y < 0)
				cout << x << "x^2 - " << 0 - y << "x " << " = 0的解为:x1 = " << setprecision(2) << x1 << " ; x2 = " << setprecision(2) << x2 << "\n" << endl;
		}
		if (z < 0) {
			if (y > 0)
				cout << x << "x^2 + " << y << "x - " << 0 - z << " = 0的解为:x1 = " << setprecision(2) << x1 << " ; x2 = " << setprecision(2) << x2 << "\n" << endl;
			if (y == 0)
				cout << x << "x^2 - " << 0 - z << " = 0的解为:x1 = " << setprecision(2) << x1 << " ; x2 = " << setprecision(2) << x2 << "\n" << endl;
			if (y < 0)
				cout << x << "x^2 - " << 0 - y << "x - " << 0 - z << " = 0的解为:x1 = " << setprecision(2) << x1 << " ; x2 = " << setprecision(2) << x2 << "\n" << endl;
		}
		break;


	case 2:
		x1 = (0 - y) / (2 * x);
		x2 = (0 - y) / (2 * x);
		y1 = (0 - d) / (2 * x);
		y2 = (0 - d) / (2 * x);

		if (z > 0) {
			if (y > 0)
				cout << x << "x^2 + " << y << "x + " << z << " = 0的解为:x1 = " << setprecision(2) << x1 << " + " << setprecision(2) << y1 << "i; x2 = " << setprecision(2) << x2 << " + " << setprecision(2) << y2 << "i" << "\n" << endl;
			if (y == 0)
				cout << x << "x^2 + " << z << " = 0的解为:x1 = " << setprecision(2) << x1 << " + " << setprecision(2) << y1 << "i; x2 = " << setprecision(2) << x2 << " + " << setprecision(2) << y2 << "i" << "\n" << endl;
			if (y < 0)
				cout << x << "x^2 - " << 0 - y << "x + " << z << " = 0的解为:x1 = " << setprecision(2) << x1 << " + " << setprecision(2) << y1 << "i; x2 = " << setprecision(2) << x2 << " + " << setprecision(2) << y2 << "i" << "\n" << endl;
		}
		if (z == 0) {
			if (y > 0)
				cout << x << "x^2 + " << y << "x " << " = 0的解为:x1 = " << setprecision(2) << x1 << " + " << setprecision(2) << y1 << "i; x2 = " << setprecision(2) << x2 << " + " << setprecision(2) << y2 << "i" << "\n" << endl;
			if (y == 0)
				cout << x << "x^2 " << " = 0的解为:x1 = " << setprecision(2) << x1 << " + " << setprecision(2) << y1 << "i; x2 = " << setprecision(2) << x2 << " + " << setprecision(2) << y2 << "i" << "\n" << endl;
			if (y < 0)
				cout << x << "x^2 - " << 0 - y << "x " << " = 0的解为:x1 = " << setprecision(2) << x1 << " + " << setprecision(2) << y1 << "i; x2 = " << setprecision(2) << x2 << " + " << setprecision(2) << y2 << "i" << "\n" << endl;
		}
		if (z < 0) {
			if (y > 0)
				cout << x << "x^2 + " << y << "x - " << 0 - z << " = 0的解为:x1 = " << setprecision(2) << x1 << " + " << setprecision(2) << y1 << "i; x2 = " << setprecision(2) << x2 << " + " << setprecision(2) << y2 << "i" << "\n" << endl;
			if (y == 0)
				cout << x << "x^2 - " << 0 - z << " = 0的解为:x1 = " << setprecision(2) << x1 << " + " << setprecision(2) << y1 << "i; x2 = " << setprecision(2) << x2 << " + " << setprecision(2) << y2 << "i" << "\n" << endl;
			if (y < 0)
				cout << x << "x^2 - " << 0 - y << "x - " << 0 - z << " = 0的解为:x1 = " << setprecision(2) << x1 << " + " << setprecision(2) << y1 << "i; x2 = " << setprecision(2) << x2 << " + " << setprecision(2) << y2 << "i" << "\n" << endl;
		}
		break;
	}
}

int main() {
	system("color f0");
	/*
	实验内容:求一元二次方程的根
	使用求根公式计算 ax² + bx + c = 0 的根,通过△分情况计算方程的根

	计算△的公式为: △ = b² - 4ac
					△ = 0 时,有一个实数根; △ > 0,有两个实数根,其计算公式为:x = (-b ± 根号△) / 2a
					△ < 0 时,有两个复数根,其计算公式为:x = (-b ± 根号-△ i) / 2a
	*/
	float a, b, c;	//定义 a,b,c 分别存储一元二次方程的三个系数。
	float Derta;	//定义Derta存储判断条件

	cout << "请输入随机方程总数:";
	int n;		//随机输出的总数
	cin >> n;
	cout << endl;

	srand((unsigned)time(NULL));		//使随机生成的式子不重复

	for (int i = 0; i < n; i++) {
		a = rand() % 100;		//随机生成100以内的整数
		b = rand() % 100;		
		c = rand() % 100;		
		Derta = b * b - 4 * a * c;
		
		function(a, b, c, Derta);
	}

	return 0;
}

一些头文件的引用:

#include<iomanip>		
/*
可调用头文件函数实现确定位数输出小数,使用时的具体语句为:cout<< setprecision(2) << m << endl;
则此时输出 m 时将输出两位小数
*/
#include<time.h>		//可实现时间函数
#include<stdlib.h>		//可调用随机函数rand()

PS:输出非常漂亮。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值