2016年10月4日 星期二

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

==============================================
/* 程式檔名:Test_C11-38-1.cpp */
/* 程式目的:請寫一程式可將字串中的'a'都換成'A'*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20151027 */

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

int main()
{
  string s,target="a";
  cout << "請輸入一個字串:";
  cin >> s;
;
  unsigned int i, count = 0, pos = 0;
  cout << endl << "在[" << s << "]中" << endl;
  while ((i = s.find(target,pos))!=string::npos) {
    count++;       // 計數器加 1
    cout << "第 " << count << " 次出現[" << target
         << "]的位置是在第 " << i+1 << " 個字" << endl; 
    pos = i + 1;   // 從前次找到的位置之後繼續搜尋
    s[i] = toupper(s[i]);
  }
  
  if (count == 0)   // 若傳回 npos
    cout << "沒有符合[" << target << "]的字串!"<< endl;
  else
    cout << "總共找到 " << count << " 次" << endl;
  cout << endl << "修改後字串:" << s << endl;
  system("PAUSE");
  return 0; 
}
==============================================
/* 程式檔名:Test_C11-38-2.cpp */
/* 程式目的:將字串中的每個英文單字(word)的字首變成大寫,例如"hello word" 會變成"Hello World"*/
/* 程設計:蘇彥儒*/
/* 完成日期:20151027 */

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

int main()
{
  string s, target=" \n\t";
  cout << "請輸入一個字串:";
  getline(cin, s);
  s[0] = toupper(s[0]);
  unsigned int i, count = 0, pos = 0;
  while ((i = s.find_first_of(target,pos))!=string::npos) {
    count++;       // 計數器加 1
    pos = i + 1;   // 從前次找到的位置之後繼續搜尋
    s[pos] = toupper(s[pos]);
  }
  cout << endl << "修改後字串:" << s << endl;
  system("PAUSE");
  return 0; 
}
==============================================
/* 程式檔名:Test_C11-38-3.cpp */
/* 程式目的:讓使用者輸入10個字串,並將此10個字串排序*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20151027 */

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

int main()
{
  cout << "本程式會將輸入的字串排序。\n";

  string str[10];
  for (int i=0;i<10;i++) {
    cout << "請輸入第 " << i+1 << " 個字串:";
    getline(cin,str[i]);
  }

  for(int i=0;i<9;i++)      // 排序的迴圈
    for(int j =i+1;j<10;j++)
      if(str[i]>str[j]) {
        string tmp = str[i];
        str[i] = str[j];
        str[j] = tmp;
      }

  cout << "排序後:\n";
  for(int i=0;i<10;i++)      // 輸出所有字串
    cout << str[i] << '\n';

}

==============================================
/* 程式檔名:Test_C11-38-4.cpp */
/* 程式目的:可檢查使用者輸入的是否為合法的電話號碼格式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20151027 */

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

int main()
{
  string idStr;      // 記錄使用者輸入資料
  bool isID = false; // 使用者輸入的格式是否正確
  
  do {
    cout << "請輸入電話號碼(xxxx-xxxxxx):";
    getline(cin,idStr);
    
    // 將所有的空白去除
    string::size_type pos=0, i;
    while((i=idStr.find_first_of(" \t",pos)) != string::npos) {
      idStr.erase(i,1);
      pos = i;
    }
    
    if ((idStr.size()==11) && (idStr.find("-")!=string::npos) && (idStr.substr(0,3).find_first_not_of("0123456789")==string::npos) && (idStr.substr(5,10).find_first_not_of("0123456789")==string::npos) )
    {
     cout << "輸入正確" << endl;
     isID = true;
    }
    else
    {
     cout << "請輸入正確的格式" << endl;    
    } 
  } while (!isID);
  
  system("PAUSE");
  return 0; 
}
==============================================
/* 程式檔名:Test_C11-38-5.cpp */
/* 程式目的:要求使用者輸入正確格式的電子郵件信箱*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */

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

string specials = "()<>@,;:\\\"[]";  // 特殊字元


bool checkname(string str)    // 檢查名稱的函式
{
//判斷是否有特殊字元 
  if(str.find_first_of(specials)!=string::npos)
    return false;

//判斷是否在可顯示字元內 
  for(int i=0;i<(int)str.size();i++)
    if(str[i] < 32 || str[i] >=127)
       return false;
    
  return true;
}

bool checkserver(string str)  // 檢查伺服器位址的函式
{
  //判斷是否有特殊字元 
  if(str.find_first_of(specials)!=string::npos)
    return false;

  int dot=0;    // 計算有幾個 '.'
  for(int i=0;i<(int)str.size();i++) {
    if(str[i] == '.')
      if((i==0) || (str[i+i] == '.'))  // 如果第 1 個字是 '.'
        return false;                  // 或連續出現兩個 '.'
      else
        dot++;
    //判斷是否有特殊字元 
    else if(str[i] < 32 || str[i] >=127)
       return false;
  }

  if(dot>0)    // 位址中至少要有一個 '.'
    return true;
  else
    return false;
}

int main()
{
  string email = "test";

  while (email != "0" ) {
    cout << "請輸入電子郵件地址(輸入0結束):";
    getline(cin,email);

    // 將所有的空白去除
    string::size_type pos=0, i;
    while((i=email.find_first_of(" \t",pos)) != string::npos) {
      email.erase(i,1);
      pos = i;
    }

    // 將字串從 @ 分成前後兩部份 name@server
    pos = email.find_first_of("@");

    if(pos == string::npos ||              // 如果沒有 @
       pos == 0 || pos == email.size()) {  // 或 @ 是第 1 個字或最後 1 字
      cout << "格式錯誤\n";
      continue;
    }

//將輸入的Email 分為帳號部分及網域部分做檢查 
    if(checkname(email.substr(0,pos)) &&
       checkserver(email.substr(pos+1)))
      cout << "格式正確\n";
    else
      cout << "格式錯誤\n";
  }
}
==============================================
/* 程式檔名:Test_C11-38-6.cpp */
/* 程式目的:請撰寫一個程式,讓使用者可以YYYY/MM/DD的格式輸入日期,並以"YYYY年MM月DD日"的
格式顯示使用者所輸入的日期*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */

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

bool check(string str)    // 檢查日期格式的函式
{                         // 檢查是否含數字以外的字元
  string legal = "0123456789";
  if(str.find_first_not_of(legal) == string::npos && (str.size() != 0))
    return true;
  else
    return false;
}


int main()
{
  string dates = "test";
  string yy,mm,dd;

  while (dates != "0" ) {
    cout << "請輸入日期字串YYYY/MM/DD(輸入0結束):";
    getline(cin,dates);

if(dates=="0")
break;

    // 分別取得年、月、日三個子字串
    string::size_type pos1, pos2;
    if ((pos1 = dates.find_first_of("/"))== string::npos) {
      break;                            // 如果找不到 /
      cout << "輸入的日期格式錯誤\n";
      }
    else
      yy = dates.substr(0,pos1);
    pos2 = dates.find_first_of("/",pos1+1);
    mm = dates.substr(pos1+1,pos2-pos1-1);
    pos1 = dates.find_first_of("/",pos2+1);
    dd = dates.substr(pos2+1);

    if(check(yy) && check(mm) && check (dd))
      cout << yy << "年"
           << mm << "月"
           << dd << "日" << '\n';
    else
      cout << "輸入的日期格式錯誤\n";
  }
}
==============================================
/* 程式檔名:Test_C11-38-7.cpp */
/* 程式目的:請設計一函式可去掉字串物件中的空白字元*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */

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

string& trim(string& str)    // 去除字串中空白的函式
{
// 將所有的空白去除
    string::size_type pos=0, i;
    while((i=str.find_first_of(" \t",pos)) != string::npos) {
      str.erase(i,1);
      pos = i;
    }
    return str;
}

int main()
{
  string strerase = "test";

  while (strerase != "0" ) {
    cout << "請輸入資料(輸入0結束):";
    getline(cin,strerase);
    cout << "刪除資料中的空格:" << trim(strerase) << endl;
  }

}
==============================================
/* 程式檔名:Test_C11-38-8.cpp */
/* 程式目的:請設計一函式可將String字串物件中的英文字母大小寫互換*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

string& change(string& str)
{
for(string::size_type i=0;i<str.size();i++)
    if(isupper(str[i]))
      str[i] = tolower(str[i]);
    else if(islower(str[i]))
      str[i] = toupper(str[i]);
  return str;
}

int main()
{
  string stra = "test";

  while (stra != "0" ) {
    cout << "請輸入資料(輸入0結束):";
    getline(cin,stra);
    cout << "刪除資料中的空格:" << change(stra) << endl;
  }
  

}

==============================================
/* 程式檔名:Test_C11-38-9.cpp */
/* 程式目的:請撰寫一個程式,可將使用者輸入的檔案名稱分成主檔名及副檔名兩部分輸出*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

int main()
{
  string filename;

  cout << "請輸入一檔案名稱:";
  cin >> filename;

  // 長檔名中可能有多個 .
  // 以最後一個點之後的子字串為副檔名
  string::size_type dot = filename.find_last_of(".");
  string mainfilename = filename.substr(0,dot);
  string subfilename  = filename.substr(dot+1);

  if(mainfilename.size() != 0) {
    cout << "主檔名為:" << mainfilename << '\n';
    if(subfilename.size() != 0)
      cout << "副檔名為:" << subfilename << '\n';
    else
      cout << "沒有副檔名";
  }
  else  // 主檔名為空字串
    cout << "無主檔名,格式錯誤";

}

==============================================
/* 程式檔名:Test_C11-38-10.cpp */
/* 程式目的:請撰寫一string物件比對程式,程式會顯示兩字串從第幾個字開始是不同的*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20161004 */
#include<iostream>
#include<string>
#include<cctype>
using namespace std;

string::size_type diff(string str1, string str2)
{
  string::size_type offset;
  for (offset = 1;offset<str1.size() || offset<str2.size();offset++)
    if(str1[offset] != str2[offset])
      break;
  return offset+1;
}

int main()
{
  string str1,str2;

  cout << "請輸入兩個字串以進行比對。\n";
  cout << "字串1:";
  getline(cin,str1);
  cout << "字串2:";
  getline(cin,str2);

  if(str1 == str2)
    cout << "兩字串內容相同。\n";
  else
    cout << "兩字串從第 " << diff(str1,str2) << " 個字開始不同。\n";

}

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

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