C++対象向けプログラミング実践——タスクと指導書(3)
実験4:継承と多態目的(1)パッケージ、継承と派生概念の理解を深め、プログラム設計における応用場面を理解する.(2)派生クラスの文法規則を熟知し、継承関係の下で、派生クラスが担うべき仕事と各種メンバーへのアクセス方法を掌握する.(3)クラス階層において、ベースクラスと派生クラスの構造と構造順序を理解する.(4)虚関数の定義方法と多態性の概念を把握し、虚関数による動的結合特性を理解する.(5)ベースクラスポインタと参照を用いて派生クラス虚関数に直接アクセスするプログラム設計方法を初歩的に把握する. ステップは、タスク内の各問題について、解題構想を分析し、設計し、プログラムを作成し、ツールの誤り訂正を観察し、デバッグすることによって、正しい結果を得る. 内容の最低数量の要求:問題1は必ずします;問題2~問題3選一.
4.1
従業員は、従業員(Employee)、マネージャ(Manager)、技術者(Technician)、販売者(Salesman)、販売マネージャ(SalesManager)に分類されるとします.これらには、名前、年齢、専門、固定給与マネージャクラス、氏名、年齢、固定給与マネージャクラス、氏名、年齢、固定給与、専門、レベルなどの属性が含まれます.補助金Technician類:氏名、年齢、固定賃金、専門、勤務時間、時給Salesman類:氏名、年齢、固定賃金、専門、売上高、販売歩合率SalesManager類:氏名、年齢、固定賃金、専門、補助金、売上高、売上引当比率クラスに含まれるメンバー関数(完了する操作はあります):(1)コンストラクション関数;(2)構造関数;(3)入力関数;(4)出力関数;(5)給与関数を計算し、給与計算方法を表2に示す.
表2給与計算方法従業員カテゴリ固定給与手当時給売上高Employee 1500 0 0 Manager 8000 2000 0 Technician 1000 100(または実際の状況に応じて変更)0 Salesm 500 0 5%(または実際の状況に応じて変更)SaleManager 2000 1000 0 2%(または実際の状況に応じて変更)
ヒント:クラス間の共通性を見つけ、継承またはマルチ継承で実現します.
4.2
動物園管理プログラム設計.ある動物園には猫や犬を含む20匹のケージがペットを飼うために使われていると仮定します.動物園はペットを収容したり、引き取ったりすることができます.猫も犬も名前の属性と「呼ぶ」方法がある(方法の内包が異なる).この20匹のケージとペットを1つのデータ項目(配列など)で管理し、実際のオブジェクトに基づいて「呼ぶ」方法を呼び出すための関数bark()を作成する必要があります.
4.3
従業員情報テーブルを設計し、従業員番号、氏名、性別、年齢を含む従業員情報データを構築する.要求は以下の通りである:(1)従業員情報表に基づき、氏名と年齢のみを含む従業員情報簡表を作成する.(2)継承手法を用いて2つのクラスを構築し,対応するオブジェクト配列を用いて10人の従業員情報を配置する.(3)配列の内容を出力するための同名display()メンバー関数を記述する.(4)実際のオブジェクトに基づいてコンテンツを出力するための関数print()を別途作成する.
実験5:演算子のリロードとストリームプログラム設計目的(1)演算子のリロードの概念と一般的な方法を理解する.(2)いくつかの一般的な演算子のリロード方法を把握する.(3)ストリームの意味と主なストリームクラス構造を理解する.(4)フロー可能クラスを構築する方法,すなわち入出力演算子を再ロードする方法を把握する.(5)簡単なストリームフォーマット制御技術を身につける;(6)ファイルフローの操作方法を把握する. ステップは、タスク内の各問題について、解題構想を分析し、設計し、プログラムを作成し、ツールの誤り訂正を観察し、デバッグすることによって、正しい結果を得る. 内容最小数量要求:問題1~問題2選一;問題3~問題6選一.
5.1
有理数分式方程式を解く有理数は2つの整数の比率であり,通常a/bと表される.ここでaは分子と呼ばれ、bは分母となる.要求:(1)分母は0にできない;(2)分子と分母には公約数があり、それに対応して簡略化され、すなわち約分である.(3)これを4則演算する.(4)有理数は大きさを比較することができる.(5)付加:重荷重<>で、点数を全体的に入力・出力できる.
5.2
文字列クラスを設計し、独自の文字列クラス(String)を設計します.要求:(1)各種のシリアル操作(サブシリアル操作、シリアルコピー、シリアル接続)は、文字列の境界を検査し、処理する.(2)複製方式でシリアル割り当てを実現し、異なるポインタが文字列を共有することを避け、データの独立性を高める.(3)適切な演算子を使用して文字列を定義する操作(たとえば、==、>=、(4)を使用して、サブストリング操作、パターンマッチングなどの高レベルの操作を定義できます.
5.3
実験2で定義した集合クラスを修正し,(1)Addを<(2)Removeを>>に変更し,機能は要素である,(2)Addを<(2)Removeを>>に変更する.(3)Assignは=に変更され,機能は集合賦値である.(4)EqualToは==に修正され,集合が等しいか否か(すなわち同じ要素があるか否か)を判別する機能である.(5)Intersectは*に変更され、機能は集合の交差を求める.(6)ユニオンは+に変更され,機能は集合を求める並列集合である.また、クラスには、(1)!=:機能は2つの集合が等しくないかどうかを判別することである.(2)(3)<=:機能は,ある集合が別の集合のサブセットであるか否かを判別する.(4)-:機能は、2つのセットの差セットを計算することです.
5.4
実験2で定義した大きな整数クラスを修正し、大きな整数の付与、加算、減算、負の取り方、大きい、小さい、大きい、等しい、小さい、等しい、等しい、等しくない、等しくない演算のリロードを実現し、そのためにリロード演算子メソッドを入力および出力する.
5.5
実験2で定義した製品クラスを修正し,クラスがフロー可能にする.次にmain関数で「入荷」と「販売」からなるメインメニューを作成します.入荷部分はキーボードからいくつかの商品を入力し、ファイルストリームで1つの製品ファイルに保存します.販売部分は製品ファイルのデータを読み込み、2級販売メニューを実現し、ユーザーが任意に各種商品を購入する過程を示し、在庫を再更新する.問題6:実験3で定義した電子クロッククラスを修正し,+,+である関数を修正し,時間,日付の自己加算1とnを実現する.
実験6:汎用プログラミング目的(1)汎用プログラム設計方法の概念を理解する.(2)関数テンプレートの定義と使用を把握する.(3)クラステンプレートの定義と使用を把握する. ステップは、タスク内の各問題について、解題構想を分析し、設計し、プログラムを作成し、ツールの誤り訂正を観察し、デバッグすることによって、正しい結果を得る. 内容最小数量要求:問題1~問題2選一;問題3必ずやります.
6.1
浮動小数点配列要素の「累加」を実現する関数テンプレートAccumulatorを作成します.要求:累積方式は1つの関数AccumulateMethodによって決定され、2つの要素の和、積、平方和などを表す可能性があり、AccumulateMethodは関数ポインタ方式でAccumulatorに渡され、デフォルト方式は累積和である.
6.2
配列要素の平均値を計算する関数テンプレートを作成します.
6.3
実験2の(5)で定義した集合をクラステンプレートとして構築した.
実験7:総合プログラミング目的(1)知識の総合運用能力を鍛える.(2)いくつかの典型的な構造の使い方を理解する. ステップ(1)タスク内の各問題について、解題構想を分析し、設計し、プログラムを作成し、ツールの誤り訂正を観察し、デバッグすることによって、正しい結果を得る.(2)自分で資料を探して、実例を通じてコードの規範性の具体的な体現を理解する. 内容:基本問題プログラム設計5最小数量要求:問題1~問題2選一.
7.1
リストを使用して、連絡先の追加、連絡先の削除、電話帳の内容の表示、連絡先の電話、ディスクの照会などの基本的な機能を必要とする小型電話帳管理プログラムを設計します.
7.2
学生クラスstudentを設計し、学号、氏名、性別、年齢、成績仮定学号が1から連続的に符号化される整数であるという基本情報を含む.このクラスの入出力演算子を再ロードし、(1)キーボードから複数の学生情報を入力してファイルに格納する機能を実現します.(2)学番で生徒の情報を検索し表示する.(3)学生の情報を学号で検索して修正する.(4)すべての成績優秀な学生の情報を表示する.
4.1
従業員は、従業員(Employee)、マネージャ(Manager)、技術者(Technician)、販売者(Salesman)、販売マネージャ(SalesManager)に分類されるとします.これらには、名前、年齢、専門、固定給与マネージャクラス、氏名、年齢、固定給与マネージャクラス、氏名、年齢、固定給与、専門、レベルなどの属性が含まれます.補助金Technician類:氏名、年齢、固定賃金、専門、勤務時間、時給Salesman類:氏名、年齢、固定賃金、専門、売上高、販売歩合率SalesManager類:氏名、年齢、固定賃金、専門、補助金、売上高、売上引当比率クラスに含まれるメンバー関数(完了する操作はあります):(1)コンストラクション関数;(2)構造関数;(3)入力関数;(4)出力関数;(5)給与関数を計算し、給与計算方法を表2に示す.
表2給与計算方法従業員カテゴリ固定給与手当時給売上高Employee 1500 0 0 Manager 8000 2000 0 Technician 1000 100(または実際の状況に応じて変更)0 Salesm 500 0 5%(または実際の状況に応じて変更)SaleManager 2000 1000 0 2%(または実際の状況に応じて変更)
ヒント:クラス間の共通性を見つけ、継承またはマルチ継承で実現します.
#include
#include
using namespace std;
class employee
{
protected:
string name,part;
int age;
double salary;
public:
employee(){
salary=1500;}
virtual void input()
{
cout<<" :"<<endl
<<" :";cin>>name;cout<<" :";cin>>age;
cout<<" :";cin>>part;
}
virtual void display()
{
cout<<" :"<<endl
<<" :"<<name<<endl<<" :"<<age<<endl
<<" :"<<part<<endl<<" :"<<salary<<endl;
}
virtual double salary_calculate()
{
return salary;
}
};
class manager:public employee //mamanger
{
protected:
int level;
double subsidy;
public:
manager(){
salary=8000;subsidy=2000;}
virtual void input(){
cout<<" :"<<endl
<<" :";cin>>name;cout<<" :";cin>>age;
cout<<" :";cin>>part;cout<<" :";cin>>level;
}
virtual void display(){
cout<<" :"<<endl
<<" :"<<name<<endl<<" :"<<age<<endl
<<" :"<<part<<endl<<" :"<<level<<endl
<<" :"<<salary<<endl<<" :"<<subsidy<<endl;
}
virtual double salary_calculate()
{
return salary+subsidy;
}
};
class technician:public employee //technician
{
protected:
double worktime;
double timesalary;
public:
technician(){
salary=1000;timesalary=100;}
virtual void input(){
cout<<" :"<<endl
<<" :";cin>>name;cout<<" :";cin>>age;
cout<<" :";cin>>part;
}
virtual void display()
{
cout<<" :"<<endl
<<" :"<<name<<endl<<" :"<<age<<endl
<<" :"<<part<<endl
<<" :"<<salary<<endl<<" :"<<timesalary<<endl;
}
virtual double salary_calculate()
{
cout<<" ( ):";cin>>worktime;
return salary+worktime*timesalary;
}
};
class salesman:public employee //saler
{
protected:
double value;
double percentage;
public:
salesman(){
salary=500;percentage=0.05;}
virtual void input()
{
cout<<" :"<<endl
<<" :";cin>>name;cout<<" :";cin>>age;
cout<<" :";cin>>part;
}
virtual void display()
{
cout<<" :"<<endl
<<" :"<<name<<endl<<" :"<<age<<endl
<<" :"<<part<<endl
<<" :"<<salary<<endl<<" :"<<percentage*100<<"%"<<endl;
}
virtual double salary_calculate()
{
cout<<" :";cin>>value;
return salary+value*percentage;
}
};
class salesmanager:public employee
{
protected:
double subsidy;
double value;
double percentage;
public:
salesmanager()
{
salary=2000;subsidy=1000;percentage=0.02;}
virtual void input()
{
cout<<" :"<<endl
<<" :";cin>>name;cout<<" :";cin>>age;
cout<<" :";cin>>part;
}
virtual void display()
{
cout<<" :"<<endl
<<" :"<<name<<endl<<" :"<<age<<endl
<<" :"<<part<<endl
<<" :"<<subsidy<<endl<<" :"<<salary<<endl
<<" :"<<percentage*100<<"%"<<endl;
}
virtual double salary_calculate()
{
cout<<" :";cin>>value;
return salary+subsidy+value*percentage;
}
};
void salary_display(double s)
{
cout<<" :"<<s<<endl;
}
int main()
{
int judge;
double salary;
employee A;manager B;technician C;salesman D;salesmanager E;
cout<<endl;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (1) "<<endl
<<" (2) "<<endl
<<" (3) "<<endl
<<" (4) "<<endl
<<" (5) "<<endl
<<endl
<<" :" ;
cin>>judge;
cout<<" :";
switch(judge)
{
case 1:cout<<" "<<endl;break;
case 2:cout<<" "<<endl;break;
case 3:cout<<" "<<endl;break;
case 4:cout<<" "<<endl;break;
case 5:cout<<" "<<endl;break;
}
switch(judge)
{
case 1:
A.input();A.display();salary=A.salary_calculate();break;
case 2:
B.input();B.display();salary=B.salary_calculate();break;
case 3:
C.input();C.display();salary=C.salary_calculate();break;
case 4:
D.input();D.display();salary=D.salary_calculate();break;
case 5:
E.input();E.display();salary=E.salary_calculate();break;
}
salary_display(salary);
cout<<" !"<<endl;
return 0; }
4.2
動物園管理プログラム設計.ある動物園には猫や犬を含む20匹のケージがペットを飼うために使われていると仮定します.動物園はペットを収容したり、引き取ったりすることができます.猫も犬も名前の属性と「呼ぶ」方法がある(方法の内包が異なる).この20匹のケージとペットを1つのデータ項目(配列など)で管理し、実際のオブジェクトに基づいて「呼ぶ」方法を呼び出すための関数bark()を作成する必要があります.
#include
#include
#include
#include // 7-1, 7-1 !
using namespace std;
class animals
{
protected:
string name;
string number;
string type;
bool isfull;
public:
virtual void add_pets(){
}
virtual void del_pets(){
}
void select(string sign)
{
isfull=true;
ifstream inData("dwy.txt",ios::in);
if (!inData)
{
cout <<endl <<" !" <<endl;
system("pause");
return;
}
string str1;
string str;
while (inData>>name>>number)
{
getline(inData, str);
if (sign==number)
{
isfull=false;}
}
}
virtual void save_new(){
}
virtual void bark(){
}
};
class cat:public animals
{
public:
void bark()
{
cout<<" !"<<endl;
}
void save_new()
{
type=" ";
ofstream outData("dwy.txt", ios::app);
outData <<setiosflags(ios::left)<<setw(17)<<name<<" "<<setw(16)<<number<<" "<<setw(16)<<type<<endl;
outData.close();
}
void add_pets()
{
cout<<" :" <<endl;
cout<<" :";cin>>name;
cout<<" :";cin>>number;
select(number);
if(isfull==false){
cout<<" ! !"<<endl;
}
else{
save_new();
bark();
cout<<" !" <<endl;}
system("pause");
}
void del_pets()
{
{
string sign,str1,str; //
bool flag = true; //
cout<<" :"<<endl; //
cin>>sign;
ofstream outData("temp.txt", ios::out); //
ifstream inData("dwy.txt", ios::in); //
while (inData >>name >>number)
{
getline(inData, str); //
if ((sign==name) || (sign==number))
{
cout <<" :"<<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl; //
flag = false;
break;
}
outData <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl;
}
if (flag)
{
cout <<endl <<" !" <<endl <<endl;
}
else
{
while (getline(inData, str))
{
outData <<str <<endl;
}
outData.close();
inData.close();
ofstream out("dwy.txt", ios::out);
ifstream in("temp.txt", ios::in);
while (getline(in, str))
{
out <<str <<endl;
}
out.close(); //
in.close();
cout <<endl <<" !" <<endl <<endl;
}
system("pause");
}
}
};
class dog:public animals
{
public:
void bark()
{
cout<<" !"<<endl;
}
void save_new()
{
type=" ";
ofstream outData("dwy.txt", ios::app);
outData <<setiosflags(ios::left)<<setw(17)<<name<<" "<<setw(16)<<number<<" "<<setw(16)<<type<<endl;
outData.close();
}
void add_pets()
{
cout<<" :" <<endl;
cout<<" :";cin>>name;
cout<<" :";cin>>number;
select(number);
if(isfull==false){
cout<<" ! !"<<endl;
}
else{
save_new();
bark();
cout<<" !" <<endl;}
system("pause");
}
void del_pets()
{
{
string sign,str1,str; //
bool flag = true; //
cout<<" :"<<endl; //
cin>>sign;
ofstream outData("temp.txt", ios::out); //
ifstream inData("dwy.txt", ios::in); //
while (inData >>name >>number)
{
getline(inData, str); //
if ((sign==name) || (sign==number))
{
cout <<" :"<<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl; //
flag = false;
break;
}
outData <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl;
}
if (flag)
{
cout <<endl <<" !" <<endl <<endl;
}
else
{
while (getline(inData, str))
{
outData <<str <<endl;
}
outData.close();
inData.close();
ofstream out("dwy.txt", ios::out);
ifstream in("temp.txt", ios::in);
while (getline(in, str))
{
out <<str <<endl;
}
out.close(); //
in.close();
cout <<endl <<" !" <<endl <<endl;
}
system("pause");
}
}
};
int main()
{
int judge,judge1;
cat x;
dog y;
fstream oo("dwy.txt",ofstream::out);
while(1)
{
cout<<endl;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I I | "<<endl
<<" | I の I | "<<endl
<<" | I I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (1) "<<endl
<<" (2) "<<endl
<<" (3) "<<endl
<<endl
<<" :" ;
cin>>judge;switch(judge)
{
case 1:
cout<<" "<<endl
<<"(1) "<<endl
<<"(2) "<<endl;
cin>>judge1;
switch(judge1)
{
case 1:
x.add_pets();
system("cls"); //
break;
case 2:
y.add_pets();
system("cls"); //
break;
}
break;
case 2:
cout<<" "<<endl
<<"(1) "<<endl
<<"(2) "<<endl;
cin>>judge1;
switch(judge1)
{
case 1:x.del_pets();break;
case 2:y.del_pets();break;
}
break;
case 3:return 0;
}}}
4.3
従業員情報テーブルを設計し、従業員番号、氏名、性別、年齢を含む従業員情報データを構築する.要求は以下の通りである:(1)従業員情報表に基づき、氏名と年齢のみを含む従業員情報簡表を作成する.(2)継承手法を用いて2つのクラスを構築し,対応するオブジェクト配列を用いて10人の従業員情報を配置する.(3)配列の内容を出力するための同名display()メンバー関数を記述する.(4)実際のオブジェクトに基づいてコンテンツを出力するための関数print()を別途作成する.
#include
#include
#include
#include
using namespace std;
void Display();
void Printer();
void ShowMenu();
void brevity();
int Menu_select();
void Dispvemps();
void Dispvemp(); //
class employee
{
//
public:
char name[10],id[9];
char *GetName()
{
return name;
}
void SetName(char n[])
{
strcpy(name,n);
}
char * GetID()
{
return id;
}
void SetID(char i[])
{
strcpy(id,i);
}
virtual void Display()
{
cout<<"\t"<<GetID()<<"\t"<<GetName()<<endl;}
};
class employees:public employee
{
class employee;
protected:
char sex[4];
int age;
public:
char * GetSex()
{
return sex;
}
void SetSex(char s[])
{
strcpy(sex,s);
}
int GetAge()
{
return age;
}
void SetAge(int a)
{
age=a;
}
void WriteFile();
void ReadFile();
//virtual
void Display()
{
cout<<GetID()<<"\t"<<GetName()<<"\t"<<GetSex()<<"\t"<<GetAge()<<endl;
}
};
vector<employee>vemp; //
vector<employees>vemps;
void Printer(employee& s)
{
s.Display();
}
void AddNew() //
{
char ch[5];
int age;
employees emps;
for (int i=0;i<10;i++) // 10
{
cout<<"( )——0 :"<<endl
<<endl
<<" :";
cin.getline(ch,'
');
if (ch[0]=='0') break;
emps.SetID(ch);
cout<<" :";
cin.getline(ch,'
');
emps.SetName(ch);
cout<<" :";
cin.getline(ch,'
');
emps.SetSex(ch);
cout<<" :";
cin>>age;
emps.SetAge(age);
vemps.push_back(emps);
getchar();
emps.WriteFile();
system("cls"); //
}
}
void employees::WriteFile() //
{
ofstream curFile("Workers.txt");
for(int i=0;i<3;i++)
{
curFile<<id<<'
'<<name<<'
'<<sex<<'
'<<age<<'
';
}
curFile.close();
}
void employees::ReadFile()
{
char line[81];
ifstream inFile("Workers.txt");
inFile.getline (line, 80);
if(inFile.eof())
{
cout<<" , "<<endl;}
while(!inFile.eof())
{
cout<<line<<endl;
inFile.getline (line, 80);
}
inFile.close ();
}
void brevity()
{
class employee;
employees emps;
int top=vemps.size();
int size=vemp.size();
if (top==0) //
{
cout<<" , 。"<<endl;
return;
}
for(int i=size;i<top;i++)
{
vemp.push_back(emps);
strcpy(vemp[i].id,vemps[i].id);
strcpy(vemp[i].name,vemps[i].name);
}
cout<<" , "<<endl;
system("cls"); //
}
void Dispvemps()
{
void Printer(employee&);
if(vemps.size()==0)
{
cout<<" , 。"<<endl;
return;
}
else
{
cout<<" _ _ _ "<<endl;
for(int i=0;i<vemps.size();i++)
Printer(vemps[i]);
}
}
void Dispvemp()
{
if (vemp.size()==0)
{
cout<<" , "<<endl;
return ;
}
else
{
cout<<" "
<<endl
<<" "<<endl;
for(int i=0;i<vemp.size();i++)
Printer(vemp[i]);
}}
void ShowMenu() //
{
char Menu_sel();
void Dispvemps();
void Dispvemp();
void brevity();
while(1)
{
switch( Menu_sel())
{
case 'a':
AddNew();
break;
case 'b':
brevity();
break;
case 'c':
Dispvemps();
break;
case 'd':
Dispvemp();
break;
case 'e':
cout<<endl<<" !";
return;
default :
return;
}
}
}
char Menu_sel()
{
char s[4],ch;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (a) "<<endl
<<" (b) "<<endl
<<" (c) "<<endl
<<" (d) "<<endl
<<" (e) "<<endl
<<endl
<<" :" ;
gets(s);
ch=*s;
if(ch<'a'|| ch>'e')
cout<<" !( a-e:)";
else
return ch;
}
int main() //
{
ShowMenu();
}
実験5:演算子のリロードとストリームプログラム設計
5.1
有理数分式方程式を解く有理数は2つの整数の比率であり,通常a/bと表される.ここでaは分子と呼ばれ、bは分母となる.要求:(1)分母は0にできない;(2)分子と分母には公約数があり、それに対応して簡略化され、すなわち約分である.(3)これを4則演算する.(4)有理数は大きさを比較することができる.(5)付加:重荷重<>で、点数を全体的に入力・出力できる.
#include
using namespace std;
class Rational
{
public:
int x;
int y;
Rational(int x1=0,int y1=1){
x=x1;y=y1;}
Rational operator+(Rational r);//
Rational operator-(Rational r);//
Rational operator*(Rational r);//
Rational operator/(Rational r);//
friend istream & operator>>(istream& in,Rational &r);
friend ostream & operator<<(ostream& out,Rational &r);
};
istream & operator>>(istream& in,Rational &r)
{
in>>r.x>>r.y;
if(r.y==0){
cout<<" 0
";
exit(1);
}
return in;
}
ostream & operator<<(ostream& out,Rational &r)
{
int i;
float m,n;
if(r.x<r.y)
for(i=r.x;i>1;i--)
{
if(r.x%i==0&&r.y%i==0)
{
m=r.x/i;
r.x=m;
r.y=n;
break;
}
return out
}
if(r.x>r.y)
for(i=r.y;i>1;i--)
{
if(r.x%i==0&&r.y%i==0)
{
m=r.x/i;
n=r.y/i;
r.x=m;
r.y=n;
break;
}}
if(r.y==1)
{
out<<" :"<<r.x<<endl;
return out;
}
else
{
out<<" :"<<r.x<<"/"<<r.y<<endl;
return out;
}}
Rational Rational::operator+(Rational r) //
{
return Rational(x*r.y+y*r.x,y*r.y);
}
Rational Rational::operator-(Rational r) //
{
return Rational(x*r.y-y*r.x,y*r.y);
}
Rational Rational::operator*(Rational r) //
{
return Rational(x*r.x,y*r.y);
}
Rational Rational::operator/(Rational r) //
{
return Rational(x*r.y ,y*r.x);
system("pause");
}
int main(){
Rational A,B,C;
cout<<" A "<<endl;
cin>>A;
cout<<" B "<<endl;
cin>>B;
cout<<" :"<<endl;
C=A+B;
cout<<C;
cout<<" :"<<endl;
C=A-B;
cout<<C;
cout<<" :"<<endl;
C=A*B;
cout<<C;
cout<<" :"<<endl;
C=A/B;
cout<<C;
system("pause");
return 0;
}
5.2
文字列クラスを設計し、独自の文字列クラス(String)を設計します.要求:(1)各種のシリアル操作(サブシリアル操作、シリアルコピー、シリアル接続)は、文字列の境界を検査し、処理する.(2)複製方式でシリアル割り当てを実現し、異なるポインタが文字列を共有することを避け、データの独立性を高める.(3)適切な演算子を使用して文字列を定義する操作(たとえば、==、>=、(4)を使用して、サブストリング操作、パターンマッチングなどの高レベルの操作を定義できます.
#include
#include
using namespace std;
class String {
private:
char * Pstr;
public:
String(const char* p = NULL) {
if (p == NULL) {
Pstr = new char[1];
*Pstr = '\0';
} else {
Pstr = new char[strlen(p) + 1];
strcpy(Pstr, p); } }
String(const String& s): Pstr(new char[strlen(s.Pstr) + 1]) {
strcpy(Pstr, s.Pstr);}
~String() {
if (Pstr)
delete[] Pstr;}
String& operator=(const String& s) {
if (Pstr == s.Pstr)
return *this;
else if (NULL == this) {
delete Pstr;
Pstr = new char[strlen(s.Pstr) + 1];
strcpy(Pstr, s.Pstr);
} else {
strcpy(Pstr, s.Pstr);
}
return *this;}
friend ostream & operator<<(ostream &out, const String &s) {
out << s.Pstr;
return out;}
friend istream & operator >>(istream &in, String &s) {
in >> s.Pstr;
if(in)
return in;}
String operator+(const String &s2) {
char *p=new char[strlen(Pstr) + strlen(s2.Pstr)+1];
p=strcat(Pstr,s2.Pstr);
return String(p);}
void operator+=(const String &s2) {
strcat(Pstr, s2.Pstr); }
char& operator[](int n) {
return Pstr[n]; }
int Length() {
int n=strlen(Pstr) ;
return n;
}
bool operator==(const String &s2) {
return strcmp(Pstr, s2.Pstr) == 0;
}
bool operator<(const String &s2) {
return strcmp(Pstr, s2.Pstr) < 0; }
};
int main() {
String s1("Cprogram"), s2("Cprogram"), s3(s2), s4, s5;
cout<<" :( :hello)"<<endl;
s3 = "Hello!";
cout << " :" << s3 << endl;
cout << " :" << s1 << endl;
s3 = s2;
cout << " :" << s3 << endl;
s3 += s2;
cout << " :" << s3 << endl;
cout<<endl
<<endl
<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<endl
<<" :"<<endl;
cin >> s4;
cout << " :" << s4 << endl;
s5 = s3 + s4;
cout << " :" << s5 << endl;
s5[0] = 'g';
cout << " ( g):" << s5 << endl;
cout << " :" << s5.Length() << endl;
cout<<" :"<<endl;//
cout << boolalpha << (s3 == s1) << endl;
cout<<" :" <<endl;
cout << boolalpha << (s3 < s1) << endl;
return 0;
}
5.3
実験2で定義した集合クラスを修正し,(1)Addを<(2)Removeを>>に変更し,機能は要素である,(2)Addを<(2)Removeを>>に変更する.(3)Assignは=に変更され,機能は集合賦値である.(4)EqualToは==に修正され,集合が等しいか否か(すなわち同じ要素があるか否か)を判別する機能である.(5)Intersectは*に変更され、機能は集合の交差を求める.(6)ユニオンは+に変更され,機能は集合を求める並列集合である.また、クラスには、(1)!=:機能は2つの集合が等しくないかどうかを判別することである.(2)(3)<=:機能は,ある集合が別の集合のサブセットであるか否かを判別する.(4)-:機能は、2つのセットの差セットを計算することです.
#include
using namespace std;
const int SetCapacity = 100;
class set
{
int elements[SetCapacity]; //
int _size; //
int k; //
public:
set(int *a,int size)
{
_size=size;
for(k=0;k<_size;k++)
{
elements[k]=a[k];
}
}; //
set(const set& src)
{
this->_size=src._size;
for(k=0;k<_size;k++)
{
this->elements[k]=src.elements[k];
}
}
bool Contains(int el) // el
{
for(k=0;k<_size;k++)
{
if(elements[k]==el)
{
cout<<"Found "<<el<<" at: elements["<<k<<"]."<<endl;
return true;
}
}
cout<<"Not found."<<endl;
return false;
}
set operator <<(int el) // el
{
k=_size;
elements[k]=el;
_size++;
cout<<"Successfully added."<<endl;
}
set operator >>(int el) // el
{
bool search;
for(k=0;k<_size;k++)
{
if(elements[k]==el)
{
search=true;
break;
}
}
if(search==false)
{
cout<<"Not found."<<endl;
}
else
{
cout<<"Found "<<el<<" at: elements["<<k<<"]."<<endl;
int k0;
for(;k<_size-1;k++)
{
elements[k]=elements[k+1];
}
_size--;
}
}
set operator =(set& st) // st
{
_size=st._size;
for(k=0;k<_size;k++)
{
elements[k]=st.elements[k];
}
}
set operator ==(set& st) // st ( )
{
if(st._size!=_size)
{
cout<<"The set doesn't equals to the other set."<<endl;
}
else
{
bool judge=true;
for(k=0;k<_size;k++)
{
if(st.elements[k]!=elements[k])
{
judge=false;
}
}
if(judge==true)
{
cout<<"The set equals to the other set."<<endl;
}
else
{
cout<<"The set doesn't equals to the other set."<<endl;
}
}
}
bool Empty() //
{
if(_size==0)
{
cout<<"The set is empty."<<endl;
return true;
}
return false;
}
set operator *(set& st) // st
{
int temp[SetCapacity];
int tempsize=0;
int l;
int tempk=0;
for(k=0;k<_size;k++)
{
for(l=0;l<st._size;l++)
{
if(elements[k]==st.elements[l])
{
temp[tempk]=elements[k];
tempsize++;
tempk++;
}
}
}
return set(temp,tempsize);
}
set operator +(set& st) // st
{
int temp[SetCapacity];
int tempsize=_size;
int l;
int tempk;
bool judge=true;
k=0;
for(tempk=0;tempk<tempsize;tempk++)
{
temp[tempk]=elements[k];
k++;
}
for(l=0;l<st._size;l++)
{
for(k=0;k<_size;k++)
{
if(elements[k]==st.elements[l])
{
judge=false;
break;
}
}
if(judge==true)
{
temp[tempk]=st.elements[l];
tempk++;
tempsize++;
}
judge=true;
}
return set(temp,tempsize);
}
void print()
{
for(k=0;k<_size;k++)
{
cout<<elements[k]<<" ";
}
cout<<endl;
}
};
int main()
{
int a[5]={
1,2,3,4,5};
int b[6]={
3,4,5,6,7,8};
int size1=5;
int size2=6;
set s0(a,size1);
set s2(a,size1);
set s3(b,size2);
s0.print();
s0.Contains(5);
s0.Contains(12);
s0<<6;
s0.print();
s0>>2;
s0.print();
set s1(s0);
s1==s0;
s2==s0;
set s4=s2*s3;
set s5=s2+s3;
s4.print();
s5.print();
s5=s4;
s5.print();
return 0;
}
5.4
実験2で定義した大きな整数クラスを修正し、大きな整数の付与、加算、減算、負の取り方、大きい、小さい、大きい、等しい、小さい、等しい、等しい、等しくない、等しくない演算のリロードを実現し、そのためにリロード演算子メソッドを入力および出力する.
#include
#include
#include
#include //reverse
using namespace std;
/* */
class BigInt
{
private:
inline int compare(string s1, string s2)
{
if(s1.size() < s2.size())
return -1;
else if(s1.size() > s2.size())
return 1;
else
return s1.compare(s2);
}
public:
bool flag;//true ,false ,0
string values;//
BigInt():values("0"),flag(true){
};//
BigInt(string str)// ( )
{
values = str;
flag = true;
}
public:
friend ostream& operator << (ostream& os, const BigInt& bigInt);//
friend istream& operator>>(istream& is, BigInt& bigInt);//
BigInt operator+(const BigInt& rhs);//
BigInt operator-(const BigInt& rhs);//
BigInt operator*(const BigInt& rhs);//
BigInt operator/(const BigInt& rhs);//
BigInt operator%(const BigInt& rhs);//
};
/* '>>', */
ostream& operator << (ostream& os, const BigInt& bigInt)
{
if (!bigInt.flag)
{
os << '-';
}
os << bigInt.values;
return os;
}
/* '>>', */
istream& operator >> (istream& is, BigInt& bigInt)
{
string str;
is >> str;
bigInt.values = str;
bigInt.flag = true;
return is;
}
/*
*/
BigInt BigInt::operator+(const BigInt& rhs)
{
BigInt ret;
ret.flag = true;//
string lvalues(values), rvalues(rhs.values);
//
if (lvalues == "0")
{
ret.values = rvalues;
return ret;
}
if (rvalues == "0")
{
ret.values = lvalues;
return ret;
}
// s1 s2
unsigned int i, lsize, rsize;
lsize = lvalues.size();
rsize = rvalues.size();
if (lsize < rsize)
{
for (i = 0; i < rsize - lsize; i++)// lvalues
{
lvalues = "0" + lvalues;
}
}
else
{
for (i = 0; i < lsize - rsize; i++)// rvalues
{
rvalues = "0" + rvalues;
}
}
//
int n1, n2;
n2 = 0;
lsize = lvalues.size();
string res = "";
reverse(lvalues.begin(), lvalues.end());// ,
reverse(rvalues.begin(), rvalues.end());
for (i = 0; i < lsize; i++)
{
n1 = (lvalues[i] - '0' + rvalues[i] - '0' + n2) % 10;//n1
n2 = (lvalues[i] - '0' + rvalues[i] - '0' + n2) / 10;//n2
res = res + char(n1 + '0');
}
if (n2 == 1)
{
res = res + "1";
}
reverse(res.begin(), res.end());
ret.values = res;
return ret;
}
/*
*/
BigInt BigInt::operator-(const BigInt& rhs)
{
BigInt ret;
string lvalues(values), rvalues(rhs.values);
//
if(flag==false&&rhs.flag==false)
{
string tmp = lvalues;
lvalues = rvalues;
rvalues = tmp;
}
//
if(flag==false&&rhs.flag==true)
{
BigInt res(lvalues);
ret=res+rhs;
ret.flag = false;
return ret;
}
if(flag==true&&rhs.flag==false)
{
BigInt rel(lvalues),res(rhs.values);
ret=rel+res;
ret.flag = true;
return ret;
}
//
if (rvalues == "0")
{
ret.values = lvalues;
ret.flag = true;
return ret;
}
if (lvalues == "0")
{
ret.values = rvalues;
ret.flag = false;
return ret;
}
// s1 s2
unsigned int i, lsize, rsize;
lsize = lvalues.size();
rsize = rvalues.size();
if (lsize < rsize)
{
for (i = 0; i < rsize - lsize; i++)// lvalues
{
lvalues = "0" + lvalues;
}
}
else
{
for (i = 0; i < lsize - rsize; i++)// rvalues
{
rvalues = "0" + rvalues;
}
}
// ‘-’
int t = lvalues.compare(rvalues);// 0,str1str2
if (t < 0)
{
ret.flag = false;
string tmp = lvalues;
lvalues = rvalues;
rvalues = tmp;
}
else if (t == 0)
{
ret.values = "0";
ret.flag = true;
return ret;
}
else
{
ret.flag = true;
}
//
unsigned int j;
lsize = lvalues.size();
string res = "";
reverse(lvalues.begin(), lvalues.end());// ,
reverse(rvalues.begin(), rvalues.end());
for (i = 0; i < lsize; i++)
{
if (lvalues[i] < rvalues[i])// ,
{
j = 1;
while(lvalues[i+j] == '0')
{
lvalues[i+j] = '9';
j++;
}
lvalues[i+j] -= 1;
res = res + char(lvalues[i] + ':' - rvalues[i]);
}
else
{
res = res + char(lvalues[i] - rvalues[i] + '0');
}
}
reverse(res.begin(), res.end());
res.erase(0, res.find_first_not_of('0'));//
ret.values = res;
return ret;
}
/*
*/
BigInt BigInt::operator*(const BigInt& rhs)
{
BigInt ret;
string lvalues(values), rvalues(rhs.values);
// 0
if (lvalues == "0" || rvalues == "0")
{
ret.values = "0";
ret.flag = true;
return ret;
}
if(flag==false||rhs.flag==false)
{
ret.flag=false;
}
//
unsigned int lsize, rsize;
lsize = lvalues.size();
rsize = rvalues.size();
string temp;
BigInt res, itemp;
// lvalues
if (lvalues < rvalues)
{
temp = lvalues;
lvalues = rvalues;
rvalues = temp;
lsize = lvalues.size();
rsize = rvalues.size();
}
//
int i, j, n1, n2, n3, t;
reverse(lvalues.begin(), lvalues.end());//
reverse(rvalues.begin(), rvalues.end());
for (i = 0; i < rsize; i++)
{
temp = "";
n1 = n2 = n3 = 0;
for (j = 0; j < i; j++)
{
temp = temp + "0";
}
n3 = rvalues[i] - '0';
for (j = 0; j < lsize; j++)
{
t = (n3*(lvalues[j] - '0') + n2);
n1 = t % 10;//n1
n2 = t / 10;//n2
temp = temp + char(n1 + '0');
}
if (n2)
{
temp = temp + char(n2 + '0');
}
reverse(temp.begin(), temp.end());
itemp.values = temp;
res = res + itemp;
}
ret.values = res.values;
return ret;
}
/*
*/
BigInt BigInt::operator/(const BigInt& rhs)
{
BigInt ret;
string lvalues(values), rvalues(rhs.values);
string quotient;
string temp;
//
if(rvalues == "0")
{
ret.values = "error";//
ret.flag = true;
return ret;
}
if(lvalues == "0")
{
ret.values = "0";
ret.flag = true;
return ret;
}
if(compare(lvalues, rvalues) < 0)
{
ret.values = "0";
ret.flag = true;
return ret;
}
else if(compare(lvalues, rvalues) == 0)
{
ret.values = "1";
ret.flag = true;
return ret;
}
else
{
//
unsigned int lsize, rsize;
lsize = lvalues.size();
rsize = rvalues.size();
int i;
if(rsize > 1) temp.append(lvalues, 0, rsize-1);
for(i = rsize - 1; i < lsize; i++)
{
temp = temp + lvalues[i];
//
for(char c = '9'; c >= '0'; c--)
{
BigInt t = (BigInt)rvalues * (BigInt)string(1, c);
BigInt s = (BigInt)temp - t;
if(s.flag == true)
{
temp = s.values;
quotient = quotient + c;
break;
}
}
}
}
//
quotient.erase(0, quotient.find_first_not_of('0'));
ret.values = quotient;
ret.flag = true;
return ret;
}
/*
*/
BigInt BigInt::operator%(const BigInt& rhs)
{
BigInt ret,kj(values),ki(rhs.values);
string lvalues(values), rvalues(rhs.values);
string quotient;
string temp;
//
if(rvalues == "0")
{
ret.values = "error";//
ret.flag = true;
return ret;
}
if(lvalues == "0")
{
ret.values = "0";
ret.flag = true;
return ret;
}
if(compare(lvalues, rvalues) < 0)
{
if(flag==false)
{
ret.values=(ki-kj).values;
ret.flag = true;
return ret;
}else{
ret.values = lvalues;
ret.flag = true;
return ret;
}
}
else if(compare(lvalues, rvalues) == 0)
{
ret.values = "0";
ret.flag = true;
return ret;
}
else
{
//
unsigned int lsize, rsize;
lsize = lvalues.size();
rsize = rvalues.size();
int i;
if(rsize > 1) temp.append(lvalues, 0, rsize-1);
for(i = rsize - 1; i < lsize; i++)
{
if(temp=="0"){
temp=lvalues[i];
}else{
temp = temp + lvalues[i];
}
//
for(char c = '9'; c >= '0'; c--)
{
BigInt t = (BigInt)rvalues * (BigInt)string(1, c);
BigInt s = (BigInt)temp - t;
if(s.flag == true)
{
//cout<
temp = s.values;
quotient = quotient + c;
break;
}
}
}
}
//
quotient.erase(0, quotient.find_first_not_of('0'));
ret.values = temp;
ret.flag = true;
return ret;
}
/*
GCD
*/
BigInt gcd(BigInt a,BigInt b)
{
BigInt stemp;
//cout<//cout<
if((a-b).flag==false)// きさの
{
stemp.values=a.values;
a.values=b.values;
b.values=stemp.values;
}
if(b.values=="0") return a;
else return gcd(b,a%b);
}
/*
べき
*/
BigInt fast(BigInt a,BigInt b)
{
BigInt aa=a,t("1"),k("2");
// int b2=atoi(b1[lsize-1].c_str());
while(b.values!="0")
{
if((b%k).values!="0")
{
t=t*aa;
}
aa=aa*aa;
b=b/k;
}
return t;
}
/*
べき
*/
BigInt mod_fast(BigInt a,BigInt b,BigInt p)
{
BigInt aa=a,t("1"),k("2");
// int b2=atoi(b1[lsize-1].c_str());
while(b.values!="0")
{
if((b%k).values!="0")
{
t=(t%p)*(aa%p)%p;
}
aa=(aa%p)*(aa%p)%p;
b=b/k;
}
return t%p;
}
/*
ユークリッドを して を
*/
BigInt extgcd(BigInt a, BigInt b, BigInt& x, BigInt& y)
{
BigInt d(a.values);
if(b.values != "0"){
d = extgcd(b, a % b, y, x);
y = y-(a / b) * x;
}else {
x.values = "1";
y.values = "0";
}
return d;
}
BigInt mod_inverse(BigInt a, BigInt m)
{
BigInt x, y;
extgcd(a, m, x, y);
if(x.flag==false)
{
x.flag=true;
x=m-x;
}
return (m + x % m) % m;
}
int main()
{
BigInt a,b,n;
char op;
while(1)
{
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<"|I I|"<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<「(1) (a+b)&(a+b)mod n」<<endl
<<「(2) (a-b)&(a-b)mod n」<<endl
<<"(3) (a*b)&(a*b)mod n"<<endl
<<"(4) (a/b)&(a/b)mod n"<<endl
<<"(5) (a%b)&(a%b)mod n"<<endl
<<「(6) (a^b)&(a^b)mod n」<<endl
<<"(7) GCD(a,b)"<<endl
<<「(8)aとnの の 」<<endl
<<endl
<<「 :」;
cin >> op;
switch(op)
{
case '1':
cout<<「a、b、nの を してください:」;
cin>>a>>b>>n;
cout<<endl;
cout<「a+bの :」<<a+b<<endl;
cout<<"(a+b)mod nの :"<(a+b)%n<<endl<<endl<<endl<<endl<<endl<<endl;
break;
case '2':
cout<<「a、b、nの を してください:」;
cin>>a>>b>>n;
cout<<endl;
cout<「a-bの :」<<a-b<<endl;
cout<<"(a-b)mod nの :"<(a-b)%n<<endl<<endl<<endl<<endl<<endl<<endl;
break;
case '3':
cout<<「a、b、nの を してください:」;
cin>>a>>b>>n;
cout<<endl;
cout<「a*bの :」<<a*b<<endl;
cout<<"(a*b)mod nの :"<(a*b)%n<<endl<<endl<<endl<<endl<<endl<<endl;
break;
case '4':
cout<<「a、b、nの を してください:」;
cin>>a>>b>>n;
cout<<endl;
cout<「a/bの :」<<a/b<<endl;
cout<<"(a/b)mod nの :"<(a/b)%n<<endl<<endl<<endl<<endl<<endl<<endl;
break;
case '5':
cout<<「a、b、nの を してください:」;
cin>>a>>b>>n;
cout<<endl;
cout<「a%bの :」<<a%b<<endl;
cout<<"(a%b)mod nの :"<(a%b)%n<<endl<<endl<<endl<<endl<<endl<<endl;
break;
case '6':
cout<<「a、b、nの を してください:」;
cin>>a>>b>>n;
cout<<endl;
cout<「a^bの :」<<fast(a,b)<<endl;
cout<<"(a^b)mod nの :"<<mod_fast(a,b,n)<<endl<<endl<<endl<<endl;
break;
case '7':
cout<<「aとbの を してください:」;
cin>>a>>b;
cout<<endl;
cout<<「GCD(a,b)の :」<<gcd(a,b)<<endl<<endl<<endl<<endl<<endl;
break;
case '8':
cout<<「aとnの を してください:」;
cin>>a>>n;
cout<<endl;
cout<<aとnの :"<<mod_inverse(a,n)<<endl<<endl<<endl<<endl;
break;
default:break;
}
}
return 0;}
5.5
実験2で定義した製品クラスを修正し,クラスがフロー可能にする.次にmain関数で「入荷」と「販売」からなるメインメニューを作成します.入荷部分はキーボードからいくつかの商品を入力し、ファイルストリームで1つの製品ファイルに保存します.販売部分は製品ファイルのデータを読み込み、2級販売メニューを実現し、ユーザーが任意に各種商品を購入する過程を示し、在庫を再更新する.問題6:実験3で定義した電子クロッククラスを修正し,+,+である関数を修正し,時間,日付の自己加算1とnを実現する.
#include
#include
#include
#include
#include //
using namespace std;
class GoodsInfo
{
public:
char * name ; //
int number; //
float cost_p; //
float unit_p; //
int num_all; //
int num_now; //
int num_sell; //
float cost_all; //
float profit; //
GoodsInfo * next; //
GoodsInfo(); //
GoodsInfo(char * a,int b,float c,float u,int all,int sell) ; //
void operator= (const GoodsInfo &right); // =
void setInfo(char * a,float c,float u,int all,int sell) ; //
char* getname(){
return name;} // //
void Amend(GoodsInfo *);
void show(GoodsInfo *);
~GoodsInfo()
{
delete [] name;}
};
class GoodList :public GoodsInfo
{
public:
GoodList(); // ,
~GoodList(); // ,
GoodsInfo * head; //
GoodsInfo * p; // 1
GoodsInfo * p2; // 2( )
void create(); //
void showinfo(GoodsInfo *); //
GoodsInfo * search(); //
void saveInfo(); //
void getInfo(); //
void addInfo(GoodsInfo *); //
void sort(); //
void menu();
void delete_(); //
};
GoodsInfo::GoodsInfo() //
{
name=new char [21];
number=cost_p=unit_p=num_all=num_now=num_sell=cost_all=profit=0;
next=NULL;
}
GoodsInfo::GoodsInfo(char * a,int b,float c,float u,int all,int sell) //
{
strcpy(name,a);
number=b;
cost_p=c;
unit_p=u;
num_all=all;
num_sell=sell;
num_now=all-sell;
cost_all=c*all;
profit=(u-c)*sell;
next=NULL;
}
void GoodsInfo::operator= (const GoodsInfo &right) // =
{
strcpy(name,right.name);
number=right.number ;
cost_p=right.cost_p;
unit_p=right.unit_p ;
num_all=right.num_all ;
num_sell=right.num_sell ;
num_now=right.num_now ;
cost_all=right.cost_all;
profit=right.profit ;
}
void GoodsInfo::setInfo(char * a,float c,float u,int all,int sell) //
{
strcpy(name,a);
cost_p=c;
unit_p=u;
num_all=all;
num_sell=sell;
num_now=all-sell;
cost_all=c*all;
profit=(u-c)*sell;
next=NULL;
}
void GoodsInfo::show(GoodsInfo * h) //
{
if(h==NULL) cout<<"NULL!
";
else
{
cout<<"-----------------------------------
"
<<" : "<<h->name<<endl
<<" : "<<h->number<<endl
<<" : "<<h->cost_p<<endl
<<" : "<<h->unit_p<<endl
<<" :"<<h->num_sell <<endl
<<" : "<<h->num_now<<endl
<<" :"<<h->profit<<endl;
}
}
void GoodsInfo:: Amend(GoodsInfo * h) //
{
int F;
char S;
cout<<"~~~~~~~~~~~~~~~~~~
"
<<"^2. ^"<<endl
<<"^3. ^"<<endl
<<"^4. ^"<<endl
<<"^5. ^"<<endl
<<"~~~~~~~~~~~~~~~~~~
";
do
{
cout<<" :"<<endl;
cin>>F;
switch(F)
{
case 1 :{
cout<<"~~~~~~~~~~~~~~~~~~~~
"
<<"| |
"
<<"~~~~~~~~~~~~~~~~~~~~";
cin.ignore ();
cin.getline(h->name,21);break;}
case 2 :{
cout<<"~~~~~~~~~~~~~~~~~~~~~~~
"
<<"| :
"
<<"~~~~~~~~~~~~~~~~~~~~~~~";
cin>>h->cost_p;break;}
case 3 :{
cout<<" : ";
cin>>h->unit_p;break;}
case 4 :{
cout<<" : ";
cin>>h->num_all;break;}
case 5 :{
cout<<" : ";
cin>>h->num_sell;break;}
}
h->cost_all=h->cost_p*(h->num_all);
h->num_now=h->num_all -(h->num_sell);
h->profit=(h->unit_p -(h->cost_p ))*h->num_sell;
cout<<" ?";
cin>>S;
}
while(S=='Y');
}
GoodList::GoodList() //
{
p=new GoodsInfo [sizeof(GoodsInfo)];
head=NULL;
p2=NULL;
p->next =NULL;
}
GoodList::~GoodList() //
{
while(head!=NULL)
{
p=head;
head=p->next ;
delete [] p;
}
}
void GoodList::create() //
{
int n=0;
char f='Y';
cout<<" :"<<endl;
while(f=='Y')
{
n+=1;
if(n==1) head=p;
else
{
p=new GoodsInfo [sizeof(GoodsInfo)];
p2->next=p;
}
cout<<" : ";
if(n==1) cin.ignore ();
cin.getline (p->name,21);
cout<<" :";
cin>>p->number;
cout<<" : ";
cin>>p->cost_p;
cout<<" : ";
cin>>p->unit_p;
cout<<" : ";
cin>>p->num_all;
cout<<" : ";
cin>>p->num_sell;
p->num_now=p->num_all-(p->num_sell);
p->cost_all=p->cost_p*(p->num_all);
p->profit=((p->unit_p)-(p->cost_p))*(p->num_sell);
p2=p;
cout<<" ?('Y' ,'N' )";
cin>>f;
cin.ignore();
}
if(head!=NULL) p2->next=NULL; //P2
}
GoodsInfo * GoodList::search() //
{
if(head==NULL) {
cout<<" !
";return NULL;}
else
{
GoodsInfo *h;
h=head;
int f;
cout<<"~~~~~~~~~~~~~~~~
"
<<" :
"
<<"1.
" <<"2. "<<endl
<<"~~~~~~~~~~~~~~~~
";
cin>>f;
switch(f)
{
case 1:
{
int x,y;
cout<<" :";
cin>>x;
while(h!=NULL&&(x!=(y=h->number)))
h=h->next;
if(x==y) return h;
else
{
cout<<" !
";
return NULL;}
}
case 2:
{
char x[21];
int c;
cout<<" :";
cin.ignore ();
cin.getline(x,21);
while(h!=NULL&&(c=strcmp(x,h->name))!=0)
h=h->next;
if(c==0) return h;
else {
cout<<" !
";return NULL;}
}
}
}
}
void GoodList::showinfo(GoodsInfo * h) //
{
cout<<setw(50)<<" "<<setw(10)<<" "<<setw(8)<<" "<<setw(8)<<" "
<<setw(8)<<" "<<setw(8)<<" "<<setw(12)<<" "<<endl;
while(h!=NULL)
{
cout<<setw(10)<<h->name<<setw(10)<<h->number<<setw(8)<<h->cost_p<<setw(8)<<h->unit_p
<<setw(8)<<h->num_sell<<setw(8)<<h->num_now<<setw(12)<<h->profit<<endl;
h=h->next;
}
}
void GoodList::saveInfo() //
{
fstream File;
File.open("GoodsInformation.txt",ios::out|ios::binary);
if(!File)
{
cout<<" !
";
exit(0);
}
p=head;
GoodsInfo X;
File<<"
";
while(p!=NULL)
{
X=*p;
File<<X.name<<" "<<X.number<<" "<<X.cost_p<<" "<<X.unit_p<<" "
<<X.num_all <<" "<<X.num_sell<<" "<<X.num_now <<" "<<X.profit<<endl;
p=p->next;
}
cout<<" !
";
File.close();
}
void GoodList::getInfo() //
{
fstream File;
File.open("GoodsInformation.txt",ios::in);
if(!File)
{
cout<<" !
";
exit(0);
}
char s[81];
GoodsInfo h;
File.getline (s,81);
int n=0;
while(!File.eof ())
{
if(File.fail()) break;
n+=1;
if(n==1) head=p;
else
{
p=new GoodsInfo [sizeof(GoodsInfo)];
p2->next=p;
}
File>>h.name>>h.number>>h.cost_p>>h.unit_p
>>h.num_all>>h.num_sell>>h.num_now
>>h.profit;
*p=h;
p2=p;
}
cout<<" !"<<endl;
File.close();
}
void GoodList::addInfo(GoodsInfo * h) //
{
cout<<" :"<<endl;
p=new GoodsInfo [sizeof(GoodsInfo)];
h->next=p;
cin.ignore();
cout<<" : ";
cin.getline (p->name,21);
cout<<" :";
cin>>p->number;
cout<<" : ";
cin>>p->cost_p;
cout<<" : ";
cin>>p->unit_p;
cout<<" : ";
cin>>p->num_all;
cout<<" : ";
cin>>p->num_sell;
p->num_now=p->num_all-(p->num_sell);
p->cost_all=p->cost_p*(p->num_all);
p->profit=((p->unit_p)-(p->cost_p))*(p->num_sell);
p2=p;
p2->next=NULL;
}
void GoodList::delete_() //
{
if(head==NULL)
cout<<"NULL!
";
else
{
cout<<" :";
int n;
GoodsInfo * h;
cin>>n;
h=head;
while(h->next!=NULL&&h->number!=n)
{
p=h;
h=h->next;
}
if(h->number==n)
{
char F;
cout<<" ?";
cin>>F;
if(F=='Y')
{
if(h==head) head=h->next;
else p->next=h->next;
delete [] h;
cout<<" !
";
}
}
else cout<<" !
";
}
}
void GoodList::sort () //
{
GoodsInfo *h1,*h2,t;
int n;
if(head==NULL) cout<<"NULL!
";
else
{
int F;
cout<<"~~~~~~~~~~~~~~~~
"
<<" :
"
<<"1.
"
<<"2.
"
<<"~~~~~~~~~~~~~~~~
";
cin>>F;
switch(F)
{
case 1:
for(h1=head;h1->next!=NULL;h1=h1->next)
{
n=h1->num_sell;
for(h2=h1->next;h2!=NULL;h2=h2->next)
{
if((h2->num_sell)>(h1->num_sell))
n=h2->num_sell;
if(n!=h1->num_sell)
{
t=*h2;*h2=*h1;*h1=t;}
}
}break;
case 2:
for(h1=head;h1->next!=NULL;h1=h1->next)
{
n=h1->profit;
for(h2=h1->next;h2!=NULL;h2=h2->next)
{
if((h2->profit)>(h1->profit))
n=h2->profit;
if(n!=h1->profit)
{
t=*h2;*h2=*h1;*h1=t;}
}
}break;
}
}
showinfo(head);
}
void GoodList::menu() //
{
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (1) "<<endl
<<" (2) "<<endl
<<" (3) "<<endl
<<" (4) "<<endl
<<" (5) "<<endl
<<" (6) "<<endl
<<" (7) "<<endl
<<" (8) "<<endl
<<" (9) "<<endl
<<" (10) "<<endl
<<" (0) "<<endl
<<endl
<<" :" ;
}
int main()
{
cout<<endl<<endl<<endl<<endl<<endl<<endl
<<" ****************************************************
"
<<" *** ***
"
<<" *** ***
"
<<" *** ! ***
"
<<" *** ***
"
<<" *** ***
"
<<" ****************************************************
"
<<" ";
system("pause");
system("cls");
GoodList goods;
GoodsInfo * a;
int N;
goods.menu();
cin>>N;
while(N!=0)
{
switch(N)
{
case 1 : {
goods.create();
break;
}
case 2 : {
goods.addInfo(goods.p2);
break;
}
case 3 : {
cout<<"~~~~~~~~~~~~~~~~~~~~
"
<<"
"
<<"~~~~~~~~~~~~~~~~~~~~
";
a=goods.search();
goods.show(a);
if(a!=NULL)
goods.Amend(a);
else cout<<"*error!*
";
break;
}
case 4 : {
goods.showinfo(goods.head);
break;
}
case 5 : {
goods.saveInfo();
break;
}
case 6 : {
a=goods.search ();
goods.show (a);
break;
}
case 7 : {
goods.getInfo();
break;
}
case 8 : {
goods.sort();
break;
}
case 9 : {
system("cls");
break;
}
case 10 :{
goods.delete_();
break;
}
}
goods.menu();
cin>>N;
}
char F;
cout<<" *** ***('Y' ,'N' ) ";
cin>>F;
if(F=='Y') goods.saveInfo();
system("cls");
cout<<" !"
<<endl;
return 0;
}
実験6:汎用プログラミング
6.1
浮動小数点配列要素の「累加」を実現する関数テンプレートAccumulatorを作成します.要求:累積方式は1つの関数AccumulateMethodによって決定され、2つの要素の和、積、平方和などを表す可能性があり、AccumulateMethodは関数ポインタ方式でAccumulatorに渡され、デフォルト方式は累積和である.
#include
#define N 100
using namespace std;
template <typename T>
T AccumulateMethod(T *a,int temp,int m)
{
int k;
T sum,product,squaresum;
switch(temp)
{
case 1:
sum=0;
for(k=0;k<m;k++)
{
sum+=a[k];}
return sum;
break;
case 2:
product=1;
for(k=0;k<m;k++)
{
product*=a[k];}
return product;
break;
case 3:
squaresum=0;
for(k=0;k<m;k++)
{
squaresum+=(a[k]*a[k]);}
return squaresum;
break;
}
}
int main()
{
L1:float a[N];
float result;
int m,k,x;
int temp=1;
float(*f)(float*,int,int);
f=AccumulateMethod;
cout<<endl;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<endl
<<endl;
cout<<" :"<<endl;
cin>>m;
cout<<" :"<<endl;for(k=0;k<m;k++){
cin>>a[k];}
cout<<" :"<<endl;for(int k=0;k<m;k++){
cout<<a[k]<<" ";}cout<<endl;
while(1)
{
cout<<"(1) "<<endl
<<endl
<<"(2) "<<endl
<<endl
<<" :" ;
cin>>x;
switch(x)
{
case 1:
cout<<" :"<<endl
<<endl
<<"(1) "<<endl
<<endl
<<"(2) "<<endl
<<endl
<<"(3) "<<endl
<<endl
<<" :" ;
cin>>temp;
result=(*f)(a,temp,m);
cout<<" :"<<result<<endl;
break;
case 2:
goto L1;
}}
return 0; }
6.2
配列要素の平均値を計算する関数テンプレートを作成します.
#include
using namespace std; //
template<typename T>
T average(T a[],int m) //
{
int i=0;
double sum=0;
for(i;i<m;i++)
{
sum=sum+a[i];} //
return sum/i;} //
int main()
{
double a[50];
int x=0;
int y;
cout<<endl
<<" :"<<endl;
cin>>y;
cout<<" "<<endl;
for(x=0;x<y;x++){
cin>>a[x];} //
cout<<" :"<<average(a,y)<<endl; //
return 0;}
6.3
実験2の(5)で定義した集合をクラステンプレートとして構築した.
#include
using namespace std;
template <typename T>
class Capacity
{
private:
int size; //
T *el;
public:
Capacity(T *l,int s):el(l),size(s) //
{
el= new T[size];
}
~Capacity(){
} //
int getsize()
{
return size; //
}
void display()
{
for(int i=0;i<size;i++)
cout<<el[i]<<endl; //for el[i] ;
}};
int main()
{
int *a;
int m;
int i;
float *b;
double *c;
cout<<" "<<endl; cin>>m;
a=new int[m]; // for(int i=0;i
cin>>a[i]; //
Capacity<int> ca(a,m); //
cout<<ca.getsize()<<endl;
ca.display();
return 0;
}
実験7:総合プログラミング
7.1
リストを使用して、連絡先の追加、連絡先の削除、電話帳の内容の表示、連絡先の電話、ディスクの照会などの基本的な機能を必要とする小型電話帳管理プログラムを設計します.
#include
#include //
#include //
using namespace std;
class book // book
{
private:
string name; //
string address; //
string number; //
public:
book(); //
char inter_face(); //
void add_person(); //
void del_person(); //
void show_all(); //
void alter(); //
void select(); //
void save_new(); //
};
book::book()
{
name = "\0";
address = "\0";
number = "\0";
}
//
char book::inter_face() //
{
system("cls"); //
cout<<endl;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (1) "<<endl
<<" (2) "<<endl
<<" (3) "<<endl
<<" (4) "<<endl
<<" (5) "<<endl
<<" (6) "<<endl
<<endl
<<" :" ;
char choose;
cin>>choose;
return choose;
}
void book::add_person() //
{
cout<<" " <<endl;
cout<<" :";
cin >>name;
cout <<" : ";
cin >>number;
cout <<" : ";
cin >>address;
save_new();
cout<<" !" <<endl;
system("pause");
}
void book::del_person() //
{
string sign,str1,str; //
bool flag = true; //
cout<<" :"<<endl; //
cin>>sign;
ofstream outData("temp.txt", ios::out); //
ifstream inData("pbook.txt", ios::in); //
if (!outData || !inData) //
{
cout<<" , !" <<endl;
system("pause");
}
while (inData>>name>>number) // indata
{
getline(inData, str); //
if ((sign==name) || (sign==number)) //
{
cout <<" :"<<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl; //
flag = false;
break;
}
outData <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl;
}
if (flag)
{
cout <<endl <<" !" <<endl <<endl;
}
else
{
while (getline(inData, str))
{
outData <<str <<endl;
}
outData.close();
inData.close();
ofstream out("pbook.txt", ios::out);
ifstream in("temp.txt", ios::in);
if (!out || !in)
{
cout <<endl <<" !" <<endl <<endl;
system("pause");
return;
}
while (getline(in, str))
{
out <<str <<endl;
}
out.close(); //
in.close();
cout <<endl <<" !" <<endl <<endl;
}
system("pause");
}
void book::show_all() //
{
//
ifstream inData("pbook.txt",ios::in);
if (!inData)
{
cout <<endl <<" , !" <<endl;
system("pause");
return;
}
bool flag = true;
string record;
while (getline(inData, record))
{
if (flag)
{
cout <<endl <<" : "<<endl;
}
cout <<" \t\t"<<" \t\t"<<" \t\t"<<endl;
cout <<record <<endl;
flag = false;
}
if (flag)
{
cout <<endl <<" !" <<endl <<endl;
}
else
{
cout <<endl <<" !" <<endl <<endl;
}
system("pause");
}
//
void book::alter() //
{
ofstream outData("temp.txt", ios::out);
ifstream inData("pbook.txt", ios::in);
if (!outData || !inData) //
{
cout <<endl <<" !" <<endl;
system("pause");
return;
}
string sign;
cout <<endl <<" :";
cin >>sign;
string str1;
bool flag = true;
string str;
while (inData >>name >>number)
{
getline(inData, str);
if ((sign==name) || (sign==number))
{
cout <<endl <<" :" <<endl <<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl;
cout <<endl <<" : " <<endl;
cout <<" :" ;
cin >>name;
cout <<" :";
cin >>number;
cout <<" :";
cin >>address;
save_new();
flag = false;
break;
}
outData <<setiosflags(ios::left) <<setw(17) <<name<<" " <<number <<str <<endl;
}
if (flag)
{
cout <<endl <<" !"<<endl;
}
else
{
while (getline(inData, str))
{
outData <<str <<endl;
}
outData.close();
inData.close();
ofstream out("pbook.txt", ios::out);
ifstream in("temp.txt", ios::in);
if (!out || !in)
{
cout <<endl <<" !!!"<<endl;
system("pause");
return;
}
while (getline(in, str))
{
out <<str <<endl;
}
out.close();
in.close();
cout<<" !!!"<<endl;
}
system("pause");
}
//
void book::select() //
{
ifstream inData("pbook.txt",ios::in);
if (!inData)
{
cout <<endl <<" !" <<endl;
system("pause");
return;
}
string sign;
cout <<endl <<" : ";
cin >>sign;
string str1;
bool flag = true;
string str;
while (inData >>name >>number)
{
getline(inData, str);
if ((name==sign) || (number==sign))
{
cout <<endl <<" : " <<endl <<endl;
cout <<str1 <<endl;
cout <<setiosflags(ios::left) <<setw(17) <<name
<<number <<str <<endl;
flag = false;
system("pause");
break;
}}}
void book::save_new() //
{
ofstream outData("pbook.txt", ios::app);
if (!outData)
{
cout <<" , !"<<endl;
system("pause");
return;
}
outData << setiosflags(ios::left) << setw(17) << name<<" " << setw(16) << number <<" "<< setw(20) << address <<endl;
outData.close();
}
enum power{
a1 = '1', a2 = '2', a3 = '3', a4 = '4', a5 = '5', a6 = '6'};
int main()
{
char choose;
book song;
while (choose = song.inter_face())
{
switch (choose)
{
case a1:
song.add_person();//
break;
case a2:
song.del_person();//
break;
case a3:
song.show_all();//
break;
case a4:
song.alter();//
break;
case a5:
song.select();//
break;
case a6:
cout <<endl <<" !" <<endl <<endl;
exit(0);
break;
default:
break;}}
return 0;
}
7.2
学生クラスstudentを設計し、学号、氏名、性別、年齢、成績仮定学号が1から連続的に符号化される整数であるという基本情報を含む.このクラスの入出力演算子を再ロードし、(1)キーボードから複数の学生情報を入力してファイルに格納する機能を実現します.(2)学番で生徒の情報を検索し表示する.(3)学生の情報を学号で検索して修正する.(4)すべての成績優秀な学生の情報を表示する.
# include
# include
# include
#include // getch();
using namespace std;
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌Student ﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
class Student
{
public:
char name[20];
char Id[20];
int Age; //
int Mnum; //
int Enum; //
int sum; //
Student * Next;
void Input()
{
cout<<" :"; cin>>name;
cout<<" :"; cin>>Id;
cout<<" :"; cin>>Age;
cout<<" :"; cin>>Mnum;
cout<<" :"; cin>>Enum;
sum=Mnum+Enum;
}
void ReadFile(istream & in)
{
in>>name>>Id>>Age>>Mnum>>Enum>>sum;
}
void Show()
{
cout<<" :"<<name<<endl<<" :"<<Id<<endl<<" :"<<Age<<endl
<<" :"<<Mnum<<endl<<" :"<<Enum<<endl<<" :"<<sum<<endl<<endl<<endl;
}
};
//﹌﹌﹌﹌﹌﹌﹌﹌﹌Studentmassage ﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
class Studentmassage
{
public:
Studentmassage();
~Studentmassage();
void ShowMenu();
void Find();
void Save();
void ModifyItem();
void RemoveItem();
void Swap(Student *,Student *);
void Sort();
int ListCount();
void Display()
{
for(Student * p=Head->Next;p!=End;p=p->Next)
p->Show();
cout<<" ! ……";
getch();
}
void AddItem()
{
End->Input();
End->Next=new Student;
End=End->Next;
cout<<" !"<<endl;
cout<<" ! ……";
getch();
}
private:
Student * Head,* End;
ifstream in;
ofstream out;
Student *FindItem(char * name)
{
for(Student * p=Head;p->Next!=End;p=p->Next)// ,
if(!strcmp(p->Next->name,name))return p;
return NULL;
}
Student *FindID(char * Id)
{
for(Student * p=Head;p->Next!=End;p=p->Next)// ,
if(!strcmp(p->Next->Id,Id))return p;
return NULL;
}
};
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌ ﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
Studentmassage::Studentmassage()
{
Head=new Student;
Head->Next=new Student;
End=Head->Next;
in.open("sort.txt");
if(!in)
cout<<" , 。 。"<<endl;
else
{
while(!in.eof())
{
End->ReadFile(in);
if(End->name[0]=='\0')break;
End->Next=new Student;
End=End->Next;
}
in.close();
cout<<" !"<<endl;
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌ ﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
Studentmassage::~Studentmassage()
{
Save();
for(Student * temp;Head->Next!=End;)
{
temp=Head->Next;
Head->Next=Head->Next->Next;
delete temp;
}
delete Head,End;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌ ﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::ShowMenu()
{
cout<<endl;
cout<<" ._________________. "<<endl
<<" | _______________ | "<<endl
<<" | I I | "<<endl
<<" | I I | "<<endl
<<" | I_____________I | "<<endl
<<" !_________________! "<<endl
<<" (1) "<<endl
<<" (2) "<<endl
<<" (3) "<<endl
<<" (4) "<<endl
<<" (5) "<<endl
<<" (6) "<<endl
<<" (0) "<<endl
<<endl
<<" :" ;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌ ﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Find()
{
char name[20] ,Id[10];
int x;
Student * p=NULL;
cout<<"(1) "<<endl
<<" (2) "<<endl
<<" :";
cin>>x;
switch(x)
{
case 1:{
cout<<"\t\t :";cin>>name;
if(p=FindItem(name))
{
p->Next->Show();
cout<<" ! ……";
getch();
}
else
{
cout<<" !"<<'
'<<endl;
cout<<" ! ……";
getch();
}
}break;
case 2:
{
cout<<" :";cin>>Id;
if(p=FindID(Id))
{
p->Next->Show();
cout<<" ! ……";
getch();
}
else
{
cout<<" !"<<'
'<<endl;
cout<<" ! ……";
getch();
}
}break;
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌ ﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::ModifyItem() //
{
char name[20];
Student * p=NULL;
cout<<" :";cin>>name;
if(p=FindItem(name))
{
cout<<" , !"<<endl;
p->Next->Input();
cout<<" !"<<endl;
cout<<" ! ……";
getch();
}
else
{
cout<<" !"<<endl;
cout<<" ! ……";
getch();
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌ ﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::RemoveItem() //
{
char name[20];
Student * p=NULL,*temp=NULL;
cout<<" :"<<endl;cin>>name;
if(p=FindItem(name))
{
temp=p->Next;
p->Next=p->Next->Next;
delete temp;
cout<<" !"<<endl;
cout<<" ! ……";
getch();
}
else
{
cout<<" !"<<endl;
cout<<" ! ……";
getch();
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Swap(Student *p1, Student *p2)// combox
{
Student *temp=new Student;
strcpy(temp->name,p1->name);
strcpy(temp->Id,p1->Id);
temp->Age=p1->Age;
temp->Mnum=p1->Mnum;
temp->Enum=p1->Enum;
temp->sum=p1->sum;
strcpy(p1->name,p2->name);
strcpy(p1->Id,p2->Id);
p1->Age=p2->Age;
p1->Mnum=p2->Mnum;
p1->Enum=p2->Enum;
p1->sum=p2->sum;
strcpy(p2->name,temp->name);
strcpy(p2->Id,temp->Id);
p2->Age=temp->Age;
p2->Mnum=temp->Mnum;
p2->Enum=temp->Enum;
p2->sum=temp->sum;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
int Studentmassage::ListCount()// ,
{
if(! Head)
return 0;
int n=0;
for(Student * p=Head->Next;p!=End;p=p->Next)
{
n++;
}
return n;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Sort()//
{
cout <<"Sorting..."<<endl;
Student *p=NULL,*p1=NULL,*k=NULL;
int n=Studentmassage::ListCount();
if(n<2)
return;
for(p=Head->Next;p!=End;p=p->Next)
for(k=p->Next;k!=End;k=k->Next)
{
if(p->sum>k->sum)
{
Studentmassage::Swap(p,k);
}
}
cout <<" !"<<endl;
getch();
return;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌ ﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Studentmassage::Save()
{
out.open("sort.txt");
for(Student *p=Head->Next;p!=End;p=p->Next)
out<<p->name<<"\t"<<p->Id<<"\t"<<p->Age<<"\t"
<<p->Mnum<<"\t"<<p->Enum<<"\t"<<p->sum<<'
';
out.close();
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌ ﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
int main()
{
int x,i=0;
bool quit=false;
Studentmassage Grade;
cout<<" ……";
getch();
while(!quit)
{
system("cls");
Grade.ShowMenu();
cin>>x;
switch(x)
{
case 0:quit=true;break;
case 1:Grade.AddItem();break;
case 2:Grade.Display();break;
case 3:Grade.Sort();break;
case 4:Grade.Find();break;
case 5:Grade.RemoveItem();break;
case 6:Grade.ModifyItem();break;}}
return 0;
}