C 언어 는 간단 한 주소록 시스템 을 실현 한다.

8869 단어 C 언어통신 록
본 논문 의 사례 는 여러분 에 게 C 언어 주소록 시스템(첨삭 검사)을 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
모든 코드 는 다음 과 같 습 니 다.

#include <iostream>
#include <string>
using namespace std;

const int MAX = 1000;

//      
typedef struct person
{
    string m_name;          //  
    int m_sex;              //   1  ,0  
    int m_age;              //  
    string m_phone;         //  
    string m_addr;          //  
}person_t;

//      
typedef struct addressbooks
{
    person_t personArray[MAX];  //            
    int m_size;                 //        
}addressbooks_t;

void showMenu()
{
    cout << "************************" << endl;
    cout << "***** 1.      *****" << endl;
    cout << "***** 2.      *****" << endl;
    cout << "***** 3.      *****" << endl;
    cout << "***** 4.      *****" << endl;
    cout << "***** 5.      *****" << endl;
    cout << "***** 6.      *****" << endl;
    cout << "***** 0.      *****" << endl;
    cout << "************************" << endl;
}

void addPerson(addressbooks_t *abs)
{
    //         ,         
    if(abs->m_size == MAX)
    {
        cout  << "     ,    !" << endl;
        return ;
    }
    else
    {
        //       
        cout << "     : ";
        cin >> abs->personArray[abs->m_size].m_name;

        int sex;
        while(true)
        {
            cout << "     (1 ,0 ): ";
            cin >> sex;
            if(sex == 1 || sex == 0)
            {
                abs->personArray[abs->m_size].m_sex = sex;
                break;
            }
            else
            {
                cout << "    ,     !" << endl;
            }
        }

        cout << "     : ";
        cin >> abs->personArray[abs->m_size].m_age;

        cout << "     : ";
        cin >> abs->personArray[abs->m_size].m_phone;

        cout << "     : ";
        cin >> abs->personArray[abs->m_size].m_addr;

        //       
        ++ abs->m_size;

        cout << "    !" << endl;
        system("pause");
    }
}

void displayPerson(addressbooks_t *abs)
{
    if(abs->m_size == 0)
    {
        cout << "    !" << endl;
    }
    else
    {
        cout << "  \t" << "  \t" << "  \t" << "  \t\t" << "  \t\t" << endl;
        for(int i = 0; i < abs->m_size; ++i)
        {
            cout << abs->personArray[i].m_name << "\t";
            cout << abs->personArray[i].m_age << "\t";
            cout << (abs->personArray[i].m_sex == 1 ? " " : " ") << "\t";
            cout << abs->personArray[i].m_phone << "\t";
            cout << abs->personArray[i].m_addr << "\t";
            cout << endl;
        }
    }
    system("pause");
}

//       ,                 ,      -1
int findByName(addressbooks_t *abs,string name)
{
    for(int i = 0; i < abs->m_size; ++i)
    {
        if(name == abs->personArray[i].m_name)
        {
            return i;
        }
    }
    return -1;
}

void deletePerson(addressbooks_t *abs)
{
    string name;
    cout << "            :";
    cin >> name;
    int index = findByName(abs,name);
    if(index == -1)
    {
        cout << "       " << name << endl;
    }
    else
    {
        for(int i = index; i < abs->m_size; ++i)
        {
            abs->personArray[i] = abs->personArray[i+1];
        }

        --abs->m_size;
        cout << "    " << endl;
    }
    system("pause");
}

void findPerson(addressbooks_t *abs)
{
    string name;
    cout << "            :";
    cin >> name;
    int index = findByName(abs,name);
    if(index == -1)
    {
        cout << "       " << name << endl;
    }
    else
    {
        cout << "  \t" << "  \t" << "  \t" << "  \t\t" << "  \t\t" << endl;
        cout << abs->personArray[index].m_name << "\t";
        cout << abs->personArray[index].m_age << "\t";
        cout << (abs->personArray[index].m_sex == 1 ? " " : " ") << "\t";
        cout << abs->personArray[index].m_phone << "\t";
        cout << abs->personArray[index].m_addr << "\t";
        cout << endl;
    }
    system("pause");
}

void updatePerson(addressbooks_t *abs)
{
    string name;
    cout << "            :";
    cin >> name;
    int index = findByName(abs,name);
    if(index == -1)
    {
        cout << "       " << name << endl;
    }
    else
    {
        //       
        cout << "     : ";
        cin >> abs->personArray[index].m_name;

        int sex;
        while(true)
        {
            cout << "     (1 ,0 ): ";
            cin >> sex;
            if(sex == 1 || sex == 0)
            {
                abs->personArray[index].m_sex = sex;
                break;
            }
            else
            {
                cout << "    ,     !" << endl;
            }
        }

        cout << "     : ";
        cin >> abs->personArray[index].m_age;

        cout << "     : ";
        cin >> abs->personArray[index].m_phone;

        cout << "     : ";
        cin >> abs->personArray[index].m_addr;

        cout << "    !" << endl;
    }
    system("pause");
}

void clearPerson(addressbooks_t *abs)
{
    abs->m_size = 0;
    cout << "    !" << endl;
    system("pause");
}

int main()
{
    //         
    addressbooks_t abs;
    //           
    abs.m_size = 0;

    int select = 0;                 //           
    while(true)
    {
        system("cls");              //    
        showMenu();                 //    
        cout << "       : ";
        cin >> select;
        switch (select)
        {
        case 0:                      //0.     
            cout << "      " << endl;
            return 0;
            break;
        case 1:                      //1.     
            addPerson(&abs);
            break;
        case 2:                      //2.     
            displayPerson(&abs);
            break;
        case 3:                      //3.     
            deletePerson(&abs);
            break;
        case 4:                      //4.     
            findPerson(&abs);
            break;
        case 5:                      //5.     
            updatePerson(&abs);
            break;
        case 6:                      //6.     
            clearPerson(&abs);
            break;
        default:
            break;
        }
    }

    return 0;
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기