c语言结构学生信息结构
A. 急求!c语言中使用结构(struct)管理学生信息,谢谢
正好有个通讯录管理,如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME 11
#define MAX_SEX 3
#define MAX_BIRTHDAY 9
#define MAX_TEL 21
#define MAX_MOBLIE 21
#define MAX_FAX 21
#define MAX_ADDRESS 101
#define MAX_POSTAL_CODE 7
void add();
void show();
void search();
void delete();
void update();
void save();
void quit();
typedef struct _person
{
char name[MAX_NAME]; /*姓名*/
char sex[MAX_SEX]; /*性别*/
char birthday[MAX_BIRTHDAY]; /*出生日期*/
char tel[MAX_TEL]; /*电话*/
char moblie[MAX_MOBLIE]; /*手机*/
char fax[MAX_FAX]; /*传真*/
char address[MAX_ADDRESS]; /*地址*/
char postal_code[MAX_POSTAL_CODE]; /*邮编*/
}person;
typedef struct _addr_book
{
person per;
struct _addr_book*next;
}addr_book;
addr_book *first=NULL;
int printf_menu();
addr_book*get_last(addr_book*from);
void print_person(person*p);
void input_person(person*p);
void add()
{
char input='N';
addr_book*last=NULL;
addr_book*new_addr=(addr_book*)malloc(sizeof(addr_book) );
new_addr->next=NULL;
if (first==NULL)
{
first=new_addr;
}
else
{
last=get_last(first);
last->next=new_addr;
}
input_person(&(new_addr->per) );
printf(">继续输入?(Y继续,N返回菜单)");
getchar();
input=getchar();
if (input=='Y'||input=='y')
{
add();
}
}
void show()
{
int i=0;
addr_book*p=first;
while (p!=NULL)
{
i++;
printf ("*******第 %d 个联系人********* \n",i);
print_person(&(p->per) );
p=p->next;
}
if (i==0)
{
printf ("没有联系人!");
}
printf("按任意键返回菜单.....");
getchar();
getchar();
}
void search()
{
int count=0;
char input='N';
char name[MAX_NAME]=;
addr_book*p=first;
printf (">请输入要查找的联系人姓名(最大%d个字符):",MAX_NAME-1);
scanf ("%s",name);
while (p!=NULL)
{
if (strcmp(p->per.name,name)==0)
{
print_person(&(p->per) );
count++;
break;
}
p=p->next;
}
if (count==0)
{
printf("没有找到姓名为%s的联系人.",name);
}
printf ("要继续查找吗?(Y 继续查找,N返回菜单)");
getchar();
input=getchar();
if (input=='Y'||input=='y')
{
search();
}
}
void delete()
{
int count=0;
char input='N';
char name[MAX_NAME]=;
addr_book*p=first;
addr_book*p1=NULL;
printf ("输入要删除的联系人姓名(最大%d个字符):",MAX_NAME-1);
scanf ("%s",name);
while (count!=1)
{
if (strcmp(p->per.name,name)==0)
{
print_person(&(p->per) );
count++;
}
}
if (count==0)
{
printf("没有姓名为%s的人:",name);
}
else
{
printf("确定要删除姓名为[%s]的联系人吗?(Y 确认,N 取消)",name);
getchar();
input=getchar();
if (input=='y'||input=='Y')
{ printf("%s %s",p->per.name,first->per.name);
if (p==first)
{
first=p->next;
}
else
{
p1=first;
while (p1!=NULL)
{
if (p1->next==p)
{
p1->next=p->next;
break;
}
p1=p1->next;
}
}
free(p);
}
}
printf("继续删除其他联系人吗?(Y继续,N 返回菜单)");
getchar();
input=getchar();
if (input=='Y'||input=='y')
{
delete();
}
}
void update()
{
int count=0;
char input='N';
char name[MAX_NAME]=;
addr_book*p=first;
printf (">请输入要更新的联系人姓名(最大%d个字符):",MAX_NAME-1);
scanf ("%s",name);
while (p!=NULL)
{
if (strcmp(p->per.name,name)==0);
{
print_person(&(p->per) );
count++;
break;
}
p=p->next;
}
if (count==0)
{
printf ("没有找到姓名为%s的人.",name);
}
else
{
input_person(&(p->per));
}
printf ("继续更新其他联系人吗?(Y 继续,N 返回菜单)");
getchar();
input=getchar();
if (input=='Y'||input=='y')
{
update();
}
}
void save()
{
FILE *fp;
char file[100];
addr_book*p=first;
printf("请输入文件名:");
scanf ("%s",file);
fp=fopen(file,"w");
while (p!=NULL)
{
fprintf(fp,"姓名:%s 性别:%s 生日:%s 电话:%s 手机:%s 传真:%s 地址:%s 邮编:%s\n",p->per.name,p->per.sex,p->per.birthday,p->per.tel,p->per.moblie,p->per.fax,p->per.address,p->per.postal_code);
p=p->next;
}
fclose(fp);
printf("保存成功!\n按任意键返回菜单..\n");
getchar();
getchar();
}
void quit()
{
addr_book*pdel=first;
addr_book*p=NULL;
if (pdel==NULL) /*如果pdel的next指针不为空,表示有下一条数据*/
{
exit(0); /*先将p指向pdel,释放掉pdel,再将p重复给pdel*/
}
while (pdel->next!=NULL)
{
p=pdel->next;
free(pdel);
pdel=p;
}
free(pdel); /*如果pdel的next指针为空,表示没有下一条数据,直接删除该节点*/
exit(0);
}
int print_menu()
{
int selected=0;
char menu[]={
"+==============================================================+\n"
"| 通讯簿管理系统 |\n"
"+--------------------------------------------------------------+\n"
"| 1 添加联系人 |\n"
"| 2 显示所有联系人 |\n"
"| 3 查找联系人 |\n"
"| 4 删除联系人 |\n"
"| 5 更行联系人 |\n"
"| 6 保存 |\n"
"| 7 退出系统 |\n"
"+==============================================================+\n"
};
system("cls");
printf ("%s",menu);
printf (">请选择[1-7]:");
scanf ("%d",&selected);
if (selected<1||selected>7)
{
printf ("错误的选择!(请输入[1-7].按任意键继续...");
getchar();
getchar();
}
return selected;
}
void input_person(person *p)
{
printf (">请输入联系人信息:(无则输入0)\n");
printf (">请输入姓名(最大长度%d个字符):",MAX_NAME-1);
scanf ("%s",p->name);
printf (">请输入性别(最大长度%d个字符):",MAX_SEX-1);
scanf ("%s",p->sex);
printf (">请输入出生日期(最大长度%d个字符):",MAX_BIRTHDAY-1);
scanf ("%s",p->birthday);
printf (">请输入电话(最大长度%d个字符):",MAX_TEL-1);
scanf ("%s",p->tel);
printf (">请输入手机(最大长度%d个字符):",MAX_MOBLIE-1);
scanf ("%s",p->moblie);
printf (">请输入传真(最大长度%d个字符):",MAX_FAX-1);
scanf ("%s",p->fax);
printf (">请输入地址(最大长度%d个字符):",MAX_ADDRESS-1);
scanf ("%s",p->address);
printf (">请输入邮编(最大长度%d个字符):",MAX_POSTAL_CODE-1);
scanf ("%s",p->postal_code);
}
void print_person(person*p)
{
printf ("姓名:%s\t 性别:%s\t 生日:%s\n",p->name,p->sex,p->birthday);
printf ("电话:%s\n",p->tel);
printf ("手机:%s\n",p->moblie);
printf ("传真:%s\n",p->fax);
printf ("地址:%s\n",p->address);
printf ("邮编:%s\n",p->postal_code);
printf ("\n");
}
addr_book* get_last(addr_book* from)
{
addr_book* p=from;
while (p->next!=NULL)
{
p=p->next;
}
return p;
}
int main()
{
int flg=1;
while (flg)
{
switch (print_menu())
{
case 1:
add();
break;
case 2:
show();
break;
case 3:
search();
break;
case 4:
delete();
break;
case 5:
update();
break;
case 6:
save();
break;
case 7:
quit();
break;
}
}system("pause");
}
B. 用c语言编写学生基本信息(最好用结构体)拜托啦,急求!
在C语言中,可以使用结构体(Struct)来存放一组不同类型的数据。结构体的定义形式为:
struct 结构体名{
结构体所包含的变量或数组
};
结构体是一种集合,它里面包含了多个变量或数组,它们的类型可以相同,也可以不同,每个这样的变量或数组都称为结构体的成员(Member)。请看下面的一个例子:
struct stu{
char *name; //姓名
int num; //学号
int age; //年龄
char group; //所在学习小组
float score; //成绩
};
stu 为结构体名,它包含了 5 个成员,分别是 name、num、age、group、score。结构体成员的定义方式与变量和数组的定义方式相同,只是不能初始化。
注意大括号后面的分号;不能少,这是一条完整的语句。
结构体也是一种数据类型,它由程序员自己定义,可以包含多个其他类型的数据。
像 int、float、char 等是由C语言本身提供的数据类型,不能再进行分拆,我们称之为基本数据类型;而结构体可以包含多个基本类型的数据,也可以包含其他的结构体,我们将它称为复杂数据类型或构造数据类型。
结构体变量
既然结构体是一种数据类型,那么就可以用它来定义变量。例如:
struct stu stu1, stu2;
定义了两个变量 stu1 和 stu2,它们都是 stu 类型,都由 5 个成员组成。注意关键字struct不能少。
stu 就像一个“模板”,定义出来的变量都具有相同的性质。也可以将结构体比作“图纸”,将结构体变量比作“零件”,根据同一张图纸生产出来的零件的特性都是一样的。
你也可以在定义结构体的同时定义结构体变量:
struct stu{
char *name; //姓名
int num; //学号
int age; //年龄
char group; //所在学习小组
float score; //成绩
} stu1, stu2;
将变量放在结构体定义的最后即可。
如果只需要 stu1、stu2 两个变量,后面不需要再使用结构体名定义其他变量,那么在定义时也可以不给出结构体名,如下所示:
struct{ //没有写 stu
char *name; //姓名
int num; //学号
int age; //年龄
char group; //所在学习小组
float score; //成绩
} stu1, stu2;
这样做书写简单,但是因为没有结构体名,后面就没法用该结构体定义新的变量。
理论上讲结构体的各个成员在内存中是连续存储的,和数组非常类似,例如上面的结构体变量 stu1、stu2 的内存分布如下图所示,共占用 4+4+4+1+4 = 17 个字节。
但是在编译器的具体实现中,各个成员之间可能会存在缝隙,对于 stu1、stu2,成员变量 group 和 score 之间就存在 3 个字节的空白填充(见下图)。这样算来,stu1、stu2 其实占用了 17 + 3 = 20 个字节。
关于成员变量之间存在“裂缝”的原因,我们将在《C语言和内存》专题中的《C语言内存对齐,提高寻址效率》一节中详细讲解。
成员的获取和赋值
结构体和数组类似,也是一组数据的集合,整体使用没有太大的意义。数组使用下标[ ]获取单个元素,结构体使用点号.获取单个成员。获取结构体成员的一般格式为:
结构体变量名.成员名;
通过这种方式可以获取成员的值,也可以给成员赋值:
#include <stdio.h>
int main(){
struct{
char *name; //姓名
int num; //学号
int age; //年龄
char group; //所在小组
float score; //成绩
} stu1;
//给结构体成员赋值
stu1.name = "Tom";
stu1.num = 12;
stu1.age = 18;
stu1.group = 'A';
stu1.score = 136.5;
//读取结构体成员的值
printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!\n", stu1.name, stu1.num, stu1.age, stu1.group, stu1.score);
return 0;
}
运行结果:
Tom的学号是12,年龄是18,在A组,今年的成绩是136.5!
除了可以对成员进行逐一赋值,也可以在定义时整体赋值,例如:
struct{
char *name; //姓名
int num; //学号
int age; //年龄
char group; //所在小组
float score; //成绩
} stu1, stu2 = { "Tom", 12, 18, 'A', 136.5 };
不过整体赋值仅限于定义结构体变量的时候,在使用过程中只能对成员逐一赋值,这和数组的赋值非常类似。
C. 用c语言写:定义一个学生结构体(包含姓名,学号,语文,数学,外语,总分)。定义一个学生结构体数组。
#include<stdio.h>
structstudent{
charname[20];
intidnum;
floatscore[3];//分别存三科成绩
doubletotal;//总分
};
structstudent*highscore(structstudent*s,intn)
{
inti;
structstudent*high=s;
for(i=0;i<n;i++){
(s+i)->total=(s+i)->score[0]+(s+i)->score[1]+(s+i)->score[2];
if(high->total<(s+i)->total)
high=s+i;
}
returnhigh;
}
main()
{
structstudent*s,student[5];
//录入学生信息
for(s=student;s<student+5;s++){
printf("输入第%d个学生的信息: ",s-student+1);
printf("姓名:");
scanf("%s",s->name);
printf("学号:");
scanf("%d",&s->idnum);
printf("语文数学英语: ");
scanf("%f%f%f",&(s->score[0]),&(s->score[1]),&(s->score[2]));
}
//输出学生信息
printf(" 学生信息 姓名 学号 语文 数学 英语 ");
for(s=student;s<student+5;s++)
printf("%s %d %.1f %.1f %.1f ",s->name,s->idnum,s->score[0],s->score[1],s->score[2]);
//计算学生总分病返回总分最高的学生信息
s=highscore(student,5);
printf(" 总分最高的学生是:%s,学号:%d 成绩:语文:%.1f数学:%.1f英语:%.1f总分:%.1f ",s->name,s->idnum,s->score[0],s->score[1],s->score[2],s->total);
}
D. C语言 要求通过键盘输入三个学生信息(包括姓名、学号、班级),并输出这三个学生的所有信息。 结构体
#include<stdio.h>
struct st{//定义结构体
char name[20];
int id;
char _class[20];
};
int main()
{
struct st s[11];//定义结构体数组;
for(int i=0;i<3;i++)
scanf("%s%d%s",s[i].name,&s[i].id,s[i]._class);//输入
for(int i=0;i<3;i++)
printf("姓名:%s 学号:%d 班级:%s ",s[i].name,s[i].id,s[i]._class);//输出
}
E. c语言中怎么建立一个学生的结构体,包括名字学号性别
struct student
{
char name[10];
char sex[10];
int old;
}s[100];
main()
{
int n,k=5;char name[10];
for(i=0;i<k;i++)
{
printf("请输入姓名:");scanf("%s",s[i].name);
printf("请输入性别:");scanf("%s",s[i].sex);
printf("请输入年龄:");scanf("%s",s[i].old);
}
printf("请输入要查找的人名");scanf("%s",name);
for(i=0;i<k;i++)
{if(strcmp(th[i].name,name)==0)
printf("%s,性别%s,年龄%d",s[i].name,s[i].sex,s[i].old);
}
}
这个是简单的,没有添加和删除
F. c语言编程,用结构体记录每位学生的信息,包括姓名数学和计算机成绩,急求,谢谢!
#include "windows.h"
#include "stdio.h"
#define NAME_LENGTH 32
#define STUDENT_COUNT 3
struct TStudent
{
char szName[NAME_LENGTH];
int nScore1;
int nScore2;
};
void main()
{
printf("输入学生信息: ");
TStudent arrStudent[STUDENT_COUNT];
for(int i = 0; i < STUDENT_COUNT; i++)
{
scanf("%s %d %d", arrStudent[i].szName, &arrStudent[i].nScore1, &arrStudent[i].nScore2);
}
for(int i = 0; i < STUDENT_COUNT; i++)
{
printf("%s %.1f ", arrStudent[i].szName, (float)(arrStudent[i].nScore1 + arrStudent[i].nScore2) / 2);
}
system("pause");
return ;
}
G. 学生基本信息管理(数据结构用C语言描述)
/*头文件*/
#include <stdio.h>
#include<dos.h>
#include<stdlib.h> /*其它说明*/
#include<string.h> /*字符串函数*/
#include<mem.h> /*内存操作函数*/
#include<ctype.h> /*字符操作函数*/
#include<alloc.h> /*动态地址分配函数*/
#define LEN sizeof(STUDENT)
typedef struct stu /*定义结构体数组用于缓存数据*/
{char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}STUDENT;
/*函数原型*/
STUDENT *init(); /*初始化函数*/
int menu_select(); /*菜单函数*/
STUDENT *create(); /*创建链表*/
void print(STUDENT *head); /* 显示全部记录*/
void search(STUDENT *head); /*查找记录*/
STUDENT *delete(STUDENT *head); /*删除记录*/
STUDENT *sort(STUDENT *head); /*排序*/
STUDENT *insert(STUDENT *head,STUDENT *new); /*插入记录*/
void save(STUDENT *head); /*保存文件*/
STUDENT *load(); /*读文件*/
/*主函数界面*/
main()
{STUDENT *head,new;
head=init(); /*链表初始化,使head的值为NULL*/
for(;;) /*循环无限次*/
{switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,&new);break; /*&new表示返回地址*/
case 7:save(head);break;
case 8:head=load(); break;
case 9:exit(0); /*如菜单返回值为9则程序结束*/
}
}
}
/*初始化函数*/
STUDENT *init()
{
return NULL; /*返回空指针*/
}
/*菜单选择函数*/
menu_select()
{int n;
struct date d; /*定义时间结构体*/
getdate(&d); /*读取系统日期并把它放到结构体d中*/
printf("press any key to enter the menu......"); /*按任一键进入主菜单*/
getch(); /*从键盘读取一个字符,但不显示于屏幕*/
clrscr(); /*清屏*/
printf("********************************************************************************\n");
printf("\t\t Welcome to\n");
printf("\n\t\t The student score manage system\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. Enter the record\n"); /*输入学生成绩记录*/
printf("\t\t\t2. Print the record\n"); /*显示*/
printf("\t\t\t3. Search record on name\n"); /*寻找*/
printf("\t\t\t4. Delete a record\n"); /*删除*/
printf("\t\t\t5. Sort to make new a file\n"); /*排序*/
printf("\t\t\t6. Insert record to list\n"); /*插入*/
printf("\t\t\t7. Save the file\n"); /*保存*/
printf("\t\t\t8. Load the file\n"); /*读取*/
printf("\t\t\t9. Quit\n"); /*退出*/
printf("\n\t\t Made by Hu Haihong.\n");
printf("********************************************************************************\n");
printf("\t\t\t\t%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day); /*显示当前系统日期*/
do{
printf("\n\t\t\tEnter your choice(1~9):");
scanf("%d",&n);
}while(n<1||n>9); /*如果选择项不在1~9之间则重输*/
return(n); /*返回选择项,主函数根据该数调用相应的函数*/
}
/*输入函数*/
STUDENT *create()
{int i,s;
STUDENT *head=NULL,*p; /* 定义函数.此函数带回一个指向链表头的指针*/
clrscr();
for(;;)
{p=(STUDENT *)malloc(LEN); /*开辟一个新的单元*/
if(!p) /*如果指针p为空*/
{printf("\nOut of memory."); /*输出内存溢出*/
return (head); /*返回头指针,下同*/
}
printf("Enter the num(0:list end):");
scanf("%s",p->num);
if(p->num[0]=='0') break; /*如果学号首字符为0则结束输入*/
printf("Enter the name:");
scanf("%s",p->name);
printf("Please enter the %d scores\n",3); /*提示开始输入成绩*/
s=0; /*计算每个学生的总分,初值为0*/
for(i=0;i<3;i++) /*3门课程循环3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0 || p->score[i]>100) /*确保成绩在0~100之间*/
printf("Data error,please enter again.\n");
}while(p->score[i]<0 || p->score[i]>100);
s=s+p->score[i]; /*累加各门成绩*/
}
p->sum=s; /*将总分保存*/
p->average=(float)s/3; /*先用强制类型转换将s转换成float型,再求平均值*/
p->order=0; /*未排序前此值为0*/
p->next=head; /*将头结点做为新输入结点的后继结点*/
head=p; /*新输入结点为新的头结点*/
}
return(head);
}
/* 显示全部记录函数*/
void print(STUDENT *head)
{int i=0; /* 统计记录条数*/
STUDENT *p; /*移动指针*/
clrscr();
p=head; /*初值为头指针*/
printf("\n************************************STUDENT************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Rec | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
i, p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}
/*查找记录函数*/
void search(STUDENT *head)
{STUDENT *p; /* 移动指针*/
char s[5]; /*存放姓名用的字符数组*/
clrscr();
printf("Please enter name for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p->name,s) && p != NULL) /*当记录的姓名不是要找的,或指针不为空时*/
p=p->next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no num %s student on the list.\n",s); /*显示没有该学生*/
}
/*删除记录函数*/
STUDENT *delete(STUDENT *head)
{int n;
STUDENT *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/
char c,s[6]; /*s[6]用来存放学号,c用来输入字母*/
clrscr();
printf("Please enter the deleted num: ");
scanf("%s",s);
p1=p2=head; /*给p1和p2赋初值头指针*/
while(strcmp(p1->num,s) && p1 != NULL) /*当记录的学号不是要找的,或指针不为空时*/
{p2=p1; /*将p1指针值赋给p2作为p1的前驱指针*/
p1=p1->next; /*将p1指针指向下一条记录*/
}
if(strcmp(p1->num,s)==0) /*学号找到了*/
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("Are you sure to delete the student Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N') break; /*如果不删除,则跳出本循环*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,说明被删结点是首结点*/
head=p1->next; /*把第二个结点地址赋予head*/
else
p2->next=p1->next; /*否则将一下结点地址赋给前一结点地址*/
n=n-1;
printf("\nNum %s student have been deleted.\n",s);
printf("Don't forget to save.\n");break; /*删除后就跳出循环*/
}
}
}
else
printf("\nThere is no num %s student on the list.\n",s); /*找不到该结点*/
return(head);
}
/*排序函数*/
STUDENT *sort(STUDENT *head)
{int i=0; /*保存名次*/
STUDENT *p1,*p2,*t,*temp; /*定义临时指针*/
temp=head->next; /*将原表的头指针所指的下一个结点作头指针*/
head->next=NULL; /*第一个结点为新表的头结点*/
while(temp!=NULL) /*当原表不为空时,进行排序*/
{
t=temp; /*取原表的头结点*/
temp=temp->next; /*原表头结点指针后移*/
p1=head; /*设定移动指针p1,从头指针开始*/
p2=head; /*设定移动指针p2做为p1的前驱,初值为头指针*/
while(t->average<p1->average&&p1!=NULL) /*作成绩平均分比较*/
{
p2=p1; /*待排序点值小,则新表指针后移*/
p1=p1->next;
}
if(p1==p2) /*p1==p2,说明待排序点值大,应排在首位*/
{
t->next=p1; /*待排序点的后继为p*/
head=t; /*新头结点为待排序点*/
}
else /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
{
t->next=p1; /*t的后继是p1*/
p2->next=t; /*p2的后继是t*/
}
}
p1=head; /*已排好序的头指针赋给p1,准备填写名次*/
while(p1!=NULL) /*当p1不为空时,进行下列操作*/
{
i++; /*结点序号*/
p1->order=i; /*将结点序号赋值给名次*/
p1=p1->next; /*指针后移*/
}
printf("Sorting is sucessful.\n"); /*排序成功*/
return (head);
}
/*插入记录函数*/
STUDENT *insert(STUDENT *head,STUDENT *new)
{STUDENT *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一个结点*/
p0=new; /*p0指向要插入的结点*/
printf("\nPlease enter a new record.\n"); /*提示输入记录信息*/
printf("Enter the num:");
scanf("%s",new->num);
printf("Enter the name:");
scanf("%s",new->name);
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新记录的总分,初值为0*/
for(i=0;i<3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&new->score[i]);
if(new->score[i]>100||new->score[i]<0)
printf("Data error,please enter again.\n");
}while(new->score[i]>100||new->score[i]<0);
sum1=sum1+new->score[i]; /*累加各门成绩*/
}
new->sum=sum1; /*将总分存入新记录中*/
new->average=(float)sum1/3;
new->order=0;
if(head==NULL) /*原来的链表是空表*/
{head=p0;p0->next=NULL;} /*使p0指向的结点作为头结点*/
else
{while((p0->average<p1->average)&&(p1->next!=NULL))
{p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*p1后移一个结点*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0; /*插到原来第一个结点之前*/
else p2->next=p0; /*插到p2指向的结点之后*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;} /*插到最后的结点之后*/
}
n=n+1; /*结点数加1*/
head=sort(head); /*调用排序的函数,将学生成绩重新排序*/
printf("\nStudent %s have been inserted.\n",new->name);
printf("Don't forget to save the new file.\n");
return(head);
}
/*保存数据到文件函数*/
void save(STUDENT *head)
{FILE *fp; /*定义指向文件的指针*/
STUDENT *p; /* 定义移动指针*/
char outfile[10];
printf("Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,为只写方式*/
{
printf("Cannot open the file\n");
return; /*若打不开则返回菜单*/
}
printf("\nSaving the file......\n");
p=head; /*移动指针从头指针开始*/
while(p!=NULL) /*如p不为空*/
{
fwrite(p,LEN,1,fp); /*写入一条记录*/
p=p->next; /*指针后移*/
}
fclose(fp); /*关闭文件*/
printf("Save the file successfully!\n");
}
/* 从文件读数据函数*/
STUDENT *load()
{STUDENT *p1,*p2,*head=NULL; /*定义记录指针变量*/
FILE *fp; /* 定义指向文件的指针*/
char infile[10];
printf("Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为只读方式*/
{
printf("Can not open the file.\n");
return(head);
}
printf("\nLoading the file!\n");
p1=(STUDENT *)malloc(LEN); /*开辟一个新单元*/
if(!p1)
{
printf("Out of memory!\n");
return(head);
}
head=p1; /*申请到空间,将其作为头指针*/
while(!feof(fp)) /*循环读数据直到文件尾结束*/
{
if(fread(p1,LEN,1,fp)!=1) break; /*如果没读到数据,跳出循环*/
p1->next=(STUDENT *)malloc(LEN); /*为下一个结点开辟空间*/
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*指针后移,新读入数据链到当前表尾*/
}
p2->next=NULL; /*最后一个结点的后继指针为空*/
fclose(fp);
printf("You have success to read data from the file!\n");
return (head);
}
H. C语言编程,定义包含学号、姓名和成绩的学生信息结构类型,完成以下功能:
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#defineMAX50
typedefstruct{
charstuId[10];
charname[20];
intscore;
}Student;
voidinput(Studentstudents[MAX],intn)
{
inti;
for(i=0;i<n;i++){
scanf("%s",students[i].stuId);
scanf("%s",students[i].name);
scanf("%d",&students[i].score);
}
}
voidsort(Studentstudents[MAX],intn)
{
inti,j;
Studenttemp;
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(students[j].score<students[j+1].score){
memcpy(&temp,&students[j],sizeof(Student));
memcpy(&students[j],&students[j+1],sizeof(Student));
memcpy(&students[j+1],&temp,sizeof(Student));
}
}
}
}
voidprint(Studentstudents[MAX],intn)
{
inti;
for(i=0;i<n;i++){
printf("%s%s%d ",students[i].stuId,students[i].name,students[i].score);
}
}intmain()
{
Studentstudents[MAX];
intn;
scanf("%d",&n);
input(students,n);
sort(students,n);
printf("输出: ");
print(students,n);
system("pause");
return0;
}
运行结果:
I. C语言编程学生信息结构体,保存文本文件或二进制文件中.求大神知道啊。
你需要用到打开文本或者创建一个文本写入相关的知识,当然还有结构体;
(“结构体”跟“C语言文件”请自行网络)
//首先定义学生结构体
struct strudent{
int score;
int num;}
然后当然他有各种数据
然后在main()函数中用到fopen;
*f=fopen("x://student.txt,w);用‘w’打开的话若没有文件会创建一个文件;
if(f=NULL)//判断打开是否失败
printf("打开失败");//接下去就是写入,你可以使用二进制文件操作
******下面是资料*****
3.1 数据块存取函数
函数原型:
_CRTIMP size_t __cdecl fread(void *, size_t, size_t, FILE *);
_CRTIMP size_t __cdecl fwrite(const void *, size_t, size_t, FILE *);
当要求一次存取一组数据(如,一个数组、一个结构体变量的值),fread和fwrite函数可以解决该类问题。它们的调用形式一般为:
fread(buffer, size, count, fp);
fwrite(buffer, size, count, fp);
buffer:对于fread来说,指的是读入数据的存放地址;对于fwrite来说,是要输出数据的地址。
size:读写数据时,每笔数据的大小
count:读写数据的笔数
**********************
所以剩下的就是
fwrite(student_1, sizeof(student), 1, f);
当然,为了更好的存储数据,你完全可以把student设计成数组。够了吧,需要用到的资料网络或者个人博客上都有,我连关键字都留给你了。手打~
J. C语言结构体---查找学生信息
struct student
{
char NO[2];
char name[9];
int chinese;
int math;
};
main函数:char name[10];
两个地方的name长度不一。如果运行输入9个字符,struct 里面的name就没有结尾的'\0'
两个name要定义足够长,并且等长。