c++ - 使用用户输入从 For 循环打印图表(无矢量)
问题描述
田径部门需要一个计算“n”个篮球场面积的程序。你被雇来写一份报告,报告他们所有人所覆盖的总面积。(它们的总和)并报告其中最大的篮球场。你的程序应该包含一个函数定义来写入一个文件和函数定义计算下面最大的篮球场,除了主函数。应该执行以下操作
- 1)程序应该询问“n”(法院数量)的值
- 2) 程序要求输入每个球场的全名,然后是宽度和长度。(名称可以是复合名称,例如“St James Arena”),
- 3)计算n个区域和总和。(你应该循环和 if 语句来确定最大的法院和它的名字)
- 4) 然后输出以下报告并将其保存到硬盘中的文件中。
例子:
>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;
}
我完成了很大一部分,我只是不知道如何将它们拼凑在一起。
解决方案
我不知道如何在最后打印所有这些以创建图表。最大的法院的名字和面积我也不知道怎么说。
你不能使用向量,假设你不能自己使用/创建任何集合你需要在每次阅读法庭描述时写在文件中,还要计算当前的最大面积和总和,然后最后写总和和最大值
使用专用类的提案:
#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
推荐阅读
- botframework - 如何使用 AppSettings 将 Bot 服务迁移到框架 4.3
- node.js - 在新的 React 版本中,如何修复 TypeError: styled_components__WEBPACK_IMPORTED_MODULE_3__.default.dev?
- python - 为什么使用 * 通配符通过 Python 的 subprocess.call 调用 tar 时无法创建存档?
- python - 带有任意列的 Plotly Express 悬停模式
- java - 即使 setRepeatCount = 0,对象动画师也会重新启动
- java - 使用 DOM 解析器解析 XML 字符串返回 null
- c# - 如何在不定义父节点的情况下反序列化 XML 文件?
- java - 多个过程签名时使用 Java 获取 Oracle 过程元数据
- azure - 将 AzureRm 迁移到 Az 的问题
- workflow - alfresco 直接从文档库启动工作流程