定义学生基本信息
①输出所有学生信息
②删除某个学生后,输出所有学生信息
③修改某个学生信息后,输出所有学生信息
④查找某个学生的信息
main.c
#include"k11*.h"
int main(int argc, const char *argv[])
{struct student p[4]={{"可乐",17,88},{"雪碧",18,99},{"果粒",16,100},{"芬达",23,66}}; output_p(p,4);del_p(p,4);fix_p(p,2);find_p(p,2);return 0;
}
k11.c
#include"k11*.h"void output_p(struct student p[],int len)
{printf("---学生信息如下---\n\n");for(int i=0;i<len;i++){printf("第%d个学生: 姓名:%s 年龄:%d 成绩:%ld\n",i+1,(p+i)->name,(p+i)->age,(p+i)->sorce);}printf("----------------------------------------");putchar(10);
}
void del_p(struct student p[],int pos)
{if(pos<=4&&pos>0){for(int i=pos;i<4;i++){strcpy((p+i-1)->name,(p+i)->name);(p+i-1)->age=(p+i)->age;(p+i-1)->sorce=(p+i)->sorce;}printf("删除第%d个学生后:\n",pos);output_p(p,pos-1);}else if(pos==4){printf("删除第%d个学生后:\n",pos);output_p(p,3);}elseprintf("删除有误\n");
}void fix_p(struct student p[],int nub)
{if(nub<=3&&nub>0){printf("修改的学生姓名:");char str[32];gets(str);strcpy((p+nub-1)->name,str);printf("修改的学生年龄:");scanf("%d",&(p+nub-1)->age);printf("修改的学生成绩:");//getchar();scanf("%ld",&(p+nub-1)->sorce);putchar(10);printf("修改第%d个学生后:\n",nub);output_p(p,3);}elseprintf("修改有误\n");
}void find_p(struct student p[],int n)
{if(n<=3&&n>0){printf("第%d个学生信息为: 姓名:%s 年龄:%d 成绩:%ld\n",n,(p+n-1)->name,(p+n-1)->age,(p+n-1)->sorce);putchar(10);}
}
k11.h
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{char name[32];int age;long sorce;
};void output_p(struct student p[],int len);void del_p(struct student p[],int pos);void fix_p(struct student p[],int nub);void find_p(struct student p[],int n);
结果: