拿下c语言结构体

目录

1.定义结构

  定义嵌套结构

2.结构变量的定义

3. 结构变量的初始化

4.访问结构成员

练习访问结构体成员

5.结构体数组和结构体指针

指向结构的指针

结构体指针使用实例

练习使用结构体数组和结构体指针

6.进阶的结构体问题

7.c语言大作业 学生成绩管理系统


在c语言中 数组允许定义可存储相同类型数据项的变量,结构是 C 编程中另一种用户自定义的可用的数据类型,它允许存储不同类型的数据项。

结构体,也可简称为结构,是一种由若干成员组成的自定义数据类型。 每一个成员可以是不同的数据类型(基本数据类型或构造数据类型)。 结构类型必须先定义,然后才能使用。

结构用于表示一条记录,假设想要获得图书馆中书本的信息,可能需要跟踪每本书的下列属性:

  • Title
  • Author
  • Subject
  • Book ID

1.定义结构

为了定义结构,须使用 struct 语句。struct 语句定义了一个包含多个成员的新的数据类型,struct 语句的格式如下:

struct tag { member-list member-list member-list ... } variable-list ;

tag 是结构体标签。

member-list 是标准的变量定义,比如 int i; 或者 float f,或者其他有效的变量定义。

variable-list 结构体变量,定义在结构的末尾,最后一个分号之前,您可以指定一个或多个结构变量。下面是声明 Book 结构的方式:

struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book;

一个描述学生信息的结构类型可定义如下:

struct {   int num;               //学号   

              char name[20];     //姓名   

              char sex;               //性别,F 或 M  

              int age;               //年龄   

              float score[4];     //成绩,4门课   

              char addr[30];    //住址 

};


  定义嵌套结构

//定义结构类型date     

struct date     {      

  int month;       

  int day;         

int year;     };        

//定义嵌套结构类型 

student     struct student     {        

int num;         

char name[20];        

char sex;        

struct date birthday; //成员为结构变量         

float score; 

  };

2.结构变量的定义

定义结构变量有3种方法。

(1)直接定义结构变量:   

 struct{成员列表}变量名列表;

struct
    {
        int num;
        char name[20];
        char sex;
        float score;
    }s1,s2;

(2)先定义命名结构,再定义结构变量。 struct 结构名 {成员列表}; struct 结构名  变量名列表; 

//定义结构
    struct student
    {
        int num;
        char name[20];
        char sex;
        float score;
    };

    //定义变量
    struct student st1,st2;

(3)在定义结构类型的同时定义结构变量。   

 struct 结构名 {成员列表}变量名列表;

struct student
{
        int num;
        char name[20];
        char sex;
        float score;
} stu1,stu2;

3. 结构变量的初始化

结构变量在定义时也可以进行初始化赋值。

struct
{
      int num;
      char name[20];
      char sex;
      float score;
} student1={001,"Phoebe",'F',99.0};

dev c++调试运行过程如下

#include<stdio.h>
struct stu{
	int num;
	char name[20];
	char sex;
	float score;
}student1={1001,"Phoebe",'F',90}; //注意里面是逗号隔开 
int main(){
	printf("%d,%s,%c,%f",student1.num,student1.name,student1.sex,student1.score); 
}

4.访问结构成员

为了访问结构的成员,我们要使用成员访问运算符(.)。成员访问运算符是结构变量名称和我们要访问的结构成员之间的一个句号。

通过结构变量来访问成员的一般形式为: 结构变量名.成员名 如果成员本身又是一个结构,则必须逐级访问到最低级的成员才能使用。

下面的实例演示了访问运算符的用法:

#include <stdio.h>
#include <string.h>
 
struct Book
{
   char  title[99];
   char  author[99];
   char  subject[99];
   int   id;
};
 
int main( )
{
   struct Book Book1;        //声明 Book1,类型为 Book 
   struct Book Book2;        //声明 Book2,类型为 Book 
 
   /* Book1 信息 */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "A"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.id = 1001;
 
   /* Book2 信息 */
   strcpy( Book2.title, "Javascript");
   strcpy( Book2.author, "B");
   strcpy( Book2.subject, "Javascript Tutorial");
   Book2.id = 1002;
 
   /* 输出 Book1 信息 */
   printf( "Book 1 title : %s\n", Book1.title);
   printf( "Book 1 author : %s\n", Book1.author);
   printf( "Book 1 subject : %s\n", Book1.subject);
   printf( "Book 1 id : %d\n", Book1.id);
 
   /* 输出 Book2 信息 */
   printf( "Book 2 title : %s\n", Book2.title);
   printf( "Book 2 author : %s\n", Book2.author);
   printf( "Book 2 subject : %s\n", Book2.subject);
   printf( "Book 2 id : %d\n", Book2.id);
 
   return 0;
}


试试输出结果是什么吧

这里结构体也可以作为函数参数哦

#include <stdio.h>
#include <string.h>
 
struct Book
{
   char  title[99];
   char  author[99];
   char  subject[99];
   int   id;
};
 void printBook( struct Book book ){

   printf( "Book 1 title : %s\n", book.title);
   printf( "Book 1 author : %s\n", book.author);
   printf( "Book 1 subject : %s\n", book.subject);
   printf( "Book 1 id : %d\n", book.id);
 
  
}
int main( )
{
   struct Book Book1;        //声明 Book1,类型为 Book 
   struct Book Book2;        //声明 Book2,类型为 Book 
 
   /* Book1 信息 */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "A"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.id = 1001;
 
   /* Book2 信息 */
   strcpy( Book2.title, "Javascript");
   strcpy( Book2.author, "B");
   strcpy( Book2.subject, "Javascript Tutorial");
   Book2.id = 1002;
 
   printBook(Book1);
   printBook(Book2);
 
   return 0;
}


练习访问结构体成员

练习1.请编写一个程序,要求定义一个结构体(Student),用于存放“学号”、“性别”、“邮箱”和“QQ号码”,在用户录入数据后打印结果。

#include <stdio.h>
#include <string.h>
    
struct Student
{
        int id;
        int sex;
        char qq[32];
        char email[64];
};
    
int main()
{
        struct Student stu;
    
        printf("请输入学生ID:");
        scanf("%d", &stu.id);
    
        printf("请输入学生性别(1/0):");
        scanf("%d", &stu.sex);
    
        printf("请输入QQ号:");
        scanf("%s", stu.qq);
    
        printf("请输入Email地址:");
        scanf("%s", stu.email);
        printf("ID:%d\n", stu.id);
        printf("性别:%s\n", (stu.sex == 1) ? "男" : "女");
        printf("QQ号是:%s\n", stu.qq);
        printf("Email是:%s\n", stu.email);
    
        return 0;
}

练习2.结构体求坐标系上两点的距离。

#include<stdio.h>
#include<math.h>
struct point{
	double x;
	double y;
};
int main(){
	struct point p1,p2;
	double result;
	scanf("%lf",&p1.x);
	scanf("%lf",&p1.y);
	scanf("%lf",&p2.x);
	scanf("%lf",&p2.y);
	result=sqrt(pow((p1.x-p2.x),2)+pow((p1.y-p2.y),2));
	printf("%lf",result);
	return 0;
}

5.结构体数组和结构体指针

指向结构的指针

定义指向结构的指针,方式与定义指向其他类型变量的指针相似,如下所

struct Books *struct_pointer;

现在,在上述定义的指针变量中存储结构变量的地址。为了查找结构变量的地址,需要将 & 运算符放在结构名称的前面,如下所示:

struct_pointer = &Book1;

为了使用指向该结构的指针访问结构的成员,必须使用 -> 运算符,如下所示:

struct_pointer->title;

结构体指针使用实例

一起用指针方法实现图书信息的输出吧

#include <stdio.h>
#include <string.h>
 
struct Book
{
   char  title[99];
   char  author[99];
   char  subject[99];
   int   id;
};
 void printBook( struct Book *book ){

   printf( "Book 1 title : %s\n", book->title);
   printf( "Book 1 author : %s\n", book->author);
   printf( "Book 1 subject : %s\n", book->subject);
   printf( "Book 1 id : %d\n", book->id);
 
  
}
int main( )
{
   struct Book Book1;        //声明 Book1,类型为 Book 
   struct Book Book2;        //声明 Book2,类型为 Book 
 
   /* Book1 信息 */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "A"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.id = 1001;
 
   /* Book2 信息 */
   strcpy( Book2.title, "Javascript");
   strcpy( Book2.author, "B");
   strcpy( Book2.subject, "Javascript Tutorial");
   Book2.id = 1002;
 
   printBook(&Book1);
   printBook(&Book2);
 
   return 0;
}


练习使用结构体数组和结构体指针

练习1:编写一个程序,要求定义一个结构体(Student),用于存放 “学号”、“性别”、“邮箱” 和 “QQ号码”,在用户录入数据后打印结果。!3kaq$
练习1:使用结构体声明三维立体空间中一个点的坐标(包含 x 坐标、y 坐标和 z 坐标),支持用户录入多个点的坐标(具体录入的数量由用户自定义),并计算所有点之间的距离(比如有三组数据point1,point2和point3 就要计算1 2,1 3,和2 3的距离)。}

动动手试试看吧~代码在这里:

#include<math.h>
struct Point
{ double x;
  double y;
  double z;
};
int main(){
	int n;
	double result;
	printf("请输入需要录入的数据数量");
	scanf("%d",&n);
	struct Point pt[n];
	for(int i=0;i<n;i++){
		printf("请输入第%d个点x坐标",i+1);
		scanf("%lf",&pt[i].x);
		printf("请输入第%d个点y坐标",i+1);
		scanf("%lf",&pt[i].y);
		printf("请输入第%d个点z坐标",i+1);
		scanf("%lf",&pt[i].z);
	}
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			result=sqrt(pow((pt[i].x-pt[j].x),2)+pow((pt[i].y-pt[j].y),2)+pow((pt[i].z-pt[j].z),2));
			printf("第%d个点和第%d个点的距离是%.2f\n",i+1,j+1,result);
		}
	}
	 return 0;
}

练习2:

(1)有3个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入3个学生的数据,要求打印出每个学生的平均成绩,以及最高分学生的数据(包括学号、姓名、3门课的成绩、平均分数)。

    要求:函数参数使用结构指针;使用函数average()求每个学生的平均分,函数search()找出最高分学生的数据;输出有关的信息需在主函数中实现(即子函数average()和search()中不能有打印输出功能)。

完整代码实现:

#include<stdio.h>
struct student{
	char num[99];
	char name[99];
	float score[3];
	float ave;
}stu[3],*p;
void average(struct student *p){
	for(int i=0;i<3;i++){
		(p->ave)=(*(p->score)+*(p->score+1)+*(p->score+2))/3;
		p++;
	}
}
int search(struct student *q){ //查找平均分最高的学生 
	struct student *t;
	float max=q->ave;
	t=q;
	for(int i=0;i<3;i++){
		if(max<(q+i)->ave){
			t=q+i;
			max=(q+i)->ave;
		}
	} 
	return t-q;
}
int main(void){
	p=&stu[0];
	int j;
	for(int i=0;i<3;i++){
		j=0;
		printf("请输入学生%d的学号姓名和三科成绩\n",i+1);
		scanf("%s",p->num);
		scanf("%s",p->name);
		scanf("%f",p->score+j);
		j++;
		scanf("%f",p->score+j);
		j++;
		scanf("%f",p->score+j);
		j++;
		p++;
	}
	p=&stu[0];
	average(p);
	j=0;
	j=search(p);
	for(int i=0;i<3;i++){
		printf("学号\t姓名\t数学\t语文\t物理\t平均分\n");
		printf("%s\t%s\t%.0f\t%.0f\t%.0f\t%.2f\n",(p+i)->num,(p+i)->name,*((p+i)->score),*((p+i)->score+1),*((p+i)->score+2),(p+i)->ave);
		
	}
	printf("最高分学生成绩如下:\n");
	printf("学号\t姓名\t数学\t语文\t物理\n");
	printf("%s\t%s\t%.0f\t%.0f\t%.0f\n",(p+j)->num,(p+j)->name,*((p+j)->score),*((p+j)->score+1),*((p+j)->score+2));
}
 

(2)

有3个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入3个学生的数据,要求:

(2.1)打印出每个学生的平均成绩,以及最高分学生的数据(包括学号、姓名、3门课的成绩、平均分数)。

(2.2)有学生姓名录入出错,将学号3003学生出错的姓名Zhao改正为Zhou,并输出改正前和改正后的信息。

要求:在程序中使用结构数组

#include<stdio.h>
struct stu{
	int num;
	char *name;
	float score[3];
	float average;
}a[3];
int main(void){
	int i,j;
	float sum=0;
	char c[]="Zhou";
	char b[99];
	for(i=0;i<3;i++){
	printf("\n请输入学号 姓名 数学 语文 物理成绩\n");
	scanf("%d",&a[i].num);
	scanf("%s",b);
	a[i].name=b;
	for(j=0,sum=0;j<3;j++){
		scanf("%f",&a[i].score[j]);
		sum+=a[i].score[j];
	}
	a[i].average=sum/3;
	printf("学号\t姓名\t数学\t语文\t物理\t平均分\n");
	a[2].name=c;
	printf("%d\t%s\t%.0f\t%.0f\t%.0f\t%.0f\t",a[i].num,a[i].name,a[i].score[0],a[i].score[1],a[i].score[2],a[i].average);
}
    printf("最高分学生成绩如下:\n");
	sum=0;
	for(i=0;i<3;i++)
	{
		sum=a[i].average>a[i+1].average?a[i].average:a[i+1].average;
	} 
		for(i=0;i<3;i++)
		if(sum==a[i].average)
			break;
		printf("学号\t姓名\t数学\t语文\t物理\t平均分\n");
		printf("%d\t%s\t%.0f\t%.0f\t%.0f\t%.1f\n",a[i].num,a[i].name,a[i].score[0],a[i].score[1],a[i].score[2],a[i].average);
 
}

练习3.本题使用求平均分函数、分数查找函数、二维数组及其指针方法完成。一个班有5个学生,4门课程:

① 求每门课程的平均分,并输出;

② 找出有两门以上课程不及格的学生,输出其学号、姓名和全部课程成绩及平均成绩;

③ 找出每门课程成绩均在85分以上的学生,输出其学号、姓名和全部课程成绩及平均成绩。

提示:用二维数组存放学生和课程,直接初始化二维数组以简化调试(即不使用scanf语句)。

   学生成绩的测试数据如下:

stuNo  name  math  eng  phys chem

3001   Zhang  82   78   88   93

3002   Wang  46   68   62   57

3003   Li     92   86   85   87

3004   Sun    52   77   55   47

3005   Zhao   58   90   85   77

#include<stdio.h>
struct student{
	int num;
	char name[20];
	float score[4];
}s[99]; 
void compute_average_scores(struct student *s,int n){
	float total[4]={};
	for(int i=0;i<n;i++){
		for(int j=0;j<4;j++){
			total[j]+=s[i].score[j];
		}
	}
	printf("average of math:%.1f\n",total[0]/n);
	printf("average of english:%.1f\n",total[1]/n);
	printf("average of physics:%.1f\n",total[2]/n);
	printf("average of chemistry:%.1f\n",total[3]/n);
}
void print(struct student *s){
	printf("Student Number:%d",s->num);
	printf("Student Name:%s\n",s->name);
	printf("Math:%g\n",s->score[0]);
	printf("English:%g\n",s->score[1]);
	printf("Physics:%g\n",s->score[2]);
	printf("Chemistry:%g\n",s->score[3]);
	printf("Average:%.1f\n",(s->score[0]+s->score[1]+s->score[2]+s->score[3])/4);
}
int main(){
	struct student s[]={
		 {3001, "Zhang", {82, 78, 88, 93}},
        {3002, "Wang", {46, 68, 62, 57}},
        {3003, "Li", {92, 86, 85, 87}},
        {3004, "Sun", {52, 77, 55, 47}},
        {3005, "Zhao", {58, 90, 85, 77}}
		};
		int n=sizeof(s)/sizeof(struct student);
		compute_average_scores(s,n);
		printf("students with at least two scores<60:\n");
		for(int i=0;i<n;i++){
			int count=0;
			for(int j=0;j<4;j++){
				if(s[i].score[j]<60){
					count++;
				}
			}
		if(count>=2){
			print(&s[i]);
		}
		} 
		printf("students with all scores>=85:\n" );
		for(int i=0;i<n;i++){
			int count=0;
			for(int j=0;j<4;j++){
				if(s[i].score[j]>=85){
					count++;
					if(count==4){
						print(&s[i]);
					}
				}
			}
		}
			return 0;
}
	

6.进阶的结构体问题

设计一个表示时间的结构体,可以精确表示年、月、日、小时、分、秒。

(1)使用结构体方法,存入计算机当前的时间值:年、月、日、小时、分、秒,然后完整输出该结构体变量中的值(年月日小时分秒)。

(2)使用结构体方法,计算当前计算机时间离本周日的24点还剩余多少分秒(倒计时),并能实时/连续显示倒计时信息。

这里可以用算星期几神器——基姆拉尔森计算公式捏。

公式是这样的:w = (y + y /4 + y / 400 - y / 100 + 2 * m + 3 * (m + 1)/5 + d) % 7;

注: 这个公式中,w = 0对应的是星期一,w = 6对应的是星期日。

是不是简单了很多?

#include <stdio.h>
int CalculateWeekDay(int y, int m,int d){
if(m==1||m==2) m+=12,y--;
int iWeek = (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
switch(iWeek){
case 0: printf("星期一\n"); break;
      case 1: printf("星期二\n"); break;
      case 2: printf("星期三\n"); break;
      case 3: printf("星期四\n"); break;
      case 4: printf("星期五\n"); break;
      case 5: printf("星期六\n"); break;
      case 6: printf("星期日\n"); break;
}
return iWeek;
}
struct time
{
int year;
int mo;
int day;
int hour;
int min;
int sec;
}i;
int main()
{
scanf("%d %d %d %d %d %d",&i.year,&i.mo,&i.day,&i.hour,&i.min,&i.sec);
int z=7-CalculateWeekDay(i.year,i.mo,i.day)-1;
long long s=z*86400;
long long c=(i.hour*3600+i.min*60+i.sec);
long long b=s-c;
printf("%d %d %d %d\n",s,c,b,z);
i.year=0;
i.mo=0;
i.day=b/86400;
i.hour=(b%86400/3600);
i.min=(b%86400%3600/60);
i.sec=(b%86400%3600%60);
printf("%d %d %d %d %d %d\n",i.year,i.mo,i.day,i.hour,i.min,i.sec);
for(long long s1=c;i.day>=0;s1++){
i.sec-=1;
if(i.sec<0){
i.sec=60;
i.min--;
}
if(i.min<0){
i.min=60;
i.hour--;
}
if(i.hour<0){
i.hour=23;
i.day--;
}
if(i.year==0&&i.mo==0&&i.day==0&&i.hour==0&&i.min==0&&i.sec==0)break;
printf("%d %d %d %d %d %d\n",i.year,i.mo,i.day,i.hour,i.min,i.sec);
}
return 0;
}

二位卫生检查员(姓名张甲和李乙)对学生宿舍(例如宿舍GS1、...、GS4,宿舍WS1、...、WS4)的卫生状况进行检查,对每个宿舍的地面干净、床铺整洁、卫生间状况进行评分(评分A、B、C分别对应好、中、差),然后对检查结果进行分析和公告。张甲通过分析所有宿舍信息,张甲获得并存储最好和最差宿舍的卫生状况信息;李乙通过分析所有宿舍信息,李乙获得并存储所有宿舍的平均卫生状况信息。

    要求:使用结构体数组结构指针方法;分析宿舍信息并获得结果由函数(例如result()或更多函数)实现;主函数中实现打印输出卫生状况信息。

    验证实例如下:

宿舍  地面  床铺 卫生间

GS1    C    B    C

GS2    A    B    A

GS3    B    A    C

GS4    B    B    B

张甲宣布:最好宿舍GS2,卫生评分A-

张甲宣布:最差宿舍GS1,卫生评分C+

李乙宣布平均卫生状况:

宿舍  地面  床铺 卫生间

GS1-4  B    B+    B-

宿舍  地面  床铺 卫生间

WS1    B    A    B

WS2    C    B    B

WS3    A    B    B

WS4    C    B    C

张甲宣布:最好宿舍WS3,卫生评分B+

张甲宣布:最差宿舍WS4,卫生评分C+

李乙宣布平均卫生状况:

宿舍  地面  床铺 卫生间

WS1-4  B-   B+    B-

不能只看不练哦~写一下试试看 可以私信我一起寻找更好的解决办法*

7.c语言大作业 学生成绩管理系统

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#define LEN sizeof(struct student)
#define FORMAT "%-8d%-15s%-12.1lf%-12.1lf%-12.1lf%-12.1lf\n"
#define DATA stu[i].num,stu[i].name,stu[i].elec,stu[i].expe,stu[i].requ,stu[i].sum
struct student/*定义学生成绩结构体*/
{ int num;/*学号*/
  char name[15];/*姓名*/
  double elec;/*选修课*/
  double expe;/*实验课*/
  double requ;/*必修课*/
  double sum;/*总分*/
};
struct student stu[50];/*定义结构体数组*/
void in();/*录入学生成绩信息*/
void show();/*显示学生信息*/
void order();/*按总分排序*/
void del();/*删除学生成绩信息*/
void modify();/*修改学生成绩信息*/
void menu();/*主菜单*/
void insert();/*插入学生信息*/
void total();/*计算总人数*/
void search();/*查找学生信息*/
void main()/*主函数*/
{ int n;
  menu();
  scanf("%d",&n);/*输入选择功能的编号*/
  while(n)
  { switch(n)
     { case 1: in();break;
       case 2: search();break;
       case 3: del();break;
       case 4: modify();break;
       case 5: insert();break;
       case 6: order();break;
       case 7: total();break;
       default:break;
     }
    getch();
    menu();/*执行完功能再次显示菜单界面*/
    scanf("%d",&n);
  }
}

void in()/*录入学生信息*/
{ int i,m=0;/*m是记录的条数*/
  char ch[2];
  FILE *fp;/*定义文件指针*/
  if((fp=fopen("data.txt","a+"))==NULL)/*打开指定文件*/
     { printf("can not open\n");return;}
  while(!feof(fp)) { 
	  if(fread(&stu[m] ,LEN,1,fp)==1)
		  m++;/*统计当前记录条数*/
  }
  fclose(fp);
  if(m==0) 
	  printf("No record!\n");
  else 
  {
	  system("cls");
          show();/*调用show函数,显示原有信息*/
  }
  if((fp=fopen("data.txt","wb"))==NULL)
     { printf("can not open\n");return;}
  for(i=0;i<m;i++) fwrite(&stu[i] ,LEN,1,fp);/*向指定的磁盘文件写入信息*/
  printf("please input(y/n):");
  scanf("%s",ch);
while(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)/*判断是否要录入新信息*/
    {
    printf("number:");scanf("%d",&stu[m].num);/*输入学生学号*/
    for(i=0;i<m;i++)
	    if(stu[i].num==stu[m].num)
	    {
	    printf("the number is existing,press any to continue!");
	    getch();
	    fclose(fp);
	    return;
	    }
     printf("name:");scanf("%s",stu[m].name);/*输入学生姓名*/
     printf("elective:");scanf("%lf",&stu[m].elec);/*输入选修课成绩*/
     printf("experiment:");scanf("%lf",&stu[m].expe);/*输入实验课成绩*/
     printf("required course:");scanf("%lf",&stu[m].requ);/*输入必修课成绩*/
     stu[m].sum=stu[m].elec+stu[m].expe+stu[m].requ;/*计算出总成绩*/
     if(fwrite(&stu[m],LEN,1,fp)!=1)/*将新录入的信息写入指定的磁盘文件*/
       { printf("can not save!"); getch(); }
     else { printf("%s saved!\n",stu[m].name);m++;}
     printf("continue?(y/n):");/*询问是否继续*/
     scanf("%s",ch);
  }
  fclose(fp);
  printf("OK!\n");
 }

void show()
 { FILE *fp;
   int i,m=0;
   fp=fopen("data.txt","rb");
   while(!feof(fp))
   {
   if(fread(&stu[m] ,LEN,1,fp)==1) 
   m++;
   }  
   fclose(fp);
   printf("number  name           elective    experiment  required    sum\t\n");
   for(i=0;i<m;i++)
       { 
	   printf(FORMAT,DATA);/*将信息按指定格式打印*/
       }
     }
 
void menu()/*自定义函数实现菜单功能*/
{
  system("cls");
  printf("\n\n\n\n\n");
  printf("\t\t|---------------------STUDENT-------------------|\n");
  printf("\t\t|\t 0. exit                                |\n");
  printf("\t\t|\t 1. input record                        |\n");
  printf("\t\t|\t 2. search record                       |\n");
  printf("\t\t|\t 3. delete record                       |\n");
  printf("\t\t|\t 4. modify record                       |\n");
  printf("\t\t|\t 5. insert record                       |\n");
  printf("\t\t|\t 6. order                               |\n");
  printf("\t\t|\t 7. number                              |\n");
  printf("\t\t|-----------------------------------------------|\n\n");
  printf("\t\t\tchoose(0-7):");
}

void order()/*自定义排序函数*/
{ FILE *fp;
  struct student t;
  int i=0,j=0,m=0;
  if((fp=fopen("data.txt","r+"))==NULL)
     { 
	printf("can not open!\n");
        return;
  }
  while(!feof(fp)) 
  if(fread(&stu[m] ,LEN,1,fp)==1) 
	  m++;
  fclose(fp);
  if(m==0) 
  {
	  printf("no record!\n");
	  return;
  }
  if((fp=fopen("data.txt","wb"))==NULL)
     {
	  printf("can not open\n");
	  return;}
  for(i=0;i<m-1;i++)
      for(j=i+1;j<m;j++)/*双重循环实现成绩比较并交换*/
          if(stu[i].sum<stu[j].sum)
          { t=stu[i];stu[i]=stu[j];stu[j]=t;}
	  if((fp=fopen("data.txt","wb"))==NULL)
     { printf("can not open\n");return;}
  for(i=0;i<m;i++)/*将重新排好序的内容重新写入指定的磁盘文件中*/
      if(fwrite(&stu[i] ,LEN,1,fp)!=1)
       { 
        printf("%s can not save!\n"); 
        getch();
      }
  fclose(fp);
  printf("save successfully\n");
}
void del()/*自定义删除函数*/
{FILE *fp;
  int snum,i,j,m=0;
  char ch[2];
  if((fp=fopen("data.txt","r+"))==NULL)
     { printf("can not open\n");return;}
  while(!feof(fp))  if(fread(&stu[m],LEN,1,fp)==1) m++;
  fclose(fp);
  if(m==0) 
  {
	  printf("no record!\n");
	  return;
  }
  printf("please input the number:");
  scanf("%d",&snum);
    for(i=0;i<m;i++)
     if(snum==stu[i].num)
	     break;
     printf("find the student,delete?(y/n)");
     scanf("%s",ch);
      if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)/*判断是否要进行删除*/
      for(j=i;j<m;j++)
	      stu[j]=stu[j+1];/*将后一个记录移到前一个记录的位置*/
      m--;/*记录的总个数减1*/
      if((fp=fopen("data.txt","wb"))==NULL)
     { printf("can not open\n");return;}
  for(j=0;j<m;j++)/*将更改后的记录重新写入指定的磁盘文件中*/
      if(fwrite(&stu[j] ,LEN,1,fp)!=1)
       { printf("can not save!\n");
      getch();}
  fclose(fp);
  printf("delete successfully!\n");
}

void search()/*自定义查找函数*/
{ FILE *fp;
  int snum,i,m=0;
  char ch[2];
  if((fp=fopen("data.txt","rb"))==NULL)
     { printf("can not open\n");return;}
  while(!feof(fp))  if(fread(&stu[m],LEN,1,fp)==1) m++;
  fclose(fp);
  if(m==0) {printf("no record!\n");return;}
  printf("please input the number:");
  scanf("%d",&snum);
  for(i=0;i<m;i++)
     if(snum==stu[i].num)/*查找输入的学号是否在记录中*/
     { printf("find the student,show?(y/n)");
     scanf("%s",ch);
      if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0) 
        {
          printf("number  name           elective    experiment  required    sum\t\n");
          printf(FORMAT,DATA);/*将查找出的结果按指定格式输出*/
	  break;
     }
     }   
  if(i==m) printf("can not find the student!\n");/*未找到要查找的信息*/
}

void modify()/*自定义修改函数*/
{ FILE *fp;
  int i,j,m=0,snum;
  if((fp=fopen("data.txt","r+"))==NULL)
     { printf("can not open\n");return;}
  while(!feof(fp))  
 if(fread(&stu[m],LEN,1,fp)==1) m++;
  if(m==0) {printf("no record!\n");
  fclose(fp);
  return;
  }
  printf("please input the number of the student which do you want to modify!\n");
  scanf("%d",&snum);
  for(i=0;i<m;i++)
	  if(snum==stu[i].num)/*检索记录中是否有要修改的信息*/
		  break;
	  printf("find the student!you can modify!\n");
	  printf("name:\n");
	  scanf("%s",stu[i].name);/*输入名字*/
          printf("\nelective:");
	  scanf("%lf",&stu[i].elec);/*输入选修课成绩*/
          printf("\nexperiment:");
	  scanf("%lf",&stu[i].expe);/*输入实验课成绩*/
          printf("\nrequired course:");
	  scanf("%lf",&stu[i].requ);/*输入必修课成绩*/
	  stu[i].sum=stu[i].elec+stu[i].expe+stu[i].requ;
	  if((fp=fopen("data.txt","wb"))==NULL)
     { printf("can not open\n");return;}
	  for(j=0;j<m;j++)/*将新修改的信息写入指定的磁盘文件中*/
	  if(fwrite(&stu[j] ,LEN,1,fp)!=1)
       { printf("can not save!"); getch(); }
  fclose(fp);
 }

void insert()/*自定义插入函数*/
{ FILE *fp;
  int i,j,k,m=0,snum;
  if((fp=fopen("data.txt","r+"))==NULL)
     { printf("can not open\n");return;}
  while(!feof(fp))  
 if(fread(&stu[m],LEN,1,fp)==1) m++;
  if(m==0) {printf("no record!\n");
  fclose(fp);
  return;
  }
  printf("please input position where do you want to insert!(input the number)\n");
  scanf("%d",&snum);/*输入要插入的位置*/
  for(i=0;i<m;i++)
	  if(snum==stu[i].num)
		  break;
	  for(j=m-1;j>i;j--)
           stu[j+1]=stu[j];/*从最后一条记录开始均向后移一位*/
	  printf("now please input the new information.\n");
          printf("number:");
	  scanf("%d",&stu[i+1].num);
	  for(k=0;k<m;k++)
	    if(stu[k].num==stu[i+1].num)
	    {
	    printf("the number is existing,press any to continue!");
	    getch();
	    fclose(fp);
	    return;
	    }
	  printf("name:\n");
	  scanf("%s",stu[i+1].name);
          printf("\nelective:");
	  scanf("%lf",&stu[i+1].elec);
          printf("\nexperiment:");
	  scanf("%lf",&stu[i+1].expe);
          printf("\nrequired course:");
	  scanf("%lf",&stu[i+1].requ);
	  stu[i+1].sum=stu[i+1].elec+stu[i+1].expe+stu[i+1].requ;
	  if((fp=fopen("data.txt","wb"))==NULL)
     { printf("can not open\n");return;}
	  for(k=0;k<=m;k++)
	  if(fwrite(&stu[k] ,LEN,1,fp)!=1)/*将修改后的记录写入磁盘文件中*/
       { printf("can not save!"); getch(); }
  fclose(fp);
 }

void total()
{ FILE *fp;
  int m=0;
  if((fp=fopen("data.txt","r+"))==NULL)
     { printf("can not open\n");return;}
  while(!feof(fp))  
	  if(fread(&stu[m],LEN,1,fp)==1) 
		  m++;/*统计记录个数即学生个数*/
  if(m==0) {printf("no record!\n");fclose(fp);return;}
  printf("the class are %d students!\n",m);/*将统计的个数输出*/
  fclose(fp);
 }

结构体完美结束❤刚学敲代码的大一小白在此 有不好的地方烦请大家指正~熬夜打字不易 谢谢大家喜欢呀 希望能对你有所帮助

目录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值