首页 > 解决方案 > 使用用户输入从 For 循环打印图表(无矢量)

问题描述

田径部门需要一个计算“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;

}

我完成了很大一部分,我只是不知道如何将它们拼凑在一起。

标签: c++

解决方案


我不知道如何在最后打印所有这些以创建图表。最大的法院的名字和面积我也不知道怎么说。

你不能使用向量,假设你不能自己使用/创建任何集合你需要在每次阅读法庭描述时写在文件中,还要计算当前的最大面积和总和,然后最后写总和和最大值

使用专用类的提案:

#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

推荐阅读