栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

学生管理系统-C语言

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学生管理系统-C语言

学生管理系统

对于学生的管理包含增删改查四个主要功能

学生管理系统有一个结构体学生,里面包含了学生的属性,目前就有name、age、No(学号),最多管理100个学生
已经解决乱输入会死循环的问题,同时只有输入正确的格式才能添加修改删除查看

#include 
#include 
#include 

int indenx=0;
int num;
char m[30];

//定义结构体
struct Student
{
    char name[30];
    int age;
    int No;
};

//显示系统功能
void show()
{
    printf("~~~~~~~~~~~~~~~~~n");
    printf("~~~1、增加学生~~~n");
    printf("~~~2、删除学生~~~n");
    printf("~~~3、修改学生~~~n");
    printf("~~~4、查找学生~~~n");
    printf("~~~5、查看学生~~~n");
    printf("~~~9、显示菜单~~~n");
    printf("~~~0、退出系统~~~n");
    printf("~~~~~~~~~~~~~~~~n");
}

//定义一个Student类型的数组,数组名为stu,大小为100
struct Student stu[100];

//释放缓存空间
void safe_flush(FILE *fp)
{
	int ch;
	while( (ch = fgetc(fp)) != EOF && ch != 'n' );          
}
//load 遍历数数组查找对应学号的人并将位置给pos
int pos=0;//用来接收学生所在的位置
int load(int n)
{
    for(int i = 0;i < 100;i++)
    {  
        if(stu[i].No==n)
        {
            pos = i;
            return 1;
        }
    }
    return 0;
}

//add添加学生
int add()
{
    int n;
    for(int i = 0;i < 100;i++)
    {
        if(stu[i].No==0)
        {
            printf("请输入学号:");
            scanf("%d",&n);
            
            //判断学生学号是否存在如果已经存在则使用while语句判断重新输入学号
            while(1)
            {
                int l=load(n);
                if(l==1)
            	{
                	printf("学号已经存在!");
                	printf("请重新输入学号:");
            		scanf("%d",&n);
            	}
                break;
            }
            //判断输入的学号是否有非法字符
            while(1)
    		{
        		int j=0;
        		for(;j<30;j++)
            	{
                    m[j]=getchar();
                    if(m[j]=='n')
                    {
                        m[j]='';
                        break;
                    }
            	}
            	if(j>0)
            	{
            		printf("输入错误的学号!");
            		safe_flush(stdin);
            		printf("请重新输入学生的学号:");
    				scanf("%d",&stu[i].age);
            	}
        		else
        		{
        		break;    
        		}
    		}

            stu[i].No=n;
            printf("请输入年龄:");
            scanf("%d",&stu[i].age);
            
            //判断输入的年龄是否有非法字符
            while(1)
    		{
        		int j=0;
        		for(;j<30;j++)
            	{
                    m[j]=getchar();
                    if(m[j]=='n')
                    {
                        m[j]='';
                        break;
                    }
            	}
            	if(j>0)
            	{
            		printf("输入错误的年龄!");
            		safe_flush(stdin);
            		printf("请重新输入学生的年龄:");
    				 scanf("%d",&stu[i].age);
            	}
        		else
        		{
        		break;    
        		}
    		}
            
            printf("请输入姓名:");
            scanf("%s",m);
			//将输入的字符串复制给stu[i].name,
            //stu[i].name相当于Student结构体里面的字符数组的数组名name
            strcpy(stu[i].name,m);  
            return 1;
        } 
        else if(i==99)
        {
            printf("系统满了,不能添加学生!");
        }
    }
    
     
     return 0;
}

//delete根据输入学生的学号删除学生信息
int delete()
{
    printf("请输入删除学生的学号:");
    scanf("%d",&num);
    
    while(1)
    {
        int j=0;
        for(;j<30;j++)
            {
                    m[j]=getchar();
                    if(m[j]=='n')
                    {
                        m[j]='';
                        break;
                    }
            }
            if(j>0)
            {
            printf("输入错误的学号!");
            safe_flush(stdin);
            printf("请重新输入删除学生的学号:");
    		scanf("%d",&num);
            }
        else
        {
        break;    
        }
    }
    
    int l = load(num);
    if(l==0)
    {
        printf("对不起,删除的学生不存在!");
        return 0;
    }
    
    else 
    {
        int i = l;
        for(int j=0;j<100-i;j++)
        {
                if(stu[pos+1].No==0)
                {
                    stu[pos].No=0;
            		stu[pos].age=0;
            		strcpy(stu[pos].name," ");
                    return 1;
                }
                stu[pos].No=stu[pos+1].No;
            	stu[pos].age=stu[pos+1].age;
            	strcpy(stu[pos].name,stu[pos+1].name);
                pos++;
          }
            return 1;
            
        }
}


//updata工具输入的学生学号、对学生进行修改
int updata()
{
    
    printf("请输入修改学生的学号:");
    scanf("%d",&num);
    
    while(1)
    {
        int j=0;
        for(;j<30;j++)
            {
                    m[j]=getchar();
                    if(m[j]=='n')
                    {
                        m[j]='';
                        break;
                    }
            }
            if(j>0)
            {
            printf("输入错误的学号!");
            safe_flush(stdin);
            printf("请重新输入修改学生的学号:");
    		scanf("%d",&num);
            }
        else
        {
        break;    
        }
    }
   
    int l = load(num);
    if(l==0)
    {
        printf("对不起,更新的学生不存在!");
        return 0;
            
    }
    else
    {
        int a=0;
        printf("1、修改年龄t");
    	printf("2、修改姓名n");
        scanf("%d",&a);
        switch(a)
        {
            case 1:
                printf("请输入年龄:");
            	scanf("%d",&stu[pos].age);
                
                 while(1)
    			{
        			int j=0;
        			for(;j<30;j++)
            		{
                    	m[j]=getchar();
                    	if(m[j]=='n')
                    	{
                        	m[j]='';
                        	break;
                    	}
            		}
            		if(j>0)
            		{
            			printf("输入错误的年龄!");
            			
            			printf("请重新输入学生的年龄:");
    				 	scanf("%d",&stu[pos].age);
            		}
        			else
        			{
        			break;    
        			}
    			}
                
                return 1;
           	case 2:
               	printf("请输入姓名:");
            	scanf("%s",m);
            	strcpy(stu[pos].name,m);
            	return 1;
         }
         return 0;
        }
}

//find函数工具输入的学号查找学生
int find()
{
    printf("请输入查找学生的学号:");
    scanf("%d",&num);
    
    while(1)
    {
        int j=0;
        for(;j<30;j++)
            {
                    m[j]=getchar();
                    if(m[j]=='n')
                    {
                        m[j]='';
                        break;
                    }
            }
            if(j>0)
            {
            printf("输入错误的学号!");
            printf("请重新输入查找学生的学号:");
    		scanf("%d",&num);
            }
        else
        {
         	int l = load(num);
    		if(l==0)
    		{
       			 printf("对不起,查找的学生不存在!");
        		 return 0;
            
    		}
    		else 
    		{
        		printf("学号:%d 姓名:%s 年龄:%d ",stu[pos].No,stu[pos].name,stu[pos].age);
        		return 1;
    		}    
        }
    }
}

//list函数遍历数组,输出数组中的数据
void list()
{
    for(int i = 0;i < 100;i++)
    {  
        if(stu[i].No==0)
        {
            break;
        }
        printf("学号:%d 姓名:%s 年龄:%d n",stu[i].No,stu[i].name,stu[i].age);
    }
}



int main()
{
    int c=10;
    show();
    while(1)
    { 
        
        printf("请输入操作:");
       	scanf("%d",&c);
       
        switch(c)
        {
            case 1:
                add();
                break;
            case 2:
                delete();
                break;
            case 3:
                updata();
                break;
            case 4:
                find();
                break;
            case 5:
                list();
                break;
            case 9:
                show();
                break;
            case 0:
                return 0;
            default:
                printf("请输入正确的操作!n");
        }
		safe_flush(stdin);//释放scanf的缓存空间
        c=10;//为了防止输入正常操作后,输入字符串重复正常操作,
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1033148.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号