2015年4月9日 星期四

(綠色皮) 最新C++程式語言, 施威銘研究室 著, 旗標 第十章 運算子多載 '參考' 答案

==============================================
/* 程式檔名:Test_C10-40-1.cpp */
/* 程式目的:請撰寫一個日曆類別,內有記錄年、月、日的資料成員,並多載++/--運算子,用以遞增即遞減日期*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150407 */

#include<iostream>
using namespace std;

class RecordDate{
public:
       RecordDate(int,int,int);
       void Dateshow()
       {
        cout << year << "年" << month << "月" << day << "日" << endl;     
       }      
       RecordDate& operator++();
       RecordDate& operator--();
private:
        int year,month,day; 
        static int monthdays[12];      
};

int RecordDate::monthdays[12]={31,28,31,30,31,30,31,31,30,31,30,31};

RecordDate::RecordDate(int y=2014,int m=1,int d=1)
{
         year=y;
         month=(m>=1 && m<=12)?m:1;  
         day=(d>=1 && d<=monthdays[m-1])?d:1;           
}

RecordDate& RecordDate::operator++()
{
     if(++day > monthdays[month-1]){
      day = 1;
      if(++month == 13){
       month=1;
       year++;
      }
     }
     return *this;          
}

RecordDate& RecordDate::operator--()
{
     if(--day ==0)
      if(--month==0){
       month=12;
       year--;
      }
      day=monthdays[month-1];
     return *this;          
}

int main()
{
     RecordDate c1(2014,12,31);
     RecordDate c2(2015);
     (++c1).Dateshow();
     (--c2).Dateshow();
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C10-40-2.cpp */
/* 程式目的:請多載簡法運算子,將兩物件相減,可傳回兩個日期之間相差幾天*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150407 */

#include<iostream>
using namespace std;

class RecordDate{
public:
       RecordDate(int,int,int);
       void Dateshow()
       {
        cout << year << "年" << month << "月" << day << "日" << endl;     
       }      
       RecordDate& operator++();
       RecordDate& operator--();
       int operator-(RecordDate c);
private:
        int year,month,day; 
        static int monthdays[12];      
};

int RecordDate::monthdays[12]={31,28,31,30,31,30,31,31,30,31,30,31};

RecordDate::RecordDate(int y=2014,int m=1,int d=1)
{
         year=y;
         month=(m>=1 && m<=12)?m:1;  
         day=(d>=1 && d<=monthdays[m-1])?d:1;           
}

RecordDate& RecordDate::operator++()
{
     if(++day > monthdays[month-1]){
      day = 1;
      if(++month == 13){
       month=1;
       year++;
      }
     }
     return *this;          
}

RecordDate& RecordDate::operator--()
{
     if(--day ==0)
      if(--month==0){
       month=12;
       year--;
      }
      day=monthdays[month-1];
     return *this;          
}

int RecordDate::operator-(RecordDate c)
{
      int days1 = 365 * year;
      if(month > 1)
        for(int i=1;i<month;i++)
          days1 += monthdays[i-1];
      days1 += day;
      int days2 = 365 * c.year;
      if(c.month > 1)
        for(int i=1;i<c.month;i++)
          days2 += monthdays[i-1];
      days2 += c.day;
    
      return (days1>days2)?(days1-days2):(days2-days1);
}


int main()
{
     RecordDate c1(2014,12,31);
     RecordDate c2(2015,12,31);
     c1.Dateshow();
     c2.Dateshow();
     cout << "之間相差 " << c2 - c1 << " 天";
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C10-40-3.cpp */
/* 程式目的:試去除多載的減法運算子,改成設計可將類別物件轉型為int型別(例如西元1年1月1日代表1)的型別轉換運算子,在做減法運算*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150408 */

#include<iostream>
using namespace std;

class RecordDate{
public:
       RecordDate(int,int,int);
       void Dateshow()
       {
        cout << year << "年" << month << "月" << day << "日" << endl;     
       }      
       RecordDate& operator++();
       RecordDate& operator--();
       operator int();
private:
        int year,month,day; 
        static int monthdays[12];      
};

int RecordDate::monthdays[12]={31,28,31,30,31,30,31,31,30,31,30,31};

RecordDate::RecordDate(int y=2014,int m=1,int d=1)
{
         year=y;
         month=(m>=1 && m<=12)?m:1;  
         day=(d>=1 && d<=monthdays[m-1])?d:1;           
}

RecordDate& RecordDate::operator++()
{
     if(++day > monthdays[month-1]){
      day = 1;
      if(++month == 13){
       month=1;
       year++;
      }
     }
     return *this;          
}

RecordDate::operator int()
{
      int days = 365 * year;
      if(month > 1)
        for(int i=1;i<month;i++)
          days += monthdays[i-1];
      days += day;
    
      return days;

}


int main()
{
     RecordDate c1(2014,12,31);
     RecordDate c2(2015,12,31);
     c1.Dateshow();
     c2.Dateshow();
     cout << "之間相差 " << c2 - c1 << " 天";
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C10-40-4.cpp */
/* 程式目的:請設計類別,其中可存放10個整數。請多載()運算子,可在括號中放入一整數參數,若物件中已有該參數,即傳回true,沒有則傳回false*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150408 */

#include<iostream>
using namespace std;

class Numbers {
public:
       Numbers(int,int,int,int,int,int,int,int,int,int);
       bool operator()(int);
private:
        int ten[10];
};

Numbers::Numbers(int a,int b,int c,int d,int e,int f,int g,int h,int i,int j)
{
      ten[0] = a;   ten[1] = b;
      ten[2] = c;   ten[3] = d;
      ten[4] = e;   ten[5] = f;
      ten[6] = g;   ten[7] = h;
      ten[8] = i;   ten[9] = j;
}

bool Numbers::operator()(int x)
{
      for(int i=0;i<10;i++) 
        if (ten[i]==x)
          return true;
    
      return false;
}


int main()
{
      Numbers n1(9,7,1,5,3,11,13,15,17,19);
      Numbers n2(4,8,2,6,10,18,12,16,20,14);
    
      cout << "n1 中" << (n1(8)? "含有 ":"不含 ") << 8 << endl;
      cout << "n2 中" << (n2(8)? "含有 ":"不含 ") << 8 << endl;
      system("PAUSE");
      return 0;   
}


==============================================
/* 程式檔名:Test_C10-40-5.cpp */
/* 程式目的:請多載>>、<<運算子,代表將物件中的數字做由小到大即大到小的排序*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150408 */

#include<iostream>
using namespace std;

class Numbers {
public:
       Numbers(int,int,int,int,int,int,int,int,int,int);
       bool operator()(int);
       void operator>>(int);
       void operator<<(int);
       void show();
private:
        int ten[10];
};

void swap(int& a, int& b)
{
       int temp = a;
       a = b;
       b = temp;
}


Numbers::Numbers(int a,int b,int c,int d,int e,int f,int g,int h,int i,int j)
{
      ten[0] = a;   ten[1] = b;
      ten[2] = c;   ten[3] = d;
      ten[4] = e;   ten[5] = f;
      ten[6] = g;   ten[7] = h;
      ten[8] = i;   ten[9] = j;
}

bool Numbers::operator()(int x)
{
      for(int i=0;i<10;i++)
        if (ten[i]==x)
          return true;
    
      return false;
}

void Numbers::operator>>(int x) 
{
      if(x <= 1)       
        return;
      else if (x > 10)  
        x = 10;
    
      for(int i=0;i<x-1;i++) 
        for(int j=i+1;j<x;j++)
          if (ten[i]<ten[j])
            swap(ten[i],ten[j]);
}

void Numbers::operator<<(int x)
{
      if(x <= 1)      
        return;
      else if (x > 10)  
        x = 10;
    
      for(int i=0;i<x-1;i++) 
        for(int j=i+1;j<x;j++)
          if (ten[i]>ten[j])
            swap(ten[i],ten[j]);
}

void Numbers::show()
{
      for(int i=0;i<10;i++)
        cout << ten[i] << ' ';
}


int main()
{
      Numbers n1(9,7,1,5,3,11,13,15,17,19);
      Numbers n2(4,8,2,6,10,18,12,16,20,14);
    
      cout << "將 n1 前 7 個數字由大到小排序:" ;
      n1>>7;
      n1.show();
    
      cout << "\n將 n2 全部數字由小到大排序:" ;
      n2<<100;
      n2.show();
      system("PAUSE");
      return 0;
}



==============================================
/* 程式檔名:Test_C10-40-6.cpp */
/* 程式目的:請撰寫一個代表圓形的類別,內有圓心坐標及半徑的資料成員:並多載++/--運算子,用以遞增及遞減半徑*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150408 */

#include<iostream>
using namespace std;

class Circle {
public:
       Circle(double, double, double);
       Circle& operator++();
       Circle& operator--();
       void show();
private:
        double x,y,r;
};

Circle::Circle(double x, double y, double r)
{
      this->x = x;
      this->y = y;
      this->r = r;
}

Circle& Circle::operator++()
{
      r += 1;
      return *this;
}

Circle& Circle::operator--()
{
      if (r>=1)
        r -= 1;
      else
        r = 0;  
    
      return *this;
}

void Circle::show()
{
     cout << "圓座標為 (" << x << ',' << y << "), 半徑為 " << r << endl;
}


int main()
{
      Circle r1(2.5,3,7);
      (++r1).show();
    
      Circle r2(4,1.6,0.5);
      (--r2).show();
      system("PAUSE");
      return 0;
}
==============================================
/* 程式檔名:Test_C10-40-7.cpp */
/* 程式目的:請多載++/--運算子,代表將圓心的x坐標向左及右移動*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150408 */

#include<iostream>
using namespace std;

class Circle {
public:
       Circle(double, double, double);
       Circle& operator++();
       Circle& operator--();
       Circle& operator>>(double);
       Circle& operator<<(double);
       void show();
private:
        double x,y,r;
};

Circle::Circle(double x, double y, double r)
{
      this->x = x;
      this->y = y;
      this->r = r;
}

Circle& Circle::operator++()
{
      r += 1;
      return *this;
}

Circle& Circle::operator--()
{
      if (r>=1)
        r -= 1;
      else
        r = 0;  
    
      return *this;
}

Circle& Circle::operator>>(double shift)
{
      x += shift;
      return *this;
}

Circle& Circle::operator<<(double shift)
{
      x -= shift;
      return *this;
}


void Circle::show()
{
     cout << "圓座標為 (" << x << ',' << y << "), 半徑為 " << r << endl;
}


int main()
{
      Circle r1(2.5,3,7);
      (r1>>0.5).show();
    
      Circle r2(4,1.6,0.5);
      (r2<<4).show();

      system("PAUSE");
      return 0;
}
==============================================
/* 程式檔名:Test_C10-40-8.cpp */
/* 程式目的:請為Ch10-07.cpp的迷你整數類別設定前置與後置遞減運算子*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150409 */

#include <iostream> 
using namespace std;
class ByteInt{
public:
ByteInt(int i) {c = (char) i;}
void show() {cout << (int) c << endl;}
ByteInt &operator++();
ByteInt operator++(int);
ByteInt &operator--();        
        ByteInt operator--(int);      
private:
char c;
};

ByteInt& ByteInt::operator++()
{
c++;
cout << "前置 ++" << endl;
return *this;
}

ByteInt ByteInt::operator++(int)
{
ByteInt tmp = *this;
c++;
cout << "後置 ++" << endl;
return tmp;
}

ByteInt& ByteInt::operator--()  
{
      c--;
      return *this;
}

ByteInt ByteInt::operator--(int) 
{
      ByteInt tmp = *this;         
      c--;
      return tmp;                   
}


int main()
{
     ByteInt b(5);
(--b).show();
(b--).show();
b.show();
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C10-40-9.cpp */
/* 程式目的:請修改第八張的範例程式Stack堆疊類別,將push()、pop()函式的功能改用>>、<<運算子實作。
            (例如若sk為Stack堆疊類別物件,則sk<<5;表示將5放入堆疊中)*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150409 */

#include <iostream>
#define MaxSize 20
using namespace std;

class Stack {
public:
       void init() { sp = 0; }
       Stack& operator<<(int);
       int operator>>(int&);
private:
        int sp;               
        int buffer[MaxSize];
        static void Error() { cout << "\nStack Error\n"; }
};

Stack& Stack::operator<<(int data) 
{
      if(sp == MaxSize)         
        Error();
      else
        buffer[sp++] = data;    
      return *this;
}

int Stack::operator>>(int& data)
{
       if(sp == 0) {            
         Error();
         return 0;
       }
       data = buffer[--sp]; 
       return data;
}

int main()
{
       Stack st1, st2;
       st1.init();
       st2.init();
    
       st1 << 1 << 2 << 3;
       st2 << 7 << 8 << 9;
    
       int i;
       cout << (st1>>i);
       cout << (st2>>i);
       cout << (st1>>i);
       cout << (st2>>i);
       cout << (st1>>i);
       cout << (st2>>i);
       system("PAUSE");
       return 0;

}
==============================================
/* 程式檔名:Test_C10-40-10.cpp */
/* 程式目的:請設計可讓Stack堆疊類別物件直接用串流物件輸出及輸入的方法*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150409 */

#include <iostream>
#define MaxSize 20
using namespace std;

class Stack {
      friend ostream& operator<<(ostream&, Stack&);
      friend istream& operator>>(istream&, Stack&);
public:
      void init() { sp = 0; } 
      Stack& operator<<(int);
      int operator>>(int&);
private:
      int sp;               
      int buffer[MaxSize];  
      static void Error() { cout << "\nStack Error\n"; }
};

Stack& Stack::operator<<(int data)
{
      if(sp == MaxSize)         
        Error();
      else
        buffer[sp++] = data;    
                                
      return *this;
}

int Stack::operator>>(int& data)
{
       if(sp == 0) {             
         Error();
         return 0;
       }
       data = buffer[--sp]; 
       return data;
}

ostream& operator<<(ostream& o, Stack& s)
{
      if(s.sp==0)
        o << "Stack Empty!\n";
      else {
        for(int i=0;i<s.sp;i++)
          o << s.buffer[i] << ' ';
        o << endl;
      }
      return o;
}

istream& operator>>(istream& i, Stack& s)
{
      if(s.sp < MaxSize)
        i >> s.buffer[s.sp++];
      return i;
}


int main()
{
       Stack stk;
       stk.init();
    
       cout << "請輸入五個要存入堆疊的整數\n";
    
       for(int i=0;i<5;i++)
         cin >> stk;
    
       cout << "目前堆疊中所存的數字是:" << stk;
       system("PAUSE");
       return 0;
}
==============================================


2015年3月8日 星期日

(綠色皮) 最新C++程式語言, 施威銘研究室 著, 旗標 第九章 建構函式與解構函式 '參考' 答案

 ==============================================
/* 程式檔名:Test_C9-39-1.cpp */
/* 程式目的:設計一個Car類別,其資料成員包括油箱目前油量及車重,並設計必要的建構函式,建構函式需檢查參數中的目前油量及車重是否超過指定的上限值*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150127 */

#include<iostream>
using namespace std;

class Car{
 public:
        Car(double,double);
        void show()
        {
         cout << "目前油量:" << gas << "目前車重:" << carweight << endl;     
        }
        
 private:
        double gas;
        double carweight;
};
Car::Car(double g,double w)
{
 if(g > 100)
 {
  g=100;
  cout << "油箱超過重量,自動重設100公升" << endl;   
 }   
 gas=g;
 if(w > 3000)
 {
  w=3000;
  cout << "車重超過重量,自動重設3000公斤" << endl;   
 }   
 carweight=w;             
}


int main()
{
     Car car1(80,2500);
     car1.show();
     Car car2(120,2500);
     car2.show();
     Car car3(50,3200);
     car3.show();
     system("PAUSE");
     return 0;    

}

 ==============================================
/* 程式檔名:Test_C9-39-2.cpp */
/* 程式目的:續上題,請在類別加入一靜態資料成員記錄目前物件總數,並在建構函式及解建構函式中維護正確的物件總數值*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150127 */

#include<iostream>
using namespace std;

class Car{
 public:
        Car(double,double);
        ~Car(); 
        void show()
        {
         cout << "目前油量:" << gas << "目前車重:" << carweight << endl;     
        }
        int howmanycar()
        {
         return counter;    
        }
        
 private:
        double gas;
        double carweight;
        static int counter;
};
int Car::counter=0;
Car::Car(double g,double w)
{
 if(g > 100)
 {
  g=100;
  cout << "油箱超過重量,自動重設100公升" << endl;   
 }   
 gas=g;
 if(w > 3000)
 {
  w=3000;
  cout << "車重超過重量,自動重設3000公斤" << endl;   
 }   
 carweight=w; 
 counter++;            
}

Car::~Car()
{
 counter--;
}

int main()
{
  
    Car car(80,2500);
    cout << "現在有 " << car.howmanycar() << " 輛車" << endl;
    Car *car2 = new Car (120,2500);
    cout << "現在有 " << car.howmanycar() << " 輛車" << endl;
    Car *car3 = new Car (50,3200);
    cout << "現在有 " << car.howmanycar() << " 輛車" << endl;
    delete car2;
    delete car3;
    cout << "刪除 car2 car3" << endl;
    cout << "現在有 " << car.howmanycar() << " 輛車" << endl;
    system("PAUSE");
    return 0;    

}

 ==============================================
/* 程式檔名:Test_C9-39-3.cpp */
/* 程式目的:包含一個類別Dates,並在建構函式中初始化一個包含有7個元素的字串陣列,各個元素對應到星期一到星期日的英文縮寫,並提供askDATe(),傳入1~7傳回對應英文縮寫*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150127 */

#include<iostream>
using namespace std;

class Dates{
 public:
         Dates();
         char* askDate(int);                  
 private:
         char* name[7];
         int step;
        
};

Dates::Dates()
{
    name[0] = "Mon";    
    name[1] = "Tue";    
    name[2] = "Wed";    
    name[3] = "Thu";    
    name[4] = "Fri";    
    name[5] = "Sat";    
    name[6] = "Sun";                 
}

char* Dates::askDate(int i)
{
    if (i>7)
    i=7;    
    else if (i<1)    
    i=1;    
    return name[i-1];
}

int main()
{
    Dates date;    
    cout << "星期一到星期天的英文縮寫是:\n";    
    for(int i =1;i<=7;i++)    
    cout << date.askDate(i) << '\t';
    cout << endl;
    system("PAUSE");
    return 0; 
}


 ==============================================
/* 程式檔名:Test_C9-39-4.cpp */
/* 程式目的:承上題,再加入一個方法toChinese(),可用星期的英文縮寫為參數呼叫之,函式會傳回對應的中文星期名稱*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150306 */
#include<cstring>
#include<iostream>
using namespace std;

class Dates{
 public:
         Dates();
         char *askDate(int);
         char *toChinese(char *);                
 private:
         char* name[7];
         int step;
};

Dates::Dates()
{
    name[0] = "Mon";    
    name[1] = "Tue";    
    name[2] = "Wed";    
    name[3] = "Thu";    
    name[4] = "Fri";    
    name[5] = "Sat";    
    name[6] = "Sun";                 
}

char* Dates::askDate(int i)
{
    if (i>7)
    i=7;    
    else if (i<1)    
    i=1;    
    return name[i-1];
}

char* Dates::toChinese(char *chname)
{
    static char cname[][7] = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};

    for (int i=0;i<7;i++)
    {
        if(strcmp(chname,name[i])==0)
        return cname[i];
    }
}

int main()
{
    Dates date;

    for(int i =1;i<=7;i++)
    cout << date.askDate(i) << '\t' << date.toChinese(date.askDate(i)) << '\n';
    system("PAUSE");
    return 0; 
}


 ==============================================
/* 程式檔名:Test_C9-39-5.cpp */
/* 程式目的:修改Clock 類別,增加建構函式可設定目前的時間,另有一setAlarm()成員函式可設定鬧鐘時間 
/* 程式設計:蘇彥儒*/
/* 完成日期:20150306 */
#include<iostream>
using namespace std;

class Time{
 public:
        Time(int h=12, int m=0, int s=0)
        {
                 hour=h; min=m; sec=s;
                 hour = (h>=0 && h<=23) ? h:12;
                 min = (m>=0 && m<=59) ? m:0;
                 sec = (s>=0 && s<=59) ? s:0;
        }
        void set(int h,int m,int s)
        {
         hour=h;min=m;sec=s;       
        }
        void show()
        {
         cout << hour << "點" << min << "分" << sec << "秒" << endl;     
        }
        
 private:
        int hour, min, sec;
};


class Clock{
 public:
        Clock(int h, int m, int s=0): clock_time(h,m,s) {};
        void setAlarm(int h,int m,int s)
        {
         alarm_time.set(h,m,s);     
        }
        void show(); 
 private:
        Time clock_time;
        Time alarm_time;
};

void Clock::show()
{
        cout << "目前的時間:" << endl; 
        clock_time.show();
        cout << "鬧鐘的時間:" << endl; 
        alarm_time.show();
}

int main()
{
     Clock old_time(10,1,12);
     old_time.setAlarm(8,11,10);
     old_time.show();
     system("PAUSE");
     return 0;    

}

 ==============================================
/* 程式檔名:Test_C9-39-6.cpp */
/* 程式目的:請撰寫一個代表矩形類別Rectangle,並提供多種建構方法,例如可以指定左上角頂點座標及長寬值。 
/* 程式設計:蘇彥儒*/
/* 完成日期:20150306 */

#include<iostream>
using namespace std;

class Rectangle{
public:
       Rectangle(int,int,double,double);
       Rectangle(double,double,double,double);
       
       void show()
       {
        cout << "x1=" << x1 << endl;  
        cout << "x2=" << x2 << endl;
        cout << "y1=" << y1 << endl;
        cout << "y2=" << y2 << endl;   
       }
       void showwh()
       {
        cout << "weigh=" << weight << endl;  
        cout << "hight=" << hight << endl;
        cout << "x1=" << x1 << endl;
        cout << "y1=" << y1 << endl;   
       }
private:
        double x1,x2;
        double y1,y2;
        int weight,hight;      
};

Rectangle::Rectangle(double x1,double x2,double y1,double y2)
{
        this->x1=x1;
        this->x2=x2;
        this->y1=y1;
        this->y2=y2;      
}
Rectangle::Rectangle(int wt,int ht,double x1,double y1)
{
        this->weight=wt;
        this->hight=ht;
        this->x1=x1;
        this->y1=y1;      
}

int main()
{
    
     Rectangle c1(5,6,4.3,3.5); 
     c1.showwh();
     cout << endl;
     Rectangle c2(10.1,10.2,30.6,30.7);
     c2.show();
     
     system("PAUSE");
     return 0;    

}

 ==============================================
/* 程式檔名:Test_C9-39-7.cpp */
/* 程式目的:請承上題,新增一個成員函式,傳回bool型別,表示該矩形是否為正方形。 
/* 程式設計:蘇彥儒*/
/* 完成日期:20150306 */

#include<iostream>
using namespace std;

class Rectangle{
public:
       Rectangle(int,int,double,double);
       Rectangle(double,double,double,double);
       bool checkRe(void);
       void show()
       {
        cout << "x1=" << x1 << endl;  
        cout << "x2=" << x2 << endl;
        cout << "y1=" << y1 << endl;
        cout << "y2=" << y2 << endl;   
       }
       void showwh()
       {
        cout << "weigh=" << weight << endl;  
        cout << "hight=" << hight << endl;
        cout << "x1=" << x1 << endl;
        cout << "y1=" << y1 << endl;   
       }
private:
        double x1,x2;
        double y1,y2;
        int weight,hight;      
};

Rectangle::Rectangle(double x1,double x2,double y1,double y2)
{
        this->x1=x1;
        this->x2=x2;
        this->y1=y1;
        this->y2=y2;      
}
Rectangle::Rectangle(int wt,int ht,double x1,double y1)
{
        this->weight=wt;
        this->hight=ht;
        this->x1=x1;
        this->y1=y1;      
}

bool Rectangle::checkRe()
{
        double x = (x1 > x2) ? (x1 - x2) : (x2 - x1);
        double y = (y1 > y2) ? (y1 - y2) : (y2 - y1);     
        if(x == y)
        {
return true;
        }
        else
        {
return false;
}
}

int main()
{
    
     Rectangle c1(5,6,4.3,3.5); 
     c1.showwh();
     cout << endl;
     Rectangle c2(4.0,2.0,4.0,2.0);
     c2.show();
     
     cout << "是否為正方形:";
     if(c2.checkRe())
     {
      cout << "是" << endl;                
     } 
     else
     {
      cout << "否" << endl;    
     }
     
     system("PAUSE");
     return 0;    

}

 ==============================================
/* 程式檔名:Test_C9-39-8.cpp */
/* 程式目的:圓形類別增加一個建構函式,可用Rectangle物件為參數*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150306 */
#include<iostream>
using namespace std;

class Rectangle{
public:
       Rectangle(int,int,double,double);
       Rectangle(double,double,double,double);
       bool checkRe(void);
       void show()
       {
        cout << "x1=" << x1 << endl;  
        cout << "x2=" << x2 << endl;
        cout << "y1=" << y1 << endl;
        cout << "y2=" << y2 << endl;   
       }
       
private:
        double x1,x2;
        double y1,y2;
        int weight,hight;      
};

bool Rectangle::checkRe()
{
        double x = (x1 > x2) ? (x1 - x2) : (x2 - x1);
        double y = (y1 > y2) ? (y1 - y2) : (y2 - y1);     
        if(x == y)
        {
return true;
        }
        else
        {
return false;
}
}

Rectangle::Rectangle(double x1,double x2,double y1,double y2)
{
        this->x1=x1;
        this->x2=x2;
        this->y1=y1;
        this->y2=y2;      
}

class Circle{
public:
       Circle(double,double,double);
       Circle(double,double,double,double);
       Circle(Rectangle);
       double area()
       {
        return 3.1415926 * r * r;       
       }
       double circum()
       {
        return 3.1415926 * 2 * r;       
       }
private:
        double x,y;
        double r;      
};

Circle::Circle(double x0,double y0,double r0 =1)
{
     x=x0;                      
     y=y0;
     r=r0;            
}

Circle::Circle(double x0,double y0,double x1,double y1)
{
     double w = min ((x1>x0)?(x1-x0):(x0-x1) , (y1>y0)?(y1-y0):(y0-y1));
     r = w/2;
     x = x0 + r;
     y = y0 + r;         
}

Circle::Circle(Rectangle rect1)
{
rect1.show();

cout << "是否為正方形?";
if(rect1.checkRe())
cout << "  是!" << endl;
else
cout << "  否!" << endl;
}



int main()
{
     Circle c1(3,5),c2(10,10,30,30);
     cout << "c1 的面積為 " << c1.area() << endl;
     cout << "c2 的圓周長為 " << c2.circum() << endl; 
     
     Rectangle add1(2.0, 2.0, 5.0, 5.0);
Circle c3(add1);
 
     system("PAUSE");
     return 0;    

}

 ==============================================
/* 程式檔名:Test_C9-39-9.cpp */
/* 程式目的:請設計一學生類別,內函學號、學生姓名,數學及英文成績,並設計適當的建構函式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150309 */
#include<iostream>
using namespace std;

class student{
public:
       student(char*,double,double); 
       void show();
private:
        int ID;
        static int counter;
        char *name;
        double math;
        double eng;    
};

int student::counter=0;

student::student(char* name,double math,double eng)
{      
        counter++;
        ID=99900000 + counter;   
        this->name=name;
        this->math=math;
        this->eng=eng;            
}

void student::show()
{      
        cout << "學生ID:" << ID << "\t名子:" << name << "\t數學成績:" <<math << "\t英文成績:" << eng << endl;           
}

int main()
{
     student* stu[2]={new student("陳小新",80.8,85.2),new student("王曉明",50.8,90.2)};

for(int i =0;i<2;i++) 
      stu[i]->show();

     system("PAUSE");
     return 0;    

}

 ==============================================
/* 程式檔名:Test_C9-39-10.cpp */
/* 程式目的:請設計一學生類別,內函學號、學生姓名,數學及英文成績,並設計適當的建構函式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20150309 */
#include<iostream>
using namespace std;

class student{
public:
       student(char*,double,double); 
       void show();
       student(student &);
private:
        int ID;
        static int counter;
        char *name;
        double math;
        double eng;    
};

int student::counter=0;

student::student(student& st)
{
    counter++;
    name=st.name;
        ID=99900000 + counter ;
    math=0;
    eng=0;
}

student::student(char* name,double math,double eng)
{      
        counter++;
        ID=99900000 + counter;   
        this->name=name;
        this->math=math;
        this->eng=eng;            
}

void student::show()
{      
        cout << "學生ID:" << ID << "\t名子:" << name << "\t數學成績:" <<math << "\t英文成績:" << eng << endl;           
}


int main()
{
     student *stu[2]={new student("陳小新",80.8,85.2),new student("王曉明",50.8,90.2)};
     student stua=*stu[1];
for(int i =0;i<2;i++) 
      stu[i]->show();
     stua.show();
      
     system("PAUSE");
     return 0;    
}


 ==============================================