roberthsu2003 / cAndC-

48 stars 18 forks source link

請依據lesson2_5,建立50位學生,學生依據總分由高而低排序 #31

Closed roberthsu2003 closed 6 months ago

roberthsu2003 commented 8 months ago

name:stu35 總分:278 排名:1

a86685566 commented 8 months ago
#include <iostream>
#include <time.h>

using namespace std;

typedef struct student{
  string name;
  int chinese;
  int english;
  int math;
  int score;
  string scoretext; // 個人練習用
}Student;

int main() {
  srandom(time(NULL));
  int min = 50;
  int max = 100;
  int nums = 50;
  Student student[nums];
  for(int i=0; i<nums; i++){
    student[i].name = "學生" + to_string(i+1) + "號";
    student[i].chinese = random() % (max-min+1)+min;
    student[i].english = random() % (max-min+1)+min;
    student[i].math = random() % (max-min+1)+min;
    student[i].score = student[i].chinese + student[i].english + student[i].math;
    student[i].scoretext = " (國文:" + to_string(student[i].chinese) + " 英文:" + to_string(student[i].english) + " 數學:" + to_string(student[i].math) + ")";  // 個人練習用
  }

  //泡沫排序
  for(int i = 0; i < nums-1; i++){
    for(int j = i+1; j < nums; j++){
     if (student[i].score < student[j].score){
        Student temp = student[i];
        student[i] = student[j];
        student[j] = temp;
     }
    }
  }

  for(int i=0; i<nums; i++){
    cout << "姓名:" << student[i].name << endl;
    cout << "排名:" << to_string(i+1) << endl;
    cout << "總分:" << student[i].score << student[i].scoretext << endl;
    cout << "===============" << endl;
  }
}
jackak1003 commented 8 months ago
#include <iostream>
#include <time.h>

using namespace std;

typedef struct student{
  string name;
  int chinese;
  int english;
  int math;
  int sum_total;
}Student;

int main() {
  srandom(time(NULL));
  int min = 40;
  int max = 100;
  Student student[50];
  for(int i=0; i<50; i++){
    student[i].name = "stu" + to_string(i+1); //讓字串相加 int強制轉string
    student[i].chinese = random() % (max-min+1)+min;
    student[i].english = random() % (max-min+1)+min;
    student[i].math = random() % (max-min+1)+min;
    student[i].sum_total=student[i].chinese+student[i].english+student[i].math;
  }

  int first=student[0].sum_total;
  int last=student[0].sum_total; 

  for(int i=0; i<50; i++){
    if(student[i].sum_total>first)
      first=student[i].sum_total;
  }

  for(int i=0; i<50; i++){
    if(student[i].sum_total<last)
      last=student[i].sum_total;
  }

  cout<<"first:"<<first<<endl;
  cout<<"last:"<<last<<endl;

  cout << "===============" << endl;

  //泡沫排序
  for(int i=0; i < 50; i++){
    for(int j=i+1; j<50; j++){
      if (student[i].sum_total > student[j].sum_total){
        //由小到大,對調
        Student temp = student[i];
        student[i] = student[j];
        student[j] = temp;
      }
    }
    }

    for(int i=0; i<50; i++){
      cout << "name:" << student[i].name << endl;
      cout << "chinese:" << student[i].chinese << endl;
      cout << "english:" << student[i].english << endl;
      cout << "math:" << student[i].math << endl;
      cout<<"total:"<<student[i].sum_total<<endl;
      cout << "rank:" << i+1 << endl;
      cout << "===============" << endl;
    }

}