问题1
三维空间中的点可以看作是三维向量,两个三维向量相减可以用这两点之间的距离来表示,构造一个 三维向量类multVector,重载“-”减号运算符计算两个三维向量之间的距离。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include<iostream> #include<string> #include<cmath> using namespace std; class multVector { private: float x, y, z; public: multVector() { x = 0.0, y = 0.0, z = 0.0; } multVector(float x, float y, float z) :x(x), y(y), z(z) {} float operator- (const multVector& m) { return sqrt((this->x - m.x) * (this->x - m.x) + (this->y - m.y) * (this->y - m.y) + (this->y - m.z) * (this->y - m.z)); } }; int main() { float x1, y1, z1; float x2, y2, z2; cout << "请输入第一个点的坐标" << endl; cin >> x1 >> y1 >> z1; multVector m1(x1, y1, z1); cout << "请输入第二个点的坐标" << endl; cin >> x2 >> y2 >> z2; multVector m2(x2, y2, z2); cout << "两点之间的距离是:" << endl; cout << m2 - m1 << endl;
return 0; }
|
问题2
给一个大小为2n(n为正整数)的数组,计算数组中相邻的两个元素的差,该差值构成一个新的,大小为原来数组一半,即大小为n的数组。例如原有一个大小为6的整形数组:{21, 15, 7, 43,12,9 },两个相邻元素中后一个减前一个得到新的数组:{15-21,43-7, 9-12}={-6, 36, -3}。同理,一个浮点型数组也可以做同样的运算,即数组元素类型作为参数可以变化。利用函数模板方法实现这个算法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include <iostream> using namespace std; template <class T> void func(T *a,int n) { for (int i = 0;i < n-1;i++) { a[i] = a[i + 1] - a[i]; if (i % 2 == 0) { cout << a[i] << "\n"; } } } int main() { int n; cout << "请输入数组元素个数(偶数):" << endl; cin >> n; if (n % 2 == 0) { int* a = new int[n]; cout << "请输入数组元素:" << endl; for (int i = 0;i < n;i++) { cin >> a[i]; } func(a, n); delete[]a; } else { cout << "请输入偶数!!" << endl; } }
|
问题3
给自己设计一个个人记账小程序。假设你有两张卡,一个是工行储蓄卡,一个是纺大食堂饭卡,构造一个Account类,这两张卡作为这个类的对象,记录和显示这两个卡的交易情况以及两张卡的余额。同时用一个静态成员变量来记录两个卡总的余额。(该题总分16分) 参考要求: 构造一个Account类。数据成员包括:卡余额:Balance,卡名称:CarName,静态成员变量用于统计两个卡的总余额:TotalBalance;项目名称:Item,用于记录消费或者收入名称,可以用一个字符串数组。如吃一次晚餐,可以记作:“晚餐”,如收到父母打款,可以记作:“父母给生活费”等,金额amount,用于记录每一次花费或者收入的金额,可以用一个浮点型数组。(2分) 成员函数:一个构造函数,用于初始化你的卡对象。(3分)成员函数Transaction,用于记录你的消费或者收入,两个参数分别为:项目和金额,收入是金额为正,消费是金额为负。(3分)成员函数Display,用于显示你当前的余额和所有交易情况。(3分)在main函数中实现至少一次收入和消费,然后显示当前两个卡的余额和交易情况。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include<iostream> #include<string> #include<cmath> using namespace std; static float TotalBalance=0.0; class Acount { private: float Balance=0.0; string item; string name; public: Acount( string name,float Balance):name(name),Balance(Balance) {} float Transaction() { float cost; cout << "请输入消费事件以及金额:" << endl; cin >> item>>cost; Balance = Balance + cost; TotalBalance = TotalBalance + cost; return Balance; }; void Display() { cout <<name << "交易记录为:" << endl; cout << item << endl; cout << name << "余额为:" << Balance << endl; cout << name << "总余额为:" << TotalBalance << endl; }; ~Acount() {}; };
int main() { string card1;string card2; cout << "请输入卡1的名称" << endl; cin >> card1; cout << "请输入卡2的名称" << endl; cin >> card2; Acount a(card1,0); a.Transaction(); a.Display(); Acount b(card2,0); b.Transaction(); b.Display();
return 0; }
|
问题4
工程预算一般包括材料费用和人工费用,这两个费用都由净费用以及管理费用之和构成。其中管理费用由净费用乘上费率产生。这两个费用的净费用计算方法及费率都不一样。其中材料费用的净费用的计算方法为材料数量乘以材料单价。人工费用的净费用的计算方法为人员工资加上员工福利。工程总预算由这两个费用总和构成,用类继承及多态方法编程实现这样工程预算的计算。(该题总分为16分) (1)构造一个预算类:Budget类,包含一个计算费用的虚函数。(4分) (2)以这个类作为基类,派生出两个派生类:材料类:Material(3分),人工类:Labour。(3分) (3)在main函数中用多态方法动态联编实现工程预算的计算。(6分) 参考要求如下: Budget 基类,这个类中包括成员变量:名称:Name,可用于保存每个对象的名称,如材料,人工。 净费用:netCost,用以保存净费用。费率:FeeRate,用以保存管理费率。总费用:Cost,用于保存费用,Cost计算方法为:Cost=netCost(1+FeeRate/100.00)。包括成员函数:成员函数:Display,用于显示净费用netCost,费率FeeRate,总费用Cost。虚函数:GetBudget,用于计算每项的总费用。 派生类:材料 Material类,包含数量Amount和单价Price两个成员变量。净费用的计算方法为数量价格,管理费费率为:3.12%。例如:材料数量为11吨,每吨单价为1.2万元,则净费用为111.2=13.2万元,加上管理费,则材料总费用为13.2(1+3.12%)=13.6188万元。 派生类:人工 Labour类,包含工资Salary和福利Welfare两个成员变量,净费用的计算方法为工资+福利,管理费率为:14.6%。例如:工资为9万元,福利为3万元,则净费用为9+3=12万元,加上管理费,则人工总费用为(9+3)*(1+14.6%)=13.752万元。 在两个派生类中,覆盖GetBudget这个虚函数,按照不同的计算方法计算出总费用。 在main函数中,构造两个派生类对象,两个派生类有各自的构造函数来初始化各自数据,要求用动态联编方法实现GetBudget计算出各个派生类的总费用,并计算出总预算金额。不要求键盘输入数据,输出格式不作严格要求,最终参考输出结果如下图所示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include<iostream> #include<string> #include<cmath> using namespace std; class Budget { private: string Name; float netcost,Feerate,cost;
public: Budget() { netcost = 0.0;Feerate = 0.0;cost = 0.0; }; void display() { cout << "项目\t净费用(万元)\t费率(%)\t总费用(万元)" << endl; cout << netcost << "\t" << Feerate << "\t" << cost << endl; }; virtual float GetBudget() { cost = netcost * (1 + Feerate);return cost; } };
class Material :public Budget { private: float amount,price, Feerate,cost, netcost; public: Material(float Feerate, float amount, float price) : Feerate(Feerate), amount(amount), price(price) { netcost = amount*price; cost = 0.0; } virtual float GetBudget() { cost = ((amount * price) * (1+Feerate));return cost; } void display() { cout << "项目\t净费用(万元)\t费率(%)\t总费用(万元)" << endl; cout << "材料\t" << netcost << "\t\t" << Feerate*100 << "\t" << GetBudget() << endl; }; }; class Labour :public Budget { private: float Salary, Welfare, Feerate,cost,netcost; public: Labour(float Feerate, float Salary, float Welfare) : Feerate(Feerate), Salary(Salary), Welfare(Welfare) { netcost = Salary + Welfare; cost = 0.0; } virtual float GetBudget() { cost = ((Salary+Welfare) * (1 + Feerate)); return cost; } void display() { cout << "项目\t净费用(万元)\t费率(%)\t总费用(万元)" << endl; cout << "人工\t" << netcost << "\t\t" << Feerate*100 << "\t" << GetBudget() << endl; } };
int main() { Material m(0.0132, 11, 1.2); Labour l(0.146, 9, 3); m.display(); l.display(); cout << "总预算:\t\t\t\t" << m.GetBudget() + l.GetBudget() << endl; return 0; }
|
v1.4.14