田径部门需要一个程序来计算“ n”个篮球场的面积。您被雇用撰写所有人员所覆盖的总面积的报告。 (它们的总和),并报告其中最大的篮球场。您的程序应包含要写入文件的函数定义和计算除主要功能之外的最大的篮球场的函数定义。 应当执行以下操作
示例:
>BASKETBALL COURTS AREA REPORT
>Court Name Length Width
>Bay Arena 30 28
>Michael Center 26 23
>The total area covered by all of the them together is: 1438
>The largest basketball court is Bay Arena: 840
所以我想出了如何使用setw()获取用户数据并按照我想要的方式打印它,但是每次它通过for循环时,它都会破坏图表。看起来像这样:
Court name: (user input)
Length: (user input)
Width: (user input) /
(prints court name) (prints length) (prints width)
Court name: (user input)
Length: (user input)
Width: (user input) /
(prints court name) (prints length) (prints width)
而且确实如此,但是有许多法院是必需的。我不知道如何仅在最后打印所有图表以创建图表。我也不知道怎么说最大的法院的名字和地区。
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main (){
string court;
int numberofCourts, length, width, area, total;
cout << "Number of courts: ";
cin >> numberofCourts;
cin.ignore();
for (int i=1; i<=numberofCourts; i++){
cout << "Name: ";
getline (cin, court);
cout << "Length: ";
cin >> length;
cout << "Width: ";
cin >> width;
if (i<=numberofCourts){
cout << setw(10)<< court;
cout << setw(10)<< length;
cout << setw(10)<< width << endl;
}
cin.ignore();
area= length*width;
total+=area;
}
cout << "The total area covered by all of the them together is: " << total << endl;
cout << "The largest basketball court is " << endl;
}
我完成了很大一部分,我只是不知道如何将它们拼凑在一起。
答案 0 :(得分:0)
我不知道如何仅在最后打印所有图表以创建图表。我也不知道怎么说最大的法院的名字和地区。
您不能使用向量,假设您每次阅读法院说明时都无法自己使用/创建任何集合,则需要在文件中写入,还要计算当前的最大面积和总和,最后写入总和和最大
使用专用课程的提案:
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct Court {
string name;
int length;
int width;
bool read();
void write(std::ostream & os, size_t wname, size_t wlength, size_t wwidth) const;
int area() const { return length * width; }
};
bool Court::read() {
cin.ignore();
cout << "Name: ";
if (!getline(cin, name))
return false;
cout << "Length: ";
if (!(cin >> length))
return false;
cout << "Width: ";
if (!(cin >> width))
return false;
return true;
}
void Court::write(std::ostream & os, size_t wname, size_t wlength, size_t wwidth) const
{
os << name;
if (name.length() < wname)
os << setw(wname - name.length()) << ' ';
os << setw(wlength) << length << setw(wwidth) << width << endl;
}
void stats(int & max, int & sum, int area)
{
if (area > max)
max = area;
sum += area;
}
int main ()
{
int numberofCourts;
cout << "Number of courts: ";
if (!(cin >> numberofCourts)) {
cerr << "invalid numner of courts" << endl;
return -1;
}
ofstream os("courts.txt");
if (!os.is_open()) {
cerr << "cannot open 'courts.txt'" << endl;
return -1;
}
os << "BASKETBALL COURTS AREA REPORT" << endl
<< "Court Name Length Width" << endl;
int max = 0, sum = 0;
for (int i = 0; i != numberofCourts; ++i) {
Court court;
if (!court.read()){
cerr << "error reading a court" << endl;
return -1;
}
court.write(os, 23, 6, 22);
stats(max, sum, court.area());
}
os << "The total area covered by all of the them together is: " << sum << endl;
os << "The largest basketball court is: " << max << endl;
return 0;
}
编译和执行:
pi@raspberrypi:/tmp $ g++ -pedantic -Wextra -Wall court.cc
pi@raspberrypi:/tmp $ ./a.out
Number of courts: 2
Name: Bay Arena
Length: 30
Width: 28
Name: Michael Center
Length: 26
Width: 23
pi@raspberrypi:/tmp $ cat courts.txt
BASKETBALL COURTS AREA REPORT
Court Name Length Width
Bay Arena 30 28
Michael Center 26 23
The total area covered by all of the them together is: 1438
The largest basketball court is: 840