多语言版实现学生成绩管理系统:
学生成绩管理系统的实现,包括添加学生、录入成绩、计算平均成绩和显示成绩等功能:
分别使用了C++语言、java语言、python语言、C语言进行了实现,提供了完整代码及下载地址
不想看,也可以直接下载源码:C语言版本 C++版本 Java版本 Python版本
C++代码实现: 下载地址
使用了class
来定义学生的类,并使用列表来存储学生对象。用户可以选择添加学生、录入成绩、显示平均成绩或退出程序。根据用户的选择,程序会执行相应的操作
// 学生成绩管理系统的实现,包括添加学生、录入成绩、计算平均成绩和显示成绩等功能:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Student {
string name;
int id;
vector<int> scores;
Student(string n, int i) : name(n), id(i) {}
void addScore(int score) {
scores.push_back(score);
}
double getAverageScore() {
if (scores.size() == 0) {
return 0.0;
}
int sum = 0;
for (int score : scores) {
sum += score;
}
return static_cast<double>(sum) / scores.size();
}
};
int main() {
vector<Student> students;
int choice;
do {
cout << "1. Add student" << endl;
cout << "2. Enter scores" << endl;
cout << "3. Show average score" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
string name;
int id;
cout << "Enter student name: ";
cin >> name;
cout << "Enter student ID: ";
cin >> id;
students.push_back(Student(name, id));
break;
}
case 2: {
int id, score;
cout << "Enter student ID: ";
cin >> id;
for (Student& student : students) {
if (student.id == id) {
cout << "Enter score for " << student.name << ": ";
cin >> score;
student.addScore(score);
break;
}
}
break;
}
case 3: {
int id;
cout << "Enter student ID: ";
cin >> id;
for (const Student& student : students) {
if (student.id == id) {
cout << "Average score for " << student.name << " is: " << student.getAverageScore() << endl;
break;
}
}
break;
}
case 4:
cout << "Exiting program" << endl;
break;
default:
cout << "Invalid choice" << endl;
}
} while (choice != 4);
return 0;
}
java代码实现: 下载地址
使用了class
来定义学生的类,并使用ArrayList
来存储学生对象。用户可以选择添加学生、录入成绩、显示平均成绩或退出程序。
// 学生成绩管理系统的实现,包括添加学生、录入成绩、计算平均成绩和显示成绩等功能:
import java.util.ArrayList;
import java.util.Scanner;
public class Student {
private String name;
private int id;
private ArrayList<Integer> scores;
public Student(String name, int id) {
this.name = name;
this.id = id;
this.scores = new ArrayList<Integer>();
}
public void addScore(int score) {
this.scores.add(score);
}
public double getAverageScore() {
if (this.scores.size() == 0) {
return 0.0;
}
int sum = 0;
for (int score : this.scores) {
sum += score;
}
return (double) sum / this.scores.size();
}
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("1. Add student");
System.out.println("2. Enter scores");
System.out.println("3. Show average score");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter student name: ");
String name = scanner.next();
System.out.print("Enter student ID: ");
int id = scanner.nextInt();
students.add(new Student(name, id));
break;
case 2:
System.out.print("Enter student ID: ");
id = scanner.nextInt();
for (Student student : students) {
if (student.id == id) {
System.out.print("Enter score for " + student.name + ": ");
int score = scanner.nextInt();
student.addScore(score);
break;
}
}
break;
case 3:
System.out.print("Enter student ID: ");
id = scanner.nextInt();
for (Student student : students) {
if (student.id == id) {
System.out.println("Average score for " + student.name + " is: " + student.getAverageScore());
break;
}
}
break;
case 4:
System.out.println("Exiting program");
break;
default:
System.out.println("Invalid choice");
}
} while (choice != 4);
scanner.close();
}
}
python代码实现:下载地址
使用了class
来定义学生的类,并使用列表来存储学生对象。用户可以选择添加学生、录入成绩、显示平均成绩或退出程序
# 学生成绩管理系统的实现,包括添加学生、录入成绩、计算平均成绩和显示成绩等功能:
class Student:
def __init__(self, name, id):
self.name = name
self.id = id
self.scores = []
def add_score(self, score):
self.scores.append(score)
def get_average_score(self):
if len(self.scores) == 0:
return 0.0
return sum(self.scores) / len(self.scores)
students = []
while True:
print("1. Add student")
print("2. Enter scores")
print("3. Show average score")
print("4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
name = input("Enter student name: ")
id = int(input("Enter student ID: "))
students.append(Student(name, id))
elif choice == 2:
id = int(input("Enter student ID: "))
for student in students:
if student.id == id:
score = int(input(f"Enter score for {student.name}: "))
student.add_score(score)
break
elif choice == 3:
id = int(input("Enter student ID: "))
for student in students:
if student.id == id:
print(f"Average score for {student.name} is: {student.get_average_score()}")
break
elif choice == 4:
print("Exiting program")
break
else:
print("Invalid choice")
C语言代码实现: 下载地址
使用了struct
来定义学生的结构体,并使用数组来存储学生对象。用户可以选择添加学生、录入成绩、显示平均成绩或退出程序
// 学生成绩管理系统的实现,包括添加学生、录入成绩、计算平均成绩和显示成绩等功能:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LENGTH 50
typedef struct {
char name[MAX_NAME_LENGTH];
int id;
int scores[5];
int num_scores;
} Student;
void addStudent(Student students[], int *num_students) {
if (*num_students < MAX_STUDENTS) {
printf("Enter student name: ");
scanf("%s", students[*num_students].name);
printf("Enter student ID: ");
scanf("%d", &students[*num_students].id);
students[*num_students].num_scores = 0;
(*num_students)++;
} else {
printf("Maximum number of students reached\n");
}
}
void enterScores(Student students[], int num_students) {
int id, score;
printf("Enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Enter score for %s: ", students[i].name);
scanf("%d", &score);
students[i].scores[students[i].num_scores] = score;
students[i].num_scores++;
break;
}
}
}
void showAverageScore(Student students[], int num_students) {
int id, sum = 0, count = 0;
printf("Enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
for (int j = 0; j < students[i].num_scores; j++) {
sum += students[i].scores[j];
count++;
}
if (count > 0) {
printf("Average score for %s is: %.2f\n", students[i].name, (float) sum / count);
} else {
printf("No scores available for %s\n", students[i].name);
}
break;
}
}
}
int main() {
Student students[MAX_STUDENTS];
int num_students = 0;
int choice;
do {
printf("1. Add student\n");
printf("2. Enter scores\n");
printf("3. Show average score\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(students, &num_students);
break;
case 2:
enterScores(students, num_students);
break;
case 3:
showAverageScore(students, num_students);
break;
case 4:
printf("Exiting program\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}
4
5 |-----|
+---+ | |
| | | |
| | 3 3 | |
| | | |
| +---+ ----- | |
| | | | | | |----|3
2 | | 2 | | 2 | | | |
| | | | | | | |
+---+ | ----+ | +---+ | | | |
| | | | | | | | | |
1 | | 1 1 | | 1 | | 1 | | | |
| | | | | | | | | |
+---+ +---+ +---+ +---+ +---+ | | | |
| | | | | | | |
0 | | 0 0 | | 0 | | | |
| | | | | | | |
+---+ +-------+ +---+ | +--- | |+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18