标题:新人求助!麻烦各位帮我看看这程序错在哪里
只看楼主
myrepent
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-3-7
结帖率:0
已结贴  问题点数:20 回复次数:1 
新人求助!麻烦各位帮我看看这程序错在哪里
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct  
{
    char name[15];
    char tel[15];
    char Email[30];
    char QQ[15];
}people;
void Menu();
void Add();
int Isused(char *tel);
void Display();
void Serch();
void Modify();
void Delete();
void main()
{
    Menu();
}
void Menu()
{
    int m;
    while (1)
    {
        printf("\t\t\t欢迎使用手机通讯录");
        printf("**********************************************************");
        printf("**\t<<-1->>所有联系人\t\t**");
        printf("**\t<<-2->>添加加联系人\t\t**");
        printf("**\t<<-3->>查找加联系人\t\t**");
        printf("**\t<<-4->>修改联系人\t\t**");
         printf("**\t<<-5->>删除联系人\t\t**");
        printf("**\t<<-0->>退出通讯录\t\t**");
        printf("**********************************************************");
        printf("\n请按数字进行选择:");
        scanf("%d",&m);
        if (m>=0&&m<=5)
        {
            switch(m)
            {
            case 1:Display();break;
            case 2:Add();break;
            case 3:Serch();break;
            case 4:Delete();break;
            case 5:Modify();break;
            case 0:exit(0);
            }
            printf("\n\n操作完毕,请再次选择!");
        }
        else
            printf("\n\n选择错误,请再次选择!");
    }
}
void Display()
{
    people one;
    FILE *fp;
    if((fp=fopen("通讯录.txt"."rt"))==NULL)
    {
        printf("\n打开文件失败!");
        exit(0);
    }
    printf("\n\n%20s\n","---联系人列表---");
    printf("\n%-10s%-10s%-10s%-15s\n","姓名","联系电话","E-mail","QQ号码");
    fread(&one,sizeof(people),1,fp);
    while (!feof(fp))
    {
       printf("%-10s%-10s%-10s%-10s\n",one.name,one.tel,one.Email,one.QQ);
       fread(&one,sizeof(people),1,fp);
    }
    fclose(fp);
}
void Add()
{
    people one;
    FILE *fp;
    int flag=0;
    if((fp=fopen("通讯录.txt"."r"))==NULL)
    {
        printf("\n打开文件失败!");
        exit(0);
    }
    printf("\n---请依次输入你要添加的联系人信息---/n");
    printf("\n姓  名:");
    scanf("%s",one.name);
    printf("\n联系电话:");
    scanf("%s",one.tel);
    while (!flag)
    {
        flag=Isused(one.tel);
        if(!flag)
        {
            printf("你输入的联系电话重复,请重新输入:");
            scanf("%s",one.tel);
        }
    }
    printf("\nE-mail:");
    scanf("%s",one.Email);
    printf("\nQQ号码:");
    scanf("%s",one.QQ);
    if (fwrite(&one,sizeof(people),1,fp))
        printf("写入信息错误!\n");
    fclose(fp);
}
int Isused(char *tel)
{
    people one;
    FILE *fp;
    if((fp=fopen("通讯录.txt"."r"))==NULL)
    {
        printf("\n打开文件失败!");
        exit(0);
    }
    fread(&one,sizeof(people),1,fp);
    while (!feof(fp))
    {
        if (strcmp(tel,one.tel)==0)
        {
            fclose(fp);
            return 0;
        }
        fread(&one,sizeof(people),1,fp);
    }
    fclose(fp);
    return 1;
}
void Serch()
{
    people one;
    FILE *fp;
    int k=0;
    char na[15];
    if((fp=fopen("通讯录.txt"."r"))==NULL)
    {
        printf("\n打开文件失败!");
        exit(0);
    }
    fread(&one,sizeof(people),1,fp);
    while (!feof(fp))
    {
        if (strcmp(na,one.name)==0)
        {
            k++;
            if (k==1)
            {
                printf("查到姓名为%s的信息:\n",na);
                printf("\n%-10s%-10s%-10s%-15s\n","姓名","联系电话","E-mail","QQ号码");
            }
            printf("%-10s%-10s%-10s%-10s\n",one.name,one.tel,one.Email,one.QQ);
        }
        fread(&one,sizeof(people),1,fp);   
    }
    if(k==0)
        printf("\n\n对不起,没有此人信息!");
    else
        printf("\n\n此次共查到姓名为%s的信息%d条!\n",k,na);
}
void Modify()
{
    int k=0;
    long a;
    char na[15];
    FILE *fp;
    people one;
    printf("\n请输入你要修改的联系人姓名:");
    scanf("%s",na);
    if((fp=fopen("通讯录.txt"."rt+"))==NULL)
    {
        printf("\n打开文件失败!");
        exit(0);
    }
    a=ftell(fp);
    fread(&one,sizeof(people),1,fp);
    while (!feof(fp))
    {
        if (!strcmp(na,one.name))
        {
            k=1;
            break;
        }
        a=ftell(fp);
    fread(&one,sizeof(people),1,fp);
    }
    if (k)
    {
        printf("查到姓名为%s的信息,原信息为:\n",na);
        printf("\n%-10s%-10s%-10s%-15s\n","姓名","联系电话","E-mail","QQ号码");
        printf("%-10s%-10s%-10s%-10s\n",one.name,one.tel,one.Email,one.QQ);
        printf("\n请依次输入新信息\n");
        printf("\n姓  名:");
        scanf("%s",one.name);
        printf("\n联系电话:");
        scanf("%s",one.tel);
        printf("\nE-mail:");
        scanf("%s",one.Email);
        printf("\nQQ号码:");
        scanf("%s",one.QQ);
        fseek(fp,a,0);
        if(fwrite(&one,sizeof(people),1,fp)!=1)
            printf("写入信息错误!\n");
    }
    else
        printf("\n对不起,没有该联系人,无法进行修改");
    fclose(fp);
}
void Delete()
{
    int m,k=0;
    long a,b;
    char na[15];
    FILE *fp1,*fp2;
    people one;
    printf("\n请输入你要删除的联系人姓名:");
    scanf("%s",na);
    if((fp1=fopen("通讯录.txt","rt+"))==NULL)
    {
        printf("打开文件失败!");
        exit(0);
    }
    if ((fp2=fopen("备用.txt","wt+"))==NULL)
    {
        printf("\n打开文件失败!");
        exit(0);
    }
    fread(&one,sizeof(people),1,fp1);
    while (!feof(fp1))
    {
        if (strcmp(na,one.name)==0)
        {
            k=1;
            b=ftell(fp1);
            break;
        }
        if(fwrite(&one,sizeof(people),1,fp2)!=1)
            printf("写入信息错误!\n");
        fread(&one,sizeof(people),1,fp1);
    }
    if (k==1)
    {
        printf("查到姓名为%s的信息,详细信息为:\n",na);
        printf("\n%-10s%-10s%-10s%-15s\n","姓名","联系电话","E-mail","QQ号码");
        printf("%-10s%-10s%-10s%-10s\n",one.name,one.tel,one.Email,one.QQ);
        printf("是否删除该联系人?按1为取消;按0为删除\n");
        scanf("%d",&m);
        if (m==0)
        {
            fseek(fp1,b,0);
            fread(&one,sizeof(people),1,fp1);
            while (!feof(fp1))
            {
                if(fwrite(&one,sizeof(people),1,fp2)!=1)
                    printf("写入信息错误!\n");
                fread(&one,sizeof(people),1,fp1);
            }
            rewind(fp2);
            fclose(fp1);
            if ((fp1=fopen("通讯录.txt","wt"))==NULL)
            {
                printf("\n打开文件失败!");
                exit(0);
            }
            fread(&one,sizeof(people),1,fp2);
            while (!feof(fp2))
            {
                if(fwrite(&one,sizeof(people),1,fp1)!=1)
                    printf("写入信息错误!\n");
                fread(&one,sizeof(people),1,fp2);
            }
            fclose(fp1);
            fclose(fp2);
        }
    }
    else
    {
        printf("\n对不起,没有此人信息!");
        fclose(fp1);
        fclose(fp2);
    }
}
搜索更多相关主题的帖子: void 手机通讯录 include 联系人 people 
2012-03-07 22:14
笨坦克
Rank: 2
等 级:论坛游民
帖 子:34
专家分:35
注 册:2012-3-7
得分:20 
不好意思,我实在看不懂。。。本以为是个简单的、、、结果。
你能写这么长,不容易啊,加油
2012-03-09 20:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-362518-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.174351 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved