4. 下面是一个结构模板:
struct box { char maker[40]; float height; float width; float length; float volume; };
a. 编写一个函数,它将box结构的引用作为形参,并显示每个成员的值。
b. 编写一个函数,它将box结构的引用作为形参,并将volume成员设置为其他3边的乘积。
void show(const box &pst) { std::cout << pst.maker << " " << pst.height << " " << pst.width << " " << pst.length << " " << pst.volume << endl; }
void product(box &pst) { pst.volume = pst.height*pst.width*pst.length; }
5. 为让函数fill()和show()使用引用参数。需要对程序清单7.15做哪些修改?
#include <iostream> #include <array> #include <string> const int Seasons = 4; const std::array<std::string, Seasons> Snames = { "春季", "夏季", "秋季", "冬季" }; void fill(std::array<double, Seasons> &pa); //修改 void show(std::array<double, Seasons> &da); //修改 int main() { std::array<double, 4> expenses; fill(expenses); //修改 show(expenses); system("pause"); return 0; } void fill(std::array<double, Seasons> &pa) //修改 { for (int i = 0; i < Seasons; i++) { std::cout << "输入 " << Snames[i] << " 支出: "; std::cin >> pa[i]; //修改 } } void show(std::array<double, Seasons> &da) //修改 { double total = 0.0; std::cout << "\n*****开支*****\n"; for (int i = 0; i < Seasons; i++) { std::cout << Snames[i] << ": $" << da[i] << '\n'; total += da[i]; } std::cout << "总计: $" << total << '\n'; }
签到成功!签到时间:下午12:47:00,每日打卡,生活更精彩哦~