自己修改程序错误一

本文档展示了一个C++编写的通讯录管理系统,修复了查找和删除操作的问题,并增加了文件读写功能。查找操作现在能正确终止,删除操作不再显示错误。系统允许用户建立通讯录,插入、查询和删除联系人,按搜索次数排序,并能将通讯录写入文件。
摘要由CSDN通过智能技术生成

原程序:

#include<bits/stdc++.h>
using namespace std;
typedef struct Lnode
{
    int num;
    string name;
    string telephone;
    int searchTime;
    struct Lnode* next;

}Lnode, *Linklist;

void insertSequential(Linklist &head);

int str_to_int(char* s)
{
    int i = 0;
    int result = 0;
    while(s[i])
    {
        result =result*10 + (s[i]-'0');
        i++;
    }
    return result;
}
void creatIncerLInk(Linklist &head)
{   Linklist q = head;
    int way;
    cout<<"******************请建立通讯录******************"<<endl;
    cout<<"*            1 从本地文件中读取通讯录            *"<<endl;
    cout<<"*            2 手动建立通讯录                   *"<<endl;
    cout<<"*            0 这次先不建立通讯录啦              *"<<endl;
    cout<<"************************************************"<<endl;
    cout <<"请选择:"<<endl;
    cin >>way;
    switch(way)
    {
        case 1:
        {
            FILE*fptr = fopen("D:/tongxunlu/mytxt.txt", "r");
            if(!fptr)
            {
                perror("fail");
            }
            char *s,  *temp, *savestr;
            while(fgets(s, 50, fptr))
            {
                Linklist p = new Lnode;
                temp = strtok(s, " ");
                p->num = str_to_int(temp);
                temp = strtok(NULL, " ");
                p->name = temp;
                temp = strtok(NULL, " ");
                p->telephone = temp;
                p->searchTime = 0;

                q->next = p;
                q = q->next;
            }
            q->next = NULL;
            return;
        }
        case 2:
        {
            int n;
            cout <<" 请输入你要建立的通讯录个数"<<endl;
            cin >> n;
            while(n--)
                insertSequential(head);
            return;
        case 0:
            return;
        }
        default:
            cout <<"没有此选项"<<endl;
            return;
    }
}

void changeNode(Linklist &p, Linklist &q)
{
    int tNum, tTime,searchTime;
    string tName, tTelephone;
    tNum = p->num;
    tTime = p->searchTime;
    tName = p->name;
    tTelephone = p->telephone;

    p->num = q->num;
    p->searchTime = q->searchTime;
    p->name = q->name;
    p->telephone = q->telephone;

    p->num = tNum;
    p->searchTime = searchTime;
    p->name = tName;
    p->telephone = tTelephone;
}

void sortBytime(Linklist &head)
{
    Linklist p = head->next;
    Linklist q = p;
    while(p->next)
    {
        while(q&&q->next!=p)
        {
            if(q->searchTime>q->next->searchTime)
                changeNode(q, q->next);
        }
    }
}

void searchName(Linklist &head)
{
    cout << "请输入要查询的姓名:"<<endl;
    string hisName;
    cin >>hisName;
    Linklist p = head->next;
    while(p)
    {
        if(p->name == hisName)
        {
            break;
        }
    }
    if(!p)
    {
        cout << "不存在该联系人!"<<endl;
    }
    else
    {
        cout <<"num:"<<p->num<<" "<<"name:"<<p->name<<" "<<"telephone:"<<p->telephone<<endl;
        p->searchTime++;
    }
}

void searchNum(Linklist &head)
{
    cout << "请输入要查询的学号:"<<endl;
    int hisNum;
    cin >>hisNum;
    Linklist p = head->next;
    while(p)
    {
        if(p->num == hisNum)
        {
            break;
        }
    }
    if(!p)
    {
        cout << "不存在该联系人!"<<endl;
    }
    else
    {
        cout <<"num:"<<p->num<<" "<<"name:"<<p->name<<" "<<"telephone:"<<p->telephone<<endl;
        p->searchTime++;
    }
}

void search_friend(Linklist &head)
{
    int way;
    cout<<"*****************请选择查询方法******************"<<endl;
    cout<<"*            1 按姓名查询                       *"<<endl;
    cout<<"*            2 按学号查询                       *"<<endl;
    cout<<"*            0 这次先不查啦                     *"<<endl;
    cout<<"************************************************"<<endl;
    cin >>way;
    switch(way)
    {
        case 1:
        {
            searchName(head);
        //    sortBytime(head);
            break;
        }
        case 2:
        {
            searchNum(head);
        //    sortBytime(head);
            break;
        }
        case 0:
            break;
    }
}

void deleteElem(Linklist &head)
{
    int i;
    cout <<" 请输入删除的序号:"<<endl;
    cin >> i;
    linklist p = head;
    for(int t = 1; t < i&&p; t++)
        p = p->next;
    if(!p)
        cout << "不存在第"<<i<<"个节点"<<endl;
    else
    {
        p->next = p->next->next;
    }
}

void delNum(Linklist &head)
{
    int num;
    cout <<" 请输入要删除的联系人学号:"<<endl;
    cin >> num;
    Linklist p = head;
    while(p->next->num!=num && p)
    {
        p = p->next;
    }
    if(!p)
        cout <<"不存在此联系人"<<endl;
    else
        p->next = p->next->next;
}

void delName(Linklist &head)
{
    int name;
    cout <<" 请输入要删除的联系人姓名:"<<endl;
    cin >> name;
    Linklist p = head;
    while(p->next->name!=name && p)
    {
        p = p->next;
    }
    if(!p)
        cout <<"不存在此联系人"<<endl;
    else
        p->next = p->next->next;
}

void delnode(Linklist &head)
{
    int way;
    cout<<"*****************请选择删除方法******************"<<endl;
    cout<<"*            1 按序号删除                       *"<<endl;
    cout<<"*            2 按学号删除                       *"<<endl;
    out<<"*             3 按姓名删除                       *"<<endl;
    cout<<"*            0 这次先不删除啦                     *"<<endl;
    cout<<"************************************************"<<endl;
    cin >> way;
    switch(way)
    {
        case 1:
            deleteElem(head);
            break;
        case 2:
            delNum(head);
            break;
        case 3:
            delName(head);
            break;
        case 0:
            break;
    }
}

void insertSequential(Linklist &head)//新插入的节点放到最前面
{
    cout <<"请输入要加入通讯录的id, 名字和电话号码"<<endl;
    Linklist q = new Lnode;
    cin >> q->num >>q->name>>q->telephone;
    q->searchTime = 0;
    q->next = head->next;
    head->next = q;
}
void printList(Linklist &head)
{
    Linklist p = head->next;
    while(p)
    {
        cout << p->num<<" "<<p->name<<" "<<p->telephone<<endl;
        p = p->next;
    }
}

int main()
{
    Linklist head = new Lnode;
    head->next = NULL;
    int menu = 1;
    while(menu)
    {
    printf("\n          ***************** ^@^欢迎使用通讯录系统***********\n");
    printf("          *                  1 通讯录的建立                *\n");
    printf("          *                  2 插入通讯记录                *\n");
    printf("          *                  3 查询通讯记录                *\n");
    printf("          *                  4 删除通讯记录                *\n");
    printf("          *                  5 显示通讯录信息              *\n");
    printf("          *                  0 退出管理系统                *\n");
    printf("          **************** ^@^欢迎使用通讯录系统************\n");
    cout <<" 请选择0-5:"<<endl;
    cin>>menu;
    switch(menu)
    {
        case 1:
        {   
            creatIncerLInk(head);
            break;
        }
        case 2:
            insertSequential(head);
            break;
        case 3:
            search_friend(head);
            break;
        case 4:
            delnode(head);
            break;
        case 5:
            printList(head);
            break;

    }
    }
}

问题:

1.查找操作后一直循环;

2删除操作不显示

解决:

1.while循环没加p==->next

2. Linklist写成linklist

修改后:

#include<bits/stdc++.h>
using namespace std;
typedef struct Lnode
{
    int num;
    string name;
    string telephone;
    int searchTime;
    struct Lnode* next;

}Lnode, *Linklist;

void insertSequential(Linklist &head);

int str_to_int(char* s)
{
    int i = 0;
    int result = 0;
    while(s[i])
    {
        result =result*10 + (s[i]-'0');
        i++;
    }
    return result;
}
void creatIncerLInk(Linklist &head)
{   Linklist q = head;
    int way;
    cout<<"******************请建立通讯录******************"<<endl;
    cout<<"*            1 从本地文件中读取通讯录            *"<<endl;
    cout<<"*            2 手动建立通讯录                   *"<<endl;
    cout<<"*            0 这次先不建立通讯录啦              *"<<endl;
    cout<<"************************************************"<<endl;
    cout <<"请选择:"<<endl;
    cin >>way;
    switch(way)
    {
        case 1:
        {
            FILE*fptr = fopen("D:/tongxunlu/mytxtin.txt", "r");
            if(!fptr)
            {
                perror("fail");
            }
            char *s,  *temp, *savestr;
            while(fgets(s, 50, fptr))
            {
                char *find;
                find = strchr(s, '\n');
                if(find)
                    *find = '\0';
                Linklist p = new Lnode;
                temp = strtok(s, " ");
                p->num = str_to_int(temp);
                temp = strtok(NULL, " ");
                p->name = temp;
                temp = strtok(NULL, " ");
                p->telephone = temp;
                p->searchTime = 0;

                q->next = p;
                q = q->next;
            }
            q->next = NULL;
            return;
        }
        case 2:
        {
            int n;
            cout <<" 请输入你要建立的通讯录个数"<<endl;
            cin >> n;
            while(n--)
                insertSequential(head);
            return;
        case 0:
            return;
        }
        default:
            cout <<"没有此选项"<<endl;
            return;
    }
}

void changeNode(Linklist &p, Linklist &q)
{
    int tNum, tTime,searchTime;
    string tName, tTelephone;
    tNum = p->num;
    tTime = p->searchTime;
    tName = p->name;
    tTelephone = p->telephone;

    p->num = q->num;
    p->searchTime = q->searchTime;
    p->name = q->name;
    p->telephone = q->telephone;

    q->num = tNum;
    q->searchTime = searchTime;
    q->name = tName;
    q->telephone = tTelephone;
}

void sortBytime(Linklist &head)
{
    
    for(Linklist p = head->next; p->next != NULL; p = p->next)
    for(Linklist q = head->next; q->next != NULL; q = q->next)
    {
        if(q->searchTime<q->next->searchTime)
            changeNode(q, q->next);
    }
}

void searchName(Linklist &head)
{
    cout << "请输入要查询的姓名:"<<endl;
    string hisName;
    cin >>hisName;
    Linklist p = head->next;
    while(p)
    {
        if(p->name == hisName)
        {
            break;
        }
        p=p->next;
    }
    if(!p)
    {
        cout << "不存在该联系人!"<<endl;
    }
    else
    {
        cout <<"num:"<<p->num<<" "<<"name:"<<p->name<<" "<<"telephone:"<<p->telephone<<endl;
        p->searchTime=p->searchTime+1;
    }
}

void searchNum(Linklist &head)
{
    cout << "请输入要查询的学号:"<<endl;
    int hisNum;
    cin >>hisNum;
    Linklist p = head->next;
    while(p)
    {
        if(p->num == hisNum)
        {
            break;
        }
        p=p->next;
    }
    if(!p)
    {
        cout << "不存在该联系人!"<<endl;
    }
    else
    {
        cout <<"num:"<<p->num<<" "<<"name:"<<p->name<<" "<<"telephone:"<<p->telephone<<endl;
        p->searchTime=p->searchTime+1;
    }
}

void search_friend(Linklist &head)
{
    int way;
    cout<<"*****************请选择查询方法******************"<<endl;
    cout<<"*            1 按姓名查询                       *"<<endl;
    cout<<"*            2 按学号查询                       *"<<endl;
    cout<<"*            0 这次先不查啦                     *"<<endl;
    cout<<"************************************************"<<endl;
    cin >>way;
    switch(way)
    {
        case 1:
        {
            searchName(head);
        //    sortBytime(head);
            break;
        }
        case 2:
        {
            searchNum(head);
        //    sortBytime(head);
            break;
        }
        case 0:
            break;
    }
}

void deleteElem(Linklist &head)
{
    int i;
    cout <<" 请输入删除的序号:"<<endl;
    cin >> i;
    Linklist p = head;
    for(int t = 1; t < i&&p; t++)
        p = p->next;
    if(!p)
        cout << "不存在第"<<i<<"个节点"<<endl;
    else
    {
        p->next = p->next->next;
    }
}

void delNum(Linklist &head)
{
    int num;
    cout <<" 请输入要删除的联系人学号:"<<endl;
    cin >> num;
    Linklist p = head;
    while(p->next->num!=num && p)
    {
        p = p->next;
    }
    if(!p)
        cout <<"不存在此联系人"<<endl;
    else
        p->next = p->next->next;
}

void delName(Linklist &head)
{
    string name;
    cout <<" 请输入要删除的联系人姓名:"<<endl;
    cin >> name;
    Linklist p = head;
    while(p->next->name!=name && p)
    {
        p = p->next;
    }
    if(!p)
        cout <<"不存在此联系人"<<endl;
    else
        p->next = p->next->next;
}

void delnode(Linklist &head)
{
    int way;
    cout<<"*****************请选择删除方法******************"<<endl;
    cout<<"*            1 按序号删除                       *"<<endl;
    cout<<"*            2 按学号删除                       *"<<endl;
    cout<<"*             3 按姓名删除                       *"<<endl;
    cout<<"*            0 这次先不删除啦                     *"<<endl;
    cout<<"************************************************"<<endl;
    cin >> way;
    switch(way)
    {
        case 1:
            deleteElem(head);
            break;
        case 2:
            delNum(head);
            break;
        case 3:
            delName(head);
            break;
        case 0:
            break;
    }
}

void insertSequential(Linklist &head)//新插入的节点放到最前面
{
    cout <<"请输入要加入通讯录的id, 名字和电话号码"<<endl;
    Linklist q = new Lnode;
    cin >> q->num >>q->name>>q->telephone;
    q->searchTime = 0;
    q->next = head->next;
    head->next = q;
}
void printList(Linklist &head)
{
    Linklist p = head->next;
    while(p)
    {
        cout <<"num:"<< p->num<<" "<<"name:"<<p->name<<" "<<"telephone:"<<p->telephone<<endl;
        p = p->next;
    }
}

void toMytxt(Linklist &head)
{
    ofstream fpto;
    fpto.open("D:/tongxunlu/mytxtout.txt", ios::out);
    Linklist p = head->next;
    while(p)
    {
        fpto<<p->num<<" "<<p->name<<" "<<p->telephone<<endl;
        p = p->next;
    }
    fpto.close();
}

int main()
{
    Linklist head = new Lnode;
    head->next = NULL;
    int menu = 1;
    while(menu)
    {
    printf("\n          ***************** ^@^欢迎使用通讯录系统***********\n");
    printf("          *                  1 通讯录的建立                *\n");
    printf("          *                  2 插入通讯记录                *\n");
    printf("          *                  3 查询通讯记录                *\n");
    printf("          *                  4 删除通讯记录                *\n");
    printf("          *                  5 显示通讯录信息              *\n");
    printf("          *                  6 将通讯录写入我的文件         *\n");
    printf("          *                  0 退出管理系统                *\n");
    printf("          **************** ^@^欢迎使用通讯录系统************\n");
    cout <<" 请选择0-6:"<<endl;
    cin>>menu;
    switch(menu)
    {
        case 1:
        {   
            creatIncerLInk(head);
            break;
        }
        case 2:
            insertSequential(head);
            break;
        case 3:
        {
            search_friend(head);
            sortBytime(head);
            break;
        }
        case 4:
            delnode(head);
            break;
        case 5:
            printList(head);
            break;
        case 6:
            toMytxt(head);
            break;
        default:
            exit(0);
            break;

    }
    }
}

另外,文件读取操作是这样的:

按行读:

FILE*fptr = fopen("D:/tongxunlu/mytxtin.txt", "r");
if(!fptr)
{
      perror("fail");
}
char *s;
while(fgets(s, 50, fptr)

ifstream也可以用来读取(c++)

写入文件:

    ofstream fpto;
    fpto.open("D:/tongxunlu/mytxtout.txt", ios::out);
    Linklist p = head->next;
    while(p)
    {
        fpto<<p->num<<" "<<p->name<<" "<<p->telephone<<endl;
        p = p->next;
    }
    fpto.close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值