练习1.教务系统设计

设计要求:

1.可录入学生信息,包含(姓名、学号、数学成绩、语文成绩);

2.输入学生姓名,输出该生相关信息;

3.排序,按照数学成绩或者语文成绩排序,排序依据由用户指定;

4.更改学生信息;

5.输出所有记录;

6.退出;


以下附本人自己写的代码,程序不够精简,尚可完善,还望赐教。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int num = 0;
struct student
{
    char name[10];
    int id;
    int chinese;
    int math;
};

typedef struct student STU;

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

void SearchInfo(STU *s[])
{
    char tar_name[10] = {0};
    int j;
    printf("please input the target name:\n");
    scanf("%s", tar_name);
    for(j = 0;j < num;j++)
    {
	if(strcmp(s[j]->name, tar_name) == 0)
	{
		printf("The Information of the student is:\n");
		printf("%s %d %d %d\n", s[j]->name, s[j]->id, s[j]->chinese, s[j]->math);
               break;
	}
     }
     if(j == num)
     {
	printf("Not find!!\n");
      }
}

void SortInfo(STU *s[])
{
     printf("************************************\n\n"); 
     printf("******please choose the sort by:****\n\n");
     printf("****1.chinese             2.Math****\n\n");
     printf("************************************\n\n");
     int choice = 0;
     int i, j, k;
     STU *tmp;
     scanf("%d", &choice);
     if(1 == choice)
     {
	for(i = 0;i < num;i++)
	{
		for(j = i + 1;j < num ;j++)
		{
                    if(s[i]->chinese > s[j]->chinese)
		     {
			tmp = s[j];
			s[j] = s[i];
                       s[i] = tmp;
		      }
		 }
	  }
	  for(k = 0;k < num;k++)
	  {
		printf("%s %d %d %d\n", s[k]->name, s[k]->id, s[k]->chinese,s[k]->math);
	   }
        }
	if(2 == choice)
	{
	    for(i = 0;i < num ;i++)
	    {
		for(j = i + 1;j < num ;j++)
		{
		     if(s[i]->math > s[j]->math)
		     {
			tmp = s[j];
			s[j] = s[i];
                       s[i] = tmp;
		      }
		 }
	      }
	
	     for(k = 0;k < num;k++)
	     {
		printf("%s %d %d %d\n", s[k]->name, s[k]->id, s[k]->chinese,s[k]->math);
	     }
	}

}
void ShowInfo(STU *s[])
{
    int i;
    for(i = 0;i < num;i++)
    {
	printf("%s %d %d %d\n", s[i]->name, s[i]->id, s[i]->chinese, s[i]->math);
     }
}
void ChangeInfo(STU *s[])
{
    char tar_name[10] = {0};
    int i = 0;
    printf("**********BEFORE  CHANGE*********\n");
    ShowInfo(s);
    printf("*********************************\n");
    printf("Please input the student you want to change:\n");
    scanf("%s",tar_name);
    printf("Please input NEW Information:\n");
    while(s[i] != NULL)
    {
	if(strcmp(s[i]->name, tar_name) == 0)
	{
	    scanf("%s",s[i]->name);
	    scanf("%d",&s[i]->id);
	    scanf("%d",&s[i]->chinese);
	    scanf("%d",&s[i]->math);
	    printf("Change Success!\n");
	    break;
	 }
	i++;
     }
     printf("~~~~~~~~~~AFTER  CHANGE~~~~~~~~~~\n");
     ShowInfo(s);
     printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
void show()
{
    printf("************************************\n\n");
    printf("*****WELCOME TO TEACHER SYSTEM******\n\n");
    printf("************************************\n\n");
    sleep(1);
    system("clear");
}
void PrintInfo()
{
     printf("************************************\n");
     printf("***1.InsertInfo         2.SearchInfo\n");
     printf("***3.SortInfo           4.ChangeInfo\n");
     printf("***5.ShowInfo           6.Quit      \n");
     printf("************************************\n");
     printf("Please input your choice:\n");
}
int main()
{
    STU *stu[100] = {0};
    char choice[10];
    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);
	     default:
         	printf("Unkown choice!\n");
		break;
	}
    }
    return 0;
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值