- 论坛徽章:
- 1
|
谁能帮帮忙???
今天心情不好,想写个程序。要不这帖就删了。
- #include <stdio.h>;
- #include <stdlib.h>;
- struct stu {
- int number; // if the number is too large, change to use char string.
- char name[11]; // five Chinese character.
- int score;
- };
- struct stu arr[10];
- void cut_ln(char *);
- void sort(struct stu *);
- int main(void)
- {
- int ch;
- int i;
- for (i = 0; i < 11; i++) {
- printf("Please input the number of the student: ");
- scanf("%d", &arr[i].number);
- getchar();
- printf("Please input the name of the student: ");
- fgets(arr[i].name, 11, stdin);
- cut_ln(arr[i].name);
- printf("Please input the score of the student: ");
- scanf("%d", &arr[i].score);
- getchar();
- printf("Do you want to be continue(y/n): ");
- ch = getchar();
- if (ch == 'n' || ch == 'N') break;
- }
- sort(arr);
- for (; i >;= 0; i--) {
- printf("%d, %s, %d\n", arr[11-i-1].number, arr[11-i-1].name, arr[11-i-1].score);
- }
- exit(0);
- }
- // change the '/n' in the str string into '\0'.
- void cut_ln(char *str) {
- for (; *str != '\n'; str++)
- ; // do nothing.
- *str = '\0';
- return ;
- }
- // sort the array.
- void sort(struct stu *arr) {
- int i, j;
- struct stu tmp;
- for (i = 0; i < 11; i++)
- for (j = i; j < 11; j++)
- if (arr[i].score >; arr[j].score) {
- tmp = arr[i];
- arr[i] = arr[j];
- arr[j] = tmp;
- }
- return ;
- }
复制代码 |
|