C++ HW8-1 for WallPower

撰寫一個C++打卡鐘程式,有打卡與查詢功能。打卡功能為輸入人ID後再輸入到達與離開時間。查詢功能為輸入個人ID後顯示上次打卡資料。
參考操作畫面如圖。
參考答案。
#include <string>
#include <stdio.h>
#include <iostream>
using namespace std;

typedef struct Data
{
 int id;
 string enter, exit;
};

void show(Data _data[], int index);
Data read();
Data _data[20];
int now = 0;
int select;
int index;

int main()
{
 while (1)
 {
  cout << "1) 打卡 2) 查詢 0) 離開:";
  cin >> select;
  if (select == 1)
  {
   if (now < 20)
   {
    _data[now] = read();
    now++;
   }
   else
    cout << "名單已滿!" << endl;
  }
  else if (select == 2 && now != 0)
  {
   cout << "想查詢誰的資料:";
   cin >> index;
   show(_data, index);
  }
  else if (select == 0)
   break;
  fflush(stdin);
 }
 system("pause");
 return 0;
}

void show(Data _data[], int index)
{
 for (int i = 0; i < 20; i++)
  if (_data[i].id == index)
   cout << "上次到啥時間為:" << _data[i].enter << endl << "上次離開時間為:" << _data[i].exit << endl;
}

Data read()
{
 Data newdata;
 cout << "請輸入你在公司的ID:";
 cin >> newdata.id;
 cout << "您今天到班的時間是:";
 cin >> newdata.enter;
 cout << "   離開的時間是:";
 cin >> newdata.exit;
 cout << "謝謝,下一位。" << endl;
 return newdata;
}

留言

這個網誌中的熱門文章

UVA 11321 Java