注册 登录
编程论坛 C++教室

自己写的课设,一个电话簿的管理系统,还不全面,但有个小问题弄不明白,

未未来 发布于 2013-07-13 17:03, 779 次点击
main.cpp
程序代码:
#include<iostream>
#include<fstream>
#include<string>
#include"dianhuabu.hpp"
using namespace std;
string x;
int main(){
    cout<<"欢迎使用电话簿管理系统!"<<endl;
    cout<<"请输入你想打开的电话簿文件号码。"<<endl;
    cin>>x;
    fstream inout(x.c_str(),fstream::in|fstream::out);
    fr *head,*p1,*p2;
    int l=0;
    //从文件读取数据建立链表
    while(!inout.eof()){
       ++l;
    p1=new fr;
   inout>>p1->name>>p1->numb;
    if(l==1){p1->next=NULL;head=p1;}
    else p2->next=p1;
    p2=p1;
   }p2->next=NULL;
   inout.close();
   int m;
   do{
     cout<<"请输入你的选择。"<<endl;
        cout<<"\t\t\t\t1.插入联系人及联系方式。"<<endl;
     cout<<"\t\t\t\t2.修改现有的联系人的联系方式。"<<endl;
     cout<<"\t\t\t\t3.查询某人的联系方式。"<<endl;
     cout<<"\t\t\t\t4.删除某联系人。"<<endl;
     cout<<"\t\t\t\t5.输出此文件的所有联系人列表。"<<endl;
     cout<<"\t\t\t\t6.退出系统。"<<endl;
   
     cin>>m;
     string a,b,c,s;
     fr *n;
      switch (m){
          case 1:
            
            n=new fr;
          cout<<"请输入联系人姓名及其联系方式。"<<endl;
          cin>>n->name>>n->numb;
          n->next=NULL;
         head= inse(head,n);
    break;
          case 2:
          cout<<"请输入需要修改的联系人姓名。"<<endl;
   
           cin>>a;
        cout<<"请输入联系人新的联系方式。"<<endl;
        cin>>b;
        head=rese(head,a,b);
        break;   
          case 3:
               cout<<"请输入需要查询的联系人姓名。"<<endl;
    cin>>s;
                     cout<<endl;
                     quse(head,s);
                     break;
           case 4:
           cout<<"请输入需要删除的联系人姓名。"<<endl;
            cin>>c;
            head=dese(head,c);
            break;
          case 5:
          exse(head);
          break;
          }

 } while(m>=1&&m<=5);

ofstream out(x.c_str(),ofstream::out);
while(head!=NULL)
{
    out<<head->name<<" "<<head->numb<<endl;
    head=head->next;
}
out.close();

delete p1;

return 0;
}

dianhuabu.hpp
程序代码:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class fr{
public:
fr *inse(fr *f,fr*x);//插入
fr *rese(fr *f,string a,string b);//修改
void *quse(fr *f,string a);//查询
fr *dese(fr *f,string a);//删除
void *exse(fr *f);//输出
string name;
string numb;
fr *next;
};
fr *inse(fr *f,fr*x){
     fr *head;
    head=f;
    while((head->next)!=NULL)
{head=head->next;    }
head->next=x;
x->next=NULL;
return f;
}


fr *rese(fr *f,string a,string b){
    fr*head;
    head=f;
    while(head->name!=a){
        head=head->next;
    }
    head->numb=b;
return f;
}


void quse(fr *f,string a){
        fr*head;
    head=f;
    while(head->name!=a){
        head=head->next;
    }
    cout<<head->name<<" "<<head->numb<<endl;
}

fr *dese(fr *f,string a){
    fr *head,*mark;
    head=f;
    if(f->name==a)
    f=f->next;
    else{
        

    while((head->name)!=a){
        mark=head;
        head=head->next;
    }
    mark->next=head->next;    }


 
    return f;
}
void *exse(fr *f){
    fr *head;
    head=f;
    while(head!=NULL){
        cout<<head->name<<" "<<head->numb<<endl;
        head=head->next;
    }
}

文件
1.dat
jim 138292929229
tom 134222233223
jim 152222343222


只有本站会员才能查看附件,请 登录


我插入一个联系人之后
在让输出现有的链表
为什么插入的和原来链表最后一个之间有一个空行。
9 回复
#2
peach54602013-07-15 16:06
太长了...

我先问你一点,你自己调试过没?
#3
未未来2013-07-15 17:16
回复 2楼 peach5460
调了 能力范围只能找不出问题。
#4
love云彩2013-07-15 19:48
我用VS帮你调试了一下,这样显示出错:1>  main.cpp
1>c:\users\vergil\documents\visual studio 2012\projects\consoleapplication75\consoleapplication75\dianhuabu.hpp(71): error C4716: “exse”: 必须返回一个值
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
#5
未未来2013-07-15 20:47
回复 4楼 love云彩
你说的这个 我已经 改过来 了,我用的编译器竟然没保存,,还是谢谢你,
#6
love云彩2013-07-15 22:11
那你将你修改之后的代码贴上来看看
#7
未未来2013-07-16 23:02
回复 6楼 love云彩
只有本站会员才能查看附件,请 登录

初识密码 000000
打开 通讯录1.dat
然后每次插入 联系人和联系人方式的时候 与原来的链表数据最后都有一个空行,我不知道怎么会出现
#8
peach54602013-07-17 11:08
完全不会玩,太深奥了
#9
未未来2013-07-17 19:03
回复 8楼 peach5460
#10
peach54602013-07-18 11:22
以下是引用未未来在2013-7-17 19:03:45的发言:


又是加密又是读文件的
我搞了半天,环境总搭建不对

你不要告诉我这段代码一口气写完的,中间就没调试过咧...
要不你把这些旁枝末节都去掉再发给我?
1