❶ 求一C语言设计
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MENU_NUM 7
#define N 3
typedef struct s1
{
char no[11]; /*学号由10个字符组成*/
char name[15]; /*学生姓名*/
float score[N]; /*各门课成绩*/
float sum; /*总分*/
float average; /*平均分*/
int order; /*名次*/
struct s1 *next; /*指向后继结点的指针*/
}STUDENT; /*定义结构体类型*/
STUDENT * head=NULL;
void InputData( STUDENT * ptr );
void PrintMenu( );
int Menu_Select( );
void Init( );
void create( );
void print( ) ;
void Delete( );
void append();
void computer();
main()
{
int key;
while(1) {
key=Menu_Select();
switch(key)
{
case 0: Init();
//初试化链表。也就是若链表不为空则释放链表中所有数据,将head置为空(NULL)
break;
case 1: create();
//创建链表,输入数据
break;
case 2: Delete();
//删除一个指定学号的记录数据
break;
case 3: print();
//打印链表中所有数据
break;
case 4: computer();
//计算链表中所有人的总分和平均分
break;
case 5: append();
//追加一个数据到链表的尾部
break;
case 6: Init();
//释放链表
exit(0);
}
};
}
void PrintMenu( )
{
int i;
char * menu[]={
"0. Init list",
"1. Enter list",
"2. Delete a record from list",
"3. Print list",
"4. Compute the score",
"5. Insert record to list",
"6. Quit"
};
printf("\n\n");
for (i=0;i<MENU_NUM;i++)
printf("%s\n",menu[i]);
printf("\nEnter your choice(0-6):");
}
int Menu_Select( )
{
int key;
PrintMenu( );
scanf("%d",&key);
return key;
}
void Init( ) //初始化单链表。也就是释放链表中的所有数据
{
STUDENT *p,*ptr;//定义两个临时指针变量p,ptr
p=head;ptr=head;//将两个临时变量指向头指针head ;
while(ptr!=NULL)
{
ptr=ptr->next;//ptr指向下一个结构数据
free(p);//释放p所指向的结构数据的内存
p=ptr;//将p指向ptr所指向的数据
}
head=NULL;//将head指向NULL
}
void create( ) //创建单链表
{
STUDENT *pt, *pth=NULL; //定义两个指针变量:pt指向即将申请新的数据内存,pth指向当前数据
while(1)
{
pt=(STUDENT *)malloc(sizeof(STUDENT));//让pt指向新申请的内存空间
InputData(pt);//输入数据,存放到pt所指向的结构数据。注意让pt的next指向NULL
if (strcmp(pt->no,"@")==0)
{
free(pt);//释放pt所指向的内存空间
break;//退出循环
}
else if (head==NULL)
{
pth=pt;
head=pt;//将头指针head和pth指向pt
}
else
{
pth->next=pt; //将pth的next指向pt;
pth=pt; //将pth指向pt;
}
};
}
void print( ) //打印单链表中所有数据
{ int i=0;
STUDENT *p;//第一个指针p
p=head;//将p指向head
printf("\n");
printf("******************************STUDENT******************************\n");
printf("|rec|no | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|---|----------|----------------|----|----|----|------|-----|-----|\n");
//打印表头
while (p!=NULL)
{
printf("|%3d|%10s|%-16s|%4.1f|%4.1f|%4.1f|%6.2f|%5.1f|%5d|\n",
++i,p->no,p->name,p->score[0],p->score[1],p->score[2],
p->sum,p->average,p->order); //打印p所指向的结构中的所有数据。注意打印数据间的分隔线
p=p->next;//将p指向p的下一个结构数据
}
printf("********************************END********************************\n");//打印表尾
}
void Delete( ) //删除一个记录
{
STUDENT *p,*pth;//定义两个指针p,pth
char no[11];//定义一个整数no(用来存储输入的学号)
printf("intput delete no\n");
scanf("%s",no);//用输入语句输入一个学号存储到no中
p=head;pth=head;//将p和pth都指向头指针
if (strcmp(p->no,no)==0) //也就是若头指针指向的数据需要删除
{
head=head->next;//将head指针指向head的下一个数据;
free(p);//释放p所指向的数据
}
else
{
p=p->next;//将p指向p的下一个数据
while ( p!=NULL)
{
if (strcmp(p->no,no)==0) //找到了要删除的数据
{
pth->next=p->next;//将pth的next指向p的next
free(p);//释放p
break;//退出循环
}
else
{
pth=pth->next;//将pth指向pth的next 或 将pth指向p
p=p->next;//将p指向p的next
}
}
}
}
void append()
{
STUDENT *p,*pth;//定义两个指针变量p,pth
pth=head;//将pth指向head
while ( pth->next!=NULL)
{
pth=pth->next;//ptr指向ptr的next
}
p=(STUDENT *)malloc(sizeof(STUDENT));//将p指向新申请的内存空间
InputData(p);p->next=NULL;//数据数据存储到p所指向的内存空间,注意将p的next置为NULL
pth->next=p;//将ptr的next指向p
}
void InputData( STUDENT *ptr )
{ int i;
printf("enter no:");
scanf("%s",ptr->no);
if (strcmp(ptr->no,"@")==0 ) return;
printf("enter name:");
scanf("%s",ptr->name);
for(i=0;i<3;i++)
{
printf("shuru chengji\n");
scanf("%f",&ptr->score[i]);
}
ptr->sum=0;
ptr->average=0;
ptr->order=0;
ptr->next=NULL;
}
void computer()
{
STUDENT *p;
p=head;
for(;p;p=p->next)
{
p->sum=p->score[0]+p->score[1]+p->score[2];
p->average=(p->score[0]+p->score[1]+p->score[2])/3;
}
}
❷ 高手们,C语言编学生档案管理,帮忙下
# include<iostream.h>
# include<string.h>
# include<stdio.h>
# include<stdlib.h>
# include<fstream.h>
//*****定义一个学生原子的的数据结构*****//
struct stuatom
{
char *name;
int id;
char sex;
float math, eng, comp, totll, aver;
void show();
void setup();
};
//*********定义一系列对学生的操作**********//
class student
{
private:
stuatom ob[100];
int stulen;
public:
student();
void input();
void order();
void save();
void Query();
void read();
void add();
void del();
};
//********对学生数据的初始化(类的构造函数)**********//
student::student()
{
//用for循环对全部数组中的数据初始化
for(int i=0;i<100;i++)
{
ob[i].math=ob[i].eng=ob[i].comp =ob[i].totll =ob[i].aver =0;
ob[i].id =0;
ob[i].sex =' ';
ob[i].name =NULL;
}
this->stulen =0;
}
//********输入学生的数据,并判断是否在规定数据域内*******//
void stuatom::setup()
{
char n[20]; char s;
int b;
//如果输入学好在数据域内,跳出循环并且赋值。
//如果不再数据域内,一直循环到输入数据符合数据域为止
do {
cout<<" 学号: ";
cin>>b;
if(b>1020||b<1001)
cout<<"Bad data input!!"<<endl<<endl;
}while (b<1001||b>1020);
id=b;
//如果输入学好在数据域内,跳出循环并且赋值。
//如果不再数据域内,一直循环到输入数据符合数据域为止
do{
name=new char[strlen(n)+1];
cout<<" 姓名: ";
cin>>n;
if( strlen(n)>6 || strlen(n)<4 )
cout<<"Bad data input!!"<<endl<<endl;
}while ( strlen(n)>6 && strlen(n)<4 );
strcpy(name,n);
cout<<" 性别(m/f):" ;
cin>>s;
//如果输入学好在数据域内,跳出循环并且赋值。
//如果不再数据域内,一直循环到输入数据符合数据域为止
while (s!='m' && s!='f')
{
cout<<"Bad data input!!"<<endl<<endl;
cout<<" 性别(m/f):";
cin>>s;
}
sex=s;
float m, e, co;
cout<<" 数学: ";
cin>>m;
//如果输入学好在数据域内,跳出循环并且赋值。
//如果不再数据域内,一直循环到输入数据符合数据域为止
while (m<0 || m>100)
{
cout<<"Bad data input!!"<<endl<<endl;
cout<<" 数学: ";
cin>>m;
}
math=m;
cout<<" 英语: ";
cin>>e;
//如果输入学好在数据域内,跳出循环并且赋值。
//如果不再数据域内,一直循环到输入数据符合数据域为止
while (e<0 || e>100)
{
cout<<"Bad data input!!"<<endl<<endl;
cout<<" 英语: ";
cin>>e;
}
eng=e;
cout<<" 计算机: ";
cin>>co;
//如果输入学好在数据域内,跳出循环并且赋值。
//如果不再数据域内,一直循环到输入数据符合数据域为止
while (co<0 || co>100)
{
cout<<"Bad data input!!"<<endl<<endl;
cout<<" 计算机: ";
cin>>co;
}
comp=co;
totll=math+eng+comp;
aver=(math+eng+comp)/3;
}
//*******按照规定格式把该学生的数据显示在屏幕上******//
void stuatom::show()
{
cout.setf(ios::left);
cout.width(6);
cout<<""<<id<<" ";
cout.width(8);
cout<<name;
cout.width(10);
cout<<sex;
cout.width(9);
cout<<math;
cout.width(9);
cout<<eng;
cout.width(11);
cout<<comp;
cout.width(10);
cout<<totll<<aver<<endl;
}
//**************输入学生的数据***********************//
void student::input()
{
int n;
cout<<endl<<"输入将要录入的学生数目: ";
cin>>n;
int j;
//通过循环输入要求输入学生个数的学生的数据。
for(j=0; j<n; j++)
{
cout<<" 输入学生信息 "<<j<<endl;
ob[j].setup();
}
this->stulen=n; //学生个数赋值
//学生数据显示格式
system("cls");
cout<<endl<<"----------------------------- 学生信息表 ------------------------------------"<<endl;
cout<<endl<<" 学号 姓名 性别 数学 英语 计算机 总分 平均分"<<endl;
//通过循环输出所有学生数据。
for(j=0; j<n; j++)
{
ob[j].show();
}
cout<<endl;
cout<<" 是否保存? (Y/N): ";
char Y;
cin>>Y;
system("cls");
}
//**************按照一定格式显示所要查询学生的信息。**************//
void student::Query()
{
int x , i;
cout<<endl<<" 输入要查询学生的学号: ";
cin>>x;
cout<<endl<<" 学号 姓名 性别 数学 英语 计算机 总分 平均分"<<endl;
for(i=0;i<=this->stulen ;i++)
{ if (x==ob[i].id)
{
cout.setf(ios::left);
cout.width(6);
cout<<""<<ob[i].id<<" ";
cout.width(8);
cout<<ob[i].name;
cout.width(10);
cout<<ob[i].sex;
cout.width(9);
cout<<ob[i].math;
cout.width(9);
cout<<ob[i].eng;
cout.width(11);
cout<<ob[i].comp;
cout.width(10);
cout<<ob[i].totll<<ob[i].aver<<endl;
}
}
getchar();
}
//*******************保存学生数据**************************//
void student::save()
{
int i;
ofstream outfile;
outfile.open("list.txt",ios::trunc);
if(!outfile)
{
cout<<"Cannot open output file!\n,";
}
//通过循环把所有的学生数据保存在list.txt里边。
for(i=0; i<this->stulen; i++)
{
outfile<<ob[i].id<<" "<<ob[i].name<<" "<<ob[i].sex<<" "<<
ob[i].math<<" "<<ob[i].eng<<" "<<ob[i].comp<<" "<<ob[i].totll<<" "<<ob[i].aver<<endl;
}
outfile.close();
}
//*************显示所有学生数据*********************//
void student::read()
{
int i;
system("cls");
cout<<endl<<"----------------------------- 学生信息表 ------------------------------------"<<endl;
cout<<endl<<" 学号 姓名 性别 数学 英语 计算机 总分 平均分"<<endl;
for(i=0; i<this->stulen; i++)
{
ob[i].show();
}
getchar();
}
//*******************一个学生的数据****************//
void student::add()
{
int i, d=this->stulen ;
cout<<"输入要添加学生的信息:"<<endl;
ob[d].setup();
cout<<endl<<"----------------------------- 学生信息表 ------------------------------------"<<endl;
cout<<endl<<" 学号 姓名 性别 数学 英语 计算机 总分 平均分"<<endl;
for(i=0; i<=d; i++)
{
ob[i].show();
}
ofstream fout("list.txt",ios::app);
if(!fout)
{
cout<<"Cannot open output file!\n,";
}
//把添加的学生数据添加到list.txt里边去。
fout<<ob[d].id<<" "<<ob[d].name<<" "<<ob[d].sex<<" "<<
ob[d].math<<" "<<ob[d].eng<<" "<<ob[d].comp<<" "<<ob[d].totll<<" "<<ob[d].aver<<endl;
fout.close();
getchar();
}
//*********************删除指定名字学生的数据*******************//
void student::del()
{
int i,p; char x[8];
cout<<" 输入要删除学生名字:"<<endl;
cin>>x;
//通过for循环查找要删除学生的姓名
for(i=0; i<stulen; i++)
{
if(strcmp(ob[i].name,x)==0)
{
p=i;
cout<<endl<<"学号 姓名 性别 数学 英语 计算机 总成绩 平均成绩"<<endl;
cout<<ob[i].id<<" "<<ob[i].name<<" "<<ob[i].sex<<" "<<ob[i].math<<" "<<ob[i].eng<<" "<<ob[i].comp<<" "<<ob[i].totll<<" "<<ob[i].aver<<endl;
break;
}
else
continue;
}
cout<<endl<<"----------------------------- 学生信息表 ------------------------------------"<<endl;
cout<<endl<<" 学号 姓名 性别 数学 英语 计算机 总分 平均分"<<endl;
//删除了之后,应该把后面的数据往前移,把要删除的数据覆盖,并且学生长度减1
stulen--;
for(i;i<stulen;i++)
{
ob[i]=ob[i+1];
}
this->read ();
cout<<" 删除成功!"<<endl;
getchar();
}
//***********把学生成绩排序******************//
void student::order()
{
int k,j;
float t; char n[20];
//排序算法。
for(j = 0; j<=(2-1); j++)
{
for(k=1; k<=(2-j); k++)
{
if(ob[k].totll < ob[k + 1].totll)
{
t = ob[k].totll;
ob[k].totll = ob[k+1].totll;
ob[k+1].totll = t;
strcpy(n, ob[k].name);
strcpy(ob[k].name, ob[k+1].name);
strcpy(ob[k+1].name, n);
}
cout<<" 成绩排名:"<<endl;
cout<<" 姓名 总成绩 名次"<<endl;
for(k=0; k<=stulen; k++)
{
cout<<" ";
cout.setf(ios::left);
cout.width(9);
cout<<ob[k].name;
cout.width(9);
cout<<ob[k].totll<<k<<endl;
}
getchar();
}
}
}
//********************选择菜单。*****************//
void menu()
{
cout<<"\n\n";
cout<<"------------------ 学生成绩系统 -----------------"<<endl<<endl;
cout<<"\t\t1.录入与保存学生信息.\n";
cout<<"\t\t2.读取学生信息.\n";
cout<<"\t\t3.删除学生信息.\n";
cout<<"\t\t4.追加学生信息.\n";
cout<<"\t\t5.查询学生信息.\n";
cout<<"\t\t6.显示成绩名次.\n";
cout<<"\t\t7.退出系统......\n\n\n";
cout<<"\t\t请选择功能项: ";
}
//---------------------------------------------------------------------------------------
void main()
{
student a;
while(1)
{
int SEL;
system("cls");
menu();
cin>>SEL;
switch(SEL)
{
case 1:
system("cls"); a.input();a.save();break;
case 2:
system("cls"); a.read(); break;
case 3:
system("cls"); a.del(); break;
case 4:
system("cls"); a.add();break;
case 5:
system("cls"); a.Query();break;
case 6:
system("cls"); a.order();break;
case 7:
cout<<endl<<" 按任意键退出.... "<<endl;
getchar();
exit(0);
default:
cout<<"Bad input!!\n";
break;
}
}
}
❸ 选题一:学生档案管理 1.题目描述 编写一个程序来管理学生档案,系统能实现以下功能: 输入信息:学
这里有一个问题
要在指定的位置插入学生信息
不知道这个位置是如何定义的?
这样的程序一般都是使用链表来完成的
而如果要进行排序的话我个人更喜欢使用双向链表来完成
不过题目要求使用单链表(没有规定死?那还是用双链表吧)
程序的代码量说大其实并不大
不过对于普通的那种作业来说代码量就稍显多了点
如果不想自己写这个代码的话
支付宝60元我帮你写好了
❹ 求C语言学生档案管理界面的源代码
简短的代码 原创 花了2小时搞的#include "stdafx.h"
#include "stdio.h"
#include "string.h"int main(int argc, char* argv[])
{
struct days
{
int year;
int mon;
int day;
};
struct max
{
int num;
char name[20];
float cpp;
float data;
float english;
struct days mon;
}stu[100]={0,"",0,0,0,0,0,0};
int a,b,c,yanz=0;
char name[50];
FILE *fp;
printf("****************************ZX学生管理系统1.0测试版*****************************\n");
loop:printf("1.通过学号查找信息\n2.通过姓名查找信息\n3.添加学生信息\n4.删除学生信息\n5.文件操作\n请输入操作代码(1~6):");
scanf("%d",&a);
switch(a)
{
case 1:
printf("请输入学生学号:");
scanf("%d",&c);
for(b=0;b<100;b++)
if(stu[b].num==c)
printf("学号:%d\n姓名:%s\nC++:%f分\n数据结构:%f分\n英语:%f分\n平均分%f\n出生日期:%d年%d月%d日\n\n",stu[b].num,stu[b].name,stu[b].cpp,stu[b].data,stu[b].english,(stu[b].cpp+stu[b].data+stu[b].english)/3,stu[b].mon.year,stu[b].mon.mon,stu[b].mon.day);
break;
case 2:
printf("请输入学生姓名:");
scanf("%s",&name);
for(b=0;b<100;b++)
if((strcmp(stu[b].name,name))==0 && stu[b].num!=0)
printf("学号:%d\n姓名:%s\nC++:%f分\n数据结构:%f分\n英语:%f分\n平均分%f\n出生日期:%d年%d月%d日\n\n",stu[b].num,stu[b].name,stu[b].cpp,stu[b].data,stu[b].english,(stu[b].cpp+stu[b].data+stu[b].english)/3,stu[b].mon.year,stu[b].mon.mon,stu[b].mon.day);
break;
case 3:
for(b=0;b<100;b++)
{
if(stu[b].num==0)
{printf("请输入新学生的学号:");<br> scanf("%d",&stu[b].num);<br> printf("请输入新学生的姓名:");<br> scanf("%s",&stu[b].name);<br> printf("请输入新学生的C++分:");<br> scanf("%f",&stu[b].cpp);<br> printf("请输入新学生的数据结构分:");<br> scanf("%f",&stu[b].data);<br> printf("请输入新学生的英语分数:");<br> scanf("%f",&stu[b].english);<br> printf("请输入新学生的出生日期(****,**,**):");<br> scanf("%d,%d,%d",&stu[b].mon.year,&stu[b].mon.mon,&stu[b].mon.day);<br> break;}
}
if(b>=99) printf("空间已满!\n");
break;
case 4:
printf("请输入要删除的那个学生学号:");
scanf("%d",&c);
for(b=0;b<100;b++)
if(stu[b].num==c)
{
stu[b].num=NULL;
printf("删除成功!\n");
yanz=1;
}
if(yanz==0) printf("找不到学号为%d学生的信息!",c);
break;
case 5:
printf("1.保存信息文件\n2.新建文件\n3.读取文件\n请输入操作代码:");
scanf("%d",&c);
if(c==1)
{
printf("请输入输入文件路径:");
scanf("%s",&name);
if((fp=fopen(name,"ab"))!=0)
{
for(b=0;b<100;b++)
if(stu[b].num!=0)fwrite(&stu[b],sizeof(struct max),1,fp);
fclose(fp);
}
else printf("保存文件失败,可能文件被保护或磁盘写满!\n");
}
else if(c==2)
{
printf("请输入输入文件名:");
scanf("%s",&name);
fp=fopen(name,"wb");
fclose(fp);
}
else if(c==3)
{
printf("请输入输入文件路径:");
scanf("%s",&name);
if((fp=fopen(name,"rb"))!=0)
{
for(b=0;b<100;b++)
fread(&stu[b],sizeof(struct max),1,fp);
fclose(fp);
}
else printf("读取文件失败,可能文件不存在或被保护!\n");
}
else printf("输入错误!\n");
}
goto loop;
return 0;
}
❺ 2、学生档案管理系统(至少包括:学号,姓名,身份证号码,家庭地址)急求 谢谢
唉,曾经我也这样,csdn上有许多,可以下载的,不是学生管理系统的也可以改一下。
❻ 用C语言设计学生档案管理
/*用C语言设计学生档案管理
1. 题目描述
编写一个程序来管理学生档案,系统能实现以下功能:
输入信息:学生信息的输入;
修改信息:对学生信息进行添加、删除与修改;
查询:能够根据学号或姓名查询某个学生的信息;
输出:输出所有学生信息或查询学生信息的结果。
2. 设计提示
1)先确定学生档案管理的数据结构。如每个学生信息:学号、姓名、性别、年龄、地址 ……等,每个数据项各用什么数据类型;
2)划分实现学生档案管理的功能模块:如主菜单、输入数据、修改、查询、输出等功能,并确定各功能模块的实现算法。
3)画出各模块的流程图或S-R图;
4)选择C语言的技术:普通数组、结构体数组、函数、指针、单链表或文件等。
5)编写程序代码。*/
#include<stdio.h>
#include<stdlib.h>
#include<string>
typedef struct student{
char ID[10];
char name[10];
char sex[3];
int age;
char addr[30];
struct student *next;
}stu;
void Input(stu *&head)
{
stu *temp,*current;
temp=(stu *)malloc(sizeof(stu));
printf("输入学生信息:\n");
printf("学号\t姓名\t性别\t年龄\t地址\t\n");
fflush(stdin);
scanf("%s%s%s%d%s",temp->ID,temp->name,temp->sex,&temp->age,temp->addr);
temp->next=NULL;
if(head==NULL)
{head=(stu *)malloc(sizeof(stu));head->next=temp;}
else
{
current=head->next;
while(current->next)
current=current->next;
current->next=temp;}
printf("添加成功\n");
system("pause");
}
void FindByID(stu *&head)
{
if(head==NULL)
{printf("数据为空\n");
system("pause");return ;}
else
{stu *current =head->next;
bool flag=0;
char a[10];
printf("输入要查询的学号:\n");
fflush(stdin);
scanf("%s",a);
do
{
if(strcmp(current->ID,a)==0)
{flag=1;break;}
current=current->next;
}while(current!=NULL);
if(flag)
{
printf("学号:%s 姓名:%s 性别:%s 年龄:%d 地址:%s\n",current->ID,current->name,current->sex,current->age,current->addr);
system("pause");
}
else
{
printf("没有找到\n");
system("pause");
}
}
}
void FindByName(stu *&head)
{
if(head==NULL)
{printf("数据为空\n");
system("pause");return ;}
else
{
stu *current =head->next;
bool flag=0;
char a[10];
printf("输入要查询的姓名:\n");
fflush(stdin);
scanf("%s",a);
do
{
if(strcmp(current->name,a)==0)
{flag=1;break;}
current=current->next;
}while(current!=NULL);
if(flag)
{
printf("学号:%s 姓名:%s 性别:%s 年龄:%d 地址:%s\n",current->ID,current->name,current->sex,current->age,current->addr);
system("pause");
}
else
{
printf("没有找到\n");
system("pause");
}
}
}
void Delete(stu *&head)
{
if(head==NULL)
{printf("数据为空\n");
system("pause");return ;}
else
{ stu *current =head->next;
stu *prev=NULL;
char a[10];
printf("输入要删除的学号:\n");
fflush(stdin);
scanf("%s",a);
while(current!=NULL)
{
if(strcmp(current->ID,a)==0)break;
prev=current;
current=current->next;
}
if(prev==NULL)
{head->next=current->next;free(current);}
else
{prev->next=current->next;
free(current);}
printf("删除成功\n");
system("pause");
}
}
void Show(stu *&head)
{
stu *current =head->next;
if(current==NULL)
{printf("数据为空\n");
system("pause");return ;}
printf("学号\t姓名\t性别\t年龄\t地址\t\n");
while(current!=NULL)
{
printf("%s\t%s\t%s\t%d\t%s\n",current->ID,current->name,current->sex,current->age,current->addr);
current=current->next;
}
system("pause");
}
void Modify(stu *&head)
{
if(head==NULL)
{printf("数据为空\n");
system("pause");return ;}
bool flag=0;
char a[10];
printf("输入要查询的学号:\n");
fflush(stdin);
scanf("%s",a);
stu *current=head->next;
do
{
if(strcmp(current->ID,a)==0)
{flag=1;break;}
current=current->next;
}while(current!=NULL);
if(flag)
{
printf("学号:%s 姓名:%s 性别:%s 年龄:%d 地址:%s\n",current->ID,current->name,current->sex,current->age,current->addr);
printf("请重新输入该学生信息:\n");
printf("学号\t姓名\t性别\t年龄\t地址\t\n");
scanf("%s%s%s%d%s",current->ID,current->name,current->sex,¤t->age,current->addr);
printf("修改成功\n");
printf("pause");
}
else
{
printf("没有找到\n");
system("pause");
}
}
void Print()
{
system("cls");
printf(" 请选择功能(按0退出)\n");
printf(" 1------添加学生\n");
printf(" 2------通过学号查询学生\n");
printf(" 3------通过姓名查询学生\n");
printf(" 4------删除学生\n");
printf(" 5------修改学生\n");
printf(" 6------显示所有学生\n");
}
int main()
{
char ch;
stu *head=NULL;
do
{
Print();
fflush(stdin);
ch=getchar();
//system("cls");
switch(ch)
{
case '1':Input(head);break;
case '2':FindByID(head);break;
case '3':FindByName(head);break;
case '4':Delete(head);break;
case '5':Modify(head);break;
case '6':Show(head);break;
}
}while(ch!='0');
}
❼ C语言程序设计--班级档案管理系统
给,已经编译运行确认了:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define N 10
int M=0;
struct student
{
char num[13];
char name[20];
char sex;
char address[30];
int age;
};
typedef struct LB
{
struct student XINXI;
struct LB *next;
}LB_1,*LB_2;
void CAIDANG()
{
printf(" \t \t \t (欢迎使用本班级档案管理系统)\t\t\t\t\n");
printf("★*★*★*★*★*★*★*★*★*★ 欢迎进入我们的系统 ★*★*★*★*★*★*★*★*★*★\n\n");
printf("1.录入学生资料\t\t\t\t\t4.查询学生信息\n\n");
printf("2.修改学生信息\t\t\t\t\t5.删除学生信息\n\n");
printf("3.保存学生信息\t\t\t\t\t0.退出档案系统\n\n");
printf(" \n");
printf("★*★*★*★*★*★*★*★*★*★ 欢迎进入本系统 ★*★*★*★*★*★*★*★*★*★\n");
}
/*文本颜色*/
void color()
{
textbackground(8);
textcolor(9);
clrscr();
}
void Wrong()
{
printf("\n对不起您的输入错误!\n");
}
void SORRY()
{
printf("\n对不起该同学现没有任何信息!\n");
}
void printe(LB_1 *p)/* 本函数用于输出英文 */
{
printf(" %-2s %s\t %s\t%s\t %d\t \n",p->XINXI.num,p->XINXI.name,p->XINXI.sex,p->XINXI.address,p->XINXI.age);
}
/* 该函数用于定位链表中符合要求的接点,并返回该指针 */
LB_1 *Locate(LB_2 l,char findmess[],char nameornum[])
{
LB_1 *r;
if(strcmp(nameornum,"num")==0) /* 按学号查询 */
{
r=l->next;
while(r!=NULL)
{
if(strcmp(r->XINXI.num,findmess)==0)
return r;
r=r->next;
}
}
else if(strcmp(nameornum,"name")==0) /* 按姓名查询 */
{
r=l->next;
while(r!=NULL)
{
if(strcmp(r->XINXI.name,findmess)==0)
return r;
r=r->next;
}
} return 0;
}
/*以下是增加学生信息的函数*/
void TIANJIA(LB_2 l)
{
LB_1 *p,*r,*s;
char num[13];
r=l; s=l->next;
while(r->next!=NULL) /*些处循环一直到最后*/
r=r->next;
while(1)
{
printf(" 请输入添加的学生学号(输入'0'返回上一级菜单:)");
scanf("%s",num);
if(strcmp(num,"0")==0)
break;
while(s)
{
if(strcmp(s->XINXI.num,num)==0)
{
printf("(必看提示):学号为'%s'的学生已有信息,若要修改请你输入'2 修改'!\n",num);
printe(s);
printf("\n");
return;
}
s=s->next;
} p=(LB_1 *)malloc(sizeof(LB_1));
strcpy(p->XINXI.num,num);
printf(" 请你输入学号:");
scanf("%s",p->XINXI.num);
getchar();
printf(" 请你输入姓名:");
scanf("%s",p->XINXI.name);
getchar();
printf(" 请你输入性别:");
scanf("%s",p->XINXI.sex);
getchar();
printf(" 请你输入地址:");
scanf("%s",p->XINXI.address);
getchar();
printf(" 请你输入年龄:");
scanf("%d",&p->XINXI.age);
getchar();
/* 下面是把指针变量转到链表的下一个结点中以便循环的时候使用 */
p->next=NULL;
r->next=p;
r=p; M=1;
}
}
/*以下是删除学生信息的函数*/
void SHANCHU(LB_2 l)
{
int sel;
LB_1 *p,*r;
char findmess[20];
if(!l->next)
{
printf("\n******(必看提示):对不起,现文件中没有信息:所以您不能查询!\n");
return;
}
printf("\n <必看提示> <以学号删除请输入1\n> <以姓名删除请输入2\n> PLEASE:");
scanf("%d",&sel);
if(sel==1)
{
printf("请输入要删除的学生的学号PLEASE:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{ r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n*******(必看提示):该学生的信息已完全删除成功!\n");
M=1;
}
else
SORRY();
}
else if(sel==2)
{
printf("请输入要删除的学生的姓名PLEASE:");
scanf("%s",findmess);
p=Locate(l,findmess,"name");
if(p)
{ r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n******(必看提示):该学生信息已成功删除!\n");
M=1;
}
else
SORRY();
}
else
Wrong();
}
/*以下是查询学生信息*/
void CHAXIONG(LB_2 l)
{ int sel;
char findmess[20];
LB_1 *p;
if(!l->next)
{ printf("\n******(必看提示):对不起,现文件中没有信息:所以您不能查询!\n");
return;
}
printf("\n以学号查询请输入1:\n以姓名查询请输入2:\n以性别查询请输入3:\n以性别查询请输入4:\n PLEASE:");
scanf("%d",&sel);
if(sel==1)/*输入学号查询*/
{
printf("请你输入要查找的学号:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{ printf("\t\t\t\t查找结果\n");
printf(" 学号:%s\n姓名:%s\n性别:%s\n地址:%s\n年龄:%d\n",p->XINXI.num,p->XINXI.name,p->XINXI.sex,p->XINXI.address,p->XINXI.age);
}
else
SORRY();
}
if(sel==2) /* 输入姓名查询 */
{
printf("请你输入要查找的姓名:");
scanf("%s",findmess);
p=Locate(l,findmess,"name");
if(p)
{ printf("\t\t\t\t查找结果\n");
printf(" 学号:%s\n姓名:%s\n性别:%s\n地址:%s\n年龄:%d\n",p->XINXI.num,p->XINXI.name,p->XINXI.sex,p->XINXI.address,p->XINXI.age);
}
else
SORRY();
}
else
Wrong();
}
/*以下是修改学生信息的函数*/
void XIUGEI(LB_2 l)
{
LB_1 *p;
long int i,j;
char findmess[20];
if(!l->next)
{ printf("\n(必看提示):对不起,现文件中没有信息:所以您不能查询!\n");
return;
}
for(i=1;i<=2;i++)
{
printf("\n\n\n\n\n");
printf("请输入密码:");
scanf("%s",&j);
if(j==2007)break;
else
{if(j!=2007)
return(CAIDANG());
}
}
printf("请你输入要修改的学生学号:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
{ printf("请你输入新学号(原来是%s):",p->XINXI.num);
scanf("%s",p->XINXI.num);
printf("请你输入新姓名(原来是%s):",p->XINXI.name);
scanf("%s",p->XINXI.name);
printf("请你输入新性别(原来是%s):",p->XINXI.sex);
scanf("%s",p->XINXI.sex);
printf("请你输入新地址(原来是%s):",p->XINXI.address);
scanf("%s",p->XINXI.address);
printf("请你输入新年龄(原来是%d):",p->XINXI.age);
scanf("%s",p->XINXI.age);
getchar();
M=1;
}
else
SORRY();
}
/*保存在文件中*/
void BAOCONG(LB_2 l)
{
FILE* fp;
LB_1 *p;
int flag=1,count=0;
fp=fopen("c:\\lyg","wr");
if(fp==NULL)
{
printf("\n=====>提示:重新打开文件时发生错误!\n");
exit(1);
}
p=l->next;
while(p)
{
if(fwrite(p,sizeof(LB_1),1,fp)==1)
{ p=p->next;
count++;
}
else
{ flag=0;
break;
}
}
if(flag)
{ printf("\n 提示;正在保存文件 共保存了%d条信息\n",count);M=0;
}
fclose(fp);
}
/*以下是主函数*/
void main()
{
LB_2 l;/* 连表 */
FILE *fp; /* 文件指针 */
int sel;long i,j,d;
char ch;
char jian;
int count=0;
LB_1 *p,*r;
color();
clrscr();
l=(LB_1*)malloc(sizeof(LB_1));
l->next=NULL;
r=l;
fp=fopen("C:\\lyg","rb");
if(fp==NULL)
{
printf("\t\t\t\t\n\n");
printf("\n");
printf(" *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*\n"
" *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*\n"
" Welcome to come to our system!!! \n"
" You can only inquire your own messages! If you want to see about other \n"
" students' messages,you need first to send inquirment to them,asking for \n"
" agreement,then you can visit his/her messages! \n"
" Have a good time! now you can come in!!! \n"
" *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*\n"
" *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*\n"
" 注意:系统启动码为 : ILOVEMYSCHOOLVERYMUCH !!! \n"
" *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*\n"
" *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*\n");
printf("欢迎使用,本系统中现没有任何记录,您要先创建文件请按以下操做进行PLEASE:\n");
printf("\n 提示:文件还不存在,是否创建?(y/n)请做选择--\n");
printf("\n");
scanf("%c",&jian);
if(jian=='y'||jian=='Y')
{fp=fopen("C:\\lyg","wb");
clrscr();
}
{for(i=1;i<=2;i++)
{
printf("\n\n\n\n\n");
printf("请输入密码:");
scanf("%ld",&j);
if(j==2007)break;
else
{if(j!=2007)return;
}
}
}
exit(0);clrscr();
}
while(!feof(fp))
{
p=(LB_1*)malloc(sizeof(LB_1));
if(fread(p,sizeof(LB_1),1,fp)) /* 将文件的内容放入链表中 */
{
p->next=NULL;
r->next=p;
r=p; /*把该结点放入链表中*/
count++;
}
}
fclose(fp); /* 关闭文件 */
while(1)
{
CAIDANG();
printf("请你选择操作:");
scanf("%d",&sel);
clrscr();
printf("\n\n\n");
if(sel==0)
{
if(M==1)
{ getchar();
printf("\n(必看提示):链表中的信息已经改动,如果确认把改动保存在文件中请做选择(y/n)?\n");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
BAOCONG(l);
}
break;clrscr();
}
switch(sel)
{
case 1: TIANJIA(l);break;
case 4: CHAXIONG(l);break;
case 3: BAOCONG(l);break;
case 2: XIUGEI(l);break;
case 5: SHANCHU(l);break;
default: getchar();break;
}
} end:
getchar();
}
❽ 求c++学生档案管理系统 急!!
/*
这是本人刚学习C++不久时写的一个学生信息管理系统,基本功能没问题,至于算法,可能有些不是很好的地方。
*/
/*下面分几个文件,你把文件分别保存,最后打开“学生信息管理系统.cpp”直接编译、连接。此程序在VC6.0环境下测试通过。
*/
//文件“学生信息管理系统.cpp”
#include<iostream>
#include<fstream>
#include"common.h"
#include"信息录入.cpp"
#include"信息查询.cpp"
#include"信息排序.cpp"
#include"信息修改.cpp"
#include"信息删除.cpp"
using namespace std;
int main()
{
int a;
h1: cout<<" —*—*—学生信息管理系统—*—*—"<<endl<<endl;
cout<<"1----学生基本信息录入 2----学生信息查询"<<endl;
cout<<"3----学生信息排序 4----学生信息修改"<<endl;
cout<<"5----学生信息删除 0----退出"<<endl;
ifstream infile("record_n.txt",ios::in);
infile>>record_n; //读取record_n,获知文件student.dat中保存有几个学生的数据
infile.close();
h2: cout<<"已经记录有"<<record_n<<"个学生的数据,请选择:";
cin>>a;
switch(a)
{
case 0:return 0;break;
case 1:info_input();break;
case 2:info_inquire();break;
case 3:info_sort();break;
case 4:info_update();break;
case 5:info_delete();break;
default:cout<<"Error!"<<endl<<endl;goto h2;
}
cout<<endl;
goto h1;
return 0;
}
//文件“信息修改.cpp”
#include<iostream>
#include<fstream>
#include <iomanip>
using namespace std;
/*————————————信息修改函数——————————————*/
void info_update()
{
int i,j,answer,num;
student stud1[n],stud2[n];
cout<<"请输入要修改的学生学号(返回请输入0):";
cin>>num;
if(num==0)goto h0;
else
{
ifstream infile("student.dat",ios::binary);
if(!infile)
{
cerr<<"Open file error!"<<endl;
exit(1);
}
if(record_n==0)cout<<"Error!\n没有记录"<<endl;
else
{
for(i=0;i<record_n;i++) //读取所有数据到一个数组
{
infile.read((char *)&stud1[i],sizeof(stud1[i]));
}
}
/*进行对比,找到相应学号*/
infile.close();
for(i=0;i<record_n;i++)
{
if(num==stud1[i].num) //判断,如果符合查询条件则输出到屏幕
{
cout<<"学号:"<<setw(20)<<stud1[i].num<<" 姓名:"<<setw(20)<<stud1[i].name<<endl;
cout<<"性别:"<<setw(20)<<stud1[i].sex<<" 出生年月:"<<setw(10)<<stud1[i].birthday.year<<"年"<<stud1[i].birthday.month<<"月 "<<endl;
cout<<"电话号码:"<<setw(16)<<stud1[i].phone<<" 来自省份:"<<setw(16)<<stud1[i].address<<endl<<endl;
j=i; //记录要修改学号的序号
}
}
for(i=0;i<record_n;i++) //此句和下一句if语句是判断有无该学号,无则提示
{
if(num==stud1[i].num)break;
}
if(i>=record_n)
{
cout<<"Error!\n未找到该学号,";
goto h0;
}
for(i=0;i<j;i++)
stud2[i]=stud1[i];
for(i=j+1;i<record_n;i++)
stud2[i]=stud1[i];
cout<<"请按照姓名、性别、出生年、月、电话号码、省份的顺序输入学生新信息(若要修改学号请删除该记录后重新录入新信息):"<<endl;
/*将对应学号的信息和其他学号信息记录到stud2[],清空原文件后写入文件*/
stud2[j].num=stud1[j].num;
cin>>stud2[j].name>>stud2[j].sex>>stud2[j].birthday.year>>stud2[j].birthday.month>>stud2[j].phone>>stud2[j].address;
ha: cout<<"是否确定修改该记录?确定输入1,返回输入0"<<endl;
cin>>answer;
if(answer==0)goto h0;
if(answer!=0&&answer!=1)
{
cout<<"Error!"<<endl;
goto ha;
}
if(answer==1)
{
fstream iofile("student.dat",ios::out|ios::binary);
if(!iofile)
{
cerr<<"Open file error!"<<endl;
exit(1);
}
for(i=0;i<record_n;i++)
{
iofile.write((char *)&stud2[i],sizeof(stud2[i]));
}
iofile.close();
cout<<"该学生信息修改完毕!"<<endl;
}
}
h0: cout<<endl;
}
//文件“信息删除.cpp”
#include<iostream>
#include<fstream>
#include <iomanip>
using namespace std;
/*————————————信息删除函数——————————————*/
void info_delete()
{
int i,j,answer,num;
student stud1[n],stud2[n];
cout<<"返回请输入0,删除全部学生信息请输入-1"<<endl;
cout<<"请输入要删除的学生学号:";
cin>>num;
if(num==-1)
{
ofstream outfile("record_n.txt",ios::out);
if(!outfile)
{
cerr<<"Open file error!"<<endl;
abort();
}
outfile<<0; //数据个数恢复为0
outfile.close();
fstream iofile("student.dat",ios::out|ios::binary);
if(!iofile)
{
cerr<<"Open file error!"<<endl;
exit(1);
}
iofile.close();
cout<<"学生信息已经全部删除"<<endl;
}
else
{
if(num==0)goto h0;
else
{
ifstream infile("student.dat",ios::binary);
if(!infile)
{
cerr<<"Open file error!"<<endl;
exit(1);
}
if(record_n==0)cout<<"Error!\n没有记录"<<endl;
else
{
for(i=0;i<record_n;i++) //读取所有数据到一个数组
{
infile.read((char *)&stud1[i],sizeof(stud1[i]));
}
}
infile.close();
/*进行对比,找到相应学号*/
for(i=0;i<record_n;i++)
{
if(num==stud1[i].num) //判断,如果符合查询条件则输出到屏幕
{
cout<<"学号:"<<setw(20)<<stud1[i].num<<" 姓名:"<<setw(20)<<stud1[i].name<<endl;
cout<<"性别:"<<setw(20)<<stud1[i].sex<<" 出生年月:"<<setw(10)<<stud1[i].birthday.year<<"年"<<stud1[i].birthday.month<<"月 "<<endl;
cout<<"电话号码:"<<setw(16)<<stud1[i].phone<<" 来自省份:"<<setw(16)<<stud1[i].address<<endl<<endl;
j=i; //记录要删除学号的序号
}
}
for(i=0;i<record_n;i++) //此句和下一句if语句是判断有无该学号,无则提示
{
if(num==stud1[i].num)break;
}
if(i>=record_n)
{
cout<<"Error!\n未找到该学号,";
goto h0;
}
ha: cout<<"是否确定删除该记录?确定输入1,返回输入0"<<endl;
cin>>answer;
if(answer==0)goto h0;
if(answer!=0&&answer!=1)
{
cout<<"Error!"<<endl;
goto ha;
}
if(answer==1)
{
/*将对应学号去掉后,剩余学生信息用stud2[]记录,清空原文件,再写入文件*/
for(i=0;i<j;i++)
stud2[i]=stud1[i];
for(i=j;i<record_n-1;i++)
stud2[i]=stud1[i+1];
ofstream outfile("record_n.txt",ios::out);
if(!outfile)
{
cerr<<"Open file error!"<<endl;
abort();
}
outfile<<record_n-1; //记录数据个数
outfile.close();
fstream iofile("student.dat",ios::out|ios::binary);
if(!iofile)
{
cerr<<"Open file error!"<<endl;
exit(1);
}
for(i=0;i<record_n-1;i++)
{
iofile.write((char *)&stud2[i],sizeof(stud2[i]));
}
iofile.close();
cout<<"该学号已经删除"<<endl;
}
}
}
h0: cout<<endl;
}
//文件“信息排序.cpp”
#include<iostream>
#include<fstream>
#include <iomanip>
using namespace std;
/*————————————信息排序函数————————————*/
void info_sort()
{
int i;
cout<<endl<<"1—按学号排序,2—按出生年月排序,0—返回"<<endl;
h3: cout<<"请选择:";
cin>>i;
switch(i)
{
case 0:goto h7;
case 1:sort_num();break;
case 2:sort_day();break;
default:cout<<"Error!"<<endl<<endl;goto h3;
}
h7: cout<<endl;
}
/*—————按照学号排序函数—————*/
void sort_num()
{
int i,j;
student stud1[n],t;
ifstream infile("student.dat",ios::binary);
if(!infile)
{
cerr<<"Open file error!"<<endl;
exit(1);
}
if(record_n==0)cout<<"Error!\n没有记录"<<endl;
else
{
for(i=0;i<record_n;i++)
{
infile.read((char *)&stud1[i],sizeof(stud1[i]));
}
}
for(i=0;i<record_n-1;i++) //用起泡法对所有学号进行排序
{
for(j=0;j<record_n-i-1;j++)
{
if(stud1[j].num>stud1[j+1].num)
{
t=stud1[j];
stud1[j]=stud1[j+1];
stud1[j+1]=t;
}
}
}
infile.close();
/*将排序好的数据写入文件*/
ofstream outfile("student.dat",ios::out|ios::binary);
if(!outfile)
{
cerr<<"Open file error!"<<endl;
exit(1);
}
for(i=0;i<record_n;i++)
{
outfile.write((char *)&stud1[i],sizeof(stud1[i]));
}
outfile.close();
cout<<"排序完成";
cout<<endl;
}
/*————按照出生年月排序函数————*/
void sort_day()
{
int i,j;
student stud1[n],t;
ifstream infile("student.dat",ios::binary);
if(!infile)
{
cerr<<"Open file error!"<<endl;
exit(1);
}
if(record_n==0)cout<<"Error!\n没有记录"<<endl;
else
{
for(i=0;i<record_n;i++)
{
infile.read((char *)&stud1[i],sizeof(stud1[i]));
}
}
/*以下对学生的出生年月进行排序*/
for(i=0;i<record_n-1;i++) //用起泡法对学生出生年份进行排序
{
for(j=0;j<record_n-i-1;j++)
{
if(stud1[j].birthday.year>stud1[j+1].birthday.year)
{
t=stud1[j];
stud1[j]=stud1[j+1];
stud1[j+1]=t;
}
}
}
for(i=0;i<record_n-1;i++) //用起泡法对出生年份相同的学生的出生月份进行排序
{
for(j=0;j<record_n-i-1;j++)
{
if(stud1[j].birthday.year==stud1[j+1].birthday.year)
{
if(stud1[j].birthday.month>stud1[j+1].birthday.month)
{
t=stud1[j];
stud1[j]=stud1[j+1];
stud1[j+1]=t;
}
}
}
}
infile.close();
/*将排序好的数据写入文件*/
ofstream outfile("student.dat",ios::out|ios::binary);
if(!outfile)
{
cerr<<"Open file error!"<<endl;
exit(1);
}
for(i=0;i<record_n;i++)
{
outfile.write((char *)&stud1[i],sizeof(stud1[i]));
}
outfile.close();
cout<<endl<<"排序完成"<<endl;
}
//文件“信息录入.cpp”
#include<iostream>
#include<fstream>
#include <iomanip>
using namespace std;
/*——————信息录入函数——————*/
void info_input()
{
int i=0,j;
student stud1[n];
student *p,*p1;
p=stud+i;
p1=stud1+record_n;
ifstream infile("student.dat",ios::binary);
if(record_n==0)cout<<endl;
else
{
if(!infile)
{
cerr<<"Open file error!"<<endl;
exit(1);
}
for(i=0;i<record_n;i++) //for语句读取记录到一个数组,便于进行比较,使输入学号不重复
{
infile.read((char *)&stud1[i],sizeof(stud1[i]));
}
infile.close();
cout<<endl;
}
cout<<"请按以下顺序输入学生信息:学号、姓名、性别、出生年、月、电话号码、省份";
cout<<endl<<"如果要终止录入学生信息,请输入0,然后回车"<<endl<<endl;
for(i=0;i<n;i++,p++,p1++)
{
cout<<"请输入第"<<record_n+i+1<<"个学生的信息:";
cin>>p->num;
p1->num=p->num;
if(p->num==0)break;
for(j=0;j<record_n+i;j++)
{
if(p->num==stud1[j].num)
{
cout<<"Error!\n输入的学号与已有记录重复!"<<endl;
goto hi;
}
}
cin>>p->name>>p->sex;
cin>>p->birthday.year>>p->birthday.month;
cin>>p->phone>>p->address;
ofstream outfile("student.dat",ios::app|ios::binary);
if(!outfile)
{
cerr<<"Open file error!"<<endl;
abort();
}
outfile.write((char *)&stud[i],sizeof(stud[i]));
outfile.close();
fstream iofile("record_n.txt",ios::out);
if(!iofile)
{
cerr<<"Open file error!"<<endl;
abort();
}
iofile<<record_n+i+1; //记录数据个数
iofile.close();
}
hi: cout<<endl;
}
//文件“信息查询.cpp”
#include<iostream>
#include<fstream>
#include <iomanip>
using namespace std;
/*——————————————信息查询函数————————————*/
void info_inquire()
{
int i;
h6: cout<<endl<<"1—按学号查询, 2—按姓名查询, 3—按性别查询,"<<endl;
cout<<"4—输出全部学生信息, 0—返回"<<endl;
h3: cout<<"请选择:";
cin>>i;
switch(i)
{
case 0:goto h7;
case 1:acc_num();break;
case 2:acc_name();break;
case 3:acc_sex();break;
case 4:output_all();break;
default:cout<<"Error!"<<endl<<endl;goto h3;
}
goto h6;
h7: cout<<endl;
}
/*————按照学号查询函数—————*/
void acc_num()
{
int i;
int num;
student stud1[n];
ifstream infile("student.dat",ios::binary);
if(!infile)
{
cerr<<"Open file error!"<<endl;
goto h5;
}
h4: cout<<"请输入要查询的学生的学号(返回请输入0):"<<endl;
cin>>num;
if(num==0)
{
infile.close();
goto h5;
}
for(i=0;i<record_n;i++) //for语句读取记录到一个数组
{
infile.read((char *)&stud1[i],sizeof(stud1[i]));
}
infile.close();
for(i=0;i<record_n;i++)
{
if(num==stud1[i].num) //判断,如果符合查询条件则输出到屏幕
{
cout<<"学号:"<<setw(20)<<stud1[i].num<<" 姓名:"<<setw(20)<<stud1[i].name<<endl;
cout<<"性别:"<<setw(20)<<stud1[i].sex<<" 出生年月:"<<setw(10)<<stud1[i].birthday.year<<"年"<<stud1[i].birthday.month<<"月 "<<endl;
cout<<"电话号码:"<<setw(16)<<stud1[i].phone<<" 来自省份:"<<setw(16)<<stud1[i].address<<endl<<endl;
}
}
for(i=0;i<record_n;i++) //此句和下一句if语句是判断有无该学号,无则提示
{
if(num==stud1[i].num)break;
}
if(i>=record_n)cout<<"Error!\n未找到该学号,";
goto h4;
h5: cout<<endl;
}
/*————按照姓名查询函数—————*/
void acc_name()
{
int i;
char name[20];
student stud1[n];
ifstream infile("student.dat",ios::binary);
if(!infile)
{
cerr<<"Open file error!"<<endl;
goto h5;
}
h4: cout<<"请输入要查询的学生的姓名(返回请输入0):"<<endl;
cin>>name;
if(strcmp(name,"0")==0)
{
infile.close();
goto h5;
}
for(i=0;i<record_n;i++)
{
infile.read((char *)&stud1[i],sizeof(stud1[i]));
}
infile.close();
for(i=0;i<record_n;i++)
{
if(strcmp(name,stud1[i].name)==0)
{
cout<<"学号:"<<setw(20)<<stud1[i].num<<" 姓名:"<<setw(20)<<stud1[i].name<<endl;
cout<<"性别:"<<setw(20)<<stud1[i].sex<<" 出生年月:"<<setw(10)<<stud1[i].birthday.year<<"年"<<stud1[i].birthday.month<<"月 "<<endl;
cout<<"电话号码:"<<setw(15)<<stud1[i].phone<<" 来自省份:"<<setw(15)<<stud1[i].address<<endl<<endl;
}
}
for(i=0;i<record_n;i++) //此句和下一句if语句是判断有无该姓名,无则提示
{
if(strcmp(name,stud1[i].name)==0)break;
}
if(i>=record_n)cout<<"Error!\n未找到该姓名,";
goto h4;
h5: cout<<endl;
}
/*————按照性别查询函数—————*/
void acc_sex()
{
int i;
char sex[5];
student stud1[n];
ifstream infile("student.dat",ios::binary);
if(!infile)
{
cerr<<"Open file error!"<<endl;
goto h5;
}
h4: cout<<"请输入要查询的学生的性别(返回请输入0):"<<endl;
cin>>sex;
if(strcmp(sex,"0")==0)
{
infile.close();
goto h5;
}
for(i=0;i<record_n;i++)
{
infile.read((char *)&stud1[i],sizeof(stud1[i]));
}
infile.close();
for(i=0;i<record_n;i++)
{
if(strcmp(sex,stud1[i].sex)==0)
{
cout<<"学号:"<<setw(20)<<stud1[i].num<<" 姓名:"<<setw(20)<<stud1[i].name<<endl;
cout<<"性别:"<<setw(20)<<stud1[i].sex<<" 出生年月:"<<setw(10)<<stud1[i].birthday.year<<"年"<<stud1[i].birthday.month<<"月 "<<endl;
cout<<"电话号码:"<<setw(15)<<stud1[i].phone<<" 来自省份:"<<setw(15)<<stud1[i].address<<endl<<endl;
}
}
for(i=0;i<record_n;i++) //此句和下一句if语句是判断有无该性别的学生记录,无则提示
{
if(strcmp(sex,stud1[i].sex)==0)break;
}
if(i>=record_n)cout<<"Error!\n未找到该性别的学生记录,";
goto h4;
h5: cout<<endl;
}
/*————输出全部学生信息—————*/
void output_all()
{
int i;
student stud1[n];
ifstream infile("student.dat",ios::binary);
if(!infile)
{
cerr<<"Open file error!"<<endl;
goto h0;
}
if(record_n==0)cout<<"Error!\n没有记录"<<endl;
else
{
for(i=0;i<record_n;i++)
{
infile.read((char *)&stud1[i],sizeof(stud1[i]));
cout<<"第"<<i+1<<"个学生数据:"<<endl;
cout<<"学号:"<<setw(20)<<stud1[i].num<<" 姓名:"<<setw(20)<<stud1[i].name<<endl;
cout<<"性别:"<<setw(20)<<stud1[i].sex<<" 出生年月:"<<setw(10)<<stud1[i].birthday.year<<"年"<<stud1[i].birthday.month<<"月 "<<endl;
cout<<"电话号码:"<<setw(15)<<stud1[i].phone<<" 来自省份:"<<setw(15)<<stud1[i].address<<endl<<endl;
}
}
infile.close();
h0: cout<<endl;
}
//文件“common.h”
/*—————————定义类—————————定义类——————*/
struct Day //定义结构体类型Day
{
int year;
int month;
};
class student //定义类student
{
public:
int num;
char name[20];
char sex[5];
Day birthday;
char phone[15];
char address[15];
};
/*—————全局变量、函数声明————全局变量、函数声明—————*/
const int n=200; //n=200,表示最多可记录200个学生的数据,可以修改,以适应需要
int record_n=0; //记录数据个数,初始化为0
student stud[n];
void info_input(); //信息录入函数
void info_inquire(),acc_num(),acc_name(),acc_sex(),output_all();//信息查询函数
void info_sort(),sort_num(),sort_day(); //信息排序函数
void info_update(); //信息修改函数
void info_delete(); //信息删除函数