实验
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;
}