?? opera.cpp
字號:
#include "opera.hpp"
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
int i, tall;
int compare(const void *m, const void *n) // 對地址簿排序的函數
{
return strcmp((*(class extpersonType *)m).fn, (*(class extpersonType *)n).fn);
}
void addressBookType::getin() // 1、數據庫讀取
{
char fname[50];
cout << "請輸入數據庫文件名:";
cin >> fname;
ifstream fin(fname); // 處理文件找不到的情況
if (!fin)
{
cout << "對不起,文件未找到!" << endl;
return;
}
fin >> total;
for (i = 0; i < total; i++)
{
fin.get();
fin.getline(person[i].fn, 30);
fin.getline(person[i].ln, 30);
fin.getline(person[i].sex, 10);
fin.getline(person[i].birthday, 20);
fin.getline(person[i].street, 50);
fin.getline(person[i].city, 30);
fin.getline(person[i].provice, 30);
fin.getline(person[i].postcode, 30);
fin >> person[i].att;
fin >> person[i].tel;
}
cout << "讀取完畢,共讀取通訊錄" << total << "條..." << endl;
}
void addressBookType::namesort() // 2、排序
{
qsort(person, total, sizeof(person[0]), compare);
cout << "排序完成..." << endl;
}
void addressBookType::searchfn() // 3、按姓氏查詢
{
char sfn[30];
cout << "請輸入需要查詢的姓氏:";
cin >> sfn;
cout << "查詢到的人員名單:" << endl;
tall = 0;
for (i = 0; i < total; i++)
if (!strcmp(sfn, person[i].fn))
{
tall++;
cout << person[i].fn << " " << person[i].ln << endl;
}
cout << "總計找到符合條件人員" << tall << "名" << endl;
system("pause");
}
void addressBookType::prn() // 4、打印指定人員詳細資料
{
char sfn[30], sln[30];
cin.get();
cout << "請輸入需要打印人員的姓氏:";
cin.getline(sfn, 30);
cout << "請輸入需要打印人員的名字:";
cin.getline(sln, 30);
for (i = 0; i < total; i++)
if (!strcmp(sfn, person[i].fn) && !strcmp(sln, person[i].ln))
person[i].out();
}
void addressBookType::searchbir() // 5、打印生日在指定日期間的人員名單
{
char fbir[20], sbir[20], ts[20];
cout << "請輸入兩個生日,用空格隔開(例如 19880601):";
cin >> fbir >> sbir;
if (strcmp(fbir, sbir) > 0) // 處理生日1比生日2大的判斷
{
strcmp(ts, fbir);
strcmp(fbir, sbir);
strcmp(sbir, ts);
}
cout << "查詢到的人員名單:" << endl;
tall = 0;
for (i = 0; i < total; i++)
if (strcmp(person[i].birthday, fbir) >= 0 && strcmp(person[i].birthday, sbir) <= 0)
{
tall++;
cout << person[i].fn << " " << person[i].ln <<endl;
}
cout << "總計找到符合條件人員" << tall << "名" << endl;
system("pause");
}
void addressBookType::serach_bfn()
{
char fn1[30], fn2[30], ts[30];
cin.get();
cout << "請輸入需要查詢姓氏1:";
cin.getline(fn1, 30);
cout << "請輸入需要查詢姓氏2:";
cin.getline(fn2, 30);
cout << fn1 << " " << fn2 << endl;
if (strcmp(fn1, fn2) > 0) // 處理姓氏1比姓氏2大的判斷
{
strcmp(ts, fn1);
strcmp(fn1, fn2);
strcmp(fn2, ts);
}
cout << "查詢到的人員名單:" << endl;
tall = 0;
for (i = 0; i < total; i++)
if (strcmp(person[i].fn, fn1) >= 0 && strcmp(person[i].fn, fn2) <= 0)
{
tall++;
cout << person[i].fn << " " << person[i].ln <<endl;
}
cout << "總計找到符合條件人員" << tall << "名" << endl;
system("pause");
}
void addressBookType::print_att() // 7、打印屬性
{
int i, need_att, tall = 0;
for (i = 0; i <= 2; i++)
cout << i << "、" << id[i] << endl;
cout << "請選擇你需要查詢的成員身份:";
cin >> need_att;
cout << "查詢到的人員名單:" << endl;
tall = 0;
for (i = 0; i < total; i++)
if (person[i].att == need_att)
{
tall++;
cout << person[i].fn << " " << person[i].ln <<endl;
}
cout << "總計找到符合條件人員" << tall << "名" << endl;
system("pause");
}
void addressBookType::print_all() // 8、輸出所有資料,用以檢驗程序
{
for (i = 0; i < total; i++)
person[i].out();
system("pause");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -