系统代码实现如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
struct student
{
int age;
char name[50];
int id;
};
void add_stu(struct student arr[],int *count,int max);
void show_stu(struct student arr[],int count);
void del_stu(struct student arr[],int *count);
void find_stu(struct student arr[],int count);
void mod_stu(struct student arr[],int count);
int main(int argc, const char *argv[])
{
int count = 0;
struct student arr[MAX];
add_stu(arr,&count,MAX);
add_stu(arr,&count,MAX);
add_stu(arr,&count,MAX);
add_stu(arr,&count,MAX);
show_stu(arr,count);
del_stu(arr,&count);
show_stu(arr,count);
find_stu(arr,count);
mod_stu(arr,count);
show_stu(arr,count);
return 0;
}
void add_stu(struct student arr[],int *count,int max)
{
if(*count >= max)
{
printf("已达到学生数量的上限\n");
return;
}
printf("请输入学生姓名:\n");
scanf("%s",arr[*count].name);
printf("请输入学生的年龄:\n");
scanf("%d",&(arr[*count].age));
printf("请输入学生的学号\n");
scanf("%d",&(arr[*count].id));
(*count)++;
}
void show_stu(struct student arr[],int count)
{
printf("学生信息打印如下\n");
for(int i=0;i<count;i++)
{
printf("姓名:%s 年龄:%d 学号:%d\n",arr[i].name,arr[i].age,arr[i].id);
}
}
void del_stu(struct student arr[],int *count){
printf("请输入要删除学生的学号:\n");
int del_id;
scanf("%d",&del_id);
for(int i=0;i<(*count);i++){
if(arr[i].id==del_id){
(*count)--;
for(int j=i;j<(*count);j++)
{
arr[j]=arr[j+1];
}
break;
}
}
}
void find_stu(struct student arr[],int count){
int stu_id;
printf("请输入你要查找学生的位数:\n");
scanf("%d",&stu_id);
if(stu_id<0 || stu_id>count){
printf("输入位置无效,请重新输入\n");
return;
}
printf("该学生的姓名为:%s ",arr[(stu_id-1)].name);
printf("年龄:%d ",arr[(stu_id-1)].age);
printf("学号:%d\n",arr[(stu_id-1)].id);
}
void mod_stu(struct student arr[],int count){
int stu_id;
printf("请输入你要修改学生的位数:\n");
scanf("%d",&stu_id);
if(stu_id<0 || stu_id>count){
printf("输入位置无效,请重新输入\n");
return;
}
printf("请输入学生修改后的姓名:\n");
scanf("%s",arr[(stu_id-1)].name);
printf("请输入学生修改后的年龄:\n");
scanf("%d",&(arr[(stu_id-1)].age));
printf("请输入修改后学生的学号\n");
scanf("%d",&(arr[(stu_id-1)].id));
}
运行结果如下: