Closed roberthsu2003 closed 3 weeks ago
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
// 定義結構
typedef struct student{
string name;
int chinese;
int english;
int math;
int sum = 0;
double average;
}Student;
void bubble(Student *ptr, int n){
Student tmp;
for(int i = 0; i < n - 1; i++){
for(int j = i + 1; j < n; j++){
if( ptr[i].average < ptr[j].average ){
tmp = ptr[j];
ptr[j] = ptr[i];
ptr[i] = tmp;
}
}
}
}
int main() {
int n = 50;
int min = 50;
int max = 100;
Student tmp;
srand( time( NULL ) );
Student students[n];
for(int i = 0; i < n; i++){
string name_ori = "學生" + to_string(i+1);
students[i].name = name_ori;
students[i].chinese = rand() % (max - min +1) + min;
students[i].english = rand() % (max - min +1) + min;
students[i].math = rand() % (max - min +1) + min;
students[i].sum = students[i].chinese + students[i].english + students[i].math;
students[i].average = students[i].sum / 3.0;
}
bubble(students, n);
for(int i = 0; i < n; i++){
cout << "名次:" << i + 1 << endl;
cout << students[i].name << endl;
cout << "中文:" << students[i].chinese << " ";
cout << "英文:" << students[i].english << " ";
cout << "數學:" << students[i].math << "\n";
cout << "總分:" << students[i].sum << "\n";
printf("平均:%.2lf\n\n", students[i].average);
}
}