roberthsu2003 / cAndC-

51 stars 18 forks source link

請將以下程式的建立一個function,處理泡沫排序法 #53

Open roberthsu2003 opened 1 week ago

roberthsu2003 commented 1 week 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;

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;
    }

    for(int i = 0; i < n-1; i++){
        for(int j = i + 1; j < n; j++){
            if( students[i].average < students[j].average ){
                tmp = students[i];
                students[i] = students[j];
                students[j] = tmp;
            }
        }
    }

    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);
    }

}
Gin-4869 commented 1 week 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);
  }

}