第4章 实验

实验

CPU类

在这里插入图片描述

#include <string>
#include <iostream>
using namespace std;
enum CPU_Rank { P1 = 1, P2, P3, P4, P5, P6 };
class CPU {
private:
	CPU_Rank rank;
	int frequency;
	float voltage;
public:
	CPU(CPU_Rank rank, int freq, float vol)
	{
		rank = rank;
		frequency = freq;
		voltage = vol;
		cout << "构造" << endl;
	}
	~CPU(){
		cout << "析构" << endl;
	}

	CPU_Rank getRank() { return rank; }
	int getFreq() { return frequency; }
	float getVol() { return voltage; }


	void run() {
		cout << "run" << endl;
	}
	void stop() {
		cout << "stop" << endl;
	}
};

int main() {
	CPU a(P1, 300, 2.8);
	a.run();
	a.stop();
	return  0;
}

RAM类,CD-ROM类

在这里插入图片描述

computer类

在这里插入图片描述

作业题

最大公约数

#include <string>
#include <iostream>
using namespace std;

class Integer {
private:
	int _num;
public:
	//构造函数
	Integer(int num) {
		_num = num;
	}
	//计算当前Integer 和 b之间的最大公约数
	int gcd(Integer b) {
		int remainder=_num%b._num;
		if (remainder == 0)
			return b._num;
		Integer integer(remainder);
		return b.gcd(integer);
	}
};
int main() {
	int a, b;
	cin >> a >> b;
	Integer A(a);
	Integer B(b);
	cout << A.gcd(B) << endl;
	return 0;
}

反转整数

#include <iostream>
using namespace std;

class Integer {
private:
	int _num;
	//getLength()函数获取_num长度
	int getLength() {
	}
public:
	//Integer类构造函数
	Integer(int num) {
		_num = num;
	}
	//反转_num
	int inversed() {
		int a = _num;
		int results=0;
		while (a) {
			results = results * 10 + a % 10;
			a = a / 10;
		}
		return results;
	}
};

int main() {
	int n;
	cin >> n;
	Integer integer(n);
	cout << integer.inversed() << endl;
	return 0;
}

一元二次方程求解

对于一元二次方程ax^2 + bx + c = 0,解可以分为很多情况。
若该方程有两个不相等实根,首先输出1,换行,然后从小到大输出两个实根,换行;
若该方程有两个相等实根,首先输出2,换行,然后输出这个这个实根,换行;
若该方程有一对共轭复根,输出3,换行;
若该方程有无解,输出4,换行;
若该方程有无穷个解,输出5,换行;
若该方程只有一个根,首先输出6,换行,然后输出这个跟,换行;

只得了70分??

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class Equation {
private:
	int _a, _b, _c;
public:
	Equation(int a, int b, int c) {
		_a = a;
		_b = b;
		_c = c;
	}
	void solve() {
		int delta = _b * _b - 4 * _a*_c;
		double x1, x2;
		if (_a == 0 && _b != 0) { //一个解
			cout << 6 << endl;
			x1 = -_c / _b;
			cout << setiosflags(ios::fixed) << setprecision(2) << x1 << endl;
		}
		else if (_a == 0 && _b == 0) {
			if (_c == 0) { //无穷
				cout << 5 << endl;
			}
			else { //无解
				cout << 4 << endl;
			}
		}
		else if (delta > 0) {
			cout << 1 << endl; //两个
			x1 = (-_b - sqrt(delta)) / (2 * _a);
			x2= (-_b + sqrt(delta)) / (2 * _a);
			cout <<setiosflags(ios::fixed)<<setprecision(2)<< x1 << " " << x2 << endl;
		}
		else if (delta == 0) {
			cout << 2 << endl; //两个相等
			x1 = -_b / (2 * _a);
			cout << setiosflags(ios::fixed)<< setprecision(2) << x1<< endl;
		}
		else { //复根
			cout << 3 << endl;
		}
	}
};
int main() {
	int a, b, c;
	cin >> a >> b >> c;
	Equation tmp(a, b, c);
	tmp.solve();
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值