プログラム設計とアルゴリズム(三)第02週試験

18512 ワード

**
001:プログラミング空欄:学生情報ハンドラ
**学生情報処理プログラムを実装し、学生の4年間の平均成績を計算することを説明します.
学生を表すクラスを実現し、クラス内のすべてのメンバー変数が「プライベート」であることが要求される.
以下のプログラムのStudioクラスを追加して、上記の機能を実現します.
#include #include #include #include #include #include using namespace std;
class Student{//ここでコードを補充します};
int main(){Student student;//クラスのオブジェクトstudent.input();//入力データstudent.calculate()//平均成績student.output()//出力データ}
入力
入力データは1行で、氏名、年齢、学号、第1学年平均成績、第2学年平均成績、第3学年平均成績、第4学年平均成績を含む.ここで、名前はアルファベットとスペースからなる文字列(名前が20文字を超えず、スペースが文字列の両端に現れないことを保証する入力)であり、年齢、学番、学年の平均成績はいずれも負の整数ではない.情報の間はカンマで区切られている.
しゅつりょく
名前、年齢、学号、4年間の平均成績を含む1行のデータを出力します.メッセージ間はカンマで区切られています.
サンプル入力
Tom Hanks,18,7817,80,80,90,70
サンプル出力
Tom Hanks,18,7817,80
ヒント
すべてのメンバー変数がプライベートであるクラスで実装する必要があります.出力結果では,4年間の平均成績は必ずしも整数とは限らない.
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

class Student {
//          
private:
    char c, name[21];
    int age, StuNum, FScore, SScore, TScore, ForthScore, AllScore;

public:
    void input() {
        cin.getline(name, 20, ',');
        cin >> age >> c >> StuNum >> c >> FScore >> c >> SScore >> c >> TScore >> c >> ForthScore;
    }

    void calculate() {
        AllScore = (FScore + SScore + TScore + ForthScore)/4;
    }

    void output() {
        cout << name << "," << age << "," << StuNum << "," << AllScore;
    }
};

int main() {
	Student student;        //       
	student.input();        //     
	student.calculate();    //       
	student.output();       //     
}

002:Strange class copy
説明
Please complete the following program to ensure that the output is 9 22 5.
#include using namespace std; class Sample{public:int v;//ここでコードを補充します};void PrintAndDouble(Sample o) { cout << o.v; cout << endl; } int main() { Sample a(5); Sample b = a; PrintAndDouble(b); Sample c = 20; PrintAndDouble©; Sample d; d = a; cout << d.v; return 0; }
入力
None
しゅつりょく
9 22 5
サンプル入力
None
サンプル出力
9 22 5
/*
  :
(1)Sample(const Sample & c) Sample(Sample & c)   ,            
     Sample c = 20         
(2)                 , , ,     Sample a(c);  Sample a=c;              
     ,             A   
     ,        A   
*/
#include 
using namespace std;
class Sample {
public:
	int v;
//          
    Sample(const Sample & c){
        v = c.v + 2;
    }
    Sample(){}
    Sample(int a){
        v = a;
    }
};
void PrintAndDouble(Sample o)
{
	cout << o.v;
	cout << endl;
}
int main()
{
	Sample a(5);
	Sample b = a;
	PrintAndDouble(b);
	Sample c = 20;
	PrintAndDouble(c);
	Sample d;
	d = a;
	cout << d.v;
	return 0;
}


003:超単純な複数クラス
説明
次のプログラムの出力は次のとおりです.
3+4i 5+6i
Complexクラスのメンバー関数を補完してください.メンバー変数を追加できません.
#include #include #include using namespace std; class Complex { private: double r,i; public: void Print() { cout << r << “+” << i << “i” << endl; }//ここであなたのコードを補充します};int main() { Complex a; a = “3+4i”; a.Print(); a = “5+6i”; a.Print(); return 0; }
入力
なし
しゅつりょく
3+4i 5+6i
サンプル入力
なし
サンプル出力
3+4i 5+6i
#include 
#include 
#include 
using namespace std;
class Complex {
private:
    double r,i;
public:
    void Print() {
        cout << r << "+" << i << "i" << endl;
    }
//          
    Complex(){}/*    Complex a*/
    /* const    ,         char str[]    */
    Complex(const char str[5]){
        r = str[0] - '0';
        i = str[2] - '0';
    }
};
int main() {
    Complex a;
    a = "3+4i"; a.Print();
    a = "5+6i"; a.Print();
    return 0;
}

004:どこからの出力
説明
プログラムを空にして、指定した結果を出力します.
#include using namespace std; class A { public: int i; A(int x) { i = x; }//ここであなたのコードを補充します};int main() { A a(1); A * pa = new A(2); delete pa; return 0; }
入力
なし
しゅつりょく
2 1
サンプル入力
なし
サンプル出力
2 1
#include 
using namespace std;
class A {
	public:
		int i;
		A(int x) { i = x; }
//          
        ~A(){
            cout << i << endl;
        }
};
int main()
{
	A a(1);
	A * pa = new A(2);
	delete pa;
	return 0;
}