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



2014年12月7日 星期日

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

 ==============================================
/* 程式檔名:Test_C7-62-1.cpp */
/* 程式目的:將一維陣列中放入十個整數,並計算每個陣列元素的平方和*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141204 */
   
#include<iostream>
using namespace std;   
#define ab(x) x*x
int main()
{
     int a[10]={1,2,3,4,5,6,7,8,9,10};
     
     for(int i=0;i<10;i++)
     {
      cout << ab(a[i]) << endl;        
     }
     system("PAUSE");
     return 0;    

}
 ==============================================
/* 程式檔名:Test_C7-62-2.cpp */
/* 程式目的:在字元陣列中放入未按順序排列的26個英文字母,然後將字母依照a~z順序從螢幕排列*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141204 */
   
#include<iostream>
using namespace std;   
int main()
{
     char a[26]={'q','w','e','r','t','y','u','i',
                 'o','p','a','s','d','f','g','h',
                 'j','k','l','z','x','c','v','b','n','m'};
     char temp;
     for(int i=0;i<26;i++)
     {
      for(int j=i+1;j<26;j++)
      {
        if(a[i]>a[j])
        {
         temp=a[i];
         a[i]=a[j];
         a[j]=temp;                            
        }
      }        
     }
     for(int i=0;i<26;i++)
     {
       cout << a[i];
     }
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-3.cpp */
/* 程式目的:宣告2個整數型別的一維陣列,其中一個設定初值,將此陣列的值,複製到另外一個陣列*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141204 */
   
#include<iostream>
using namespace std;   
int main()
{
     int a[1]={10};
     int b[1];
     b[0]=a[0];
     cout << b[0] << endl;
     
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-4.cpp */
/* 程式目的:在陣列中存入6個字元密碼,使用者從鍵盤輸入密碼,檢查輸入是否正確,只有三次機會*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141204 */
   
#include<iostream>
using namespace std;   
int main()
{
     char a[]="aaa123";
     char b[6];
     
     int flag=0,i=1;
     do
     {
      cout << "請輸入六個字元密碼:";
      cin >> b;
      if(strcmp(a,b)==0)
      {
       cout << "輸入正確" << endl;
       flag=1;
      }
      else
      {
       cout << "密碼錯誤 " << i++ << " 次" << endl;
       cout << "請重新輸入" << endl;
       if(i>3)
       {
        cout << "密碼錯誤三次,程式結束 " << endl ;
        break;
       }
      }
     }while(flag!=1);
     
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-5.cpp */
/* 程式目的:利用指標把變數i=5的值從螢幕輸出*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;   
int main()
{
     int i=5;
     int *p=&i;
     cout << *p << endl;
     
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-6.cpp */
/* 程式目的:從螢幕輸出變數abcd的位址*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;   
int main()
{
     int a,b,c,d;
     cout << "a = " << &a << endl;
     cout << "b = " << &b << endl;
     cout << "c = " << &c << endl;
     cout << "d = " << &d << endl;
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-7.cpp */
/* 程式目的:將abcd的位址指定給指標*e*f*g*h並從螢幕輸出*e*f*g*h的內容以及位址*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;   
int main()
{
     int a,b,c,d;
     int *e = &a,*f = &b,*g = &c,*h = &d;
     //*f,*g,*h;
     cout << "內容 " << *e << "\t位址 " << e << endl;
     cout << "內容 " << *f << "\t位址 " << f << endl;
     cout << "內容 " << *g << "\t位址 " << g << endl;
     cout << "內容 " << *h << "\t位址 " << h << endl;
     
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-8.cpp */
/* 程式目的:利用指標從螢幕輸出陣列array[5]={2,3,4,5,6}的內容*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;   
int main()
{
     int *p;
     int array[5]={2,3,4,5,6};
     p=array;
     
     for(int i=0;i<5;i++)
     {
      cout << *(p+i);            
     }
     cout << endl; 
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-9.cpp */
/* 程式目的:試寫一程式,包含三個函式,排序,總和,最大值三個功能*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;

void Sortingall(int allarray[10]);
int addall(int allarray[10]);
int maxall(int allarray[10]);
 
int main()
{
     int array[10]={45,65,24,49,68,78,45,12,32,40};
     
     int allsum=0,max=0,Sortall[10];
     
     Sortingall(array);
     allsum=addall(array);
     max=maxall(array);
     
     cout << "總和 : " << allsum << endl;
     cout << "最大值 : " << max << endl;
     system("PAUSE");
     return 0;    
}

void Sortingall(int allarray[10])
{
     int temp;
     for(int i=0;i<10;i++)
     {
      for(int j=i+1;j<10;j++)
      {
        if(allarray[i] > allarray[j])
        {
            temp=allarray[i];
            allarray[i]=allarray[j];
            allarray[j]=temp;
        }      
      }     
     }
     cout << "排序 : ";
     for(int i=0;i<10;i++)
     {
      cout << *(allarray+i) << " ";       
     }
     cout << endl;
}

int addall(int allarray[10])
{
     int sum=0;
     for(int i=0;i<10;i++)
     {
      sum=sum+allarray[i];        
     }
     return sum;
}


int maxall(int allarray[10])
{
     int maxall=0;
     for(int i=0;i<10;i++)
     {
      if(allarray[i] > maxall)
      {
       maxall=allarray[i];                      
      }    
     }
     return maxall;
}

 ==============================================
/* 程式檔名:Test_C7-62-10.cpp */
/* 程式目的:用陣列存放十名學生資料國英數三科,並計算出每個學生的總分及平均*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;

void sumstr(int stug[10],int stueng[10],int stumath[10]);
 
int main()
{
     int stug[10],stueng[10],stumath[10];
     
     for(int i=1;i<=10;i++)
     {
      cout << "請輸入第 " << i << "位同學成績" << endl;
      cout << "國文:";
      cin >> stug[i];    
      cout << "英文:";
      cin >> stueng[i]; 
      cout << "數學:";
      cin >> stumath[i];      
     }
     sumstr(stug,stueng,stumath);
     
     system("PAUSE");
     return 0;    
}

void sumstr(int stug[10],int stueng[10],int stumath[10])
{
     int sum=0;
     for(int i=1;i<=10;i++)
     {
      sum=stug[i]+stueng[i]+stumath[i];  
      cout << "第 "<< i << "名總成績:" << sum    
           << "總平均:" << sum/3.0 << endl;   
     }
}



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

2014年11月16日 星期日

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

==============================================
/* 程式檔名:Test_C6-44-1.cpp */
/* 程式目的:可印出指定行數的"HELLO C++" 訊息*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141113 */
   
#include<iostream>
using namespace std;   
void alle(int x)
{
     for(int i=0;i<x;i++)
     {
      cout << "HELLO C++" << endl;    
     }          
     
}

int main()
{
     int usernumber;
     cout << "請輸入行數: " ;
     cin >> usernumber;
     alle(usernumber);
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C6-44-2.cpp */
/* 程式目的:在main()輸入兩個數字從函式比大小 傳回main顯示*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141113 */
   
#include<iostream>
using namespace std;   


float bigger(float x,float y)
{
     if(x>y)
      return x;
     else
      return y;     
}

int main()
{
     float a,b;
     cout << "請輸入第一個數字:";
     cin >> a;
     cout << "請輸入第二個數字:";
     cin >> b;
     cout << "較大的值為:" << bigger(a,b) << endl;
     
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C6-44-3.cpp */
/* 程式目的:遞迴函數計算1+2+3+...+100*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141113 */
   
#include<iostream>
using namespace std;   


long double dqa(int x)
{
    if (x==0)
        return 1;
    else
        return x*dqa(x-1);      
}

int main()
{
     cout << "1+2+3+...+100="<< dqa(100) << endl;
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C6-44-4.cpp */
/* 程式目的:計算1/1+1/2+1/3+...+1/n*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141113 */
   
#include<iostream>
using namespace std;   


long double dqa(int x)
{
    if (x==0)
        return 1;
    else
        return 1.0/x+dqa(x-1);      
}

int main()
{
     int addnum;
     cout << "請輸入一個整數:";
     cin >> addnum;
     cout << "1/1+1/2+1/3+...+1/n=" << dqa(addnum)-1 << endl;
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C6-44-5.cpp */
/* 程式目的:計算1+2+3+...n*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141113 */
   
#include<iostream>
using namespace std;   

long double dqa(int x)
{
    if (x==0)
        return 1;
    else
        return x+dqa(x-1);      
}

int main()
{
     int addnum;
     cout << "請輸入一個整數:";
     cin >> addnum;
     cout << "1+2+3+...n=" << dqa(addnum)-1 << endl;
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C6-44-6.cpp */
/* 程式目的:計算1到指定參數n之間所有可以被13整除的數值*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141114 */
   
#include<iostream>
using namespace std;   


int main()
{
     int addnum;
cout << "計算1到指定參數n之間所有可以被13整除的數值\n";
     cout << "請輸入一個整數:";
     cin >> addnum;
     for(int i=1;i<addnum;i++)
{
if(i%13==0)
{
cout << i << " ";
}
 
}
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C6-44-7.cpp */
/* 程式目的:模擬 pow 行為的函式已遞迴方式計算任意數的任意數次方*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141114 */
   
#include<iostream>
#include<ctime> 
using namespace std;   

int poww(int x,int y)
{
     static int sum=0;
     if(y==0)
      return 1;
     else
      return (x*poww(x,y-1));
}

int main()
{
     int number,powe;
     cout << "請輸入一個數字:";
     cin >> number;
     cout << "請輸入一個次方:";
     cin >> powe;
     if(number==0 && powe==0)
     {
         cout << "無解\n";     
     }
     else
     {
         clock_t starttime=clock();
         cout << number << " 的 " << powe << " 次方等於:" << poww(number,powe) << endl; 
         clock_t endtime=clock(); 
         cout << (double)(endtime-starttime)/CLK_TCK << " 秒\n";   
     }
     
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C6-44-8.cpp */
/* 程式目的:請設計一多載函式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141114 */
   
#include<iostream> 
using namespace std; 

void volume (char x,char y)
{
     cout << "兩個參數都是字元\n"
          << "第一個字元:" << x
          << "第二個字元:" << y << endl;     
}

void volume (int x,char y)
{
     cout << "兩個參數一個字元一個數字\n";
     for(int i=0;i<x;i++)
     {
      cout << y;        
     }  
     cout << endl;   
}

void volume (int x,int y)
{
     cout << "兩個參數都是數字\n"
          << "兩數相乘:" << x*y << endl;
}

int main()
{
     volume('a','b');
     volume(5,'b');
     volume(5,6);
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C6-44-9.cpp */
/* 程式目的:求最大公因數的函式*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141114 */
   
#include<iostream> 
using namespace std; 
void eco(int x,int y)
{
    int a,b,c;
    c=x%y;
    b=y;
    while(c!=0)
    { 
     a=b;
     b=c;
     c=a%b;      
    }
    cout << x << "與" << y <<"的最大公因數:" << b << endl;     
}

int main()
{
     int a,b;
     cout << "求兩數的最大公因數\n";
     cout << "請輸入第一個數字:";
     cin >> a;
     cout << "請輸入第二個數字:";
     cin >> b;
     eco(a,b); 
     system("PAUSE");
     return 0;    

}

==============================================
/* 程式檔名:Test_C6-44-10.cpp */
/* 程式目的:換零錢機器*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141114 */

#include<iostream> 
using namespace std; 

int allmoney(int,int);
void chanmoney(int);

int main()
{
     int money,num,chmoney,chnum;
     int sum,chsum;
     cout << "換零錢機器\n"
          << "只接受100,200,500,1000元\n"
          << "輸入要換的紙鈔 1.100, 2.200, 3.500, 4.1000元\n";
     cin >> money;    
    
     if (money>4 || money<1)
        cout << "輸入紙鈔錯誤\n";       
     else 
     { 
        cout << "輸入張數:\n";
        cin >> num;   
        sum=allmoney(money,num);
        cout << "總金額:" << sum;
        chanmoney(sum);
     } 
     system("PAUSE");
     return 0;    
}

int allmoney(int money,int num)
{
        switch(money)
        {
            case 1:
                   {
                   cout << num << " 張100元";
                   return(num*100);
                   break;
                   }                
            case 2:
                   {
                   cout << num << " 張200元";
                   return(num*200);
                   break;  
                   }
            case 3:
                   {
                   cout << num << " 張500元";
                   return(num*500); 
                   break;
                   }  
            case 4:
                   {
                   cout << num << " 張1000元";
                   return(num*1000); 
                   break;
                   }                    
        }
     
}

void chanmoney(int sum)
{     
    int allchsum,chmoney,chnum;   
    int fiveten=0,ten=0,five=0,one=0;
    cout << "選擇零錢面額(1.一元、2.五元、3.十元、4.五十元):\n";
    cin >> chmoney;
    if (chmoney>4 || chmoney<1)
        cout << "輸入零錢面額錯誤\n";       
    else 
    {                
        cout << "輸入個數:\n";
        cin >> chnum; 
        switch(chmoney)
        {
                case 1:
                       {
                       one=chnum;             
                       break;
                       }
                case 2:
                       {
                       five=chnum*5;             
                       break;
                       }    
                case 3:
                       {
                       ten=chnum*10;               
                       break;
                       }        
                case 4:
                       {
                       fiveten=chnum*50;              
                       break;                          
                       } 
        }
        if(one>sum || five>sum || ten>sum || fiveten>sum)
            cout << "超過投入金額\n";
        else 
        {
            switch(chmoney)
            {
                case 1:
                       {
                       cout << "兌換 " << chnum << "個1元\n";
                       allchsum=chnum;             
                       break;
                       }
                case 2:
                       {
                       cout << "兌換 " << chnum << "個5元\n";
                       allchsum=chnum*5;               
                       break;
                       }
                case 3:
                       {
                       cout << "兌換 " << chnum << "個10元\n";
                       allchsum=chnum*10;               
                       break;
                       }
                case 4:
                       {
                       cout << "兌換 " << chnum << "個50元\n";
                       allchsum=chnum*50;               
                       break; 
                       }                          
            }
            sum=sum-allchsum;
            cout << "剩下金額 "<< sum << "找出零錢\n";
            fiveten=(sum-sum%50);
            ten=((sum-fiveten)-(sum-fiveten)%10);
            five=((sum-fiveten-ten)-(sum-ten)%5);
            one=((sum-ten)%5);
            cout << "五十元:" << fiveten/50;
            cout << "十元:" << ten/10;
            cout << "五元:" << five/5;
            cout << "一元:" << one; 
            cout << endl;
        }
    }                

}

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