C语言学习之结构体.day2

结构体的内容:

1.定义和引用结构体  数组和指针

2.union共用体

3.枚举类型 如enum a={1,2,3};

4.用typedef 声明新类型名

1.建立一个简单链表,它由3个学生数据的结点组成,要求输出各结点中的数据。

(理解节点是什么    理解节点中内存是怎么分配的和节点之间怎么形成链表的)

#include <stdio.h>
struct student//声明结构体类型struct student
{
   int num;//学号
   float score;//成绩
   struct student *next;//next为指针,包含地址信息
};
int main()
{
   struct student a={10101,90.0},b={10102,91.0},c={10103,92.0},*head,*p;
   //定义3个结构体变量作为链表的节点 head,p为指向 struct student类型 的指针变量
   head=&a;//头指针指向a的地址
   a.next=&b;
   b.next=&c;
   c.next=NULL;//c指针指向空
   p=head;//p指向头指针指向的地址

   printf("struct student 结构体占%d字节\n",sizeof(struct student));
   printf("struct student类型节点内存分配如下:\n");
   printf("\nnum占%d字节\nscore占%d字节\n*next指针占%d字节\n",
          sizeof(int),sizeof(float),sizeof(struct student *));
   printf("a,b,c节点的首地址分别为:\n%d\n%d\n%d\n",&a,&b,&c);//打印
   printf("p指针指向地址为:%d\nhead指针指向地址为:%d\n",p,head);
   printf("a,b,c三个节点中的数据如下:\n");
   for(p=head;p!=NULL;p=p->next)//更新p指向 指向地址不为NULL就执行
      printf("%d %5.1f%12d\n",p->num,p->score,p->next);//输出p指针指向的节点的数据
   return 0;
}

2.通过函数,建立一个有3名学生数据的单向动态链表并输出链表。

#include <stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)//定义宏
struct Student//声明结构体类型
{
   long num;
   float score;
   struct student *next;
};
int n=0;//全局变量
int main()
{  struct Student *head;
   struct Student *creat();//函数声明
   void print(struct Student *head);//函数声明
   head=creat();//函数调用,返回第一个节点的起始地址
   print(head);//函数调用
   return 0;
}

struct Student *creat()//函数定义,返回值为一个指向链表头的指针
{
   struct Student *head;//头指针指向链表首地址
   struct Student *p1,*p2;
   p2=p1=(struct Student *)malloc(LEN);
   //开辟一个新单元,p1,p2指针指向首地址
   scanf("%ld,%f",&p1->num,&p1->score);
   //输入第一个学生学号和成绩
   head=NULL;
   while(p1->num!=0)//p1指向的节点num不为0
   {
      n=n+1;
      if(n==1)head=p1;//头指针指向p1指向的地址
      else p2->next=p1;//第一个节点的指针 指向p1
      p2=p1;//重新使得  p2指向p1
      p1=(struct Student *)malloc(LEN);
      //申请内存空间
      scanf("%ld,%f",&p1->num,&p1->score);
      //输入其他学生学号和成绩
   }
   p2->next=NULL;
   return (head);
}
void print(struct Student *head)//输出链表
{
   struct Student *p;
   printf("\nNow,These %d records are:\n",n);
   p=head;
   for(;p!=NULL;p=(*p).next)
   printf("%ld %5.1f\n",p->num,p->score);
   //p指针不为空,打印节点中的学号,成绩
}

3.有若干个人员的数据,其中有学生和教师。学生的数据中包括: 姓名、号码、性别、职业、班级。教师的数据包括: 姓名、号码、性别、职业、职务。要求用同一个表格来处理。

思路:

循环n次

读入号码、姓名、性别、职业

读入class

读入position

输出“输入错”

循环n次

输出:号码、姓名、性别、职业、班级

输出:号码、姓名、性别、职业、职务

#include <stdio.h>
#include<stdlib.h>
struct						//声明无名结构体类型
{	int num;					//成员num(编号)
	char name[10];			//成员name(姓名)
	char sex;				//成员sex(性别)
	char job;				//成员job(职业)

	union					   //声明无名共用体类型
	{	int clas;	   	//成员clas(班级)
		char position[10];	//成员position(职务)
	}category;				//成员category是共用体变量

}person[2];					//定义结构体数组person,有两个元素

int main()
{  int i;
   for(i=0;i<2;i++)
   { 
     printf("please enter the data of person:\n");
     scanf("%d %s %c %c",&person[i].num,person[i].name,
           &person[i].sex,&person[i].job);
           
     if(person[i].job=='s') //如果是学生,输入班级
      scanf("%d",&person[i].category.clas);
     else if(person[i].job=='t')//如果是老师,输入职务
      scanf("%s",person[i].category.position);
     else
      printf("Input error!");
   }
   
   printf("\n");
   printf("No.name sex job class/position\n");
   for(i=0;i<2;i++)
   {
     if (person[i].job=='s')			//若是学生
			printf("%-6d%-10s%-4c%-4c%-10d\n",
         person[i].num,person[i].name,person[i].sex,
         person[i].job,person[i].category.clas);
		else									//若是教师
			printf("%-6d%-10s%-4c%-4c%-10s\n",
          person[i].num, person[i].name,person[i].sex,
          person[i].job,person[i].category.position);
   }
   return 0;
}

4.口袋中有红、黄、蓝、白、黑5种颜色的球若干个。每次从口袋中先后取出3个球,问得到3种不同颜色的球的可能取法,输出每种排列的情况(使用枚举类型)。

#include <stdio.h>
#include<stdlib.h>

int main()
{
   enum Color{red,yellow,blue,white,black};//声明枚举类型
   enum Color i,j,k,pri;//枚举类型变量
   int n,loop;
   for(i=red;i<=black;i++)                    //外循环
      for(j=red;j<=black;j++)                 //中循环
        if(i!=j)                             //两球不同色
        {
           for(k=red;k<=black;k++)           //内循环
           {
            if((k!=i)&&(k!=j))               //三球不同色
           {
              n=n+1;
              printf("%-4d",n);             //符合条件次数加1
              for(loop=1;loop<=3;loop++)    //先后对3个球处理
              {
                 switch(loop)
                 {
                    case 1:pri=i;break;    //第一个球的颜色
                    case 2:pri=j;break;    //第二个球的颜色
                    case 3:pri=k;break;    //第三个球的颜色
                    default:break;
                 }
                 switch(pri)              //根据球的颜色输出相应文字
                 {
                    case red:printf("%-10s","red");break;
                    case yellow:printf("%-10s","yellow");break;
                    case blue:printf("%-10s","blue");break;
                    case white:printf("%-10s","white");break;
                    case black:printf("%-10s","black");break;
                    default:break;
                 }
              }
              printf("\n");

           }
          }
        }
        printf("\ntotal:%5d\n",n);
   return 0;
}

#include"stdio.h" #include"stdlib.h" #define NULL 0 struct student { long num; char name[20]; int score[6]; struct student *next; }; void show() { printf("\nthere is a cataloge as follow.\n"); printf("***************************************\n"); printf("* *\n"); printf("* 1. create *\n"); printf("* 2. Insert *\n"); printf("* 3. print *\n"); printf("* 4. delete *\n"); printf("* 5. modify *\n"); printf("* 6. save_to_file *\n"); printf("* 7. lode from file *\n"); printf("* 8. exit *\n"); printf("***************************************\n"); printf("please input 1--8 to choice what you want:\n"); } struct student *create() { struct student *head,*p,*last; int i; int temp2; char name[20]; p=head=(struct student *)malloc(sizeof(struct student)); head->next=NULL; last=p; while(1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); /*last->next=p; last=p;*/ p->next=NULL; printf("number:"); scanf("%ld",&p->num); if(p->num==0)break; getchar(); printf("name:"); scanf("%s",p->name); printf("score:"); for(i=0;iscore[i]=temp2; } printf("next student's information.\n"); } free(p); return head; } void Insert(struct student *head) { struct student *p,*q; int score; long num; int i; printf("\nEnter the student's information you want to insert.\n"); printf("number:"); scanf("%ld",&num); q->num=num; getchar(); printf("name:"); scanf("%s",q->name); printf("score:(chinese,math,english,biology,physics,chemistry)\n"); for(i=0;iscore[i]=score; } q->next=NULL; p=head; while(p->next->numnum&&p->next!=NULL) p=p->next; q->next=p->next; p->next=q; if(p->next==NULL) p->next=q; } void delete(struct student *head) { struct student *p,*q; long num; printf("enter the student's information you want to delete.\n"); printf("number:"); scanf("%ld",&num); getchar(); p=head; while(p->next!=NULL&&p->next->num!=num) p=p->next; q=p->next; p->next=p->next->next; free(q); } void print(struct student *head) { struct student *p; int i; p=head->next; if(p==NULL) { printf("\nthere is no information.\n"); exit(0); } printf("\nnumber\tnamme\tchinese\tmath\tenglish\tbiology\tphysics\tchemistry\n"); while(p!=NULL) { printf("\n%ld\t%s",p->num,p->name); for(i=0;iscore[i]); p=p->next; } } void modify(struct student *head) { struct student *p; int choice,i; long num; char name[20]; int score[6]; printf("\nEnter what student's information you want to modify.\n"); printf("number:"); scanf("%ld",&num); getchar(); printf("\nname:"); scanf("%s",name); printf("\n"); p=head->next; while(p->num!=num&&p->name[20]!=name[20]&&p!=NULL) p=p->next; printf("\nplease choice what you want to modify:1-number 2-name 3-score.\n"); scanf("%d",&choice); switch(choice) { case 1:printf("\nEnter the true number:"); scanf("%ld",&num); p->num=num; break; case 2:printf("\nEnter the true name:"); scanf("%s",p->name); break; case 3:printf("\nEnter the right score:"); for(i=0;iscore[i]=score[i]; } break; } } void save_in(struct student *head) { struct student *p; FILE *fp; char file_name[30]; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"w"))==NULL) { printf("can't open the file.\n"); exit(0); } p=head; while(p->next!=NULL) { fwrite((void*)p->next,sizeof(struct student),1,fp); p=p->next; } fclose(fp); } struct student *load_from_file() { struct student *head,*p,*last; FILE *fp; char file_name[30]; head=(struct student *)malloc(sizeof(struct student)); last=head; head->next=NULL; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"r"))==NULL) { printf("can't open the file.\n"); exit(0); } p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; while(fp=fread((void *)p,sizeof(struct student),1,fp)==1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; } free(p); fclose(fp); return head; } void main() { struct student *la; int choice; /*char Yes_No;*/ la=(struct student *)malloc(sizeof(struct student)); la->next=NULL; while(1) { show(); scanf("%d",&choice); switch(choice) { case 1:la=create(); break; case 2:Insert(la); break; case 3:print(la); break; case 4:delete(la); break; case 5:modify(la); break; case 6:save_in(la); break; case 7:la=load_from_file(); break; case 8:exit(0); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值