第10讲 结构 单元作业
有一组关于学生成绩的信息,编写函数max,该函数返回值为分数最高的学生的信息(包括学号和分数)。再编写主函数对其进行调用并输出最高分者的信息。假设结构类型定义为:
struct student
{
char *num;
int score;
};
提示:num定义为指针类型,注意向其复制内容之前要先为其申请空间。
yzy's version:
1 # include <iostream> 2 #define N 256 3 using namespace std; 4 struct student 5 { 6 char* num; 7 int score; 8 }; 9 struct student max(struct student s[N],int n) 10 { 11 struct student t = {0,s[0].score }; 12 t.num = (char*)malloc(sizeof(char)); 13 for (int i = 0; i < n; i++) 14 { 15 if (s[i].score > t.score) 16 { 17 t.score = s[i].score; 18 t.num = s[i].num; 19 } 20 } 21 return t; 22 } 23 int main() 24 { 25 struct student s[N] = {}, t = {}; 26 int n,i; 27 t.num = (char*)malloc(sizeof(char)); 28 cout << "输入学生数:" << endl; 29 /*char a[N]; 30 s->num = a;*/ 31 cin >> n; 32 cout << "输入各学生序号及成绩:" << endl; 33 for (i = 0; i < n; i++) 34 { 35 s[i].num = (char*)malloc(sizeof(char)); 36 cin >> s[i].num >> s[i].score; 37 } 38 t=max(s,n); 39 cout << "最高分者序号为" << *t.num <<",分值为:" << t.score << endl; 40 system("pause"); 41 return 0; 42 }
good version:
1 #include <iostream> 2 3 4 5 using namespace std; 6 7 8 9 struct Student { 10 11 char* num; 12 13 int score; 14 15 }; 16 17 18 19 Student max(const Student* students, int count) { 20 21 if (count <= 0) { 22 23 cerr << "Error: Empty student array" << endl; 24 25 exit(EXIT_FAILURE); 26 27 } 28 29 30 31 const Student* maxStudent = &students[0]; 32 33 34 35 for (int i = 1; i < count; ++i) { 36 37 if (students[i].score > maxStudent->score) { 38 39 maxStudent = &students[i]; 40 41 } 42 43 } 44 45 46 47 return *maxStudent; 48 49 } 50 51 52 53 int main() { 54 55 const int MAX_STUDENTS = 100; 56 57 58 59 int numStudents; 60 61 cout << "Enter the number of students: "; 62 63 cin >> numStudents; 64 65 66 67 if (numStudents <= 0 || numStudents > MAX_STUDENTS) { 68 69 cerr << "Invalid number of students" << endl; 70 71 return EXIT_FAILURE; 72 73 } 74 75 76 77 Student* students = new Student[numStudents]; 78 79 80 81 for (int i = 0; i < numStudents; ++i) { 82 83 students[i].num = new char[20]; // Assuming a maximum length of 19 for the student number 84 85 cout << "Enter student " << i + 1 << " information:" << endl; 86 87 cout << "Student Number: "; 88 89 cin >> students[i].num; 90 91 cout << "Score: "; 92 93 cin >> students[i].score; 94 95 } 96 97 98 99 Student highestScoreStudent = max(students, numStudents); 100 101 102 103 cout << "Student with the highest score:" << endl; 104 105 cout << "Student Number: " << highestScoreStudent.num << endl; 106 107 cout << "Score: " << highestScoreStudent.score << endl; 108 109 110 111 for (int i = 0; i < numStudents; ++i) { 112 113 delete[] students[i].num; 114 115 } 116 117 118 119 delete[] students; 120 121 122 123 system("pause"); 124 125 126 127 return 0; 128 129 }
2. 编写程序,定义一个日期结构变量,计算某日期是本年度的第几天。提示:为简单起见,可定义一个存放12个月中每个月总天数的数组。
yzy's version:
1 # include <iostream> 2 #define N 256 3 using namespace std; 4 struct time 5 { 6 int month; 7 int date; 8 }; 9 int change(struct time t,bool run) 10 { 11 int i,num=0, a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }, b[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 }; 12 if (run == 1) 13 for (i = 0; i < t.month-1; i++) 14 num += b[i]; 15 else 16 for (i = 0; i < t.month-1; i++) 17 num += a[i]; 18 num += t.date; 19 return num; 20 } 21 int main() 22 { 23 struct time t; 24 bool run; 25 int num; 26 cout << "今年是不是闰年?(是:1;否:0)" << endl; 27 cin >> run; 28 cout << "输入一日期(月/日)" << endl; 29 cin >> t.month >> t.date; 30 num = change(t, run); 31 cout << "这是今年的第" << num << "天" << endl; 32 system("pause"); 33 return 0; 34 }
good version:
1 #include <iostream> 2 3 4 5 using namespace std; 6 7 8 9 struct Date { 10 11 int year; 12 13 int month; 14 15 int day; 16 17 }; 18 19 20 21 int dayOfYear(const Date& date) { 22 23 const int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 24 25 26 27 int totalDays = 0; 28 29 30 31 for (int i = 1; i < date.month; ++i) { 32 33 totalDays += daysInMonth[i]; 34 35 } 36 37 38 39 totalDays += date.day; 40 41 42 43 // Adjust for leap year 44 45 if ((date.year % 4 == 0 && date.year % 100 != 0) || (date.year % 400 == 0 && date.month > 2)) { 46 47 totalDays++; 48 49 } 50 51 52 53 return totalDays; 54 55 } 56 57 58 59 int main() { 60 61 Date inputDate; 62 63 64 65 cout << "Enter date (year month day): "; 66 67 cin >> inputDate.year >> inputDate.month >> inputDate.day; 68 69 70 71 int result = dayOfYear(inputDate); 72 73 74 75 cout << "The day of the year for the given date is: " << result << endl; 76 77 78 79 system("pause"); 80 81 82 83 return 0; 84 85 }
3. 使用结构数组输入10本书的名称和单价,调用函数按照书名的字母顺序序进行排序,在主函数输出排序结果。
yzy's version:
1 #include <iostream> 2 #define N 5 3 using namespace std; 4 struct book 5 { 6 char name[100]; 7 int price; 8 }; 9 struct book *sort(struct book b[]) 10 { 11 int i, j; 12 struct book a; 13 for(i=0;i<N-1;i++) 14 for (j = 0; j < N - 1 - i; j++) 15 { 16 //for(int z=0; b[j].name[z] != '\0' && b[j + 1].name[z] != '\0';z++) 17 if (strcmp(b[j].name, b[j+1].name) > 0) 18 { 19 a = b[j]; 20 b[j] = b[j + 1]; 21 b[j + 1] = a; 22 } 23 } 24 struct book* p = b; 25 return p; 26 } 27 int main() 28 { 29 struct book b[N],*p; 30 int i; 31 for (i = 0; i < N; i++) 32 cin >> b[i].name >> b[i].price; 33 p = sort(b); 34 cout << "排序后结果:" << endl; 35 for (i = 0; i < N; i++) 36 cout << p[i].name << ' ' << p[i].price << endl; 37 system("pause"); 38 return 0; 39 }
good version:
1 #include <iostream> 2 3 #include <cstring> 4 5 #include <algorithm> 6 7 8 9 using namespace std; 10 11 12 13 struct Book { 14 15 char title[100]; 16 17 double price; 18 19 }; 20 21 22 23 bool compareBooks(const Book& book1, const Book& book2) { 24 25 return strcmp(book1.title, book2.title) < 0; 26 27 } 28 29 30 31 int main() { 32 33 const int NUM_BOOKS = 10; 34 35 Book books[NUM_BOOKS]; 36 37 38 39 for (int i = 0; i < NUM_BOOKS; ++i) { 40 41 cout << "Enter details for book " << i + 1 << ":" << endl; 42 43 cout << "Title: "; 44 45 cin.getline(books[i].title, sizeof(books[i].title)); 46 47 cout << "Price: "; 48 49 cin >> books[i].price; 50 51 cin.ignore(); // Ignore the newline character left in the input buffer 52 53 } 54 55 56 57 sort(books, books + NUM_BOOKS, compareBooks); 58 59 60 61 cout << "Books sorted by title:" << endl; 62 63 for (int i = 0; i < NUM_BOOKS; ++i) { 64 65 cout << "Title: " << books[i].title << ", Price: " << books[i].price << endl; 66 67 } 68 69 70 71 system("pause"); 72 73 74 75 return 0; 76 77 }