#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
class Date{
public:
int year; //年
int month; //月
int day; //日
Date( ) {year = 2020; month = 1; day = 1;} //三個都是建構式
Date(int y, int m, int d) : year(y), month(m), day(d) {}
Date(const Date& b) {year = b.year; month = b.month; day = b.day;}
~Date(){}
};
class Base0 //基類
{
public:
Base0(){}
Base0(char num[8],char nam[10],int y,int m,int d,float sco):birth(y,m,d)
{strcpy(no,num);
strcpy(name,nam);
score=sco; //建構式
}
void show_base0(){ //輸出Base0的資料成員
cout<<no<<" "<<name<<" "<<birth.year<<" "
<<birth.month<<" "<<birth.day<<" "<<score<<" ";
}
protected:
char no[8]; //編號
char name[10]; //姓名
Date birth; //生日
float score; //成績
};
class Derived: public Base0 //派生類Derived公有繼承Base0
{
public:
Derived(){}
Derived(char num[],char nam[],int y,int m,int d,float sco,char ph[15],char address[10]):Base0(num,nam,y,m,d,sco)
{
strcpy(phone,ph);
strcpy(addr,address);
}
char * getNo(){return no;} //得到編號
void setScore(float sco){score=sco;} //設定成績
void show_d() //輸出資料成員
{
show_base0();
cout<<phone<<" "<<addr<<endl;
}
bool operator==(char num[8]){ //運算子多載,比較編號是否相等
if(strcmp(no,num)==0)
return true;
else
return false;
}
friend void inputDerData(Derived d[10],int &len ); //友元函式,從資料檔案讀資料
friend void outputDerData(const Derived d[10],int len );//友元函式,寫資料到資料檔案
private:
char phone[9]; //電話
char addr[10]; //地址
};
//從文本檔案der.dat讀資料到記憶體陣列d
void inputDerData(Derived d[10],int &len )
{
ifstream infile("der.dat",ios::in); //1.打開
//定義輸入檔案流物件,以輸入方式打開磁盤檔案f1.dat
if(!infile) //2.判斷
{ cerr<<"open error!"<<endl;
exit(1);
}
int i=0;
while(!infile.eof()) //3.讀出
{ infile>>d[i].no>>d[i].name
>>d[i].birth.year>>d[i].birth.month>>d[i].birth.day
>>d[i].score>>d[i].phone>>d[i].addr; //從磁盤檔案讀數,順序存放在d陣列中
i++;
}
len=i;
infile.close(); //4.關閉
}
void outputDerData(const Derived d[10],int len )
{//將記憶體陣列d資料寫進文本檔案der.dat
if (len==0)
return;
ofstream outfile("der.dat",ios::out);
//定義輸入檔案流物件,以輸入方式打開磁盤檔案f1.dat
if(!outfile)
{ cerr<<"open error!"<<endl;
exit(1);
}
int i;
for( i=0;i<len-1;i++)
{ outfile<<d[i].no<<" "<<d[i].name<<" "
<<d[i].birth.year<<" "<<d[i].birth.month<<" "<<d[i].birth.day<<" "
<<d[i].score<<" "<<d[i].phone<<" "<<d[i].addr<<endl; //從磁盤檔案讀數,順序存放在d陣列中
}
i=len-1;
outfile<<d[i].no<<" "<<d[i].name<<" "
<<d[i].birth.year<<" "<<d[i].birth.month<<" "<<d[i].birth.day<<" "
<<d[i].score<<" "<<d[i].phone<<" "<<d[i].addr;
outfile.close();
}
bool searchNo(Derived d[10],int len,char number[8])
{//在長度為len的d陣列中查找編號為number的元素,找到回傳true.否則回傳false
int i;
for(i=0;i<len;i++)
{ if(d[i]==number)
{ return true; }
}
return false;
}
void traverse( Derived d[10],int len){
//遍歷長度為len的d陣列
int i;
if(len==0)
{cout<<"親!還沒有資料 "<<endl; return ;}
cout<<"編號,姓名,年月日,成績,電話,地址"<<endl;
for(i=0;i<len;i++){
d[i].show_d();
}
}
int searchIdx(Derived d[10],int len,char number[8])
{//在長度為len的d陣列中查找編號為number的元素,找到回傳位序.否則回傳0
int i;
for(i=0;i<len;i++)
{ if(d[i]==number)
{ return i+1; }
}
return 0;
}
void insertObj(Derived d[10],int &len,Derived d1){
//在長度為len的d陣列中插入物件d1,先查找編號,找到不插入.否則插入
bool res=searchNo(d,len,d1.getNo());
if(res)
{cout<<d1.getNo()<<" has exist "<<endl;}
else
{d[len++]=d1;cout<<d1.getNo()<<" inserted"<<endl;}
}
void modifyObj(Derived d[10],int len,char num[8],float newsco){
//在長度為len的d陣列中找到物件d0,修改其成績為newsco
int index=searchIdx(d,len,num);
if(index)
{ cout<<"修改成績為 "<<newsco<<endl;
d[index-1].setScore(newsco);}
else
{cout<<num<<" not found!!!";}
}
void deleteObj(Derived d[10],int &len,char num[8]){
//在長度為len的d陣列中找到物件d0,修改其成績為newsco
int index=searchIdx(d,len,num);
if(index)//找到則洗掉
{
for(int i=index-1;i<len-1;i++)
{d[i]=d[i+1];}
len--;
}
else
{cout<<num<<" not found!!!";}
}
/*
//讀二進制資料檔案
void inputDerData(Derived t[10],int &len )
{
ifstream infile("DBin.dat",ios::in|ios::binary);
//定義輸入檔案流物件,以輸入方式打開磁盤檔案f1.dat
if(!infile)
{ cerr<<"open error!"<<endl;
exit(1);
}
cout<<"read from binary"<<endl;
for(int i=0;i<3;i++){
infile.read((char *)&t[i],sizeof(t[i]));
cout<<t[i].no<<" "<<t[i].name<<" "<<t[i].age<<" "<<t[i].score<<" "
<<t[i].phone<<" "<<t[i].addr<<endl;
}
//從文本檔案讀資料,建二進制檔案,此時個數多1個error
int i=0;
cout<<"read from binary"<<endl;
while(!infile.eof())
{
infile.read((char *)&t[i],sizeof(t[i]));
cout<<t[i].no<<" "<<t[i].name<<" "<<t[i].age<<" "<<t[i].score<<" "
<<t[i].phone<<" "<<t[i].addr<<endl;
i++;
}
len=i;
cout<<"in read binary len is "<<len<<endl;
infile.close();
}
*/
/*
void outputDerData(const Derived t[10],int len )
{//寫二進制資料檔案
ofstream outfile("DBin.dat",ios::out|ios::binary);
//定義輸入檔案流物件,以輸入方式打開磁盤檔案f1.dat
if(!outfile)
{ cerr<<"open error!"<<endl;
exit(1);
}
cout<<"write to binary file"<<endl;
for(int i=0;i<len;i++)
{
cout<<t[i].no<<" "<<t[i].name<<" "<<t[i].age<<" "<<t[i].score<<" "
<<t[i].phone<<" "<<t[i].addr<<endl;
outfile.write((char*)&t[i],sizeof(t[i]));
}
outfile.close();
}
*/
Derived getObj() //創建物件
{ char no[8];
char name[10];
int year;
int month;
int day;
float score;
char phone[9];
char addr[10];
cout<<"請輸入待操作物件的每個屬性:編號,姓名,年月日,成績,電話,地址"<<endl;
cin>>no>>name>>year>>month>>day>>score>>phone>>addr;
Derived d1(no,name,year,month,day,score,phone,addr);
return d1;
}
void inputObjNo(char num[8]) //讀入編號
{ cout<<"請輸入待操作物件的編號"<<endl;
cin>>num;
}
void inputObjSco(float &newsco){ //讀入成績
cout<<"輸入修改后的成績"<<endl;
cin>>newsco;
}
int main(){
Derived d[30];
int i,d_len=0;
bool result;
char number[8];
float newsco;
Derived d1;
int choose;
while(1){
cout<<endl<<endl;
cout<<"*----------操作選單----------*"<<endl;
cout<<"* *"<<endl;
cout<<"* 1.從磁盤檔案中讀取資料 *"<<endl;
cout<<"* 2.資料存入磁盤檔案中 *"<<endl;
cout<<"* 3.遍歷資料 "<<" 4.查找資料 *"<<endl;
cout<<"* 5.插入資料 "<<" 6.修改資料 *"<<endl;
cout<<"* 7.洗掉資料 "<<" 8.退出操作 *"<<endl;
cout<<"* *"<<endl;
cout<<"*----------------------------*"<<endl;
cout<<" 請選擇你的操作(輸入數字): "<<endl;
cin>>choose;
switch(choose){
case 1: inputDerData(d,d_len); break;
case 2: outputDerData(d,d_len); break;
case 3: traverse(d,d_len); break;
case 4: inputObjNo(number);
result=searchNo(d,d_len,number);
if(result)
{cout<<number<<" found!!!"<<endl;}
else
{cout<<number<<" not found!!!"<<endl;}
break;
case 5:
d1=getObj();
insertObj(d,d_len,d1);
break;
case 6:
inputObjNo(number);
inputObjSco(newsco);
modifyObj(d,d_len,number,newsco);
break;
case 7:
inputObjNo(number);
deleteObj(d,d_len,number);
break;
case 8: exit(0);
}//switch
}//while
return 0;
}
/* 小型系統開發
編號是唯一的,不允許重復
//一、所用知識:
1.類定義:資料成員,成員函式,建構式等
2.繼承與派生:基類,派生類,類的組合等
3.運算子多載:運算子多載(==)
4.資料檔案讀寫:讀寫操作,使用友元函式實作
5.小型系統操作:檔案讀寫,增刪改查等
二、問題及解決辦法:
1.語法出錯,根據編譯器提示資訊修改
2.相鄰兩個資料成員都是陣列,第一個長度定義的小,導致字串結束標記出問題.
3.程式比較凌亂時,整理思路,模塊化設計,寫出函式實作某功能,然后進行函式呼叫
4.以檔案結束標記做條件時,資料檔案不能多空行,否則讀入垃圾資料.
5.資料檔案創建時所用字符集默認為utf-8,讀入后亂碼,另存為ansi字符集即可
6.寫入資料時最后一個物件資料后不能加回車,否則再次讀入會出現垃圾資料
三、需要改進:
1.界面單調,功能不全
2.修改資料只做了修改成績功能,修改其他資訊的未實作.可以由用戶選擇性修改資訊
3.查找資料時查找成功,應該顯示物件所有資訊.
4.插入資料中因為編號唯一,所以應該先輸入編號,判斷是否存在,如果存在就不輸入其他資訊了.
5.定義類中成員函式只寫了本系統中必須的,其他函式沒做.例如get,set函式
*/
uj5u.com熱心網友回復:
具體報的什么錯呀?uj5u.com熱心網友回復:
說exit沒有定義uj5u.com熱心網友回復:
你能幫我除錯一下嗎uj5u.com熱心網友回復:
加下這個#include <stdlib.h>uj5u.com熱心網友回復:
可以了,這是為什么呢uj5u.com熱心網友回復:
你的報錯exit沒有定義,說明這個函式沒有找到,指定一下這個函式所在頭檔案就可以了。uj5u.com熱心網友回復:
大神,能不能幫我改進一下程式,我讀了很久了uj5u.com熱心網友回復:
還是自己debug能學到東西,別人改了對你沒有什么幫助轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/47677.html
標籤:C++ 語言
下一篇:求解大佬
