码迷,mamicode.com
首页 > 其他好文 > 详细

学术休假之通讯录管理

时间:2015-03-04 14:43:22      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:c++   通讯录   iostream   博客   编程   

联系人信息包括:学号,姓名,年龄,性别,出生年月,地址,电话,E-mail等。
(1)能建立、修改和增删学生通讯录
(2)能够按多种方式进行查询


代码如下:

 

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
struct TEL
{
    int Num;         //学号
    string Name;     //姓名
    int Age;         //年龄
    string Sex;      //性别
    string Time;     //出生日期
    string Address;  //地址
    string Phone;    //电话号码
    string Email;    //电子邮件
};
TEL Tel[500];        //通讯录总容量
void Menu();         //菜单
void Establish();    //建立
void Modification(); //修改
void Delete();       //删除
void Query();        //查询
void Savedata(int);     //保存修改
int loading1();       //读取
int main()
{
    int n;
    cout<<"欢迎使用本通讯录系统."<<endl;;
    do
    {
        system("cls");
        Menu();
        cout<<"请输入操作指令:";
        cin>>n;
        if (n==0)
            break;
        switch (n)
        {
        case 1:
            Establish();
            continue;
        case 2:
            Modification();
            continue;
        case 3:
            Delete();
            continue;
        case 4:
            Query();
            continue;
        }
    }
    while (1);
    cout<<"祝您羊年大吉!!!"<<endl;
    return 0;
}

void Menu()
{
    cout<<"选项:"<<endl;
    cout<<"1.添加新的联系人"<<endl;
    cout<<"2.修改联系人信息"<<endl;
    cout<<"3.删除联系人    "<<endl;
    cout<<"4.查询联系人信息"<<endl;
    cout<<"0.退出"<<endl;
}
void Establish()
{
    int count=loading1();
    int num,age,n,i;
    string name,sex,address,time,phone,email;
    do
    {
        n=0;
        cout<<"请输入学号:";
        cin>>num;
        for (i=1; i<=num-10000; i++)
        {
            if (num==Tel[i].Num)
            {
                n=1;
                cout<<"该学号已存在,请重新输入:";
                break;
            }
        }
        if (n==1)
            continue;
        else
            break;
    }
    while (1);

    cout<<"请输入姓名:";
    cin>>name;
    cout<<"请输入性别(M为男,W为女):";
    cin>>sex;
    cout<<"请输入年龄:";
    cin>>age;
    cout<<"请输入出生日期: ";
    cin>>time;
    cout<<"请输入地址:";
    cin>>address;
    cout<<"请输入电话号码:";
    cin>>phone;
    cout<<"请输入电子邮箱:";
    cin>>email;
    Tel[i].Num=num;
    Tel[i].Name=name;
    Tel[i].Sex=sex;
    Tel[i].Age=age;
    Tel[i].Time=time;
    Tel[i].Address=address;
    Tel[i].Phone=phone;
    Tel[i].Email=email;
    Savedata(count);
}

void Modification()
{
    int count=loading1();
    int num,age,i,k,m;
    string name,sex,address,time,phone,email;
    do
    {
        k=-1;
        cout<<"请输入你想要修改对象的学号(输入0退出):";
        cin>>num;
        if (num==0)
            break;
        for (i=1; i<count; i++)
        {
            if (Tel[i].Num==num)
            {
                k=i;
                break;
            }
        }
        if (k==-1)
        {
            cout<<"你输入的对象不存在,请重新输入:";
            continue;
        }
        do
        {
            cout<<"请选择你需要修改的内容:"<<endl;
            cout<<"1.学号      2.姓名      3.年龄"<<endl;
            cout<<"4.性别      5.出生日期  6.地址"<<endl;
            cout<<"7.电话号码  8.电子邮箱  0.退出"<<endl;
            cout<<"请输入操作指令:";
            cin>>m;
            if (m==0)
                break;
            switch (m)
            {
            case 1:
                cout<<"请输入新的学号:";
                cin>>num;
                Tel[k].Num=num;
                continue;
            case 2:
                cout<<"请输入新的姓名:";
                cin>>name;
                Tel[k].Name=name;
                continue;
            case 3:
                cout<<"请输入新的年龄:";
                cin>>age;
                Tel[k].Age=age;
                continue;
            case 4:
                cout<<"请输入新的性别:";
                cin>>sex;
                Tel[k].Sex=sex;
                continue;
            case 5:
                cout<<"请输入新的出生日期:";
                cin>>time;
                Tel[k].Time=time;
                continue;
            case 6:
                cout<<"请输入新的地址:";
                cin>>address;
                Tel[k].Address=address;
                continue;
            case 7:
                cout<<"请输入新的电话号码:";
                cin>>phone;
                Tel[k].Phone=phone;
                continue;
            case 8:
                cout<<"请输入新的电子邮箱:";
                cin>>email;
                Tel[k].Email=email;
                continue;
            }
        }
        while (1);
    }
    while (1);
    Savedata(count);
}


void Delete()
{
    int count=loading1();
    int num,i,k;
    do
    {
        k=-1;
        cout<<"请输入你想要删除对象的学号(输入0退出):";
        cin>>num;
        if (num==0)
            break;
        for (i=1; i<count; i++)
        {
            if (Tel[i].Num==num)
            {
                k=i;
                break;
            }
        }
        if (k==-1)
        {
            cout<<"你输入的对象不存在,请重新输入:";
            continue;
        }
        Tel[k].Num=1;
        Tel[k].Name="1";
        Tel[k].Sex="1";
        Tel[k].Age=1;
        Tel[k].Time="1";
        Tel[k].Address="1";
        Tel[k].Phone="1";
        Tel[k].Email="1";
    }
    while (1);
    Savedata(count);
}

void Query()
{
    int count=loading1();
    int num,i,k;
    do
    {
        k=-1;
        cout<<"请输入你想要查询对象的学号(输入0退出):";
        cin>>num;
        if (num==0)
            break;
        for (i=1; i<count; i++)
        {
            if (Tel[i].Num==num)
            {
                k=i;
                break;
            }
        }
        if (k==-1)
        {
            cout<<"你输入的对象不存在,请重新输入:";
            continue;
        }
        cout<<Tel[k].Num<<" "<<Tel[k].Name<<" "<<Tel[k].Sex<<" "<<Tel[k].Age<<" ";
        cout<<Tel[k].Time<<" "<<Tel[k].Address<<" "<<Tel[k].Phone<<" "<<Tel[k].Email<<endl;
    }
    while (1);
}

int loading1()
{
    int num=1;
    ifstream infile("TELData.txt",ios::in);
    if (!infile)
    {
        cout<<"open error!";
        exit(1);
    }
    while (infile>>Tel[num].Num)
    {
        infile>>Tel[num].Name;
        infile>>Tel[num].Sex;
        infile>>Tel[num].Age;
        infile>>Tel[num].Time;
        infile>>Tel[num].Address;
        infile>>Tel[num].Phone;
        infile>>Tel[num].Email;
        num++;
    }
    infile.close();//读入完毕要关闭文件
    return num;
}

void Savedata(int num)
{
    ofstream outfile("TELData.txt",ios::out);
    if (!outfile)
    {
        cout<<"open error!";
        exit(1);
    }
    int i;
    for (i=1; i<=num; i++)
    {
        outfile<<Tel[i].Num<<" "<<Tel[i].Name<<" "<<Tel[i].Sex<<" "<<Tel[i].Age<<" "<<Tel[i].Time<<" "<<Tel[i].Address<<" "<<Tel[i].Phone<<" "<<Tel[i].Email<<endl;
    }
    outfile.close();//用完了必须关闭
}


 


运行结果:

技术分享

 

技术分享

技术分享

 

技术分享

技术分享

 技术分享

 

一上午加中午的成果,还有许多不足,比如修改后退出要输入两次0,可能是保存时的问题,这两个0也跑到文件中来了等等,。

有些累了,以后有空再升级吧。

学术休假之通讯录管理

标签:c++   通讯录   iostream   博客   编程   

原文地址:http://blog.csdn.net/liuchang54/article/details/44059569

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!