结构体指针数组完成输入学生姓名学号数学语文成绩修改排序显示等功能

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


int num=0;
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.ShowInfo           6.quit      **\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++;
        num++;
}


}

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 i,j;
    STU *tmp;
    printf("*****************************\n");
    printf("*****1.math          2.china*\n");
    printf("*****************************\n");
    int a;
    scanf("%d",&a);
    if(a==1)
    {
        for(i=0;i<num;i++)
        {
            for(j=0;j<num-i-1;j++)
            {
                if((s[j]->math)<(s[j+1]->math))
                {
                    tmp=s[j];
                    s[j]=s[j+1];
                    s[j+1]=tmp;
                 }
             }
         }
    }
    if(a==2)
    {
        for(i=0;i<num;i++)
        {
            for(j=0;j<num-i-1;j++)
            {
                if((s[j]->china)<(s[j]->china))
                {
                    tmp=s[j];
                    s[j]=s[j+1];
                    s[j+1]=tmp;
                }
            }
        }


    }
}


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[])
{
    printf("Show all information:\n");
    int i=0;
    for(i=0;i<num;i++)
    {
        printf("%10s%4d%4d%4d\n",s[i]->name,s[i]->id,s[i]->math,s[i]->china);
    }
}


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:
                ShowInfo(stu);
                break;
case 6:
exit(1);
break;
default:
printf("Unkown Input!\n");
break;
}
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值