C语言(结构体,共用体,教务系统项目的编写)

一、结构体

                struct    结构体名   {成员表列};

              (1)不能将一个结构体变量作为一个整体进行输入和输出。

      (2)可以引用结构体变量成员的地址,也可以引用结构体变量的地址。

                每个成员起始地址都是该成员类型所占长度的倍数;

                结构体的总长度是最长成员类型的倍数;

        二、结构体数组

                数组中的元素均为struct student 类型数据

                结构体成员的表现形式

                结构体变量 点  成员名

                (*p).成员名   p->成员名

        三、malloc函数: void *malloc(unsigned int size);
                其作用:是在内存的动态存储区中分配一个长度为size的连续空间。

                calloc函数 :void *calloc(unsigned n,unsigned size);

               作用:在内存的动态存储区中分配n个长度为size的连续空间。

        四、共用体
                使几个不同的变量共占同一段内存的结构称为 “共用体”类型的结构。
                定义共用体类型变量的一般形式为:
                union 共用体名
             {          
                   成员表列
               }变量表列;

                共用体变量所占的内存长度等于最长的成员的长度

        五、枚举enum    将变量的值一一列举出来,变量的值只限于列举出来的值的范围内

        六、用typedef 定义类型的方法

               (1)先按定义变量的方法写出定义体

               (2)将变量名换成新类型名

               (3)在最前面加typedef  ,然后用新类型名去定义变量

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

struct student
{
char name[10];
int id;
int math;
int china;
};
typedef struct student STU;


void show()

{
system("clear");
printf("************************************\n\n");
printf("*****WELCOME TO TEACHER SYSTEM******\n\n");
printf("************************************\n\n");
sleep(2);
system("clear");
}


void PrintInfo()
{
printf("*************************************\n\n");
printf("**1.InserInfo          2.SearchInfo**\n");
printf("**3.SortInfo           4.ChangeInfo**\n");
printf("**5.quit               6.ShowInfo  **\n");
printf("*************************************\n\n");
printf("Please input your choice :\n");
}

void InsertInfo(STU *s[])
{
int i = 0;

        printf("Please Input Information :\n");
while(1)
{
s[i] = (STU *)malloc(sizeof(STU));
if (NULL == s[i])
{
return;
}
scanf("%s", s[i]->name);
if (strcmp(s[i]->name, "end") == 0)
{
break;
}
scanf("%d%d%d", &s[i]->id, &s[i]->math, &s[i]->china);
i++;
}


}

void SearchInfo(STU *s[])
{
int i = 0;
char target[10] = {0};

        printf("Please input name you will find :\n");
scanf("%s", target);

        while(s[i] != NULL)
{
if(strcmp(s[i]->name, target) == 0)
{
printf("**************\n");
printf("name : %s\n", s[i]->name);
printf("id   : %d\n", s[i]->id);
printf("math : %d\n", s[i]->math);
printf("china: %d\n", s[i]->china);
printf("**************\n");
return;
}
i++;
}
printf("**************\n");
printf("Not Fount!!\n");
printf("**************\n");

}


void SortInfo(STU *s[])
{
  int m = 0;
int i,j;
STU *temp;
while(s[m] != NULL)
{
m++;
}
for(i = 0; i < m-1-1; i++)
{
for(j = 0; j < m-1-i-1; j++)
{
if(s[j]->math > s[j+1]->math)
{
temp =s[j];
s[j] = s[j+1];
   s[j+1] = temp;
}
}
}
for(i = 1; i < m; i++)
{
printf("%s %d %d %d\n",s[i-1]->name,s[i-1]->id,s[i-1]->math,s[i-1]->china);
}


}

void ChangeInfo(STU *s[])
{
char target[10] = {0};
int i= 0;


printf("Please Input The Name You Want Change :\n");
scanf("%s", target);

        printf("Input New Infor :\n");
        while(s[i] != NULL)
{
if(strcmp(s[i]->name, target) == 0)
{
scanf("%s", s[i]->name);
scanf("%d", &s[i]->id);
scanf("%d", &s[i]->math);
scanf("%d", &s[i]->china);
printf("Change Success!\n");
return;
}
i++;
}



}


void ShowInfo(STU *s[])
{
int i = 1;
while(s[i]!=NULL)
{

printf("%s %d %d %d\n",s[i-1]->name,s[i-1]->id,s[i-1]->math,s[i-1]->china);
i++;
}


}


int main()
{
STU *stu[100] = {0};
char choice[10] = {0};


show();


while (1)
{
PrintInfo();
scanf("%s", choice);


switch (atoi(&choice[0]))
{
case 1:
InsertInfo(stu);
break;
case 2:
SearchInfo(stu);
break;
case 3:
SortInfo(stu);
break;
case 4:
ChangeInfo(stu);
break;
case 5:
exit(1);

                               break;
case 6:
                         default:
printf("Unkown Input!\n");

break;
}
}
return 0;
}

此教务系统小程序通过结构体类型定义指针数组来实现,然后用switch语句进行选择,要注意的是定义的指针数组中的元素是野指针所以需要分配空间;

而且传入的实参也是指针的指针所以形参定义也要小心;除此之外,在开始输入信息时要要用end表明输入结束,用strcmp函数实现,因为end也是输入

信息,所以在下面的功能中要是end不在次循环内。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值