·一、目的和要求
設計一個利用檔案處理方式實作電話號碼薄的程式,具有增加資料、更新資料、查詢資料、洗掉資料以及重組檔案的功能,洗掉資料在記錄中作洗掉標志,重組檔案指在物理上洗掉作有洗掉標志的記錄,
·二、系統分析
通過選單選擇呼叫函式來實作相應的功能,這樣條理清晰、整體效果好,便于程式的除錯,
定義電話結構體:
struct TelephoneType
{ // 電話結構
bool delTag; // 洗掉標志
unsigned int seat; // 序號
char name[16]; // 姓名
char teleNo[18]; // 電話號碼
};
設計電話號碼簿類:
/ 電話號碼簿類的宣告
class TelephoneBook
{
private:
// 資料成員
fstream file; // 電話號碼簿檔案
// 輔助函式
void AddData(); // 增加資料
void UpdateData(); // 更新資料
void SearchData(); // 查詢資料
void DeleteData(); // 洗掉資料, 只作洗掉標志
void Pack(); // 在物理上洗掉作有洗掉標記的記錄
public:
// 建構式, 解構式與方法
TelephoneBook(); // 無參建構式
virtual ~TelephoneBook(){ file.close(); } // 解構式
void Run(); // 處理電話號碼簿
};
·三、程式結果截圖
打開界面

增加資料

修改資訊

查詢資訊
洗掉資訊

重組檔案 退出

·四、總結與體會
通過撰寫電話號碼簿的程式,更加深入的理解了C++檔案,熟悉了各種對檔案操作的函式,并把每個功能用單獨的函式分開來撰寫,提高了代碼的效率和可讀性,
·五、源程式
#include <iostream> #include <fstream> #include <iomanip> using namespace std; struct TelephoneType{ bool delTag; unsigned int seat; char name[16]; char teleNo[18]; }; class TelephoneBook{ private: fstream file; void AddData(); void SearchData(); void UpdateData(); void DeleteData(); void Pack(); void ShowData(); public: TelephoneBook(); virtual~TelephoneBook() { file.close(); } void Run(); }; TelephoneBook::TelephoneBook() { ifstream iFile("telph.dat"); if (iFile.fail()) { ofstream oFile("telph.dat"); if (oFile.fail()) throw("打開檔案失敗!"); oFile.close(); } else { iFile.close(); } file.open("telph.dat", ios::in|ios::out|ios::binary); if (file.fail()) throw("打開檔案失敗!"); } void TelephoneBook::AddData() { cout<<"\t增加資料"<<endl; char flag='Y'; TelephoneType tel; while(toupper(flag)=='Y') { cin>>tel.seat>>tel.name>>tel.teleNo; tel.delTag= true; file.write((char *)&tel,sizeof(tel)); cout<<"是否繼續錄入資訊?(Y/N)"; cin>>flag; } } void TelephoneBook::SearchData() { TelephoneType tel1; unsigned int seat; cout<<"輸入想要查看的序號:"; cin>>seat; file.seekg(0); file.read((char *)&tel1,sizeof(tel1)); while(!file.eof()) { if(tel1.seat==seat&&tel1.delTag) break; file.read((char *)&tel1,sizeof(tel1)); } if(seat>tel1.seat) { cout<<"超出電話資訊序號,"<<endl; } if(!file.eof()) { cout<<tel1.seat<<"\t"<<tel1.name<<"\t"<<tel1.teleNo<<"\t"<<tel1.delTag; } } void TelephoneBook::UpdateData() { TelephoneType tel2; unsigned int seat; cout<<"輸入想要修改的序號資訊"<<endl; cin>>seat; file.seekg(0); file.read((char *)&tel2,sizeof(tel2)); while(!file.eof()) { if(seat==tel2.seat) { cout<<"顯示現有資訊:"<<endl; cout<<"序號"<<"\t"<<"姓名"<<"\t"<<"號碼"<<"\t\t"<<"真值"<<endl; cout<<tel2.seat<<"\t"<<tel2.name<<"\t"<<tel2.teleNo<<"\t"<<tel2.delTag<<endl; cout<<"寫出修改后的資訊:"<<endl; cin>>tel2.seat>>tel2.name>>tel2.teleNo; file.seekg(-sizeof(TelephoneType), ios::cur); file.write((char *)&tel2,sizeof(tel2)); break; } file.read((char *)&tel2,sizeof(tel2)); } } void TelephoneBook::DeleteData() { TelephoneType tel3; unsigned int seat; cout<<"輸入做洗掉標記的序號資訊:"<<endl; cin>>seat; file.seekg(0); file.read((char *)&tel3,sizeof(tel3)); while(!file.eof()) { if(seat==tel3.seat) { break; } file.read((char *)&tel3,sizeof(tel3)); } if(!file.eof()) { cout<< "被洗掉記錄為:" << endl; cout<<"\t"<<tel3.seat<<"\t"<<tel3.name<<"\t"<<tel3.teleNo<<endl; tel3.delTag=false; file.seekg(-sizeof(TelephoneType), ios::cur); file.write((char *)&tel3, sizeof(tel3)); cout<<"洗掉成功!"<<endl; } else { cout<<"洗掉失敗!"<<endl; file.clear(); } } void TelephoneBook::ShowData() { cout<<"\t\t電話簿"<<endl; TelephoneType tel5; file.seekg(0); file.read((char *)&tel5,sizeof(tel5)); while(!file.eof()) { cout<<tel5.seat<<"\t"<<tel5.name<<"\t"<<tel5.teleNo<<"\t"<<tel5.delTag<<endl; file.read((char *)&tel5,sizeof(tel5)); } } void TelephoneBook::Pack() { TelephoneType tel4; ofstream outfile("temp.dat",ios::app|ios::binary); file.seekg(0); file.read((char *)&tel4,sizeof(tel4)); while(!file.eof()) { if(tel4.delTag) { outfile.write((char *)&tel4,sizeof(tel4)); } file.read((char *)&tel4,sizeof(tel4)); } file.close(); outfile.close(); remove("telph.dat"); rename("temp.dat","telph.dat"); file.open("telph.dat", ios::in|ios::out|ios::binary); cout<<"成功洗掉資訊!!!"<<endl; } void TelephoneBook::Run() { int select; while(select!=7) { cout<<" 電話簿"<<endl; cout<<"1.增加資料\t2.更新資料"<<endl; cout<<"3.查詢資料\t4.洗掉資料"<<endl; cout<<"5.重組檔案\t6.查看資料"<<endl; cout<<"7.退出"<<endl; cout<<"請輸入想要進行的操作"; cin>>select; switch(select) { case 1:AddData();break; case 2:UpdateData();break; case 3:SearchData();break; case 4:DeleteData();break; case 5:Pack();break; case 6:ShowData();break; case 7:break; } } } int main() { TelephoneBook b; int a; b.Run(); system("PAUSE"); return 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/285517.html
標籤:C++
上一篇:介面類使用的一些準則
下一篇:C++記憶體管理機制
