高分悬赏一C语言程序设计题(简单行编辑)

2024-11-07 11:22:22
推荐回答(1个)
回答1:

我有一个电话本程序似乎差不多喔,就差统计了。发给你看下,参考下吧。

#include
#include
#define MAX 300
FILE *book;
char number[20],name[10];
main()
{
if((book=fopen("cell phone book.txt","a+"))==NULL)
{
printf("此为第一次运行。\n");
}
else
printf("电话本己打开。\n");
start();
fclose(book);
}
start()
{
int c;
int n=1;
do
{
printf("1·查找电话。\n");
printf("2·加入新电话。\n");
printf("3·显示整个电话本。\n");
printf("4·删除电话。\n");
printf("5·退出。\n");
c=getch();
printf("您选择了%c\n",c);

switch (c)
{
case '1':search();
break;
case '2':adduser();
break;
case '3':print();
break;
case '4':del();
break;
case '5':n=0;
break;
default:
break;
}
}while(n!=0);
}
del()
{
char temp[30],name[10];
FILE *tempbook;
int n,p=0;
tempbook=fopen("temp.txt","w");
fclose(tempbook);
tempbook=fopen("temp.txt","a");
book=fopen("cell phone book.txt","r");
printf("输入要删除的人名。\n");
scanf("%s",name);
while(fgets(temp,MAX,book)!=NULL)
{
for(n=0;n<10;n++)
{
if(temp[n]!=name[n])
{
p=1;
break;
}
if(temp[n]==NULL)
break;
}
if(p==1)
{
fwrite(temp,2,5,tempbook);
fwrite(&temp[10],1,12,tempbook);
fwrite("\n",1,1,tempbook);
}
p=0;
}
fclose(tempbook);
fclose(book);
book=fopen("cell phone book.txt","w");
fclose(book);
book=fopen("cell phone book.txt","a");
tempbook=fopen("temp.txt","r");
while(fgets(temp,MAX,tempbook)!=NULL)
{
fwrite(temp,2,5,book);
fwrite(&temp[10],1,12,book);
fwrite("\n",1,1,book);
}
fclose(tempbook);
fclose(book);
}
search()
{
char tempbook[30],name[10],p=0;
int n=0;
book=fopen("cell phone book.txt","rt");
printf("输入要查找的姓名。\n");
scanf("%s",name);
while(fgets(tempbook,MAX,book)!=NULL)
{
if(tempbook[0]==name[0]&&tempbook[1]==name[1]&&tempbook[2]==name[2]&&tempbook[3]==name[3]&&tempbook[4]==name[4])
printf("%s%12s\n",tempbook,&tempbook[10]);
}
fclose(book);
}
adduser()
{
book=fopen("cell phone book.txt","a+");
printf("请输入姓名:\n");
scanf("%s",name);
printf("请输入电话号码:\n");
scanf("%s",number);
fwrite(name,2,5,book);
fwrite(number,1,12,book);
fwrite("\n",1,1,book);
fclose(book);
}
print()
{
char c;
book=fopen("cell phone book.txt","rt");
do
{
c=fgetc(book);
putchar(c);
}while(c!=EOF);
fclose(book);
}