认真、朴素、真实的活着,让他们成为好的见证,Because we share something so profound; Because we share one phrase so profound!
本文两个部分:
1 :第一次出现小错误
2:修补了之前的小错误
总是差几分,人生难完美啊,这就是继续的理由吗?
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
typedef struct info{
char name[MAX + 1];
char id[MAX + 1];
int score;
}info;
/* read a student`s infomation to a */
void get_info(struct info *a){
int c;
scanf("%s", a->name);
c = getchar();
scanf("%s", a->id);
c = getchar();
scanf("%d", &a->score);
}
/* copy a student`s infomation form "frome" to "to" */
void copy(struct info *to, struct info *from){
int i;
for(i = 0; (to->name[i] = from->name[i]) != '\0'; ++i);
to->name[i] = '\0';
for(i = 0; (to->id[i] = from->id[i]) != '\0'; ++i);
to->id[i] = '\0';
to->score = from->score;
}
//print the max and min infomation;
int main(){
struct info *max = malloc(sizeof(info));
struct info *min = malloc(sizeof(info));
struct info *a = malloc(sizeof(info));
int i, n;
scanf("%d", &n);
for(i = 0; i < n; ++i){
get_info(a);
if( i < 2){
if(i == 0)
copy(max, a);
else if(a->score > max->score){
copy(min, max);
copy(max, a);
}else
copy(min, a);
}else if(a->score > max->score)
copy(max, a);
else if(a->score < min->score)
copy(min, a);
}
printf("%s %s\n", max->name, max->id);
printf("%s %s\n", min->name, min->id);
return 0;
}
咦!被我发现了,原来是:若只有一个人输入,输出最大最小都是那个人,
人生啊! 还需认真啊,还需细心啊,真需千百万时的修炼啊
正确代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
typedef struct info{
char name[MAX + 1];
char id[MAX + 1];
int score;
}info;
void get_info(struct info *a){
int c;
scanf("%s", a->name);
c = getchar();
scanf("%s", a->id);
c = getchar();
scanf("%d", &a->score);
}
void copy(struct info *to, struct info *from){
int i;
for(i = 0; (to->name[i] = from->name[i]) != '\0'; ++i);
to->name[i] = '\0';
for(i = 0; (to->id[i] = from->id[i]) != '\0'; ++i);
to->id[i] = '\0';
to->score = from->score;
}
int main(){
struct info *max = malloc(sizeof(info));
struct info *min = malloc(sizeof(info));
struct info *a = malloc(sizeof(info));
int i, n;
scanf("%d", &n);
for(i = 0; i < n; ++i){
get_info(a);
if( i < 2){
if(i == 0)
copy(max, a);
else if(a->score > max->score){
copy(min, max);
copy(max, a);
}else
copy(min, a);
}else if(a->score > max->score)
copy(max, a);
else if(a->score < min->score)
copy(min, a);
}
if(n == 1){
printf("%s %s\n", max->name, max->id);
printf("%s %s\n", max->name, max->id);
}
else{
printf("%s %s\n", max->name, max->id);
printf("%s %s\n", min->name, min->id);
}
return 0;
}