结构体例题汇总

来源:互联网 发布:大数据分析系统翻译 编辑:程序博客网 时间:2024/04/30 10:45

例一://///结构体变量初始化和引用

#include<stdio.h>

int main()

{

         structStudent

         {

                   longintnum;

                   charname[20];

                   charsex;

                   charaddr[20];

         }a={10010,"LiMing",'M',"beijing"};//定义结构体变量并且初始化

         printf("NO.:%ld\nname:%s\nsex:%c\naddr:%s\n",a.num,a.name,a.sex,a.addr);

         return0 ;

}

 

例二:

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<stdlib.h>

int main()

{

         //声明结构体类型

         structStudent

         {

                   longintnum;

                   charname[20];

                   floatscore;

         }student1,student2;   

         //输入两个学生的信息

         printf("请输入第1个学生的学号、姓名和成绩:\n");

         scanf("%ld%s%f",&student1.num,student1.name,&student1.score);

         printf("请输入第2个学生的学号、姓名和成绩:\n");

         scanf("%ld%s%f",&student2.num,student2.name,&student2.score);

         printf("较高成绩学生的信息:\n");

         if(student1.score> student2.score )

         {

                   printf("%ld  %s %5.1f\n",student1.num,student1.name,student1.score);

                   exit(0);

         }

         else

         {

                   if(student1.score== student2.score)

                   {

                            printf("%ld  %s %5.1f\n",student1.num,student1.name,student1.score);

                            printf("%ld  %s %5.1f\n",student2.num,student2.name,student2.score);

                            exit(1);

                   }

                   else

                   {

                            printf("%ld\n%s\n%5.1f\n",student2.num,student2.name,student2.score);

                            exit(2);

                   }

         }

         return0 ;

}

例三:

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<string.h>

int main()

{

         //声明并定义结构体数组,初始化结构体数组

         structStudent

         {

                   charname[20];

                   intcount;

         }leader[3]={{"li",0},{"hu",0},{"zhang",0}};

 

         charleadname[20]={0};

         inti,j ;

         for(i= 0 ; i< 5;i++)

         {

                   scanf("%s",leadname);

                   for(j=0 ; j < 3; j++)

                   {

                            if(strcmp(leadname,leader[j].name)== 0 )

                                     leader[j].count++;

                  }

         }

         for(j= 0 ; j< 3;j++)

         printf("%s:\n%d\n",leader[j].name,leader[j].count);

         return0 ;

}

 

例四:

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<string.h>

#define LEN 3

int main()

{

         inti= 0 ;

         intj = 0 ;

         structStudent

         {

                   longintnum;

                   charname[20];

                   floatscore;

         }stu[LEN];

 

         for(i= 1 ; i< LEN+1 ; i ++)

         {

                   printf("请输入第 %d 个学生的信息(学号、姓名、成绩):\n",i);

                   scanf("%ld%s%f",&stu[i-1].num,stu[i-1].name,&stu[i-1].score);

         }

         printf("对以上学生的成绩进行排序并按照从高到低进行输出:\n");

 

         structStudent temp;

         for(i=0;i< LEN-1 ; i++)

                   for(j=i+1;j < LEN ; j++)

                   {

                            if(stu[i].score<stu[j].score)

                            {

                                     temp= stu[j] ;

                                     stu[j]= stu[i] ;

                                     stu[i]= temp ;

                            }

                   }

                   //排序后进行输出

         printf("theorder is : \n");

         for(i=0;i< LEN ; i++)

                   printf("%d  %s %f\n",stu[i].num,stu[i].name,stu[i].score);

         return0 ;

}

例五:

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<string.h>

int main()

{

         //声明结构体

         structStudent

         {

                   longintnum;

                   charname[20];

                   charsex ;

                   floatscore;

         };

         structStudent stu;

         structStudent *p ;

         p= &stu ;

         //对结构体变量赋值

         printf("请输入该学生的信息(学号、姓名、性别和成绩):\n");

         scanf("%ld%s %c %f",&stu.num,stu.name,&stu.sex,&stu.score);  //输入时要有空格

         //输出结果

         printf("NO:%ld\nname:%s\nsex:%c\nscore:%f\n",stu.num,stu.name,stu.sex,stu.score);

         printf("NO:%ld\nname:%s\nsex:%c\nscore:%f\n",(*p).num,(*p).name,(*p).sex,(*p).score);

         printf("NO:%ld\nname:%s\nsex:%c\nscore:%f\n",p->num,p->name,p->sex,p->score);

 

         return0 ;

}

 

例六://结构体数组实现学生信息的输入和输出

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#define N 3

int main()

{

         //声明和定义一个结构体数组

         structStudent

         {

                   longintnum;

                   charname[20];

                   charsex;

                   floatscore;

         }stu[3];

         structStudent *p ;

         p= stu ;

         //结构体数组初始化

         inti;

         for(i= 0 ; i< N ; i++ )

         {

                   printf("请输入第 %d 个学生的信息(学号、姓名、性别和成绩):\n",i+1);

                   scanf("%ld%s %c %f",&stu[i].num,stu[i].name,&stu[i].sex,&stu[i].score);

         }

         //用结构体数组指针输出学生的信息

         for(i= 0 ; i< N ; i++)

         {

                   printf("NO:%ld\nname:%s\nsex:%c\nscore:%f\n",(*(p+i)).num,(*(p+i)).name,(*(p+i)).sex,(*(p+i)).score);

                   printf("NO:%ld\nname:%s\nsex:%c\nscore:%f\n",(p+i)->num,(p+i)->name,(p+i)->sex,(p+i)->score);

         }

         return0 ;

}

 

例七:输出N个学生中三门课程成绩平均分数最高的学生信息

#define _CRT_SECURE_NO_WARNINGS

#define N 3

#include<stdio.h>

//声明结构体数组

struct Student  

         {

                   longintnum;

                   charname[20];

                   floatscore[3];

                   doubleaver;

         }stu[N];

int main()

{

         voidinput(struct Student studd[]);  //函数声明

         structStudent max(struct Student stu[]);//函数声明

         voidprint(struct Student stu);

         structStudent *p ;

         p= stu ;          //定义一个结构体指针,指向结构体数组

         //structStudent temp ;

         input(p);

        

         print(max(p));

         return0 ;

}

void input(struct Student studd[])

{

         inti= 0 ;

         for(i= 0 ; i< N ; i++)

         {

                   printf("请输入第 %d 个学生的信息(学号、姓名和三门课的成绩):\n" ,i+1);

                   scanf("%ld%s %f %f%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);

                   stu[i].aver= (stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;

         }

}

 

struct Student max(struct Student stu[])

{

         intm = 0 ;

         inti= 0 ;

         for(i= 0 ; i< N ; i++)

         {

                   if(stu[i].aver>stu[m].aver)

                            m= i ;

         }

         returnstu[m];

}

void print(struct Student stud)

{

         printf("成绩最高的学生是:\n");

         printf("学号:%ld\n姓名:%s\n三门课的成绩:%8.2f%8.2f%8.2f\n平均成绩:%8.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver);

}

 

 

例八:建立简单的静态链表

#include<stdio.h>

struct Student

{

         longintnum;

         doublescore ;

         structStudent *next ;

};

int main()

{

         structStudent a,b,c ;

         structStudent *head , *p;

         a.num= 10010;

         a.score= 90.9;

         b.num= 10020;

         b.score= 94.9;

         c.num= 10030;

         c.score= 92.9;

         head= &a ;

         a.next= &b;

         b.next= &c;

         c.next= NULL;

 

         while(head != NULL )

         {

                   printf("%ld%f\n",head->num,head->score);

                   head= head->next;

         }

         return0 ;

}

 

 

例九:建立动态链表(实现N个学生信息的输入与输出

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<stdlib.h>

#define LEN sizeof(struct Student)

声明结构体类型

struct Student

{

         longintnum;

         charname[20];

         doublescore;

         structStudent *next ;

};

//建立链表

struct Student *creat()

{

         structStudent *head,*p1,*p2;

         intn = 0 ;  //学生的个数变量

         intcount = 0 ;

         p1= (struct Student *)malloc(LEN); //开辟一个新单元,并且强制转换成结构体指针类型

         p2= p1;//p1和p2同时指向一个新单元

         head= NULL ;//先使 head 指针为NULL(即为0)

         printf("请输入第 %d 个学生的信息(学号、姓名和成绩):\n",n+1);

         scanf("%ld%s %lf",&p1->num,p1->name,&p1->score);

         while((p1->num)!= 0 )

         {

                   n= n+1;

                   if(n== 1)

                            head= p1 ;

                   else

                            p2->next= p1 ;

                   p2= p1 ;

                   p1= (struct Student *)malloc(LEN);

                   count++;

                   printf("请输入第 %d 个学生的信息(学号、姓名和成绩):\n",n+1);

                   scanf("%ld%s %lf",&p1->num,p1->name,&p1->score);

         }

         p2->next= NULL ;

         return(head);

}

//输出链表

void print(struct Student *head)

{

         structStudent *p;

         p= head;

         if(head!= NULL )

         {

                   printf("学号:姓名:成绩:\n");

                   do

                   {

                            printf("%5ld%15s%15.1lf\n",p->num,p->name,p->score);

                            p= p->next;

                   }while(p!= NULL );

         }

 

}

 

int main()

{

         structStudent *head;

         head= creat(); //调用creat函数,返回第1个结点的起始地址

         print(head);

         return0 ;

}

0 0