學術休假之通訊錄管理

不被看好的青春叫成長發表於2015-03-04
聯絡人資訊包括:學號,姓名,年齡,性別,出生年月,地址,電話,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也跑到檔案中來了等等。還有第二組要求沒達到

有些累了,以後有空再升級吧。

相關文章