编程构建一个student类,并在main函数中创建5个对象(对象信息如输入样例所示),其中每个对象的数据包括学号num、姓名name、年龄age以及数学maths、英语english、语文chinese三门功课的分数,然后求出每个人的平均分数ave,将学号、姓名和平均分数输出到磁盘文件stud.dat中,最后从stud.dat文件中读出这些数据,并显示在屏幕上。
时间: 2023-05-31 07:19:55 浏览: 223
### 回答1:
以下是一个可能的解答:
```python
# 定义一个Student类
class Student:
def __init__(self, num, name, age, maths, english, chinese):
self.num = num
self.name = name
self.age = age
self.maths = maths
self.english = english
self.chinese = chinese
def get_average_score(self):
return (self.maths + self.english + self.chinese) / 3
# 在main函数中创建5个对象
students = [
Student("001", "张三", 18, 90, 80, 70),
Student("002", "李四", 19, 85, 75, 65),
Student("003", "王五", 20, 95, 85, 75),
Student("004", "赵六", 21, 80, 70, 60),
Student("005", "钱七", 22, 70, 60, 50),
]
# 将学号、姓名和平均分数输出到磁盘文件stud.dat中
with open("stud.dat", "w") as f:
for student in students:
ave = student.get_average_score()
f.write(f"{student.num} {student.name} {ave:.2f}\n")
# 从stud.dat文件中读出数据,并显示在屏幕上
with open("stud.dat", "r") as f:
for line in f:
num, name, ave = line.strip().split()
print(f"学号:{num},姓名:{name},平均分数:{ave}")
```
输出结果:
```
学号:001,姓名:张三,平均分数:80.00
学号:002,姓名:李四,平均分数:75.00
学号:003,姓名:王五,平均分数:85.00
学号:004,姓名:赵六,平均分数:70.00
学号:005,姓名:钱七,平均分数:60.00
```
### 回答2:
首先,我们要创建一个Student类,包含学号、姓名、年龄以及数学、英语、语文三门功课的分数,并且定义一个计算平均分数的方法。
class Student:
def __init__(self, num, name, age, maths, english, chinese):
self.num = num
self.name = name
self.age = age
self.maths = maths
self.english = english
self.chinese = chinese
def average(self):
return (self.maths + self.english + self.chinese) / 3
接下来在main函数中,创建五个对象并获取每个学生的平均分数,然后将学号、姓名和平均分数输出到文件中。
def main():
students = []
for i in range(5):
print("请输入第{}个人的信息".format(i+1))
num = input("学号:")
name = input("姓名:")
age = input("年龄:")
maths = float(input("数学成绩:"))
english = float(input("英语成绩:"))
chinese = float(input("语文成绩:"))
student = Student(num, name, age, maths, english, chinese)
students.append(student)
with open("stud.dat", "w") as f:
for student in students:
ave = student.average()
f.write("{},{},{}\n".format(student.num, student.name, ave))
最后从文件中读取数据,并将其输出到屏幕上。
with open("stud.dat", "r") as f:
for line in f:
line = line.strip().split(",")
print("{}\t{}\t{}".format(line[0], line[1], line[2]))
if __name__ == "__main__":
main()
这样就编写完成了一个可以创建学生对象、计算平均分数并将结果输出到文件中的程序,并且还可以从文件中读取数据并将其输出到屏幕上。
### 回答3:
首先,我们需要定义一个Student类,包含学号、姓名、年龄、数学、英语、语文6个属性,以及一个计算平均分数的方法。
```
class Student {
public:
int num; // 学号
string name; // 姓名
int age; // 年龄
int maths; // 数学成绩
int english; // 英语成绩
int chinese; // 语文成绩
float calcAverage() { // 计算平均分数
return (maths + english + chinese) / 3.0f;
}
};
```
接下来,在main函数中创建5个Student对象,并为每个对象的属性赋值。
```
int main() {
Student students[5];
for (int i = 0; i < 5; i++) {
cout << "请输入第" << i+1 << "个学生的信息:\n";
cout << "学号:";
cin >> students[i].num;
cout << "姓名:";
cin >> students[i].name;
cout << "年龄:";
cin >> students[i].age;
cout << "数学成绩:";
cin >> students[i].maths;
cout << "英语成绩:";
cin >> students[i].english;
cout << "语文成绩:";
cin >> students[i].chinese;
}
```
接下来,对每个学生的平均分数进行计算,并将学生的学号、姓名和平均分数写入磁盘文件stud.dat中。
```
ofstream outfile("stud.dat"); // 打开文件
for (int i = 0; i < 5; i++) {
float ave = students[i].calcAverage(); // 计算平均分数
outfile << students[i].num << " " << students[i].name << " " << ave << endl; // 写入文件
}
outfile.close(); // 关闭文件
```
最后,从磁盘文件stud.dat中读取数据,并在屏幕上显示。
```
ifstream infile("stud.dat"); // 打开文件
cout << "学号 姓名 平均分\n";
while (!infile.eof()) {
int num;
string name;
float ave;
infile >> num >> name >> ave; // 从文件读取数据
cout << num << " " << name << " " << ave << endl; // 显示数据
}
infile.close(); // 关闭文件
return 0;
}
```
完整代码如下: