Advertisement
pigg

1198: 106計算機程式設計作業 - 16 - Structure Operation

Nov 30th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     typedef struct
  8.     {
  9.         string firstName;
  10.         string lastName;
  11.         string phone;
  12.     } StRec;
  13.     StRec str[10];
  14.     string input, input1, input2,input3;
  15.     int count = 0;
  16.     while(cin>>input)
  17.         {
  18.         if (input == "print")
  19.         {
  20.             if (count == 0)
  21.             {
  22.                 cout << "Print Error\n";
  23.             }
  24.             else
  25.             {
  26.                 for(int i=0;i<=count;i++)
  27.                 {
  28.                     if(str[i].firstName == "")
  29.                     {
  30.                         continue;
  31.                     }
  32.                     cout << str[i].firstName << "\t" << str[i].lastName << "\t" << str[i].phone<<"\n";
  33.                 }
  34.             }
  35.         }
  36.         else if (input =="insert")
  37.         {
  38.             if(count == 10)
  39.             {
  40.                 cout << "Insert Error\n";
  41.             }
  42.             else
  43.             {
  44.                 cin >> input1;
  45.                 cin >> input2;
  46.                 cin >> input3;
  47.                 str[count].firstName = input1;
  48.                 str[count].lastName = input2;
  49.                 str[count].phone = input3;
  50.                 cout << str[count].firstName << "\t" << str[count].lastName << "\t" << str[count].phone << "\n";
  51.                 count++;
  52.             }
  53.         }
  54.         else if (input == "search")
  55.         {
  56.             bool flag = false;
  57.             cin >> input1;
  58.             cin >> input2;
  59.             cin >> input3;
  60.             for (int i=0 ; i<= count ; i++)
  61.             {
  62.                 if (str[i].firstName == input1 &&
  63.                     str[i].lastName == input2 &&
  64.                     str[i].phone == input3)
  65.                     {
  66.                         cout<<i<<endl;
  67.                         flag = 1;
  68.                     }
  69.             }
  70.             if (flag == false)
  71.             {
  72.                 cout << "Search Error\n";
  73.             }
  74.         }
  75.         else if (input == "delete")
  76.         {
  77.             bool flag = false;
  78.             cin >> input1;
  79.             cin >> input2;
  80.             cin >> input3;
  81.             for (int i = 0; i <= count; i++)
  82.             {
  83.                 if (str[i].firstName == input1 &&
  84.                     str[i].lastName == input2 &&
  85.                     str[i].phone == input3)
  86.                 {
  87.                     str[i].firstName = "";
  88.                     str[i].lastName = "";
  89.                     str[i].phone = "";
  90.                 }
  91.                 flag = true;
  92.             }
  93.             if (flag == false)
  94.             {
  95.                 cout << "Delete Error\n";
  96.             }
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement