崩溃呀。。。每天的开始都会这么的奇怪。昨天还运行得好好的程序。
现在居然保存不起来。还不出错。。。排列上也有问题。。。
人在江湖【走】,怎能不挨【刀】;为了能活【口】,唯有把己【超】!come on...
崩溃呀。。。每天的开始都会这么的奇怪。昨天还运行得好好的程序。
现在居然保存不起来。还不出错。。。排列上也有问题。。。
好咯~自己K掉了~我的程序可以说是完工了。西西。就是清屏以及creat1这个函数功能加强。
清屏么办法搞啦。我在CFREE里面写的。没有头文件。。。
creat1不知道要加什么功能。。。。。
代码入下。请帮忙测试测试。有待提高。。。。
[CODE]
#include <stdio.h>
#include <string.h>
#define MAX 100
struct day
{
int month;
int day1;
};
struct mydata
{
char name[20];
struct day birthday;
char telnum[20];
}a[MAX];
static int g;
void creat1();
void load(struct mydata a[]);
void save(struct mydata a[]);
int main2(void);
void input(struct mydata a[]);
void output(struct mydata a[]);
void search(struct mydata a[]);
void array(struct mydata a[]);
void delete1(struct mydata a[]);
void modify(struct mydata a[]);
int main(void)
{
while(1)
{
int i;
printf("0.creat a new file.\n");
printf("1.load the file.\n");
printf("please select:");
scanf("%d",&i);
getchar();
switch (i)
{
case 0:creat1();break;
case 1:load(a);printf("\n\n");main2();break;
}
}
return 0;
}
int main2(void)
{
while(1)
{int i;
printf("0.save the data.\n");
printf("1.input the data.\n");
printf("2.output the data.\n");
printf("3.search the data.\n");
printf("4.array the data.\n");
printf("5.delete the data.\n");
printf("6.modify the data.\n");
printf("Please selete:");
scanf("%d",&i);
getchar();
switch (i)
{
case 0:save(a);exit(0);
case 1:input(a);break;
case 2:output(a);break;
case 3:search(a);break;
case 4:array(a);break;
case 5:delete1(a);break;
case 6:modify(a);break;
}
}
return 0;
}
void input(struct mydata a[]) //输入data
{
char ch;
printf("input the data:");
do
{
printf("\ninput the name:");
gets(a[g].name);
printf("\ninput the birthday(mm-dd):");
scanf("%d-%d",&a[g].birthday.month,&a[g].birthday.day1);
getchar();
printf("\ninput the telphone number:");
gets(a[g].telnum);
printf("countuie?(press n or N to quie)");
scanf(" %c",&ch);
getchar();
}while((ch!=78)&&(ch!=110)&&(++g<MAX));
printf("the numbers is %d\n\n",++g);
}
void array(struct mydata a[]) //排列
{
int i,j;
struct mydata t;
for(i=0;i<g;i++) //让人名从a-z排列
{
for(j=0;j<g;j++)
{
if(strcmp(a[i].name,a[j].name)<0)
{t=a[i];a[i]=a[j];a[j]=t;}
}
}
}
void output(struct mydata a[]) //输出
{
int i;
for(i=0;i<g;i++) //输出所有人物!
{
printf("%s's birthday is %d.%d telphone number is %s.\n",
a[i].name,a[i].birthday.month,a[i].birthday.day1,a[i].telnum);
}
}
void search(struct mydata a[]) //查找
{
char keyname[20];int i,flag=1;
struct mydata t;
printf("input the search name:"); //查找人物!
gets(keyname);
for(i=0;(i<=g)&&flag;i++)
{
if(strcmp(keyname,a[i].name)!=0);
else
{
printf("Find the data:%s's birthday is %d.%d telphone number is %s.\n",
a[i].name,a[i].birthday.month,a[i].birthday.day1,a[i].telnum);
flag=0;
}
}
if(flag)printf("Can't find the data!!\n");
}
void delete1(struct mydata a[]) //删除
{
char keyname[20],ch;int i,j,flag=1;
printf("input the search name:");
gets(keyname);
for(i=0;(i<g)&&flag;i++)
{
if(strcmp(keyname,a[i].name)!=0);
else
{
printf("Find the data:%s's birthday is %d.%d telphone number is %s.\n",
a[i].name,a[i].birthday.month,a[i].birthday.day1,a[i].telnum);
printf("Do you want to delete the data?(Y/N)\n");
scanf("%c",&ch);
if((ch!=78)&&(ch!=110))
{
a[MAX]=a[i];
g-=1;
printf("the date is deleted!!!\n");
flag=0;
}
else return;
}
}
for(j=i-1;j<g;j++)
a[j]=a[j+1];
if(flag)printf("Can't find the data!!\n");
}
void creat1()
{
FILE *fp1;
fp1=fopen("tongxun.bin","w+");
if((fp1!=NULL))
printf("File creat success!!!\n");
else
printf("File creat failure!!!\n");
}
void load(struct mydata a[])
{
FILE *fp1;
int i;
char ch;
if((fp1=fopen("tongxun.bin","rb"))==NULL)
{
printf("The file can not open!!!\n");
exit(0);
}
for (i=0;i<MAX;i++)
if(fread(&a[i],sizeof(struct mydata),1,fp1)!=1)
{if(feof(fp1))break;
printf("file load error!\n");
return;
}
printf("The file is loaded!!!\n");
g=i;
fclose(fp1);
}
void save(struct mydata a[])
{
int i;
FILE *fp1;
array(a);
if((fp1=fopen("tongxun.bin","wb"))==NULL)
{
printf("The file can not open!!!\n");
return;
}
for(i=0;i<g;i++)
if(fwrite(&a[i],sizeof(struct mydata),1,fp1)!=1)
{printf("file save error!\n");
return;
}
printf("file save success!!!\n");
fclose(fp1);
printf("The file is closed!!!\n");
}
void modify(struct mydata a[])
{
char keyname[20];int i,flag=1;
struct mydata t;
printf("input the search name:"); //查找人物!
gets(keyname);
for(i=0;(i<=g)&&flag;i++)
{
if(strcmp(keyname,a[i].name)!=0);
else
{
printf("Find the data is:%s's birthday is %d.%d telphone number is %s.\n",
a[i].name,a[i].birthday.month,a[i].birthday.day1,a[i].telnum);
printf("Please input the modify data:\n");
printf("input the new name:");
gets(a[i].name);
printf("\ninput the new birthday(mm-dd):");
scanf("%d-%d",&a[i].birthday.month,&a[i].birthday.day1);
getchar();
printf("\ninput the new telphone number:");
gets(a[i].telnum);
flag=0;
}
}
if(flag)printf("Can't find the data!!\n");
}
[/CODE]
完工咯。。。提前了1天多1点点。。。
[CODE]
#include <stdio.h>
#include <string.h>
#define MAX 100
struct day
{
int month;
int day1;
};
struct mydata
{
char name[20];
struct day birthday;
char telnum[20];
}a[MAX];
static int g;
void creat1();
void load(struct mydata a[]);
void save(struct mydata a[]);
void input(struct mydata a[]);
void output(struct mydata a[]);
void search(struct mydata a[]);
void array(struct mydata a[]);
void delete1(struct mydata a[]);
void modify(struct mydata a[]);
int main(void)
{
while(1)
{
int i;
printf("\n\n***************List***************\n");
printf(" 1.creat a new file.\n"); //建立新文档
printf(" 2.load the file.\n"); //读取文档。如果建立过。可以直接读取。要在读取过后才适合进行下面的操作。
printf(" 3.input the data.\n"); //输入信息
printf(" 4.output the data.\n"); //输出信息
printf(" 5.search the data.\n"); //查找信息
printf(" 6.array the data.\n"); //排列,整理顺序
printf(" 7.delete the data.\n"); //删除信息
printf(" 8.modify the data.\n"); //修改选项
printf(" 9.save the data.\n"); //保存
printf(" 0.return to system.\n"); //退出
printf("***************List***************\n\n\n");
printf("Please selete(1-9):");
scanf("%d",&i);
getchar();
switch (i)
{
case 1:creat1();break;
case 2:load(a);break;
case 3:input(a);break;
case 4:output(a);break;
case 5:search(a);break;
case 6:array(a);break;
case 7:delete1(a);break;
case 8:modify(a);break;
case 9:save(a);exit(0);
case 0:exit(0);
}
}
return 0;
}
void input(struct mydata a[]) //输入data
{
char ch;
printf("Make sure you have load the data?(Y/N)");
scanf("%c",&ch);
getchar();
if((ch!=78)&&(ch!=110))
{
printf("input the data:");
do
{
printf("\n input the name:");
gets(a[g].name);
printf("\n input the birthday(mm-dd):");
scanf("%d-%d",&a[g].birthday.month,&a[g].birthday.day1);
getchar();
printf("\n input the telphone number:");
gets(a[g].telnum);
printf("\n countuie?(press n or N to quie)");
scanf(" %c",&ch);
getchar();
}while((++g<MAX)&&(ch!=78)&&(ch!=110));
}
else
load(a);
printf("Now you can input the data:");
do
{
printf("\n input the name:");
gets(a[g].name);
printf("\n input the birthday(mm-dd):");
scanf("%d-%d",&a[g].birthday.month,&a[g].birthday.day1);
getchar();
printf("\n input the telphone number:");
gets(a[g].telnum);
printf("\n countuie?(press n or N to quie)");
scanf(" %c",&ch);
getchar();
}while((++g<MAX)&&(ch!=78)&&(ch!=110));
}
void array(struct mydata a[]) //排列
{
int i,j;
struct mydata t;
for(i=0;i<g;i++) //让人名从a-z排列
{
for(j=0;j<g;j++)
{
if(strcmp(a[i].name,a[j].name)<0)
{t=a[i];a[i]=a[j];a[j]=t;}
}
}
}
void output(struct mydata a[]) //输出
{
int i;
printf("the all data listed:\n\n");
for(i=0;i<g;i++) //输出所有人物!
{
printf("No.%2d %s's birthday is %d.%d telphone number is %s.\n",i+1,
a[i].name,a[i].birthday.month,a[i].birthday.day1,a[i].telnum);
}
}
void search(struct mydata a[]) //查找
{
char keyname[20];int i,flag=1;
struct mydata t;
printf("input the search name:"); //查找人物!
gets(keyname);
for(i=0;(i<=g)&&flag;i++)
{
if(strcmp(keyname,a[i].name)!=0);
else
{
printf(" Find the data:%s's birthday is %d.%d telphone number is %s.\n\n",
a[i].name,a[i].birthday.month,a[i].birthday.day1,a[i].telnum);
flag=0;
}
}
if(flag)printf("Can't find the data!!\n");
}
void delete1(struct mydata a[]) //删除
{
char keyname[20],ch;int i,j,flag=1;
printf("\n\ninput the search name:");
gets(keyname);
for(i=0;(i<g)&&flag;i++)
{
if(strcmp(keyname,a[i].name)!=0);
else
{
printf(" Find the data:%s's birthday is %d.%d telphone number is %s.\n",
a[i].name,a[i].birthday.month,a[i].birthday.day1,a[i].telnum);
printf(" Do you want to delete the data?(Y/N)\n");
scanf("%c",&ch);
if((ch!=78)&&(ch!=110))
{
a[MAX]=a[i];
g-=1;
printf("the date is deleted!!!\n");
flag=0;
}
else return;
}
}
for(j=i-1;j<g;j++)
a[j]=a[j+1];
if(flag)printf("Can't find the data!!\n");
}
void creat1()
{
FILE *fp1;
fp1=fopen("tongxun.bin","w+");
if((fp1!=NULL))
printf("File creat success!!!\n");
else
printf("File creat failure!!!\n");
}
void load(struct mydata a[])
{
FILE *fp1;
int i;
char ch;
if((fp1=fopen("tongxun.bin","rb"))==NULL)
{
printf("The file can not open!!!\n");
exit(0);
}
for (i=0;i<MAX;i++)
if(fread(&a[i],sizeof(struct mydata),1,fp1)!=1)
{if(feof(fp1))break;
printf("file load error!\n");
return;
}
printf("The file is loaded!!!\n");
g=i;
fclose(fp1);
}
void save(struct mydata a[])
{
int i;
FILE *fp1;
array(a);
if((fp1=fopen("tongxun.bin","wb"))==NULL)
{
printf("The file can not open!!!\n");
return;
}
for(i=0;i<g;i++)
if(fwrite(&a[i],sizeof(struct mydata),1,fp1)!=1)
{printf("file save error!\n");
return;
}
printf("file save success!!!\n");
fclose(fp1);
printf("The file is closed!!!\n");
}
void modify(struct mydata a[])
{
char keyname[20];int i,flag=1;
struct mydata t;
printf("input the search name:"); //查找人物!
gets(keyname);
for(i=0;(i<=g)&&flag;i++)
{
if(strcmp(keyname,a[i].name)!=0);
else
{
printf(" Find the data is:%s's birthday is %d.%d telphone number is %s.\n",
a[i].name,a[i].birthday.month,a[i].birthday.day1,a[i].telnum);
printf("Please input the modify data:\n\n");
printf(" input the new name:");
gets(a[i].name);
printf("\n input the new birthday(mm-dd):");
scanf("%d-%d",&a[i].birthday.month,&a[i].birthday.day1);
getchar();
printf("\n input the new telphone number:");
gets(a[i].telnum);
flag=0;
}
}
if(flag)printf("Can't find the data!!\n");
}
[/CODE]
在C-FREE上运行得好好的。却在TC中编译都无法通过。实在郁闷。因此么有清屏了。还好刷得比较快。。。
[此贴子已经被作者于2006-6-29 15:02:21编辑过]