2014年12月22日 星期一

(綠色皮) 最新C++程式語言, 施威銘研究室 著, 旗標 第八章 變數 '參考' 答案

 ==============================================
/* 程式檔名:Test_C8-43-1.cpp */
/* 程式目的:設計三角形類別,存放三邊長的資料成員,初始化物件的資料時,要檢查邊長是否合理*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141219 */

#include<iostream>
using namespace std;

class Triangle {
public :
       double onel;
       double twol;
       double thrl;
};


int main()
{
     Triangle Tri;
     Tri.onel=4;
     Tri.twol=6;
     Tri.thrl=7;
     
     cout << "第一邊:" <<Tri.onel << "第二邊:" << Tri.twol << "第三邊:" << Tri.thrl << endl;
     if(Tri.onel+Tri.twol>Tri.thrl)
     {
         if(Tri.onel+Tri.thrl>Tri.twol)
         {
             if(Tri.twol+Tri.thrl>Tri.twol)
             {
                 cout << "三角形成立" << endl; 
             }
             else
             {
                 cout << "三角形不成立" << endl;     
             }
         }
         else
         {
             cout << "三角形不成立" << endl;     
         }
     }
     else
     {
         cout << "三角形不成立" << endl;     
     }
      
     system("PAUSE");
     return 0;    

}

 ==============================================
/* 程式檔名:Test_C8-43-2.cpp */
/* 程式目的:Car 類別 加入一模擬加油的成員函式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141219 */

#include<iostream>
using namespace std;

class Car {
public :
       void init(double,double);
       double getEff() {return eff;}
       double checkGas() {return gas;}
       double addgas(double addgass){gas+=addgass;}
       double go(double);
private :
       double gas;
       double eff;
};

double Car::go(double kilo)
{
       if(gas >= (kilo/eff))
       {
        gas -= kilo/eff;
        cout << "油箱還有 " << checkGas() << " 公升油" << endl;    
        if(gas == 0)
        {
         cout << "沒油了";       
        }
        else
        {
          cout << "油量不夠,目前的油只夠跑 " << (kilo = gas * eff) << " 公里" << endl;  
          gas=0;   
        }
       }
       return kilo; 
}

void Car::init(double G,double E)
{
     gas = G;
     eff = E;     
}

int main()
{
     Car super;
     super.init(20,30);
     cout << "超級省油1公升可跑 " << super.getEff()
          << " 公里" << endl;
     cout << "現在油箱有 " << super.checkGas() << " 公升油" << endl;
     
     super.addgas(10);
     cout << "現在油箱有 " << super.checkGas() << " 公升油" << endl;
     
     while(super.checkGas() > 0)
     {
      double kilo;
      cout << "現在要開幾公里:";
      super.go(kilo);
     }
     system("PAUSE");
     return 0;    

}

 ==============================================
/* 程式檔名:Test_C8-43-3.cpp */
/* 程式目的:Car 類別 加入一模擬加油函式該成夥伴函式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141219 */

#include<iostream>
using namespace std;

class Car {
       friend void addgas(Car&,double);
public :
       void init(double,double);
       double getEff() {return eff;}
       double checkGas() {return gas;}
       double go(double);
private :
       double gas;
       double eff;
};

double Car::go(double kilo)
{
       if(gas >= (kilo/eff))
       {
        gas -= kilo/eff;
        cout << "油箱還有 " << checkGas() << " 公升油" << endl;    
        if(gas == 0)
        {
         cout << "沒油了";       
        }
        else
        {
          cout << "油量不夠,目前的油只夠跑 " << (kilo = gas * eff) << " 公里" << endl;  
          gas=0;   
        }
       }
       return kilo; 
}

void Car::init(double G,double E)
{
     gas = G;
     eff = E;     
}

void addgas(Car &g,double addgass)
{
     g.gas+=addgass;
}

int main()
{
     Car super;
     super.init(20,30);
     cout << "超級省油1公升可跑 " << super.getEff()
          << " 公里" << endl;
     cout << "現在油箱有 " << super.checkGas() << " 公升油" << endl;
     
     addgas(super,100);
     cout << "現在油箱有 " << super.checkGas() << " 公升油" << endl;
     
     while(super.checkGas() > 0)
     {
      double kilo;
      cout << "現在要開幾公里:";
      super.go(kilo);
     }
     system("PAUSE");
     return 0;    

}

 ==============================================
/* 程式檔名:Test_C8-43-4.cpp */
/* 程式目的:請設計一學生資料類別,需記錄學生姓名及學號*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141219 */

#include<iostream>
using namespace std;

class Student{
public:
       void show()
       {
            cout << stuname << endl;
            cout << stunum << endl;
       } 
       void set(char *ptr,int number)
       {
            stuname=ptr;
            stunum=number;
       }
private:
       char *stuname;
       int stunum;
};

int main()
{
     
     Student stu1;
     stu1.set("王大明",999000);
     stu1.show();
     
     system("PAUSE");
     return 0;    
}


 ==============================================
/* 程式檔名:Test_C8-43-5.cpp */
/* 程式目的:加入學生成績資料,例如可記錄英、數、C++三科成績,並提供計算平均分數的成員函式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141219 */

#include<iostream>
using namespace std;

class Student{
public:
       void show()
       {
            cout << "學生名子:" << stuname << endl;
            cout << "學生學號:" << stunum << endl;
            cout << "英文成績:" << eng << endl;
            cout << "數學成績:" << math << endl;
            cout << " C++成績:" << Cad << endl;
            cout << "成績平均:" << avg << endl;
       } 
       void set(char *ptr,int number)
       {
            stuname=ptr;
            stunum=number;
       }
       void setcl(double a,double b,double c)
       {
            eng=a;
            math=b;
            Cad=c;
            avg=(eng+math+Cad)/3.0;
       }
private:
       char *stuname;
       int stunum;
       double eng,math,Cad,avg;
};

int main()
{
     Student stu1[3];
     stu1[0].set("王大明",999000);
     stu1[0].setcl(80.2,90.2,95.6);
     stu1[1].set("吳小名",999001);
     stu1[1].setcl(50.1,60.2,78.6);
     stu1[2].set("劉阿花",999002);
     stu1[2].setcl(80.1,70.2,75.6);
     for(int i=0;i<3;i++)
     {
      stu1[i].show();
     }
     
     system("PAUSE");
     return 0;    
}


 ==============================================
/* 程式檔名:Test_C8-43-6.cpp */
/* 程式目的:試用學生類別建立學生陣列,並用成員函式對陣列作排序*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141219 */

#include<iostream>
using namespace std;

class Student{
public:
       double studentavg();
       void show()
       {
            cout << "學生名子:" << stuname << endl;
            cout << "學生學號:" << stunum << endl;
            cout << "英文成績:" << eng << endl;
            cout << "數學成績:" << math << endl;
            cout << " C++成績:" << Cad << endl;
       } 
       void set(char *ptr,int number)
       {
            stuname=ptr;
            stunum=number;
       }
       void setcl(double a,double b,double c)
       {
            eng=a;
            math=b;
            Cad=c;
       }
private:
       char *stuname;
       int stunum;
       double eng,math,Cad;
};

double Student::studentavg()
{
       return (eng+math+Cad)/3.0;       
}

int main()
{
     Student stu1[3];
     stu1[0].set("王大明",999000);
     stu1[0].setcl(80.2,90.2,95.6);
     stu1[1].set("吳小名",999001);
     stu1[1].setcl(50.1,60.2,78.6);
     stu1[2].set("劉阿花",999002);
     stu1[2].setcl(80.1,70.2,75.6);
     for(int i=0;i<3;i++)
     {
      stu1[i].show();
      cout << "學生平均:" <<stu1[i].studentavg() << endl;
      cout << endl;
     }
     cout << "排序後......" << endl;
     
     Student temp; 
     
     for(int i=0;i<3;i++)
     {
      for(int j=i+1;j<3;j++)
      {
          if(stu1[i].studentavg() < stu1[j].studentavg())
          {
           temp=stu1[j];
           stu1[j]=stu1[i];
           stu1[i]=temp;                 
          }
      }
     }
     
     for(int i=0;i<3;i++)
     {
      stu1[i].show();
      cout << "學生平均:" <<stu1[i].studentavg() << endl;
      cout << endl;
     }
     
     system("PAUSE");
     return 0;    
}


 ==============================================
/* 程式檔名:Test_C8-43-7.cpp */
/* 程式目的:Str 字串類別,加入紀錄字串長度的資料成員*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141219 */

#include<iostream>
#include<cstring>
using namespace std;

class Str{
friend int strcmp(Str&, Str&);
friend int strlen(Str&);
public:
       void show () { cout << data; }
       void set(char *ptr) { data = ptr;}
private:
       char *data;
};

int strcmp(Str& s1, Str& s2)
{
     return strcmp(s1.data,s2.data);    
}

int strlen(Str& len1)
{
     return strlen(len1.data);    
}

int main()
{
     Str hello,world;
     hello.set("Hello,World!");
     world.set("Hello,World!");
     if(strcmp(hello,world)!=0)
       cout << "兩字串的內容不同" << endl;
     else
       cout << "兩字串的內容相同" << endl;
       
     cout << "hello 字串長度:" << strlen(hello) << endl;
     cout << "world 字串長度:" << strlen(world) << endl;
     system("PAUSE");
     return 0;    

}

 ==============================================
/* 程式檔名:Test_C8-43-8.cpp */
/* 程式目的:Str 字串類別,加入可比對字串的成員函式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141219 */

#include<iostream>
#include<cstring>
using namespace std;

class Str{
friend int strcmp(Str&, Str&);
friend int strlen(Str&);
public:
       void show () { cout << data; }
       void set(char *ptr) { data = ptr;}
       int str(Str&);
private:
       char *data;
};

int Str::str(Str &s1)
{
     return strcmp(this->data,s1.data);    
}

int strcmp(Str& s1, Str& s2)
{
     return strcmp(s1.data,s2.data);    
}

int strlen(Str& len1)
{
     return strlen(len1.data);    
}

int main()
{
     Str hello,world;
     hello.set("Hello,World!");
     world.set("Hello,World!");
     if(strcmp(hello,world)!=0)
       cout << "兩字串的內容不同" << endl;
     else
       cout << "兩字串的內容相同" << endl;
        
     if(hello.str(world)!=0)
       cout << "兩字串的內容不同" << endl;
     else
       cout << "兩字串的內容相同" << endl;

     cout << "hello 字串長度:" << strlen(hello) << endl;
     cout << "world 字串長度:" << strlen(world) << endl;
     system("PAUSE");
     return 0;    

}

 ==============================================
/* 程式檔名:Test_C8-43-9.cpp */
/* 程式目的:加上計算乘法和除法的函式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141219 */

#include<iostream>
using namespace std;

class Complex{
public:
       void set(double r,double i) { real = r; image = i;}
       void show()
       { cout << '(' << real << ',' << image << "i)";}
       Complex& add(Complex a)
       { real += a.real; image += a.image; return *this;}
       Complex& minus(Complex a)
       { real -= a.real; image -= a.image; return *this;}
       Complex& Multiplication(Complex a)
       { real *= a.real; image *= a.image; return *this;}
       Complex& Division(Complex a)
       { real /= a.real; image /= a.image; return *this;}
private:
       double real;
       double image;
};

int main()
{
     Complex c1,c2;
     c1.set(3,9);
     c2.set(-1,2.5);
     
     c1.show();
     cout << " 加 ";
     c2.show();
     cout << " 等於 ";
     c1.add(c2).show();
     cout << endl; 
     
     c2.show();
     cout << " 減 ";
     c1.show();
     cout << " 等於 ";
     c2.minus(c1).show();
     cout << endl; 
     
     c2.show();
     cout << " 乘 ";
     c1.show();
     cout << " 等於 ";
     c2.Multiplication(c1).show();
     cout << endl; 
     
     c2.show();
     cout << " 除 ";
     c1.show();
     cout << " 等於 ";
     c2.Division(c1).show();
     cout << endl; 
     
     system("PAUSE");
     return 0;    
}


 ==============================================
/* 程式檔名:Test_C8-43-.10cpp */
/* 程式目的:用多載的方式,加上可做複數與double型別的計算*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141219 */

#include<iostream>
using namespace std;

class Complex{
public:
       void set(double r,double i) 
       { 
         real = r; image = i;
         cout << "real = " << real << "\t" <<  "image = " << image << endl;
       }
       void show()
       { 
         cout << '(' << real << ',' << image << "i)";     
       }
       
       Complex& add(Complex a)
       { 
         real += a.real; image += a.image; return *this;
       }
       Complex& minus(Complex a)
       { 
         real -= a.real; image -= a.image; return *this;
       }
       Complex& Multiplication(Complex a)
       { 
         real *= a.real; image *= a.image; return *this;
       }
       Complex& Division(Complex a)
       {
        real /= a.real; image /= a.image; return *this;
       }
       
       Complex& dadd(double a)
       { 
         real += a; 
         image += a; 
         cout << "初值 : " << a << endl; 
         cout << "double加法 real: " << real << endl;
         cout << "double加法image: " << image << endl;
       }
       
       Complex& minus(double a)
       { 
         real -= a; 
         image -= a; 
         cout << "初值 : " << a << endl; 
         cout << "double減法 real: " << real << endl;
         cout << "double減法image: " << image << endl;
       }
       
       Complex& Multiplication(double a)
       { 
         real *= a; 
         image *= a; 
         cout << "初值 : " << a << endl; 
         cout << "double乘法 real: " << real << endl;
         cout << "double乘法image: " << image << endl;
       }
       
       Complex& Division(double a)
       { 
         real /= a; 
         image /= a;
         cout << "初值 : " << a << endl; 
         cout << "double除法 real: " << real << endl;
         cout << "double除法image: " << image << endl;
       }
       
private:
       double real;
       double image;
};


int main()
{
     Complex c1,c2;
     c1.set(3,9);
     c2.set(-1,2.5);
     
     c1.dadd(5.5);
     c1.minus(2.5);
     c1.Multiplication(3.5);
     c1.Division(4.5);
     
     cout << endl;
     
     c1.show();
     cout << " 加 ";
     c2.show();
     cout << " 等於 ";
     c1.add(c2).show();
     cout << endl; 
     
     c2.show();
     cout << " 減 ";
     c1.show();
     cout << " 等於 ";
     c2.minus(c1).show();
     cout << endl; 
     
     c2.show();
     cout << " 乘 ";
     c1.show();
     cout << " 等於 ";
     c2.Multiplication(c1).show();
     cout << endl; 
     
     c2.show();
     cout << " 除 ";
     c1.show();
     cout << " 等於 ";
     c2.Division(c1).show();
     cout << endl; 
     
     system("PAUSE");
     return 0;    
}



沒有留言:

張貼留言