• 通讯录练习


      1 #include<iostream>
      2 #include<Windows.h>
      3 #include<string>
      4 
      5 using namespace std;
      6 
      7 #define MAX 1000
      8 
      9 #pragma region 结构体
     10 
     11 
     12 
     13 struct stcPersong
     14 {
     15     int  id;
     16     string name;
     17     int sex;
     18     int age;
     19     string adress;
     20 };
     21 
     22 struct stcMenu
     23 {
     24     stcPersong stcperson[MAX];//联系人结构体
     25     int m_size;//人数
     26 };
     27 
     28 #pragma endregion
     29 
     30 #pragma region 函数
     31 
     32 void showMenu()
     33 {
     34     cout << "********** 1.添加联系人 ********** " << endl;
     35     cout << "********** 2.显示联系人 ********** " << endl;
     36     cout << "********** 3.删除联系人 ********** " << endl;
     37     cout << "********** 4.查找联系人 ********** " << endl;
     38     cout << "********** 5.修改联系人 ********** " << endl;
     39     cout << "********** 6.清空联系人 ********** " << endl;
     40     cout << "********** 0.退出系统   ********** " << endl;
     41 }
     42 
     43 //限制输入数字
     44 int numInput()
     45 {
     46     char n;
     47     int num;
     48     //cout << "please enter a number:" << endl;
     49     cin >> n;
     50     num = n;
     51     while (1)  
     52     {
     53         if (int(num) >= 48 || int(num) <= 57|| int(num) ==46||int(num) == 8)
     54             break;
     55         else
     56         {
     57             cout << "你输入的不是一个数字!!" << endl;
     58         }
     59         cin >> n;
     60     }
     61 
     62     return n;
     63 }
     64 //仅输入数字
     65 void onlyNumber()
     66 {
     67     cout << "请输入:" << endl;
     68     string s;
     69     cin >> s;
     70     while (s.size() != 1 || s[0] <= '0' || s[0] >= '9') {
     71         cout << "输入错误,请重新输入:" << endl;
     72         cin >> s;
     73     }
     74     cout << "成功" << endl;
     75 }
     76 
     77 //添加联系人
     78 void addPerson(stcMenu * stc)
     79 {
     80     if (stc->m_size > MAX)
     81     {
     82         cout << "通讯录已满" << endl;
     83     }
     84     else
     85     {
     86         string nam;
     87         stc->stcperson[stc->m_size].id = stc->m_size;
     88         cout << "输入姓名" << endl;
     89         cin >> nam;
     90         stc->stcperson[stc->m_size].name = nam;
     91 
     92         cout << "输入性别" << endl;
     93         int se;
     94         cin >> se;
     95         stc->stcperson[stc->m_size].age = se;
     96 
     97         cout << "输入年龄" << endl;
     98         int ag = 0;
     99         ag = numInput();
    100         //cin >> ag;
    101         stc->stcperson[stc->m_size].age = ag;
    102 
    103         cout << "添加完成" << endl;
    104         stc->m_size++;
    105     }
    106     system("pause");
    107     system("cls");
    108     return;
    109 }
    110 //显示联系人
    111 void showPerson(stcMenu * stc)
    112 {
    113     if (stc->m_size == 0)
    114     {
    115         cout << "通讯录为空" << endl;
    116     }
    117     else
    118     {
    119         for (int i = 0; i < stc->m_size; i++)
    120         {
    121             cout << "序号  " << stc->stcperson[i].id << "      姓名 " << stc->stcperson[i].name <<
    122                 "    性别  " << (stc->stcperson[i].sex == 1 ? "" : "") << "    年龄" << stc->stcperson[i].age << endl;
    123         }
    124     }
    125 }
    126 //检测联系人
    127 int checkPerson(stcMenu * stc, string name)
    128 {
    129     if (stc->m_size > 0)
    130     {
    131         for (size_t i = 0; i < stc->m_size; i++)
    132         {
    133             if (stc->stcperson[0].name == name)
    134             {
    135                 return i;
    136             }
    137         }
    138         return -1;
    139     }
    140     else
    141     {
    142         cout << "通讯录为空!" << endl;
    143         return -1;
    144     }
    145 }
    146 //删除联系人
    147 void deletePerson(stcMenu * stc)
    148 {
    149     cout << "删除联系人
    ";
    150     cout << "   输入姓名:
    ";
    151     string m_name;
    152     cin >> m_name;
    153     int ret = checkPerson(stc, m_name);
    154     if (ret != -1)
    155     {
    156         for (size_t i = ret; i < stc->m_size - ret; i++)
    157         {
    158             stc->stcperson[i] = stc->stcperson[i + 1];
    159         }
    160         stc->m_size--;
    161     }
    162     else
    163     {
    164         cout << "查无此人!" << endl;
    165     }
    166     system("pause");
    167     system("cls");
    168     return;
    169 }
    170 
    171 //查找联系人
    172 void findPerson(stcMenu * stc)
    173 {
    174     cout << "查找联系人
    ";
    175     cout << "     输入姓名:
    ";
    176     string m_name;
    177     cin >> m_name;
    178     int ret = checkPerson(stc, m_name);
    179     if (ret != -1)
    180     {
    181         cout << "序号:" << stc->stcperson[ret].id << "  姓名:" << stc->stcperson[ret].name << "   年龄:" << stc->stcperson[ret].age << endl;;
    182     }
    183     else
    184     {
    185         cout << "查无此人!" << endl;
    186     }
    187     system("pause");
    188     system("cls");
    189     return;
    190 }
    191 //修改联系人
    192 void alterPerson(stcMenu * stc)
    193 {
    194     cout << "修改联系人
    ";
    195     cout << " ---- 输入姓名:
    ";
    196     string m_name;
    197     cin >> m_name;
    198     int ret = checkPerson(stc, m_name);
    199     if (ret != -1)
    200     {
    201         cout << "输入新年龄:
    ";
    202         int m_age;
    203         cin >> m_age;
    204         stc->stcperson[ret].age = m_age;
    205         cout << "修改完成
    ";
    206     }
    207     else
    208     {
    209         cout << "查无此人!" << endl;
    210     }
    211     system("pause");
    212     system("cls");
    213     return;
    214 }
    215 //清空联系人
    216 void clearPerson(stcMenu * stc)
    217 {
    218     again:
    219     int select;
    220     cout << "清空联系人
    ,确定--1,取消--2"<<endl;
    221     //cin >> select;
    222     select = numInput();
    223     if (select == 1)
    224     {
    225         cout << "已经清空联系人
    ";
    226         stc->m_size = 0;
    227     }
    228     else if (select == 2)
    229     {
    230         cout << "已经取消清空联系人
    ";
    231     }
    232     else
    233     {
    234         cout << "请输入正确选项
    ";
    235         goto again;
    236     }
    237     system("pause");
    238     system("cls");
    239     return;
    240 }
    241 
    242 
    243 #pragma endregion
    244 
    245 
    246 int main()
    247 {
    248     int select;
    249 
    250     stcMenu stcp;
    251     stcp.m_size = 0;
    252     while (true)
    253     {
    254         showMenu();
    255         cin >> select;
    256 
    257         switch (select)
    258         {
    259         case 1://1.添加联系
    260             addPerson(&stcp);
    261             break;
    262         case 2://2.显示联系人 
    263             showPerson(&stcp);
    264             break;
    265         case 3:// 3.删除联系人
    266             deletePerson(&stcp);
    267             break;
    268         case 4://4.查找联系人
    269             findPerson(&stcp);
    270             break;
    271         case 5://5.修改联系人
    272             alterPerson(&stcp);
    273             break;
    274         case 6://6.清空联系人
    275             clearPerson(&stcp);
    276             break;
    277         case 0://0.退出系统  
    278             cout << "欢迎下次使用" << endl;
    279             system("pause");
    280             return 0;
    281             break;
    282         }
    283 
    284     }
    285 
    286     return 0;
    287 }
  • 相关阅读:
    区域赛
    kd树的创建和求最近邻
    Fliptile POJ
    第六周作业
    第四次作业
    第三次作业
    第二周作业
    第一次作业
    董雅洁 我的第0次作业
    21 UI_布局 之 线性布局 xml配置方式
  • 原文地址:https://www.cnblogs.com/rtblogs/p/12027315.html
Copyright © 2020-2023  润新知