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;
}
==============================================