21xrx.com
2024-09-20 00:25:42 Friday
登录
文章检索 我的文章 写文章
用C++编程输出成绩单
2023-07-02 10:00:38 深夜i     --     --
C++ 编程 成绩单 输出 学生成绩

成绩单是学校里最常见的文档之一,它可以记录学生们的学业成绩以及评语等信息。如果您想学习如何使用C ++编程输出一个简单的成绩单,那么您来对地方了!

首先,我们需要定义一些变量存储学生的成绩等信息。请看下面的代码:


#include <iostream>

#include <string>

using namespace std;

struct Student

  string name;

  int mathScore;

  int chineseScore;

  int englishScore;

  int totalScore;

  string comment;

;

在这个示例代码中,我们定义了一个结构体类型"Student",它包含了学生的姓名、数学成绩、语文成绩、英语成绩、总分以及评语等成员变量。

接下来,我们可以输入学生的信息并计算他们的总分。代码如下:


void inputStudentData(Student& student) {

  cout << "Name: ";

  getline(cin, student.name);

  cout << "Math Score: ";

  cin >> student.mathScore;

  cout << "Chinese Score: ";

  cin >> student.chineseScore;

  cout << "English Score: ";

  cin >> student.englishScore;

  student.totalScore = student.mathScore + student.chineseScore + student.englishScore;

}

void outputStudentData(const Student& student)

  cout << "Name: " << student.name << endl;

  cout << "Math Score: " << student.mathScore << endl;

  cout << "Chinese Score: " << student.chineseScore << endl;

  cout << "English Score: " << student.englishScore << endl;

  cout << "Total Score: " << student.totalScore << endl;

  cout << "Comment: " << student.comment << endl;

void calculateComment(Student& student) {

  if (student.totalScore >= 260)

    student.comment = "Excellent";

  

  else if (student.totalScore >= 200)

    student.comment = "Good";

  

  else

    student.comment = "Average";

  

}

int main() {

  Student student;

  inputStudentData(student);

  calculateComment(student);

  outputStudentData(student);

  return 0;

}

在上面的代码中,我们定义了三个函数分别用于输入学生信息、计算评语以及输出学生信息。在主函数中,我们首先定义了一个"Student"类型的变量"student",然后调用了"inputStudentData()"函数来输入学生信息。接着,我们调用了"calculateComment()"函数来计算学生的评语,最后调用"outputStudentData()"函数来输出所有学生的信息。

现在,如果我们编译并运行这个程序,我们就会得到类似下面的输出结果:

>Name: Tom

Math Score: 80

Chinese Score: 90

English Score: 85

Total Score: 255

Comment: Good

以上就是用C ++编程输出一个简单的成绩单的全部内容。希望这个例子对您有所帮助!

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复