?? page08_14.cpp
字號:
/*
NIIT 《C++ & CGI PROGRAMMING &SCRRIPTING》 Skill Base
教材P8.14
審核:周永 Laozhou@swpi.edu.cn 23:36 2004-10-18
測試環境:Microsoft Windowsn 2000(VC++);Red Hat Linux 7.3;Red Hat Linux 9.0
*/
//包含頭文件
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//************************************************************************************
//************************************************************************************
//父類,被Customer繼承
class Person
{
//保護類型的成員變量 子類可以訪問
protected:
char name[26];
char dateOfBirth[11];
char city[26];
char mobileNo[12];
char phoneNo[11];
//公有類型的成員函數
public:
//接受用戶輸入數據的函數
void accept()
{
cout<< endl << "Name: ";
cin.getline(name, 25);
cout << endl << "Date of Birth: ";
cin >> dateOfBirth;
cin.ignore();
cout << endl << "City: ";
cin.getline(city, 25);
cout << endl << "Mobile phone number: ";
cin >> mobileNo;
cin.ignore();
cout << endl << "Residence phone number: ";
cin >> phoneNo;
cin.ignore();
} //end of accept()
//顯示對象中成員變量的函數
void display()
{
cout << endl << "Name : " << name;
cout << endl << "Date of birth : " << dateOfBirth;
cout << endl << "City : " << city;
cout << endl << "Mobile number : " << mobileNo;
cout << endl << "Residence number : " << phoneNo;
} //end of display()
}; //end of class Person
//客戶類Customer(Person的子類)
class Customer : public Person
{
//允許FileRead和FileWrite友元類訪問自己私有的成員
friend class FileRead;
friend class FileWrite;
private:
char billingAddress[51];
float amountOutstanding;
public:
void accept()
{
cout << endl << endl << "ENTER CUSTOMER'S DETAILS";
Person::accept();
cout << endl << "Customer's billing address: ";
cin.getline(billingAddress, 50);
cout << endl << "Amount due: ";
cin >> amountOutstanding;
cin.ignore();
} //end of accept()
void display()
{
cout << endl << endl << "CUSTOMER'S DETAILS ";
Person::display();
cout << endl << "Billing address : " << billingAddress;
cout << endl << "Outstanding amount: "<< amountOutstanding << endl;
} //end of display()
}; //end of class Customer
//寫Customer對象到文件中的類
class FileWrite
{
public:
void accept(Customer &custobj)
{
ofstream file;
custobj.accept();
file.open("customer.dat", ios::out | ios::app);
file.write((char *)&custobj, sizeof(custobj));
file.close();
}//end of accept()
}; //end of class FileWrite
//從文件讀取所有或者特定Customer對象的類
class FileRead
{
public:
//從文件開頭讀到結尾 顯示所有
void display_all(Customer &custobj)
{
ifstream file;
file.open("customer.dat", ios::in);
file.read((char *)&custobj, sizeof(custobj));
while( ! file.eof() )
{
custobj.display();
file.read((char *)&custobj, sizeof(custobj));
} //end of while
file.close();
} //end of display_all
//從文件開頭讀到結尾 只顯示某個特定數據(根據輸入的手機號碼)
void display_Cust(char *mobile)
{
ifstream file;
Customer custobj;
file.open("customer.dat", ios::in);
file.read((char *)&custobj, sizeof(custobj));
while( ! file.eof() )
{
if(strcmp(custobj.mobileNo, mobile) == 0)
{
custobj.display();
file.close();
return;
} //end of if
file.read((char *)&custobj, sizeof(custobj));
} //end of while
file.close();
cout << "A customer with mobile number: " << mobile << " does not exist ! " << endl;
} //end of display_Cust
}; //end of class FileRead
//四個類的編寫到此結束 下面是main函數
//************************************************************************************
//************************************************************************************
int main()
{
Customer custobj;
FileWrite writeobj;
FileRead readobj;
int choice = 0;
while(choice != 4)
{
cout << endl << "CUSTOMER MENU" << endl;
cout << "1. Accept the details of a customer" << endl;
cout << "2. Display the details of all customers" << endl;
cout << "3. Display details of one customer" << endl;
cout << "4. Quit the application" << endl;
cout << "Enter choice (1 - 4): ";
cin >> choice;
cin.ignore();
switch(choice)
{
case 1:
writeobj.accept(custobj);
break;
case 2:
readobj.display_all(custobj);
break;
case 3:
cout << "Enter Mobile Number : ";
char mobile[11];
cin >> mobile;
readobj.display_Cust(mobile);
break;
case 4:
continue;
default:
cout << "Invalid option! Please enter 1 - 4 only" << endl;
} //end of switch
} //end of while
return 0;
} //end of main
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -