1.通讯录排序:
#include<stdio.h>
struct birthday{
int year;
int month;
int day;
};
struct frineds{
char name[2];
struct birthday bd;
int number;
};
int main()
{
int i,j,n;
struct frineds a[2],x;
printf("Input n frineds:");
scanf("%d",&n);
printf("name\t birthday\t number\n");
for(i=0;i<n;i++)
scanf("%s%d%d%d%d",&a[i].name,&a[i].bd.year,&a[i].bd.month,&a[i].bd.day,&a[i].number);
for(i=0;i<n;i++)
for(j=0;j<n-1;j++){
if(a[j].bd.year>a[j+1].bd.year){
x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
if(a[j].bd.year==a[j+1].bd.year){
if(a[j].bd.month>a[j+1].bd.month){
x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
if(a[j].bd.month==a[j+1].bd.month){
if(a[j].bd.day>a[j+1].bd.day){
x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
}
}
for(i=0;i<n;i++)
printf("%s\t%d\t%d\t%d\t%d\n",a[i].name,a[i].bd.year,a[i].bd.month,a[i].bd.day,a[i].number);
return 0;
}
}
2.按等级统计学生成绩:
#include "stdio.h"
#include"string.h"
#define N 10
struct student{
int num;
char name[10];
int mark;
char grade;
};
int set_grade(int n,struct student a[])
{ int i,x=0;
for(i=0;i<n;i++)
{ if(a[i].mark>=85&&a[i].mark<=100)
{a[i].grade='A';
continue;
}
if(a[i].mark>=70&&a[i].mark<=84)
{a[i].grade='B';
continue;
}
if(a[i].mark>=60&&a[i].mark<=69)
{a[i].grade='C';
continue;
}
a[i].grade='D';x++;
}
return x;
}
void scanf_st(int n,struct student a[])
{int i;
for(i=0;i<n;i++)
{ printf("enter No. %d student:\n",i+1);
scanf("%d%s%d",&a[i].num,a[i].name,&a[i].mark);
}
}
void printf_st(int n,struct student a[])
{int i;
for(i=0;i<n;i++)
{ printf("enter No. %d student:\n",i+1);
printf("%d\t%s\t%d\t%c\n",a[i].num,a[i].name,a[i].mark,a[i].grade);
}
}
int main()
{ struct student st[N];
int count;
scanf_st(N,st);
count=set_grade(N,st);
printf_st(N,st);
printf("Not Pass num:%d\n",count);
return 0;
}
3.心得体会:
宏定义也可以使用与其他的并无差异
struct嵌套比较难,有的要加地址符号需注意,a[i].bd.year这些嵌套要注意
等级设置中&&可以应用
比较判断要层层比较判断