用C语言和Java编写同一个功能的应用程序,实现下列功能:输入20名同学的学号、姓名、三门课程成绩;计算每位同学的总分与平均分;输出成绩表、显示总分最高分与最低分学生的成绩信息。
时间: 2024-10-20 16:08:35 浏览: 48
课程设计二: 题目: 学生成绩管理系统 功能: 实现学生信息管理 包括:录入、查询、排序等功能 C语言
在C语言和Java中,你可以使用类似的数据结构和算法来实现这个功能。这里是一个简化的例子:
C语言实现:
```c
#include <stdio.h>
typedef struct {
int id;
char name[50];
float score[3];
} Student;
void input_students(Student students[], int num) {
for (int i = 0; i < num; i++) {
printf("请输入学生 %d 的信息:\n", i + 1);
scanf("%d", &students[i].id);
fgets(students[i].name, sizeof(students[i].name), stdin);
for (int j = 0; j < 3; j++) {
scanf("%f", &students[i].score[j]);
}
}
}
void calculate_scores(Student students[], int num) {
float max_score = students[0].score[0], min_score = students[0].score[0];
for (int i = 1; i < num; i++) {
float total = 0;
for (int j = 0; j < 3; j++) {
total += students[i].score[j];
}
students[i].total = total;
students[i].avg = total / 3;
if (total > max_score) {
max_score = total;
}
if (total < min_score) {
min_score = total;
}
}
}
void print_students(Student students[], int num) {
for (int i = 0; i < num; i++) {
printf("学号:%d, 姓名:%s, 总分:%f, 平均分:%f\n",
students[i].id, students[i].name, students[i].total, students[i].avg);
}
printf("最高分:%f - 学生:%s\n", max_score, find_student_with_max_score(students, num));
printf("最低分:%f - 学生:%s\n", min_score, find_student_with_min_score(students, num));
}
// 辅助函数,查找总分最高的学生
Student* find_student_with_max_score(Student students[], int num) {
Student *max_student = &students[0];
for (int i = 1; i < num; i++) {
if (students[i].total > max_student->total) {
max_student = &students[i];
}
}
return max_student;
}
// 辅助函数,查找总分最低的学生
Student* find_student_with_min_score(Student students[], int num) {
Student *min_student = &students[0];
for (int i = 1; i < num; i++) {
if (students[i].total < min_student->total) {
min_student = &students[i];
}
}
return min_student;
}
int main() {
int num_students = 20;
Student students[num_students];
input_students(students, num_students);
calculate_scores(students, num_students);
print_students(students, num_students);
return 0;
}
```
Java实现:
```java
import java.util.Scanner;
class Student {
int id;
String name;
double[] scores;
void input() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生 " + id + " 的信息:");
name = sc.nextLine();
for (int i = 0; i < 3; i++) {
scores[i] = sc.nextDouble();
}
}
double getTotal() {
double total = 0;
for (double score : scores) {
total += score;
}
return total;
}
double getAverage() {
return getTotal() / 3;
}
// 省略辅助方法,如findMaxScore和findMinScore
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numStudents = 20;
Student[] students = new Student[numStudents];
for (int i = 0; i < numStudents; i++) {
Student s = new Student();
s.id = i + 1;
s.input();
students[i] = s;
}
for (Student s : students) {
System.out.printf("学号:%d, 姓名:%s, 总分:%f, 平均分:%f%n",
s.id, s.name, s.getTotal(), s.getAverage());
}
// 计算并输出最高分和最低分
double maxScore = students[0].getTotal();
double minScore = students[0].getTotal();
for (Student s : students) {
if (s.getTotal() > maxScore) {
maxScore = s.getTotal();
} else if (s.getTotal() < minScore) {
minScore = s.getTotal();
}
}
System.out.println("最高分:" + maxScore + " - 学生:" + findMaxScore(students)[0] + ", " + findMaxScore(students)[1]);
System.out.println("最低分:" + minScore + " - 学生:" + findMinScore(students)[0] + ", " + findMinScore(students)[1]);
sc.close();
}
// 辅助方法,查找总分最高的学生
private static double[] findMaxScore(Student[] students) {
// 实现...
}
// 辅助方法,查找总分最低的学生
private static double[] findMinScore(Student[] students) {
// 实现...
}
}
```
以上两个示例展示了如何分别用C语言和Java实现同样的功能。在Java版本中,我还省略了`findMaxScore`和`findMinScore`的具体实现,因为它们类似于C语言中的辅助函数。
阅读全文