利用流格式控制,进行成绩和名字的输出,要求名字左对齐,分数右对齐--p209_1

源程序:

#include < iostream >

#include < string >

#include <iomanip>

using namespace std;

class Student //定义学生类

{

private:

  string name; //学生姓名

  float score; //学生成绩

public:

  Student(string n = 0, float s = 0) //带默认参数的构造函数

  {

    name = n;

    score = s;

  }

  string getName()

  {

    return name;

  }

  float getScore()

  {

    return score;

  }

};

void main()

{

  Student s1("liming", 98);  //建立5个学生对象

  Student s2("sdfh", 90);

  Student s3("vn.fy", 80);

  Student s4("cnbtrt", 70);

  Student s5("ryuety", 48);

  cout << setw(10) << left << "姓名" << setw(5) << right << "分数" << endl;

  cout << setw(10) << left << s1.getName() << setw(5) << right << s1.getScore() << endl;

  cout << setw(10) << left << s2.getName() << setw(5) << right << s2.getScore() << endl;

  cout << setw(10) << left << s3.getName() << setw(5) << right << s3.getScore() << endl;

  cout << setw(10) << left << s4.getName() << setw(5) << right << s4.getScore() << endl;

  cout << setw(10) << left << s5.getName() << setw(5) << right << s5.getScore() << endl;

  //设置名字的输出宽度为10,左对齐; 成绩的输出宽度为5,右对齐。

  system("pause");

}

运行结果:

 

原文地址:https://www.cnblogs.com/duanqibo/p/11900237.html