Closed roberthsu2003 closed 4 months ago
//請將結構改為class
#include <iostream>
#include <time.h>
using namespace std;
class Student_points{
public:
string name;
int chinese;
int english;
int math;
int total;
};
void create_student(Student_points *array, int m) {
srandom(time(NULL));
int min = 50;
int max = 100;
for (int i = 0; i < m; i++) {
array[i].name = "stu" + to_string(i + 1);
array[i].chinese = random() % (max - min + 1) + min;
array[i].english = random() % (max - min + 1) + min;
array[i].math = random() % (max - min + 1) + min;
array[i].total = array[i].chinese + array[i].english + array[i].math;
}
}
void sorted(Student_points *array, int m){
// 使用泡沫排序法依據總分由高而低排序
for (int i = 0; i < m - 1; i++) {
for (int j = 0; j < m - i - 1; j++) {
if (array[j].total < array[j + 1].total) {
Student_points temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
int main() {
int nums;
cout << "請輸入學生數量:";
cin >> nums;
Student_points students[nums];
create_student(students, nums);
sorted(students,nums);
for (int i = 0; i < nums; i++) {
cout << "name:" << students[i].name << endl;
cout << "總分:" << students[i].total << endl;
cout << "排名:" << i + 1 << endl;
cout << "=========" << endl;
}
}
我嘗試了很久
當我的class 為
class Student_points{
public:
string n;
int c;
int e;
int m;
Student_points(string name, int chinese, int english, int math){
n = name;
c = chinese;
e = english;
m = math;
}
};
時,無論我怎麼嘗試 都無法成功執行 Student_points students[nums];
int nums;
cout << "請輸入學生數量:";
cin >> nums;
Student_points students[nums];
我不清楚是網頁的問題 還是我的問題 希望老師或是同學如果過目到可以幫忙解答一下 我就當作備忘錄貼在這裡了
我嘗試了很久
當我的class 為
class Student_points{ public: string n; int c; int e; int m; Student_points(string name, int chinese, int english, int math){ n = name; c = chinese; e = english; m = math; } };
時,無論我怎麼嘗試 都無法成功執行
Student_points students[nums];
int nums; cout << "請輸入學生數量:"; cin >> nums; Student_points students[nums];
我不清楚是網頁的問題 還是我的問題
希望老師或是同學如果過目到可以幫忙解答一下
我就當作備忘錄貼在這裡了
先將自訂的建構式刪除就可暫時解決。
#include <iostream>
#include <time.h>
using namespace std;
class Student {
public:
string name;
int chinese;
int english;
int math;
int total;
};
void create_student(Student *array, int m) {
srandom(time(NULL));
int min = 50;
int max = 100;
for (int i = 0; i < m; i++) {
array[i].name = "stu" + to_string(i + 1);
array[i].chinese = random() % (max - min + 1) + min;
array[i].english = random() % (max - min + 1) + min;
array[i].math = random() % (max - min + 1) + min;
array[i].total = array[i].chinese + array[i].english + array[i].math;
}
}
void sorted(Student *array, int m){
// 使用泡沫排序法依據總分由高而低排序
for (int i = 0; i < m - 1; i++) {
for (int j = 0; j < m - i - 1; j++) {
if (array[j].total < array[j + 1].total) {
Student temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
int main() {
int nums;
cout << "請輸入學生數量:";
cin >> nums;
Student students[nums];
create_student(students, nums);
sorted(students,nums);
for (int i = 0; i < nums; i++) {
cout << "name:" << students[i].name << endl;
cout << "總分:" << students[i].total << endl;
cout << "排名:" << i + 1 << endl;
cout << "=========" << endl;
}
}