呵呵。。。谢谢你的指导。。。看来还是躲在宿舍里,静下心写的东西有点用。。。上了机房忙着忙那的。全部颠覆了!
人在江湖【走】,怎能不挨【刀】;为了能活【口】,唯有把己【超】!come on...
饿。静态外部变量为什么会每次都回0呢????
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <string.h>
#define MAX 3 //通讯录组的大小
struct day
{
int month;
int day1;
};
struct mydata
{
char name[20];
struct day birthday;
char telnum[13];
};
static int g;//我把这边g的定义为静态外部变量。就是希望它不会随便改变。一共输入用户的个数就为g。
void input(struct mydata a[],int g); //调用输入函数
void output(struct mydata a[],int g); //调用输出函数
void search(struct mydata a[],int g); //调用查找函数
void array(struct mydata a[],int g); //调用排列函数
void delete1(struct mydata a[],int g); //调用删除函数
int main(void)
{
struct mydata a[MAX];
while(1)
{
int i;
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.return to system.\n");
printf("Please selete:");
scanf("%d",&i);
getchar();
switch (i)
{
case 1:input(a,g);break;
case 2:output(a,g);break;
case 3:search(a,g);break;
case 4:array(a,g);break;
case 5:delete1(a,g);break;
case 6:exit(0);
}
}
return 0;
}
void input(struct mydata a[],int g) //输入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));
g++;
printf("%d",g);
}
void array(struct mydata a[],int g) //排列
{
int i,j;
struct mydata t;
for(i=0;i<g-1;i++) //让人名从a-z排列
{
for(j=1;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 g) //输出
{
int i;
for(i=0;i<=g-1;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[],int g) //查找
{
char keyname[20];int i,flag=1;
struct mydata t;
printf("input the search name:"); //查找人物!
gets(keyname);
for(i=0;(i<=g-1)&&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[],int g) //删除
{
char keyname[20];int i,flag=1;
printf("input the search name:");
gets(keyname);
for(i=0;(i<=g-1)&&flag;i++)
{
if(strcmp(keyname,a[i].name)!=0);
else
{
a[MAX]=a[i];g-=1;
printf("the data is delete!!!\n");
flag=0;
}
}
array(a,g);
if(flag)printf("Can't find the data!!\n");
}
[此贴子已经被作者于2006-6-28 14:42:52编辑过]
我用CF的构成工程做的。有几个版块列。
首先是这个。错误在这块里~可是这块单独运行是正常的(除了静态外部变量会变之外)
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#define MAX 3
struct day
{
int month;
int day1;
};
struct mydata
{
char name[20];
struct day birthday;
char telnum[13];
}a[MAX];
static int g=0;
void input(struct mydata a[],int g);
void output(struct mydata a[],int g);
void search(struct mydata a[],int g);
void array(struct mydata a[],int g);
int main2(void)
{
int i;
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("Please selete:");
scanf("%d",&i);
getchar();
switch (i)
{
case 1:input(a,g);break;
case 2:output(a,g);break;
case 3:search(a,g);break;
case 4:array(a,g);break;
}
return 0;
}
void input(struct mydata a[],int g) //输入data
{ //CF中的错误就在这一行。。。
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));
g--;
}
void array(struct mydata a[],int g) //排列
{
int i,j;
struct mydata t;
for(i=0;i<g-1;i++) //让人名从a-z排列
{
for(j=1;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 g) //输出
{
int i;
for(i=0;i<=g-1;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[],int g) //查找
{
char keyname[20];int i,flag=1;
struct mydata t;
printf("input the search name:"); //查找人物!
gets(keyname);
for(i=0;(i<=g-1)&&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 delete(struct mydata a[],int g) //删除
{
char keyname[20];int i,flag=1;
printf("input the search name:");
gets(keyname);
for(i=0;(i<=g-1)&&flag;i++)
{
if(strcmp(keyname,a[i].name)!=0);
else
{
a[MAX]=a[i];g-=1;
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");
}
这个是MAIN
#include <stdio.h>
#define MAX 3
struct day
{
int month;
int day1;
};
struct mydata
{
char name[20];
struct day birthday;
char telnum[20];
}a[MAX];
static int g=0;
void creat1();
void load(struct mydata a[],int g);
void save(struct mydata a[],int g);
int main2(void);
int main(void)
{
int i;
printf("0.creat a new file.\n");
printf("1.load the file.\n");
printf("2.save the file.\n");
printf("please select:");
scanf("%d",&i);
getchar();
switch (i)
{
case 0:creat1();break;
case 1:load(a,g);break;
case 2:save(a,g);break;
}
return 0;
}
这个是文件读取:
#include <stdio.h>
#define MAX 3
struct day
{
int month;
int day1;
};
struct mydata
{
char name[20];
struct day birthday;
char telnum[20];
}a[MAX];
void load(struct mydata a[],int g)
{
FILE *fp1;
int i;
if((fp1=fopen("tongxun.bin","rb"))==NULL)
{
printf("The file can not open!!!\n");
return;
}
for (i=0;i<g;i++)
if(fread(&a[i],sizeof(struct mydata),1,fp1)!=1)
{printf("file load error!\n");
return;
}
fclose(fp1);
}
这个是文件记录:
#include <stdio.h>
#define MAX 3
struct day
{
int month;
int day1;
};
struct mydata
{
char name[20];
struct day birthday;
char telnum[20];
}a[MAX];
void save(struct mydata a[],int g)
{
FILE *fp1;
int i,j;
struct mydata t;
for(i=0;i<g-1;i++) //让人名从a-z排列
{
for(j=1;j<g;j++)
{
if(strcmp(a[i].name,a[j].name)>0)
{t=a[i];a[i]=a[j];a[j]=t;}
}
}
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;
}
fclose(fp1);
}
建立文件的:
#include <stdio.h>
#include<stdlib.h>
#define MAX 3
void creat1()
{
FILE *fp1;
fp1=fopen("tongxun.txt","w+");
if((fp1!=NULL))
printf("File creat success!!!\n");
else
printf("File creat failure!!!\n");
}
可恶。CF出错。掉了点东西!