下面是一个通讯录,在C里面运行成功的,我想把它改为C++,但由于我对C++还不是很熟悉,在改的时候老是出现错误,特别是数据写入文件和打开文件,自己改了很久还是改不成功,麻烦高手帮忙改一下!~~~不胜感激~~~
#define N 100
#define SIZE 30
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct student
{ char num[SIZE];
char name[SIZE];
char tel[SIZE];
};
//函数声明
void myprint(); //显示菜单
int mycreat(struct student*p,int n); //输入通讯录
void mydisplay(struct student *p,int n); //显示通讯录
void mysearch(struct student*p,int n); //查找
int myadd(struct student *p, int n); //增加通讯录
int mydelete(struct student*p,int n); //删除
void sch_num(struct student *p,int n); //按学号查找
int loadinfo(struct student *p, int n); //载入通讯录信息
int saveinfo(struct student *p,int n); //保存通讯录信息
void main()
{ //文件定义
char choose,yes_no;
struct student record[N]; //存放通讯录信息,共100条
int total=0,flag; //通讯录总数
total=loadinfo(record,N); //从文件读取记录信息,不超过100条
do
{
myprint(); //显示菜单
printf("\tPlease select:");
choose=getche();
switch(choose)
{
case'1':
total= mycreat(record,total);
break;
case'2':
mydisplay(record,total);
break;
case'3':
mysearch(record,total);
break;
case'4':
total=myadd(record,total);
break;
case'5':
total=mydelete(record,total);
break;
case '6':
flag=saveinfo(record,total);
if(flag==1)
printf("\nSave successful!\n");
else
printf("\nSave failure!\n");
break;
case'0':
printf("\n");
break;
default:
printf("\n%c is a illegal option !\n",choose);
}
if(choose=='0') break;
printf("\nCan you contiune select(Y/N)? ");
do
{ yes_no=getch();
}
while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no!='Y'||yes_no!='y');
}
//显示菜单信息
void myprint()
{ //clrscr();
printf("\n\n");
printf("\n");
printf("请输入选项编号(0--6)\n");
printf("\t1---批量输入通讯录\n");
printf("\t2---显示通讯录\n");
printf("\t3---查询通讯录\n");
printf("\t4---添加通讯录\n");
printf("\t5---删除通讯录\n");
printf("\t6---保存通讯录\n");
printf("\t0---退出\n");
printf("\n");
}
//批量输入学生信息
int mycreat(struct student *p,int n)
{
int i=0;
int m;
//clrscr();
printf("添加前的记录,共有 <%d>个 :\n",n);
printf("\n记录号\t学号\t\t姓名\t\t电话号码\n");
i=0;
while((i<n)&&(strcmp(p->num,"")!=0))
{
printf("%d\t%s\t\t%s\t\t%s\n",i+1, p->num,p->name,p->tel);
i++;
p++;
}
printf("\nHow many students do you want to input(<%d):",N-n);
scanf("%d",&m);
i=0;
while(i<m)
{
printf("\n");
printf("please enter the %dth record:\n",i+1);
printf("学号:");
do
{
gets(p->num);
}while(strcmp(p->num,"")==0);
printf("姓名:");
gets(p->name);
printf("电话号码:");
gets(p->tel);
p++;
i++;
}
return m+n;
}
//显示学生信息
void mydisplay(struct student *p,int n)
{
/*clrscr();*/
int i=0;
printf("\n记录号\t学号\t\t姓名\t\t电话号码\n");
while(i<n)
{
printf("%d\t%s\t\t%s\t\t%s\n",i+1, p->num,p->name,p->tel);
p++;
i++;
}
}
void mysearch(struct student *p, int n)
{
mydisplay(p,n);
sch_num(p,n);
}
//按学号查询
void sch_num(struct student *p,int n)
{
int i,flag=0;
char nm[10];
printf("\n请输入要查询记录的学号:");
gets(nm);
for(i=0;i<n;i++)
if(strcmp(nm,p[i].num)==0)
{
printf("\n记录号\t学号\t\t姓名\t\t电话号码\n");
printf("%d\t%s\t\t%s\t\t%s\n",i+1,p[i].num,p[i].name,p[i].tel);
flag=1;
}
if(flag==0)
printf("无此人!\n");
}
//增加学生信息
int myadd(struct student *p, int n)
{
int i=0;
char c;
struct student t,*q;
//clrscr();
printf("添加前的记录:\n");
printf("\n记录号\t学号\t\t姓名\t\t电话号码\n"); i=0;
q=p;
while(i<n) //显示原有记录信息
{
printf("%d\t%s\t\t%s\t\t%s\n",i+1,q->num,q->name,q->tel);
i++;
q++;
}
do
{
printf("请输入新记录的学号");
do
{
gets(t.num);
} while(strcmp(t.num,"")==0); //学号不能为空
printf("请输入新记录的姓名:"); gets(t.name);
printf("请输入新记录的电话号码:"); gets(t.tel);
for(i=0;i<n;i++)
if(strcmp(t.num,(p+i)->num)==0 || strcmp(t.tel,(p+i)->tel)==0)
{ printf("学号或电话号码重复,要重新输入吗(Y/N)?");
c=getche();
putchar('\n');
break;
}
if(i==n) //找到最后,没有重复
{ *(p+i)=t;
printf("添加后的记录:\n");
printf("\n记录号\t学号\t\t姓名\t\t电话号码\n"); i=0;
for(i=0;i<n+1;i++)
printf("%d\t%s\t\t%s\t\t%s\n",i+1,(p+i)->num,(p+i)->name,p[i].tel);
break;
}
} while(c=='y'||c=='Y');
return n+1;
}
//删除学生信息
int mydelete(struct student *p, int n)
{ int i,x;
char nm[10];
// clrscr();
printf("删除前的记录:\n");
printf("\n记录号\t学号\t\t姓名\t\t电话号码\n"); i=0;
for(i=0;i<n;i++)
printf("%d\t%s\t\t%s\t\t%s\n",i+1,p[i].num,p[i].name,p[i].tel);
printf("\n请输入要删除记录的学号:");
gets(nm);
for(x=0; x<n; x++)
if(strcmp(nm,p[x].num)==0)
break;
if(x<n) //说明提前结束循环,找到人
{
for(i=x;i<n-1;i++)
{
p[i]=p[i+1];
}
printf("删除后的记录:\n");
printf("\n记录号\t学号\t\t姓名\t\t电话号码\n"); i=0;
for(i=0;i<n-1;i++)
printf("%d\t%s\t\t%s\t\t%s\n",i+1,p[i].num,p[i].name,p[i].tel);
}
else
printf("无此人\n");
return n-1;
}
//载入通讯录信息
int loadinfo(student *p, int n)
{
FILE *fp;
int i,all=0;
char filename[30]="txl.txt";
fp=fopen(filename,"r");
if(fp==NULL)
{
return 0;
}
fscanf(fp,"%d\t",&all); //读取记录总数
for(i=0; i<all; i++) //读取记录信息
{
fscanf(fp,"%s\t%s\t%s\n",p[i].num,p[i].name,p[i].tel);
}
fclose(fp); //关闭文件
return all;
}
int saveinfo(student *p,int n)
{
FILE *fp;
int i,all=0;
char filename[30]="txl.txt";
fp=fopen(filename,"w");
if(fp==NULL)
{
printf("\n***不能建立或打开文件%s!***",filename);
return 0;
}
fprintf(fp,"%d\t",n);
for(i=0; i<n; i++)
fprintf(fp,"%s\t%s\t%s\n",p[i].num,p[i].name,p[i].tel);
fclose(fp);
return 1;
}