-

- 加为好友
- 发送私信
- 在线聊天
|
| 发表于:2007-11-26 16:16:35 楼主 |
#include <iostream.h> #include <fstream.h> #include <string.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <windows.h> #define MAXNUM 10 //宏定义,最多存储的学生人数 int num=0; char file1[]="comfile1.txt"; struct stu{ char name[20]; char addr[40]; char tel[13]; char telw[12]; char post[7]; char email[20]; char qq[10]; }stu1[MAXNUM]; void display(int i); void infopen(); void infsave(); void dataInput(); void dataOutput(); void dataRepire(); void dataFind(); FILE *fp; void main() { char select='\0'; infopen(); while(true){ while(select <'1' ¦ ¦select>'5') { fflush(stdin); system("cls"); cout < <"同 学 通 讯 录" < <"\n\n"; cout < <"1---数据录入" < <endl; cout < <"2---数据修改" < <endl; cout < <"3---数据输出" < <endl; cout < <"4---查询" < <endl; cout < <"5---退出" < <endl; cout < <"------------" < <endl; cout < <"请选择:" < <endl; cin.get(select); } switch(select) { case '1':dataInput();break; case '2':dataRepire();break; case '3':dataOutput();break; case '4':if(num>0)dataFind();break; case '5':infsave();return; } select='\0'; } } void dataInput(){ system("cls"); while(num <MAXNUM) { cout < <"输入第" < <num+1 < <"个同学信息:" < <endl; cout < <"姓名:"; cin>>stu1[num].name; cout < <"地址:"; cin>>stu1[num].addr; cout < <"固定电话:"; cin>>stu1[num].tel; cout < <"移动电话:"; cin>>stu1[num].telw; cout < <"邮编:"; cin>>stu1[num].post; cout < <"EMAIL:"; cin>>stu1[num].email; cout < <"QQ:"; cin>>stu1[num].qq; num++; char select1='\0'; while(!(select1=='n' ¦ ¦select1=='N' ¦ ¦select1=='y' ¦ ¦select1=='Y')) { system("cls"); cout < <"\n\n继续输入?(Y/N)_"; cin>>select1; } if(select1!='n' ¦ ¦select1!='N')break; } } void dataOutput() { int i; system("cls"); for(i=0;i <num;i++) { display(i); } } void dataRepire() { cout < <"No programming!!!" < <endl; getch(); } void dataFind() { int i,j=-1; char namefind[20]; system("cls"); cout < <"输入同学姓名:"; cin>>namefind; for(i=0;i <num;i++) if(strcmp(namefind,stu1[i].name)==0) { j=i; break; } if(j!=-1) display(j); else cout < <"没有该同学信息!!!" < <endl; } void infopen() { char ch; int i=0; fp = fopen(file1,"r" ); //只读方式打开数据文件 /* 判断有无该数据文件 */ if( !fp ) //如果没有数据文件 { fp=fopen(file1,"W"); num=0; } else //如果有数据文件 { for( i=0; i <MAXNUM; i++ ) { if( feof(fp) ) //判断文件结束否 break; ch=fgetc( fp ); //从文件读取下一个字符 if(ch==EOF) //判断到文件结束否 break; fflush(stdin); //刷新标准输入 fseek(fp,i*sizeof(struct stu),SEEK_SET); fread(&stu1[i],sizeof(struct stu),1,fp); ++num; } fclose(fp); } } void infsave() { int i=0; fp=fopen(file1,"w+"); if( !fp ) //错误处理 { printf("保存失败!\n"); return; } for( i=0; i <num; ++i ) //保存操作的循环 fwrite(&stu1[i],sizeof(struct stu),1,fp); if( ferror(fp) ) //检测是否在保存的时候遇到错误,如果错误的话提示再保存 { printf("保存失败,请重新保存!"); clearerr(fp); fclose(fp); return; } else { puts("保存成功!"); fclose(fp); } } void display(int i) { cout < <"第" < <i+1 < <"个同学信息:" < <endl; cout < <"姓名:" < <stu1[i].name < <endl; cout < <"地址:" < <stu1[i].addr < <endl; cout < <"固定电话:" < <stu1[i].tel < <endl; cout < <"移动电话:" < <stu1[i].telw < <endl; cout < <"邮编:" < <stu1[i].post < <endl; cout < <"EMAIL:" < <stu1[i].email < <endl; cout < <"QQ:" < <stu1[i].qq < <endl; getch(); } |
|
|
|
0
修改
删除
举报
引用
回复
| |