目录
一、原题见谭浩强C++课本第九章(P295)第四题
题目:
建立一个对象数组,内放5个学生的数据(学号,成绩),设立一个函数max,用指针指向对象的指针作为函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
程序:
#include<iostream>
#include<cstring>
using namespace std;
class Student {
public:
string num;
double score;
public:
Student(string n, double s) {
num = n;
score = s;
}
void print() {
cout << num << " " << score << endl;
}
};
void max(Student *pt,Student t[]) {
Student* m = NULL;
double ma=0;
for (int i = 0; i < 5; i++) {
if ((pt + i)->score > ma) {
ma = (pt + i)->score;
m = pt+i;
}
}
m->print();
}
int main() {
Student t[5]{
Student("001",20),
Student("002",50),
Student("003",10),
Student("004",80),
Student("005",40),
};
Student* pt;
pt = &t[0];
max(pt, t);
}
运行:
二、原题见谭浩强C++课本第九章(P295)第九题
题目:
商店销售某一商品,每天公布统一的折扣。同时允许销售人员在销售是灵活掌握售价,在此基础上,一次购时间以上者,享受98折优惠,现已知当天3个销售员销售情况为:
销售员号(num) | 销货件数(quantity) | 销货单价(price) |
101 | 5 | 23.5 |
102 | 12 | 24.56 |
103 | 100 | 21.5 |
请编写程序,计算当日此产品的总销售款sum以及每件商品的平均单价,要求用静态成员和静态成员函数。
程序:
#include<iostream>
using namespace std;
class Sale {
private:
int num;//工号
int quantity;//每个人销售的件数
double price;//销售单价
static double sum;//总销售款
static double discount;
static int n;//总销售件数
public:
Sale(int n, int q, double p) {
num = n;
quantity = q;
price = p;
}
static double average();
friend void inputdiscount();
static void print(Sale s[]);
void cal() {
if (quantity <= 10) {
sum += quantity * discount * price;
}
else {
sum += quantity * discount * price * 0.98;
}
n += quantity;
}
};
double Sale::sum = 0;
int Sale::n = 0;
double Sale::discount = 1;
void inputdiscount() {
cin >> Sale::discount;
}
double Sale::average() {
return sum / n;
}
void Sale::print(Sale s[]) {
for (int i = 0; i < 3; i++) {
cout << "number: " << s[i].num << " quantity: " << s[i].quantity << " price: " << s[i].price << endl;
}
cout << "sum: " << sum << " sum of quantity: " << n << " average: " << average() << endl;
}
int main() {
Sale s[3]{
Sale(101,5,23.5),
Sale(102,12,23.56),
Sale(103,100,21.5),
};
inputdiscount();
for (int i = 0; i < 3; i++)
s[i].cal();
s[0].print(s);
}
运行:
三、宋桂琴白皮书项目2(P8)第1题
题目:
编写有理数(即分数,包括整数类型的分子,分母)类,并进行运算符重载,编写运算符“+”,“-”,“*”,“/”的重载函数,在主函数的函数体内实现使用运算符重载直接进行有理数对象的加减乘除运算。
程序:(部分)
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
class Rational {
protected:
int denomenator;//分母
int molecule;//分子
public:
void input() {
cout << "please enter the molecule and the denomenator" << endl;
cin >> molecule >> denomenator;
}
void reduction();
public:
friend Rational operator +(Rational, Rational);
friend Rational operator *(Rational, Rational);
friend ostream& operator <<(ostream&, Rational&);
};
inline int gcd(int a, int b) {
int r;
while (b > 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
ostream& operator <<(ostream& output, Rational& r) {
if(r.denomenator!=1){
output << r.molecule << "/" << r.denomenator;
}
else {
output << r.molecule;
}
return output;
}
void Rational::reduction() {//约分函数
int gcdred = gcd(denomenator, molecule);
denomenator /= gcdred;
molecule /= gcdred;
return;
}
Rational operator +(Rational r1, Rational r2) {//运算符加号的重载
Rational res;
int lcm_plus = r1.denomenator * r2.denomenator;
res.denomenator = lcm_plus;
res.molecule = ((lcm_plus / r1.denomenator) * r1.molecule) + ((lcm_plus / r2.denomenator) * r2.molecule);
res.reduction();
return res;
}
Rational operator *(Rational r1, Rational r2) {
Rational res;
res.denomenator = r1.denomenator * r2.denomenator;
res.molecule = r1.molecule * r2.molecule;
res.reduction();
return res;
}
int main() {
Rational a, b, res;
a.input();
b.input();
res = a + b;
cout << a << "+" << b << "=" << res<<endl;
res = a * b;
cout << a << "*" << b << "=" << res << endl;
}