【C++ Primer Plus习题】7.8
问题:
解答:
#include <iostream>
using namespace std;#define SEASONS 4typedef struct _Spend
{double money[SEASONS];
}Spend;const char* Snames[SEASONS] = { "Spring","Summer","Fall","Winter" };void fill(double* expenses)
{for (int i = 0; i < SEASONS; i++){cout << "请输入" << Snames[i] << "季的费用:";cin >> *(expenses + i);}
}void show(double* expenses)
{double total = 0.0;for (int i = 0; i < SEASONS; i++){cout << Snames[i] << "季节的费用为:" << expenses[i] << endl;total += expenses[i];}cout << "总的费用为:" << total << endl;
}int main()
{double expenses[SEASONS];fill(expenses);show(expenses);Spend spend;fill(spend.money);show(spend.money);return 0;
}
运行结果:
2024年8月31日20:06:58