5通讯录排序
#include<stdio.h>
#include<stdlib.h>
struct birth{
#include<stdlib.h>
struct birth{
int year;
int month;
int day;
};
struct students{
char name[10];
long int birth;
char phone[17];
};
int month;
int day;
};
struct students{
char name[10];
long int birth;
char phone[17];
};
int update_score(struct students *data, int n)
{
int i, j;
struct students temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (data[i].birth > data[j].birth) {
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%s ", data[i].name);
printf("%ld ", data[i].birth);
printf("%s\n", data[i].phone);
}
return 0;
}
{
int i, j;
struct students temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (data[i].birth > data[j].birth) {
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%s ", data[i].name);
printf("%ld ", data[i].birth);
printf("%s\n", data[i].phone);
}
return 0;
}
int main()
{
struct students *data;
int n, i;
scanf("%d", &n);
data = (struct students*)malloc(sizeof(struct students) * n);
for (i = 0; i < n; i++) {
scanf("%s", data[i].name);
scanf("%ld", &data[i].birth);
scanf("%s", data[i].phone);
}
update_score(data, n);
system("pause");
return 0;
}
{
struct students *data;
int n, i;
scanf("%d", &n);
data = (struct students*)malloc(sizeof(struct students) * n);
for (i = 0; i < n; i++) {
scanf("%s", data[i].name);
scanf("%ld", &data[i].birth);
scanf("%s", data[i].phone);
}
update_score(data, n);
system("pause");
return 0;
}
6按等级统计学生成绩#include<stdio.h>
#define N 10
int set_grade(struct students*p);
struct students{
int num;
char name[10];
int score;
char grade;
};
int set_grade(struct students *p,int n)
{
int i,count=0;
for(i=0;i<N;i++,p++)
{
if((*p).score<60)
{(*p).grade='D';
count++;
}
else if((*p).score>=60&&(*p).score<=69)
{(*p).grade='C';
count++;
}
else if((*p).score>=70&&(*p).score<=84)
{(*p).grade='B';
count++;
}
else
(*p).grade='A';
}
return count;
}
int main()
{
struct students st[10],*str;
int i,n,count;
str=st;
scanf("%d\n",&n);
for(i=0;i<n;i++){
scanf("%d%s%d",&st[i].num,st[i].name,&st[i].score);
}
count=set_grade(str,n);
printf("The count for failed (<60): %d\n", count);
printf("The grades:\n");
for(i = 0; i < n; i++)
printf("%d %s %c\n", st[i].num, st[i].name, st[i].grade);
return 0;}
#define N 10
int set_grade(struct students*p);
struct students{
int num;
char name[10];
int score;
char grade;
};
int set_grade(struct students *p,int n)
{
int i,count=0;
for(i=0;i<N;i++,p++)
{
if((*p).score<60)
{(*p).grade='D';
count++;
}
else if((*p).score>=60&&(*p).score<=69)
{(*p).grade='C';
count++;
}
else if((*p).score>=70&&(*p).score<=84)
{(*p).grade='B';
count++;
}
else
(*p).grade='A';
}
return count;
}
int main()
{
struct students st[10],*str;
int i,n,count;
str=st;
scanf("%d\n",&n);
for(i=0;i<n;i++){
scanf("%d%s%d",&st[i].num,st[i].name,&st[i].score);
}
count=set_grade(str,n);
printf("The count for failed (<60): %d\n", count);
printf("The grades:\n");
for(i = 0; i < n; i++)
printf("%d %s %c\n", st[i].num, st[i].name, st[i].grade);
return 0;}
心得体会:5建立通讯录时调用了函数,把结构指针作为了参数进行传递,结构指针就是指向结构类型变量的指针,第六小题也用到了结构指针,应该注意结构指针指向结构变量的三种形式,这两道题中结构指针作为函数参数传递地址值,极大提高了参数传递的效率。