1. 程式人生 > >C++經典程式程式碼大全

C++經典程式程式碼大全

//根據半徑計算圓的周長和麵積
#include <iostream.h>
const float PI=3.1416;        //宣告常量(只讀變數)PI為3.1416
float fCir_L(float);           //宣告自定義函式fCir_L()的原型 
float fCir_S(float);           //宣告自定義函式fCir_S()的原型  

//以下是main()函式 
main() 
{ 
    float r,l,s;             //宣告3個變數
    
    cout<<"r=";          //顯示字串
    cin>>r;              	//鍵盤輸入
    l=fCir_L(r);          //計算圓的周長,賦值給變數l 
    s=fCir_S(r);          //計算圓的面積,賦值給變數s 
    cout<<"l="<<l;       //顯示計算結果
    cout<<"\ns="<<s;                 
}   

//定義計算圓的周長的函式fCir_L()
float fCir_L(float x)
{ 
    float z=-1.0;         //宣告區域性變數
    if (x>=0.0)          //如果引數大於0,則計算圓的周長
        z=2*PI*x;
    return(z);          //返回函式值 
} 

//定義計算圓的面積的函式fCir_S()
float fCir_S(float x)
{ 
    float z=-1.0;         //宣告區域性變數
    if (x>=0.0)          //如果引數大於0,則計算圓的面積
        z=PI*x*x;
    return(z);           //返回函式值 
}
/* Program: P1-2.CPP
 Written by: Hap
 Date written: 02:11:10
*/


#include <iostream.h>
void main(void)
{
    double s1,s2,s3;
    s1=1.5;    /* 對變數s1賦值*/
    cout<<"s1="<<s1<<endl;
    /* 對變數s2賦值*/    s2=2.5;
    cout<<"s2="<<s2<<endl;
    s3=  /* 對變數s3賦值*/  3.5;
    cout<<"s3="<<s3<<endl;

    cout<<"s1+s2+s3="<<s1+s2+s3<<endl;        //計算並顯示
    //計算並顯示 cout<<"s1+s2+s3="<<s1+s2+s3<<endl; 
}
#include <iostream.h>
main()
{
    double  r=1.0;
    cout<<"r="<<r<<endl;
    double l;
    l=2*3.1416*r;           //計算圓的周長,賦值給變數l
    cout<<"l="<<l<<endl;    //顯示圓的周長	
    double s=3.1416*r*r;     //計算圓的面積,賦值給變數s 
    cout<<"s="<<s<<endl;    //顯示圓的面積

    cout<<"r=";            //顯示提示輸入的資訊
    cin>>r;                 //鍵盤輸入
    l=2*3.1416*r;           //計算圓的周長,賦值給變數l
    cout<<"l="<<l<<endl;    //顯示圓的周長
    s=3.1416*r*r; 
    cout<<"s="<<s<<endl;    //顯示圓的面積
}
#include <iostream.h>    //包含iostream.h標頭檔案
void main()
{
    //輸出字元常量、變數和字串
    char c1='A';
    cout<<'W';
    cout<<c1<<endl;
    cout<<"This is a test."<<endl;
    cout<<"------------------"<<endl;

    //輸出整型常量、變數和表示式
    int n=100;
    cout<<10;
    cout<<n;
    cout<<2*n<<endl;   //輸出整型表示式
    cout<<"------------------"<<endl;

    //輸出浮點型常量、變數和表示式
    double pi=3.1415926,r=10.0,s=pi*r*r;
    cout<<pi<<endl;
    cout<<r;
    cout<<s;
    cout<<2*r*pi<<endl;      //輸出浮點型表示式
    cout<<"------------------"<<endl;
    
    //一個cout可以輸出多項資料
    cout<<'W'<<" "<<c1<<endl;
    cout<<"This is a test."<<endl;
    cout<<"pi="<<pi<<" r="<<r<<" s="<<s<<endl;
}
#include <iostream.h>  //包含iostream.h標頭檔案
main()
{
    //輸入輸出字元
    char c;
    cin>>c;
    cout<<"c="<<c<<endl;

    //輸入輸出整型資料
    int n;
    cin>>n;
    cout<<"n="<<n<<endl;

    //輸入輸出浮點型資料
    double x;
    cin>>x;
    cout<<"x="<<x<<endl; 

    //輸入提示
    cout<<"n=";
    cin>>n;
    cout<<"n="<<n<<endl;

    //多項輸入
    cout<<"c n x"<<endl;
    cin>>c>>n>>x;
    cout<<"c="<<c<<" n="<<n<<" x="<<x<<endl;
}
#include <iostream.h>  //包含iostream.h標頭檔案
main()
{
    //宣告整型變數
    int a,b; 

    //從鍵盤上為整型變數賦值
    cout<<"a=";
    cin>>a;
    cout<<"b=";
    cin>>b;

    //整型數的算術運算
    cout<<a<<"+"<<b<<"="<<a+b<<endl;
    cout<<a<<"-"<<b<<"="<<a-b<<endl;
    cout<<a<<"*"<<b<<"="<<a*b<<endl;
    cout<<a<<"/"<<b<<"="<<a/b<<endl;
    cout<<a<<"%"<<b<<"="<<a%b<<endl;

    //測試溢位
    short n=32767,m;    //n取short型別的最大值
    cout<<"n="<<n<<endl;
    m=n+1;      //引起溢位
    cout<<"n+1="<<m<<endl;
}
#include <iostream.h>  //包含iostream.h標頭檔案
main()
{
    //宣告變數,並初始化
    int a=010,b=10,c=0X10; 

    //以十進位制形式顯示資料
    cout<<"DEC:";
    cout<<" a="<<a;
    cout<<" b="<<b;
    cout<<" c="<<c<<endl; 
	
    //以八進位制形式顯示資料
    cout<<"OCT:";
    cout<<oct;             //指定八進位制輸出
    cout<<" a="<<a;
    cout<<" b="<<b;
    cout<<" c="<<c<<endl;  
	
    //以十六進位制形式顯示資料
    cout<<"HEX:";
    cout<<hex;            //指定十六進位制輸出
    cout<<" a="<<a;
    cout<<" b="<<b;
    cout<<" c="<<c<<endl;  

    //八、十和十六進位制數混合運算並輸出
    cout<<"a+b+c=";
    cout<<dec;            //恢復十進位制輸出
    cout<<a+b+c<<endl;

    //測試八、十和十六進位制輸入
    cout<<"DEC:a="; cin>>a;
    cout<<"OCT:b="; cin>>b;
    cout<<"HEX:a="; cin>>c;
    cout<<"DEC:"<<dec<<endl;            //指定十進位制輸出
    cout<<"a="<<a<<endl;
    cout<<"b="<<b<<endl;
    cout<<"c="<<c<<endl;
}
#include <iostream.h>  //包含iostream.h標頭檔案
#include<iomanip.h>   // iomanip.h標頭檔案包含setprecision()的定義
main()
{
    //float型變數的宣告、輸入、計算和輸出
    float fx,fy;   
    cout<<"fx=";
    cin>>fx;
    cout<<"fy=";
    cin>>fy;
    cout<<fx<<"+"<<fy<<"="<<fx+fy<<endl;
    cout<<fx<<"-"<<fy<<"="<<fx-fy<<endl;
    cout<<fx<<"*"<<fy<<"="<<fx*fy<<endl;
    cout<<fx<<"/"<<fy<<"="<<fx/fy<<endl<<endl;
    //cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl;  Error!

    //double型變數的宣告、輸入、計算和輸出
    float dx,dy;  
    cout<<"dx=";
    cin>>dx;
    cout<<"dy=";
    cin>>dy;
    cout<<dx<<"+"<<dy<<"="<<dx+dy<<endl;
    cout<<dx<<"-"<<dy<<"="<<dx-dy<<endl;
    cout<<dx<<"*"<<dy<<"="<<dx*dy<<endl;
    cout<<dx<<"/"<<dy<<"="<<dx/dy<<endl<<endl;
    //cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl;  Error!

    //測試float和double型別資料的有效位
    fx=10.0;fy=6.0;
    float fz=fx/fy;
    dx=10.0;dy=6.0;
    double dz=dx/dy;
    cout<<"fz=";
    cout<<setprecision(20)<<fx<<"/"<<fy<<"="<<fz<<endl;
    cout<<"dz=";
    cout<<setprecision(20)<<dx<<"/"<<dy<<"="<<dz<<endl<<endl;;

    //float型溢位
    float x=3.5e14;
    cout<<"x="<<x<<endl;
    cout<<"x*x="<<x*x<<endl;
    cout<<"x*x*x="<<x*x*x<<endl;
}
#include <iostream.h>  //包含iostream.h標頭檔案
main()
{
    //字元型別變數的宣告
    char c1='A';
    char c2;

    //字元資料的運算及輸出
    c2=c1+32;
    cout<<"c1="<<c1<<endl;
    cout<<"c2="<<c2<<endl;

    //輸出字元及ASCII碼	
    cout<<c1<<" : "<<int(c1)<<endl;
    cout<<c2<<" : "<<int(c2)<<endl;
    cout<<'$'<<" : "<<int('$')<<endl;
	
    //輸入字元
    cout<<"c1 c2"<<endl;
    cin>>c1>>c2;
    cout<<"c1="<<c1<<"  c2="<<c2<<endl;
}
#include <iostream.h>  //包含iostream.h標頭檔案
main()
{
    char c1='\a',TAB='\t';  
	
    //陣鈴一聲
    cout<<c1<<endl;

    //使用水平製表符
    cout<<1<<TAB<<2<<TAB<<3<<TAB<<4<<endl;

    //使用雙引號
    cout<<"He said \"Thank you\"."<<endl;  

    //使用回車換行
    cout<<"abc\n"<<"def"<<'\n';
}

#include <iostream.h>  //包含iostream.h標頭檔案
main()
{
    //宣告bool變數,並初始化
    bool flag1=false,flag2=true; 
	
    //輸出布林常量和變數
    cout<<"false:"<<false<<endl;
    cout<<"true: "<<true<<endl;
    cout<<"flag1="<<flag1<<endl;
    cout<<"flag2="<<flag2<<endl;

    //布林變數的賦值和輸出
    int x=1;
    flag1=x>0;      //存放關係運算結果
    cout<<"flag1="<<flag1<<endl;
    flag2=flag1;    //bool型別變數相互賦值
    cout<<"flag2="<<flag2<<endl;

    //布林變數超界處理
    flag1=100; 
    cout<<"flag1="<<flag1<<endl;
    flag2=-100; 
    cout<<"flag2="<<flag2<<endl;
}
#include <iostream.h>
const double PI=3.1416;     //宣告常量(const變數)PI為3.1416
main() 
{
    //宣告3個變數
    double r,l,s; 
   
    //輸入圓的半徑
    cout<<"r=";          		
    cin>>r; 

    //計算圓的周長
    l=2*PI*r; 
    cout<<"l="<<l<<endl; 
	 
    //計算圓的面積
    s=PI*r*r; 
    cout<<"s="<<s<<endl;                 
}

#include<iostream.h>
main()
{
    //定義列舉型別,並指定其列舉元素的值
    enum color {  
         RED=3,
         YELLOW=6,
         BLUE=9
    };

    //宣告列舉變數a和b,併為列舉變數a賦初值 
    enum color a=RED;
    color b;        //合法,與C語言不同

    // 輸出列舉常量 
    cout<<"RED="<<RED<<endl;
    cout<<"YELLOW="<<YELLOW<<endl;
    cout<<"BLUE="<<BLUE<<endl;
    
    //列舉變數的賦值和輸出
    b=a;
    a=BLUE;
    cout<<"a="<<a<<endl;
    cout<<"b="<<b<<endl;
    //a=100;   錯誤!
    //a=6      也錯誤!

    //列舉變數的關係運算
    b=BLUE;                    		// 列舉變數的賦值運算
    cout<<"a<b="<<(a<b)<<endl;
}
#include <iostream.h>
const double PI=3.1416;     //宣告常量(const變數)PI為3.1416
main() 
{
    //宣告3個變數
    double r=3,l,s;   

    //計算圓的周長
    l=2*PI*r; 
    cout<<"l="<<l<<endl; 
	 
    //計算圓的面積
    s=PI*r*r; 
    cout<<"s="<<s<<endl;    

    //驗證賦值誤差
    int il,is;
    il=l;
    is=s;
    cout<<"il="<<il<<endl; 
    cout<<"is="<<is<<endl;    
}
#include <iostream.h>
main() 
{ 
    //變數宣告
    char c;
    double x,y;

    //測試自增
	cout<<"++E and E++ :"<<endl;
    c='B';
    cout<<"c="<<++c<<endl;   //輸出c=C
    c='B';
    cout<<"c="<<c++<<endl;   //輸出c=B
    x=1.5;
    y=5+ ++x;               //加號後的空格不能少
    cout<<"y="<<y<<endl;    //輸出y=7.5
    x=1.5;
    y=5+x++;
    cout<<"y="<<y<<endl;    //輸出y=6.5
    cout<<"--------------------"<<endl;

	//測試自減
	cout<<"--E and E-- :"<<endl;
    c='B';
    cout<<"c="<<--c<<endl;   //輸出c=A
    c='B';
    cout<<"c="<<c--<<endl;   //輸出c=B
    x=1.5;
    y=5+--x;
    cout<<"y="<<y<<endl;    //輸出y=5.5
    x=1.5;
    y=5+x--;
    cout<<"y="<<y<<endl;    //輸出y=6.5
}
#include <iostream.h>
main()
{
    int a=3, b=2;

    //輸出關係表示式
    cout<<a<b<<endl;
    cout<<(a<b)<<(a>b)<<(a>=b)<<(a==b)<<(a!=b)<<endl;

    bool flag=2*a<b+10;
    cout<<"flag="<<flag;
}
#include <iostream.h>
main()     
{
    float a=3.5,b=2.1,c=0;
    cout<<"a="<<a<<"  b="<<b<<"  c="<<c<<endl;

    //與運算
    cout<<"a&&b="<<(a&&b)<<endl;//輸出1
    cout<<"a&&c="<<(a&&c)<<endl;//輸出0

    //或運算
    cout<<"a||b="<<(a||b)<<endl;//輸出1
    cout<<"a||c="<<(a||c)<<endl;//輸出1

    //非運算
    cout<<"!a="<<!a<<endl<<"!c="<<!c<<endl;//輸出0  1

    //關係運算和邏輯運算
    bool flag=a>=0 && a<=5;  //變數a在[0,5]區間內
    cout<<"a=>0 && a<=5="<<flag<<endl;//輸出1

    //算術運算、關係運算和邏輯運算
    cout<<"a+5>2*b+2||a<b+3="<<(a+5>2*b+2||a<b+3)<<endl;//輸出1
}
#include <iostream.h>
main()     
{
    //按位與運算
    cout<<"24&12="<<(24&12)<<endl;
    //按位異或運算
    cout<<"24^12="<<(24^12)<<endl;
    //按位或運算
    cout<<"24|12="<<(24|12)<<endl;
    //按位取反運算
    cout<<"~24="<<(~24)<<endl;

    //左移位運算
    cout<<"5<<3="<<(5<<3)<<endl;
    cout<<"-5<<3="<<(-5<<3)<<endl;

    //右移位運算
    cout<<"5>>3="<<(5>>3)<<endl;
    cout<<"-5>>3="<<(-5>>3)<<endl;
}
#include <iostream.h>
main()
{
    int a=1,b=1,c=3;
    //顯示a,b,c的值
    cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl;

    //計算顯示(1) b+=a+2*c%5; 的結果
    b+=a+2*c%5;    	//相當於表示式語句 b=b+(a+2*c%5);
    cout<<"(1) b="<<b<<endl;

    //計算顯示(2) a<<=c-2*b; 的結果
    a=1,b=1,c=3;
    a<<=c-2*b;     	// 相當於表示式語句 a=a<<(c-2*b);
    cout<<"(2) a="<<a<<endl;

    //計算顯示(3) a*=b=c=3;的結果
    a=1,b=1,c=3;
    a*=b=c=3;      	//相當於語句組 c=3;b=c;a=a*b;
    cout<<"(3) a="<<a<<"  b="<<b<<"  c="<<c<<endl;

    //計算顯示(4) a+=b+=c;的結果
    a=1,b=1,c=3;
    a+=b+=c;       	//相當於語句組 b=b+c; a=a+b;
    cout<<"(4) a="<<a<<"  b="<<b<<"  c="<<c<<endl;

    //計算顯示(5) a-=b=++c+2;的結果
    a=1,b=1,c=3;
    a-=b=++c+2;      	//相當於語句組 ++c;b=b+c+2;a=a-b;
    cout<<"(5) a="<<a<<"  b="<<b<<"  c="<<c<<endl;
}
#include <iostream.h>
main()
{
    //用 sizeof 計算各類種常量的位元組長度
    cout<<"sizeof('$')="<<sizeof('$')<<endl;
    cout<<"sizeof(1)="<<sizeof(1)<<endl;
    cout<<"sizeof(1.5)="<<sizeof(1.5)<<endl;
    cout<<"sizeof(\"Good!\")="<<sizeof("Good!")<<endl;

    //用sizeof 計算各型別變數的位元組長度
    int i=100;
    char c='A';
    float x=3.1416; 
    double p=0.1;
    cout<<"sizeof(i)="<<sizeof(i)<<endl;
    cout<<"sizeof(c)="<<sizeof(c)<<endl;
    cout<<"sizeof(x)="<<sizeof(x)<<endl;
    cout<<"sizeof(p)="<<sizeof(p)<<endl;

    //用sizeof 計算表示式的位元組長度
    cout<<"sizeof(x+1.732)="<<sizeof(x+1.732)<<endl;

    //用 sizeof 計算各型別的位元組長度
    cout<<"sizeof(char)="<<sizeof(char)<<endl;
    cout<<"sizeof(int)="<<sizeof(int)<<endl;
    cout<<"sizeof(float)="<<sizeof(float)<<endl;
    cout<<"sizeof(double)="<<sizeof(double)<<endl;

    //用sizeof 計算陣列的位元組長度
    char str[]="This is a test.";
    int a[10];	
    double xy[10];
    cout<<"sizeof(str)="<<sizeof(str)<<endl;
    cout<<"sizeof(a)="<<sizeof(a)<<endl;
    cout<<"sizeof(xy)="<<sizeof(xy)<<endl;

    //用sizeof 計算自定義型別的長度
    struct st {
        short num;
        float math_grade;
        float Chinese_grade;
        float sum_grade;
    };
    st student1;
    cout<<"sizeof(st)="<<sizeof(st)<<endl;
    cout<<"sizeof(student1)="<<sizeof(student1)<<endl;
}
#include <iostream.h>
main()
{
    //宣告變數語句中使用順序運算
    int x, y;

    //計算中使用順序運算
    x=50; 
    y=(x=x-5, x/5); 
    cout<<"x="<<x<<endl;
    cout<<"y="<<y<<endl;
}
#include <iostream.h>
main()
{
    //測試表達式型別的轉換
    int n=100,m;
    double x=3.791,y;
    cout<<"n*x="<<n*x<<endl;
    
    //賦值型別轉換
    m=x;
    y=n;
    cout<<"m="<<m<<endl;
    cout<<"y="<<y<<endl;

    //強制型別轉換
    cout<<"int(x)="<<int(x)<<endl;
    cout<<"(int)x="<<(int)x<<endl;
    cout<<"int(1.732+x)="<<int(1.732+x)<<endl;
    cout<<"(int)1.732+x="<<(int)1.723+x<<endl;
    cout<<"double(100)="<<double(100)<<endl;
}
#include <iostream.h>
main()
{
    float a,b,s;

    cout<<"a b"<<endl;
    cin>>a>>b;	   //利用cin從鍵盤上為變數 a,b 賦值
    s=a;
    if (a<b) {
       s=b;         //if語句中只有這一個語句,可省略花括號
    }
    s=s*s;          //變數s中儲存a,b中較大的一個數的平方
    cout<<"s="<<s;
}

#include <iostream.h>
main()
{
    int x,y;
    cout<<"x=";
    cin>>x;
    if (x<=0) {            //滿足條件執行
       y=2*x; 
       cout<<"y="<<y;     //輸出結果
    }
    else  {              //不滿足條件執行
       y=x*x; 
       cout<<"y="<<y;    //輸出結果
    }
}
#include <iostream.h>
main()
{
    int a,b,c;
    int smallest;
    cout<<"a  b  c"<<endl;
    cin>>a>>b>>c;
    if (a<=b)    //外層條件語句
    {
        if (a<=c)    //內層條件語句
           smallest=a;
        else
           smallest=c;
    }
    else
    {
       if (b<=c)    //內層條件語句
           smallest=b;
       else
           smallest=c;
    }
    cout<<"Smallest="<<smallest<<endl;
}

#include <iostream.h>
main()
{
    int score;

    //從鍵盤上輸入分數
    cout<<"score=";
    cin>>score;

    //用帶else if的條件語句判斷處理
    if (score<0 || score>100)    
    {
       cout<<"The score is out of range!"<<endl;
    }
    else if (score>=90) 
       cout<<"Your grade is a A."<<endl;
    else if (score>=80) 
       cout<<"Your grade is a B."<<endl;
    else if (score>=70) 
       cout<<"Your grade is a C."<<endl;
    else if (score>=60) 
       cout<<"Your grade is a D."<<endl;
    else 
       cout<<"Your grade is a E."<<endl;
}
#include <iostream.h>
main()
{
    int n;
    cout<<"n=";
    cin>>n;
    if (n>=0 && n<=100 &&n%2==0)
       cout<<"n="<<n<<endl;
    else
       cout<<"The "<<n<<" is out of range!"<<endl;
}

#include <iostream.h>
main()
{
    int a,b,Max;
    //輸入資料
    cout<<"a=";
    cin>>a;
    cout<<"b=";
    cin>>b;

    //找出較大值
    Max=a>b?a:b;
    cout<<"Max="<<Max<<endl;
}

#include <iostream.h>
main()
{
    int a,b;
    //輸入資料
    cout<<"a=";
    cin>>a;
    cout<<"b=";
    cin>>b;

    //除法判斷
    if (b!=0 && a%b==0) {
        cout<<b<<" divides "<<a<<endl;
        cout<<"a/b="<<a/b<<endl;
    }
    else
        cout<<b<<" does not divide "<<a<<endl;
}


#include <iostream.h>
main()
{
    //x,y 為運算元,c為運算子
    int x,y,z;
    char c1;
    cin>>x>>c1>>y;   //c1

    //多路選擇語句選擇不同表示式計算語句
    switch(c1) {
          case '+':cout<<x<<"+"<<y<<"="<<x+y<<endl;
                   break;
          case '-':cout<<x<<"-"<<y<<"="<<x-y<<endl;
                   break;
          case '*':cout<<x<<"*"<<y<<"="<<x*y<<endl;
                   break;
          case '/':cout<<x<<"/"<<y<<"="<<x/y<<endl;
                   break;
          case '%':cout<<x<<"%"<<y<<"="<<x%y<<endl;
                   break;
          default :cout<<"Wrong !"<<endl; //當不符合上述情況時執行本子句
    }
}



#include<iostream.h>
float x=365.5;  //宣告全域性變數
main() {
    int x=1,y=2;
    double w=x+y;
    {
        double x=1.414,y=1.732,z=3.14;
        cout<<"inner:x="<<x<<endl;
        cout<<"inner:y="<<y<<endl;
        cout<<"inner:z="<<z<<endl;
        cout<<"outer:w="<<w<<endl;
        cout<<"::x="<<::x<<endl;    //訪問重名的全域性變數
    }
    cout<<"outer:x="<<x<<endl;
    cout<<"outer:y="<<y<<endl;
    cout<<"outer:w="<<w<<endl;

    //cout<<"inner:z="<<z<<endl;無效
    cout<<"::x="<<::x<<endl;    //訪問重名的全域性變數
}
#include<iostream.h>
main() {
    //顯示1,2,3...10
    for(int i=1;i<=10;i++)
        cout<<i<<" ";
    cout<<endl;

    //顯示10,9,8...1
    for(int j=10;j>=1;j--)
       cout<<j<<" ";
    cout<<endl;

    //顯示1,3,5...9
    for(int k=1;k<=10;k=k+2)
       cout<<k<<" ";
    cout<<endl;

    //顯示ABC...Z   
    for(char c='A';c<='Z';c++)
       cout<<c;
    cout<<endl;

    //顯示0,0.1,0.2...1.0
    for(float x=0;x<=1.0;x=x+0.1)
       cout<<x<<" ";
    cout<<endl;

    //顯示0,0.1,0.2...1.0
    for(float x1=0;x1<=1.0+0.1/2;x1=x1+0.1)
       cout<<x1<<" ";
    cout<<endl;

    //計算s=1+2+3...+100
    int s=0;
    for(int n=1;n<=100;n++)
        s=s+n;
    cout<<"s="<<s<<endl;
}
#include<iostream.h>
main()
{
    //計算s=1+2+3...+100
    int s=0,n=1;
    while(n<=100) {
        s=s+n;
        n++;
    }
    cout<<"s="<<s<<endl;

    //累加鍵盤輸入的資料
    double x,sum=0.0;
    cout<<"x=";
    cin>>x;
    while(x!=0) {
        sum+=x;
        cout<<"x=";
        cin>>x;
    }
    cout<<"sum="<<sum<<endl;
}
#include<iostream.h>
main()
{
    //計算s=1+2+3...+100
    int s=0,n=0;
    do  {
        n++;
        s+=n;
    }while(n<100);
    cout<<"s="<<s<<endl;

    //累加鍵盤輸入的資料
    double x,sum=0.0;
    do {
        cout<<"x=";
        cin>>x;
        sum+=x;
    } while(x!=0);
    cout<<"sum="<<sum<<endl;
}

#include<iostream.h>
main()
{
    //計算和列印列印乘法九九表
    for (int i=1;i<=9;i++) {
        cout<<i;
        for (int j=1;j<=9;j++)
            cout<<'\t'<<i<<"*"<<j<<"="<<i*j;
        cout<<endl;
    }
}

#include<iostream.h>
main()
{
    int x,sum=0;
    //定義標號L1
L1: cout<<"x=";
    cin>>x;
    if (x==-1)
       goto L2;          //無條件轉移語句,轉到L2語句處
    else
       sum+=x;
    goto L1;             //無條件轉移語句,轉到L1語句處
    //定義標號L2
L2: cout<<"sum="<<sum<<endl;
}


#include<iostream.h>
main()
{
    //累加鍵盤輸入的資料
    double x,sum=0.0;
    while(1) {
        cout<<"x=";
        cin>>x;
        if (x<=0) break;
        sum+=x;
    }
    cout<<"sum="<<sum<<endl;
}

#include<iostream.h>
main()
{
    int i;
    for (i=1;i<=20;i++)
   {
        if (i%3==0)   //能被 3 整除的整數,返回進行下次迴圈
            continue;
        cout<<i<<" ";
    }
    cout<<endl;
}
#include<iostream.h>
main()
{
    //宣告陣列和變數
    int a[5],i,sum;
    double avg;
	
    //從鍵盤上迴圈為陣列賦值
    for (i=0;i<5;i++) {
        cout<<"a["<<i<<"]=";
        cin>>a[i];
    }

    //直接顯示陣列元素
    cout<<a[0]<<a[1]<<a[2]<<a[3]<<a[4]<<endl;
    
    //利用for迴圈顯示陣列各元素的值
    for (i=0;i<5;i++)
        cout<<a[i]<<"  ";
    cout<<endl;

    //計算陣列元素之和,並顯示計算結果
    sum=a[0]+a[1]+a[2]+a[3]+a[4];
    cout<<"sum="<<sum<<endl;

    //利用迴圈計算陣列的累加和
    for (sum=0,i=0;i<5;i++)
        sum+=a[i];

    //顯示累加和及平均值
    cout<<"sum="<<sum<<endl;
    avg=sum/5.0;
    cout<<"avg="<<avg<<endl;
}
#include<iostream.h>
main()
{
     int i,max,index,a[5];

    //從鍵盤上為陣列賦值
     for (i=0;i<=4;i++)
     {
       cout<<"a["<<i<<"]=";
       cin>>a[i];
     }

    // 利用迴圈遍歷陣列,找出最大值的元素及其下標
    max=a[0];
    for (i=0;i<=4;i++)
    {
            if (max<a[i])
            {
                max=a[i];
                index=i;
            }
        }
    cout<<"\nMax="<<max<<"  index="<<index;
}
#include<iostream.h>
#define size 5
main()
{
    //宣告變數
    int i,j;
    float t,a[size];

    //從鍵盤上為陣列賦值
    for (i=0;i<size;i++)
    {
       cout<<"a["<<i<<"]=";
       cin>>a[i];
    }

    //對陣列按從小到大順序排序
    for (i=0;i<size-1;i++)
        for (j=i+1;j<size;j++)
            if (a[i]>a[j])
            {
               t=a[i];
               a[i]=a[j];
               a[j]=t;
            }

    //顯示排序結果
    for (i=0;i<size;i++)
       cout<<a[i]<<" ";
    cout<<endl;

    //輸入要查詢的資料
    int value;
    int found;   //找到為1,否則為0
    int	low,high,mid;   
    for (i=1;i<=3;i++) {
        cout<<"value=";
        cin>>value;
	
        //二分法查詢陣列a
        found=0;
        low=0;
        high=size-1;
        while(low<=high)
        {	
            mid=(high+low)/2;
            if (a[mid]==value)
            {
            found=1;
            break;
            }
            if (a[mid]<value)
                low=mid+1;
            else
                high=mid-1;
        }
        if (found)
            cout<<"The valu found at:a["<<mid<<"]="<<a[mid]<<endl;
        else
            cout<<"The "<<value<<" is not found!"<<endl;
    }
}
#include<iostream.h>
main()
{
	//宣告變數
    int i,j;
    float t,a[5];

    //從鍵盤上為陣列賦值
    for (i=0;i<=4;i++)
    {
       cout<<"a["<<i<<"]=";
       cin>>a[i];
    }

    //對陣列按從大到小順序排序
    for (i=0;i<=3;i++)
        for (j=i+1;j<=4;j++)
            if (a[i]<=a[j])
            {
               t=a[i];
               a[i]=a[j];
               a[j]=t;
            }

    //顯示排序結果
    for (i=0;i<=4;i++)
       cout<<a[i]<<" ";
}
#include<iostream.h>
main()
{
    //宣告二維陣列及變數 
    int a[2][3],i,j;
    
    //從鍵盤上為陣列a賦值
     for (i=0;i<2;i++) 
         for (j=0;j<3;j++) 
         {
            cout<<"a["<<i<<"]["<<j<<"]=";
            cin>>a[i][j];
          }

    //顯示陣列a
     for (i=0;i<2;i++) { 
         for (j=0;j<3;j++) 
         {
            cout<<a[i][j]<<"  ";
         }
        cout<<endl;
    }

    //找出該陣列的最大元素及其下標
    int h,l,Max=a[0][0];
     for (i=0;i<2;i++) {  
         for (j=0;j<3;j++) 
         {
            if (Max<a[i][j]) {
                Max=a[i][j];
                h=i;
                l=j;
            }
         }
    }
     cout<<"Max:"<<"a["<<h<<"]["<<l<<"]="<<a[h][l]<<endl;
}
#include<iostream.h>
main()
{
    //宣告字元陣列和變數
    char str[6];
    int i;

    //從鍵盤上輸入字串
    cout<<"str=";
    cin>>str; 
    cout<<str<<endl;
	
    //按陣列和下標變數兩種方式顯示字元陣列
    cout<<str<<endl;
    for (i=0;i<6;i++)
        cout<<str[i];
    cout<<endl;

    //字串反向輸出
    for (i=5;i>=0;i--) 
         cout<<str[i];
    cout<<endl;

    //將字元陣列變成大寫字母后輸出 
    for (i=0;i<=5;i++)
       str[i]-=32;      	//小寫字母轉換成大寫字母
    cout<<str<<endl;    	//顯示字串
}
#include<iostream.h>
main()
{
    //宣告變數和指標變數
    int a,b,c,*ip;

    //指標變數ip指向變數a
    a=100;
    ip=&a;        //使指標變數 ip 指向變數a
    cout<<"a="<<a<<endl;
    cout<<"*ip="<<*ip<<endl;
    cout<<"ip="<<ip<<endl;

    //指標變數ip指向變數b
    ip=&b;        //使指標變數 ip 指向變數b
    b=200;
    cout<<"b="<<b<<endl;
    cout<<"*ip="<<*ip<<endl;
    cout<<"ip="<<ip<<endl;

    //指標變數ip指向變數c
    ip=&c;        //使指標變數 ip 指向變數b
    *ip=a+b;
    cout<<"c="<<c<<endl;
    cout<<"*ip="<<*ip<<endl;
    cout<<"ip="<<ip<<endl;
}
#include<iostream.h>
main()
{
    //宣告陣列、變數和指標變數
    int a[2][3],i,j;
    int* ip;

    //從鍵盤上為陣列a賦值
    for (i=0;i<2;i++)  //為陣列a賦值
        for (j=0;j<3;j++) 
        {
           cout<<"a["<<i<<"]["<<j<<"]=";
           cin>>a[i][j];
         }

    //利用下標變數顯示陣列a
    for (i=0;i<2;i++) { 
        for (j=0;j<3;j++) 
        {
           cout<<a[i][j]<<"  ";
        }
        cout<<endl;
    }

    //利用指標變數顯示陣列a
    ip=&a[0][0];	 
    for (i=0;i<2;i++) { 
         for (j=0;j<3;j++) 
         {
            cout<<"a["<<i<<"]["<<j<<"]=";
            cout<<ip<<"  ";
            cout<<*ip<<endl;
            ip++;
         }
    }
}
#include<iostream.h>
main()
{
    //宣告陣列、變數和指標變數
    int a[]={1,2,3,4,5,6};
    int *ip1,*ip2;

    //測試指標的賦值運算
    ip1=a;
    ip2=ip1;   
    cout<<"*ip1="<<(*ip1)<<endl;
    cout<<"*ip2="<<(*ip2)<<endl;

    //測試指標的自增自減運算和組合運算
    ip1++;  
    ip2+=4; 
    cout<<"*ip1="<<(*ip1)<<endl;
    cout<<"*ip2="<<(*ip2)<<endl;
    
    //測試指標變數之間的關係運算
    int n=ip2>ip1;
    cout<<"ip2>ip1="<<n<<endl;
    cout<<"ip2!=NULL="<<(ip2!=NULL)<<endl;

    //指標變數之間的減法
    n=ip2-ip1;
    cout<<"ip2-ip1="<<n<<endl;
}
#include<iostream.h>
main()
{
    //宣告字元型陣列和指標變數
    char str[10];
    char *strip=str;

    //輸入輸出
    cout<<"str=";
    cin>>str;      //用字元陣列輸入字串
    cout<<"str="<<str<<endl;
    cout<<"strip="<<strip<<endl;
    cout<<"strip=";
    cin>>strip;     //用字元指標變數輸入字串
    cout<<"str="<<str<<endl;
    cout<<"strip="<<strip<<endl;

    //利用指標變數改變其指向字串的內容
    *(strip+2)='l';
    cout<<"str="<<str<<endl;
    cout<<"strip="<<strip<<endl;

    //動態為字元型指標變數分配記憶體
    strip=new char(100);
    cout<<"strip=";
    cin>>strip; //用字元指標變數輸入字串
    cout<<"str="<<str<<endl;
    cout<<"strip="<<strip<<endl;
}
#include<iostream.h>
main()
{
    // 宣告用於存放運動員號碼的陣列
    int h[]={1001,1002,1003,1004}; 
    // 宣告用於存放運動員成績的陣列
    float x[]={12.3,13.1,11.9,12.1};    
    //宣告用於存放運動姓名的字元型指標陣列
    char *p[]={"Wang hua","Zhang jian","Li wei","Hua ming"}; 
    //i,j,it是用做迴圈控制變數和臨時變數
    int i,j,it; 
    //ft 用做暫存變數
    float ft;  
    //pt為字元型指標變數用做暫存指標變數
    char *pt; 

    //用選擇法對陣列x進行排序,並相應調整陣列h和p中的資料
    for (i=0;i<=3;i++)  
        for (j=i+1;j<=3;j++)
           if (x[i]>=x[j]) {
              ft=x[i],x[i]=x[j],x[j]=ft;
              it=h[i],h[i]=h[j],h[j]=it;
              pt=p[i],p[i]=p[j],p[j]=pt;
           }

    //以下列印排序結果
    for (i=0;i<=3;i++)
       cout<<h[i]<<" ,"<<p[i]<<" ,"<<x[i]<<endl;
}
#include<iostream.h>
main()
{
    //宣告指標陣列
    char *colors[]={"Red","Blue","Yellow","Green"}; 
    //指向指標的指標變數
    char **pt;             

    //通過指向指標的變數訪問其指向的內容
    pt=colors;
    for (int i=0;i<=3;i++) {
        cout<<"pt="<<pt<<endl;
        cout<<"*pt="<<*pt<<endl;
        cout<<"**pt="<<**pt<<endl;
        pt++;
    }
}
#include<iostream.h>
main()
{
    //定義結構型別
    struct    books
    {
    char   title[20];
    char   author[15];
    int    pages;
    float  price;
    } ;
    
    //宣告結構變數
    struct books Zbk={"VC++ ","Zhang",295,35.5}; 
    books Wbk;  

    //對結構變數的輸出
    cout<<"Zbk:"<<endl;
    cout<<Zbk.title <<endl;
    cout<<Zbk.author<<endl;
    cout<<Zbk.pages<<endl;
    cout<<Zbk.price<<endl;
    cout<<"--------------------"<<endl;

    //對結構成員的運算
    Zbk.pages+=10;
    Zbk.price+=0.5;
    cout<<"Zbk.pages="<<Zbk.pages<<endl;
    cout<<"Zbk.price="<<Zbk.price<<endl;
    cout<<"--------------------"<<endl;

    //對結構變數的輸入輸出
    cout<<"Wbk.title =";
    cin>>Wbk.title;
    cout<<"Wbk.author=";
    cin>>Wbk.author;
    cout<<"Wbk.pages=";
    cin>>Wbk.pages;
    cout<<"Wbk.price=";
    cin>>Wbk.price;
    cout<<"Wbk:"<<endl;
    cout<<Wbk.title <<endl;
    cout<<Wbk.author<<endl;
    cout<<Wbk.pages<<endl;
    cout<<Wbk.price<<endl;
    cout<<"--------------------"<<endl;

    //結構變數之間的相互賦值
    books temp;
    temp=Wbk;
    cout<<"temp:"<<endl;
    cout<<temp.title<<endl;
    cout<<temp.author<<endl;
    cout<<temp.pages<<endl;
    cout<<temp.price<<endl;
}

#include<iostream.h>
main()
{
    int i;
    //定義結構型別 
    struct student {
           int  num;
           char  name[10];
           float maths;
           float physics;
           float chemistry;
           double  total;
    };

     //宣告結構陣列st
     student st[3];

     //從鍵盤上為結構陣列輸入值 
     cout<<"    num  name     maths physics chemistry "<<endl;
     for (i=0;i<3;i++)
     {
        cout<<i+1<<"   ";
        cin>>st[i].num;
        cin>>st[i].name;
        cin>>st[i].maths;
        cin>>st[i].physics;
        cin>>st[i].chemistry;
     }

    //計算每個學生的總成績
    for (i=0;i<3;i++)
         st[i].total=st[i].maths+st[i].physics+st[i].chemistry;

    //輸出結構陣列各元素的值 
    for (i=0;i<3;i++)
    {
        cout<<"st["<<i<<"]:   ";
        cout<<st[i].num<<'\t';
        cout<<st[i].name<<'\t';
        cout<<st[i].maths<<'\t';
        cout<<st[i].physics<<'\t';
        cout<<st[i].chemistry<<'\t';
        cout<<st[i].total<<endl;
     }
}
#include<iostream.h>
main()
{
    //定義結構型別
    struct human {
       char name[10];
       int sex;
       int age;
    };

    //宣告結構變數和結構指標變數,並初始化
    struct human x={"WangPing",1,30},*p=NULL;

    //結構指標變數指向物件
    p=&x;

    //顯示結構變數的值
    cout<<"x.name="<<x.name<<endl;
    cout<<"x.sex="<<x.sex<<endl;
    cout<<"x.age="<<x.age<<endl;
  
    //利用結構指標顯示結構物件中的資料
    cout<<"(*p).name="<<(*p).name<<endl;
    cout<<"(*p).sex="<<(*p).sex<<endl;
    cout<<"(*p).age="<<(*p).age<<endl;
    cout<<"p->name="<<p->name<<endl;
    cout<<"p->sex="<<p->sex<<endl;
    cout<<"p->age="<<p->age<<endl;

    //通過結構指標為結構物件輸入資料
    cout<<"name:";
    cin>>(*p).name;
    cout<<"sex:";
    cin>>(*p).sex;
    cout<<"age:";
    cin>>(*p).age;

    //顯示結構變數的值
    cout<<"x.name="<<x.name<<endl;
    cout<<"x.sex="<<x.sex<<endl;
    cout<<"x.age="<<x.age<<endl;
}
include<iostream.h>
main()
{
    //定義結構型別
    struct human {
       char name[10];
       int sex;
       int age;
       };

    //宣告結構變數和結構指標,並初始化
    struct human x={"WangPing",1,30},*p=&x;
 
    //利用結構指標顯示結構中的資料
    cout<<"(*p).name="<<(*p).name<<endl;
    cout<<"(*p).sex="<<(*p).sex<<endl;
    cout<<"(*p).age="<<(*p).age<<endl;
    cout<<"-------------------------"<<endl;

    //利用new運算子為p分配記憶體
    p=new human;

    //從鍵盤上為p指向的結構物件賦值
    cout<<"p->name=";
    cin>>p->name;
    cout<<"p->sex=";
    cin>>p->sex;
    cout<<"p->age=";
    cin>>p->age;
    cout<<"-------------------------"<<endl;

    //顯示p所指結構物件的值
    cout<<"p->name="<<p->name<<endl;
    cout<<"p->sex="<<p->sex<<endl;
    cout<<"p->age="<<p->age<<endl;
    cout<<"-------------------------"<<endl;

    //顯示結構變數的值
    cout<<"x.name="<<x.name<<endl;
    cout<<"x.sex="<<x.sex<<endl;
    cout<<"x.age="<<x.age<<endl;

    //釋放p指向的記憶體
    delete p;  
}
#include<iostream.h>
main()
{
    //定義結構型別
    struct human {
       char name[10];
       int sex;
       int age;
    };

    //宣告結構陣列和結構指標變數,並初始化
    human x[]={{"WeiPing",1,30},{"LiHua",1,25},{"LiuMin",0,23}},*p=NULL;

    //用下標變數的輸出結構陣列的元素
    for (int i=0;i<3;i++)
    {
        cout<<x[i].name<<'\t';
        cout<<x[i].sex<<'\t';
        cout<<x[i].age<<endl;
    }
    cout<<"----------------"<<endl;

    //用結構指標輸出結構陣列的元素
    for (p=x;p<=&x[2];p++)
    {
        cout<<p->name<<'\t';
        cout<<p->sex<<'\t';
        cout<<p->age<<endl;
    }
}
#include<iostream.h>
main()
{
    //定義一個包含指標成員的結構型別
    struct test {
       char *str;
       int  *ip;
    } x;

    //使用結構變數x中的整型指標ip
    x.ip=new int;    //分配1個單元
    *(x.ip)=100;
    cout<<"x.ip:"<<x.ip<<'\t'<<*(x.ip)<<endl;
    cout<<"---------------"<<endl;
    delete x.ip;
    x.ip=new int[5];    //分配5個單元
    for(int i=0;i<5;i++)
        *(x.ip+i)=100+i;
    cout<<"x.ip:"<<endl;
    for(i=0;i<5;i++)
        cout<<x.ip+i<<'\t'<<(*(x.ip+i))<<endl;
    delete x.ip;
    cout<<"---------------"<<endl;

    //使用結構變數x中的字元型指標str
    x.str=new char('A');    //分配1個單元
    cout<<"x.str:"<<(*x.str)<<endl;
    cout<<"---------------"<<endl;
    delete x.str;
    x.str=new char[5];    //分配多個單元
    *x.str='G';
    *(x.str+1)='o';
    *(x.str+2)='o';
    *(x.str+3)='d';
    *(x.str+4)='\0';
    cout<<"x.str:"<<x.str<<endl;
    delete x.str;
    cout<<"---------------"<<endl;

    //在宣告結構變數時初始化
    test y={"Very Good!",NULL};
    cout<<"y.str:"<<y.str<<endl;
    cout<<"y.ip:"<<y.ip<<endl;
}
#include<iostream.h>
main()
{
    //定義date結構
    struct date
    {
       int year;
       int month;
       int day;
    };

    //定義baby結構
    struct baby {
        int    num;
        float   weight;
        date   birthday;   // date為結構型別 
    }; 
	
    //宣告baby結構變數並初始化
    baby b1={10001,10,{2002,12,25}};

    //下列是baby結構變數b1的引用。
    cout<<"b1.num="<<b1.num<<endl;
    cout<<"b1.weight="<<b1.weight<<endl;
    cout<<"b1.birthday.year="<<b1.birthday.year<<endl;
    cout<<"b1.birthday.month="<<b1.birthday.month<<endl;
    cout<<"b1.birthday.day="<<b1.birthday.day<<endl;
    cout<<"--------------------------"<<endl;

    //宣告baby結構變數temp,並進行賦值運算
    baby temp;
    temp=b1;
    cout<<"temp.num="<<temp.num<<endl;
    cout<<"temp.weight="<<temp.weight<<endl;
    cout<<"temp.birthday.year="<<temp.birthday.year<<endl;
    cout<<"temp.birthday.month="<<temp.birthday.month<<endl;
    cout<<"temp.birthday.day="<<temp.birthday.day<<endl;
}
#include<iostream.h>
main()
{
    //定義名為list的遞迴結構  
    struct list {
          char         name[10];
          int          sex;
          int          age;
          list         *next;   //成員next為指向其自身結構的指標
    };

    //使用遞迴結構變數
    list L1={"WeiPing",1,35.5,NULL};
    cout<<"L1:"<<endl;
    cout<<"name\t"<<L1.name<<endl;
    cout<<"sex\t"<<L1.sex<<endl;
    cout<<"age\t"<<L1.age<<endl;
    cout<<"next\t"<<L1.next<<endl;
}
#include<iostream.h>
main()
{
    int i;
    //定義名為student的遞迴結構 
    struct  student {
           char name[10];
           int  math;
           int  computer;
           float sum;
           student *next;    //next成員是指向自身的結構指標 
    };

    //用student宣告3個結構指標變數
    struct student *head,*tail,*temp;  	

    //申請第1塊資料,並設定各結構指標的初值
    temp=new struct student;    //申請記憶體 
    head=temp;   // 頭指標 
    tail=head;    // 尾指標 

    //迴圈為連結串列輸入資料
    cout<<"\tname    Math   Computer"<<endl;
    for (i=1;;i++) {
        cout<<i<<"\t";
        cin>>temp->name;
        if (temp->name[0]!='*')
        {
            cin>>temp->math>>temp->computer;
            temp->sum=temp->math+temp->computer;
            temp->next=NULL;
            tail=temp;      //設定連結串列尾指標 
         }
         else
         {
          // 以下是輸入結束處理 
            delete temp;
            tail->next=NULL;
            break;
         }
        //為下一個學生申請記憶體
        temp->next=new struct student; 
        temp=temp->next;    // 使處理指標temp指向新記憶體塊
    }

    //將連結串列資料從頭到尾打印出來
    cout<<"--------------------"<<endl;
    temp=head;
    while (temp!=NULL) {
           cout<<temp->name<<","<<temp->math<<",";
           cout<<temp->computer<<","<<temp->sum<<endl;
           temp=temp->next;
     }
}
#include<iostream.h>
main()
{
    int i;
    //定義名為student的遞迴結構 
    struct  student {
           char name[10];
           int  math;
           int  computer;
           float sum;
           student *forw;    //forw成員是前指標 
           student *next;    //next成員是後指標
    };

    //用student宣告3個結構指標變數
    struct student *head,*tail,*temp; 

    //申請第1塊資料,並設定各結構指標的初值
    temp=new struct student;    //申請記憶體 
    head=temp;      // 頭指標 
    tail=head;      // 尾指標 
    head->forw=NULL;

    //迴圈為連結串列記錄輸入資料
    cout<<"\tname    Math   Computer"<<endl;
    for (i=1;;i++) {
        cout<<i<<"\t";
        cin>>temp->name;
        if (temp->name[0]!='*')
        {
            cin>>temp->math>>temp->computer;
            temp->sum=temp->math+temp->computer;
            temp->next=NULL;
            tail=temp;      //設定連結串列尾指標 
         }
         else
         {
          // 以下是輸入結束處理 
            delete temp;
            tail->next=NULL;
            break;
         }
        //為下一個學生申請記憶體
        temp->next=new struct student; 
        temp->next->forw=temp;   //設定前指標
        temp=temp->next;         //使處理指標temp指向新記憶體塊
    }

    // 將連結串列資料從頭到尾打印出來
    cout<<"head------>tail:"<<endl;
    temp=head;
    while (temp!=NULL) {
           cout<<temp->name<<","<<temp->math<<",";
           cout<<temp->computer<<","<<temp->sum<<endl;
           temp=temp->next;
     }

    // 將連結串列資料從尾到頭打印出來
    cout<<"tail------>head:"<<endl;
    temp=tail;
    while (temp!=NULL) {
           cout<<temp->name<<","<<temp->math<<",";
           cout<<temp->computer<<","<<temp->sum<<endl;
           temp=temp->forw;
     }
}
#include<iostream.h>
main()
{
    int i;
    //定義聯合型別
    union utag  {
          char    c;
          int     k;
          float   x;
    };

    //宣告聯合變數
    union utag u; 

    // 使用聯合變數中的字元型成員 
    u.c='*';
    cout<<"u.c="<<u.c<<endl;

    // 使用聯合變數中的整型成員 
    u.k=1000;
    cout<<"u.k="<<u.k<<endl;

    // 使用聯合變數中的浮點型成員 
    u.x=3.1416;
    cout<<"u.x="<<u.x<<endl;

    //宣告聯合變數時初始化
    utag u1={'A'};

    //同時引用聯合變數的各成員
    cout<<"u1.c="<<u1.c<<endl;
    cout<<"u1.k="<<u1.k<<endl;
    cout<<"u1.x="<<u1.x<<endl;
}
#include<iostream.h>
main()  
{
    //定義結構型別,併為宣告的結構變數賦初值
    struct s_tag {
           short    i;
           float x;
    } sx={100,3.1416};

    //定義聯合型別,併為宣告的聯合變數賦初值
    union   u_tag  {
            short    i;
            float x;
    } ux={1000};

    //輸出結構型別和結構變數的有關資訊
    cout<<"sizeof(struct s_tag)="<<sizeof(struct s_tag)<<endl;
    cout<<"sx.i="<<sx.i<<endl;
    cout<<"sx.x="<<sx.x<<endl;
    cout<<"sizeof(sx)="<<sizeof(sx)<<endl;
    cout<<"------------------------------"<<endl;

    //輸出聯合型別和聯合變數的有關資訊
    cout<<"sizeof(union u_tag)="<<sizeof(union u_tag)<<endl;
    ux.i=200;
    cout<<"ux.i="<<ux.i<<endl;  //輸出聯合變數ux 的i成員
    ux.x=123.456;
    cout<<"ux.x="<<ux.x<<endl;  //輸出聯合變數ux 的x成員
    cout<<"sizeof(ux)="<<sizeof(ux)<<endl;
}
#include<iostream.h>
main()
{
    //自定義型別 
    typedef  int  ARRAY_INT[50];
    int i;
    ARRAY_INT a;    //用自定義型別宣告陣列變數a 

    //以下為陣列a賦值,並列印  
    for (i=0;i<50;i++) {
       if (i%10==0)       //每10個數換一次行 
         cout<<endl;
       a[i]=i;
       cout<<a[i]<<"\t";
     }
    cout<<endl;
}
#include<iostream.h>
//定義結構型別
struct student
{
    int   num;
    char  name[20];
    float grade;
};
void main(void)
{
    //宣告陣列
    int i,size;
    char str[]="This is a string.";
    int int_values[] = {51, 23, 2, 44, 45,0,11}; 
    float float_values[] = {15.1, 13.3, 22.2, 10.4, 1.5};  
    student st_arr[]={101,"WangLin",92,102,"LiPing",85,103,"ZhaoMin",88};
		
    //顯示char型別陣列元素及其大小
    size=sizeof(str) / sizeof(char);
    cout<<"Number of elements in str: ";
    cout<<size<<endl;
    for(i=0;i<size;i++) {
        cout<<str[i];
    }
    cout<<endl;

    //顯示int型別陣列元素及其大小
    size=sizeof(int_values) / sizeof(int);
    cout<<"Number of elements in int_values: ";
    cout<<size<<endl;
    for(i=0;i<size;i++) {
        cout<<int_values[i]<<" ";
    }
    cout<<endl;

    //顯示float型別陣列元素及其大小
    size=sizeof(float_values) / sizeof(float);
    cout<<"Number of elements in float_values: ";
    cout<<size<<endl;
    for(i=0;i<size;i++) {
        cout<<float_values[i]<<" ";
    }
    cout<<endl;

    //顯示student型別陣列元素及其大小
    size=sizeof(st_arr) / sizeof(student);
    cout<<"Number of elements in st_arr: ";
    cout<<size<<endl;
    for(i=0;i<size;i++) {
        cout<<st_arr[i].num<<" ";
        cout<<st_arr[i].name<<" ";
        cout<<st_arr[i].grade<<endl;
    }
}
#include<iostream.h>
//add()函式的定義,其有返回值
double add(double x,double y)
{
    double z;
    z=x+y;
    cout<<x<<"+"<<y<<"="<<z<<endl;
    return(z);
}

main()
{
    double a=0.5,b=1.0;
	
    //以不同引數形式呼叫函式add()
    cout<<"add(1.5,2.5)="<<add(1.5,2.5)<<endl;
    cout<<"add(a,b)="<<add(a,b)<<endl;
    cout<<"add(2*a,a+b)="<<add(2*a,a+b)<<endl;
    cout<<"----------------------"<<endl;

    //以表示式方式呼叫函式add()
    double c=2*add(a,b);
    cout<<"c="<<c<<endl;
    cout<<"----------------------"<<endl;

    //以語句式方式呼叫函式add()
    add(2*a,b);
    cout<<"----------------------"<<endl;
 
    //用其他型別引數呼叫函式add()
    int n=1,m=2;
    cout<<"add("<<n<<","<<m<<")="<<add(n,m)<<endl;
}
#include<iostream.h>
//定義符號函式sgn(),其返回值為int型別
int sgn(double x)
{
    if (x>0) return(1);    //返回出口1
    if (x<0) return(-1);   //返回出口2
    return(0);          //返回出口3
}
//main()函式定義
main()
{
    double x;
    int i;
    for (i=0;i<=2;i++) {
        cout<<"x=";
        cin>>x;
        cout<<"sgn("<<x<<")="<<sgn(x)<<endl;
    }
}
#include<iostream.h>
//函式原型語句可以在這裡
//定義main()函式 
main()  
{
    //max()函式原型宣告語句
    float max(float,float);
	
    //變數宣告語句
    float a,b,Max;

    //輸入引數並計算
    cout<<"a=";
    cin>>a;
    cout<<"b=";
    cin>>b;
    Max=max(a,b);     //呼叫max()函式 
    cout<<"max("<<a<<","<<b<<")="<<Max<<endl;
}
//定義max()函式
float max(float x,float y)     //max()返回值型別為浮點型
{
    float z;
    z=(x>y)?x:y;
    return(z);
}

#include<iostream.h>
//定義f()函式
f(int x,int y)     //f()的引數以值方式傳遞
{
    ++x;
    --y;
    cout<<"x="<<x<<",y="<<y<<endl;
}
main()  {
    int a,b;

    //設定實際引數的值
    a=b=10;
    //以變數為引數呼叫f()函式
    f(a,b);

    //驗證實際引數的值
    cout<<"a="<<a<<",b="<<b<<endl;

    //以表示式引數形式呼叫f()函式
    f(2*a,a+b);
}
#include<iostream.h>

//定義公共結構型別
struct student {
       int  num;
       char  name[10];
       float maths;
       float physics;
       float chemistry;
       double  total;
};
 
//定義結構輸入函式
input_Rec(struct student *p)   //引數為student型別的結構指標變數
{
    cin>>p->num;
    cin>>p->name;
    cin>>p->maths;
    cin>>p->physics;
    cin>>p->chemistry;
}

//定義結構資料交換函式
swap_Rec(struct student *p1,struct student *p2)
{
    struct student x;

    //交換兩個記錄的資料
    x=*p1;
    *p1=*p2;
    *p2=x;
}

//輸出結構的值
put_Rec(struct student *p)
{
    cout<<p->num<<'\t';
    cout<<p->name<<'\t';
    cout<<p->maths<<'\t';
    cout<<p->physics<<'\t';
    cout<<p->chemistry<<'\t';
    cout<<p->total<<endl;
}

//定義main()函式
main() 
{
    int i,j;
    // 宣告結構指標變數和結構陣列 
    struct student *p1,a[3];  

    //輸入3個學生的資料並計算總成績
    cout<<"num\tname\tmaths\tphysics\tchemistry"<<endl;
    for (p1=a;p1<=a+2;p1++)  {
         input_Rec(p1);
         p1->total=p1->maths+p1->physics+p1->chemistry;
    }

    //對3個學生的資料排序
    for (i=0;i<=2;i++)  
         for (j=i+1;j<=2;j++)
             if (a[i].total<a[j].total)
                 swap_Rec(&a[i],&a[j]);   //交換兩個結構變數中的資料
     cout<<"-------------------"<<endl;	  //輸出一分界線

     //輸出排序後的結構陣列
    cout<<"num\tname\tmaths\tphysics\tchemistry\ttotal"<<endl;
    for (p1=a;p1<=a+2;p1++)
          put_Rec(p1);
}
#include<iostream.h>
//定義結構
struct student {
    char  name[10];
    float   grade;
};

//交換student型別的資料 
void swap(student &x,student &y)      //swap的引數為引用傳遞方式
{
    student temp;
    temp=x;
    x=y;
    y=temp;
}

//返回student型別的引用,求優者 
student& max(student &x,student &y)      //swap的引數為引用傳遞方式
{
    return (x.grade>y.grade?x:y);
}

//顯示student型別的資料 
void show(student &x)      //show的引數為引用傳遞方式
{
   cout<<x.name<<"  "<<x.grade<<endl;
}
void main()  
{
    student a={"ZhangHua",351.5},b={"WangJun",385};

    //顯示a和b的資料
    cout<<"a:";
    show(a);
    cout<<"b:";
    show(b);
    cout<<"------------------"<<endl;

    //交換a和b的資料,並顯示
    swap(a,b);    
    cout<<"a:";
show(a);
    cout<<"b:";
show(b);
    cout<<"------------------"<<endl;

    //計算和顯示成績高者
    student t=max(a,b);
    cout<<"Max:";
    show(t);
}
#include <iostream.h>
//引數帶有預設值的函式
disp(int x=1,int y=1,int z=1)
{
    cout<<"引數1: "<<x<<endl;
    cout<<"引數2: "<<y<<endl;
    cout<<"引數3: "<<z<<endl;
    cout<<"------------------"<<endl;
}

//main()函式中測試引數帶有預設值的函式disp()
void main()
{
    disp();
    disp(10);
    disp(10,20);
    disp(10,20,30);
    int a=1,b=2,c=3;
    disp(a,b,c);
}
#include <iostream.h>
//計算字串長度的函式
int str_len(const char *string)
{
    //char *temp=string; 編譯報錯!
    //*string='x';       編譯報錯!
    int i=0;
    while (*(string+i)!=NULL) 
        i++;
    return i;
}

//main()函式中測試str_len()
void main()
{
    char a[]="ABCDE";
    cout<<a<<"\t"<<str_len(a)<<endl;
    char *str="Hello!";
    cout<<str<<"\t"<<str_len(str)<<endl;
    cout<<"This is a test."<<"\t"<<str_len("This is a test.")<<endl;
}
#include<iostream.h>
void  disp(void);  //這個函式宣告語句不能少

//定義main()函式的引數和返回值型別是void型別
void  main(void)  
{
    //呼叫void型別函式
    disp();  
}
//以下定義disp()函式
void disp(void)  {
     cout<<" You are welcome."<<endl;
}
#include<iostream.h>
//函式原型語句
int  abs(int x);
long abs(long x);
float abs(float x);

//main()函式的定義
void main(void) 
{
    //宣告變數
    int i1=32767,i2=-32767;
    long l1=456789,l2=-456789;
    float x1=1.1234,x2=-1.1234;
   
    //直接在cout輸出中呼叫函式
    cout<<abs(i1)<<","<<abs(i2)<<endl;
    cout<<abs(l1)<<","<<abs(l2)<<endl;
    cout<<abs(x1)<<","<<abs(x2)<<endl;
}

//定義int型的abs()函式
int abs(int x) {
    if (x<0)
       return(-x);
    else
       return(x);
}

//定義long型的abs()函式 
long abs(long x) {
    if (x<0)
       return(-x);
    else
        return(x);
}

//定義float型 abs函式
float abs(float x) {
    if (x<0.0)
       return(-x);
    else
       return(x);
}
#include<iostream.h>
//max()為行內函數
inline int max(int x,int y)   //注意inline關鍵字
{
    return x>y?x:y;
}

//定義main()函式 
main()  
{
    int a=3,b=5,c;
    c=max(a,b);    
    cout<<"max("<<a<<","<<b<<")="<<c<<endl;
    cout<<"max("<<15<<","<<11<<")="<<max(15,11)<<endl;
}
#include<iostream.h>
main()  
{
    //函式原型宣告
    int fact(int x);
    int n,sn;

    //依次從鍵盤上輸入3個正整型資料計算它們的階乘
    for (int i=1;i<=3;i++)
    {
        cout<<i<<"   n=";
        cin>>n;
        sn=fact(n);
        cout<<n<<"!="<<sn<<endl;
    }
}

//以下是採用遞迴方法定義的fact()函式
int fact(int x)
{
   if (x==0) return(1);
      return(x*fact(x-1));  //此處又呼叫了它自身
}
#include<iostream.h>
//帶引數的main()函式
int main(int argc,char *argv[])
{
    int i;
    for(i=0;i<argc;i++)
       cout<<i<<":"<<argv[i]<<endl;
    return 0;
}
#include<iostream.h>
//用函式原型宣告要使用的函式
void show_array1(int*,int);
void show_array2(int a[],int);
void sort(int*,int);
main()
{
    //宣告陣列並初始化
    int a[]={2,4,6,1,3,5};
    int b[3][3]={{2,4,6},{1,3,5},{0,1,2}};
    
    //顯示陣列的值
    cout<<"show_array1(int*,int):"<<endl;
    show_array1(a,6);
    show_array1(&b[0][0],3*3);

    //用sort1排序並顯示
    cout<<"sort(int*,int) and show_array1(int*,int): "<<endl;
    sort(a,6);
    show_array1(a,6);
    sort(&b[0][0],3*3);
    show_array1(&b[0][0],9);

    //顯示陣列的值
    cout<<"show_array2(int a[],int):"<<endl;
    show_array2(a,6);
    show_array2(&b[0][0],3*3);
}

//顯示陣列,用指標當引數
void show_array1(int *p,int size) {
    for(int i=0;i<size;i++)
        cout<<*(p+i)<<" ";
    cout<<endl;
}

//顯示陣列,用陣列當引數
void show_array2(int a[],int size) {
    for(int i=0;i<size;i++)
        cout<<a[i]<<" ";
    cout<<endl;
}   

//對陣列按從大到小順序排序
void sort(int *p,int size) {
    int t;
    for (int i=0;i<size-1;i++)
        for (int j=i+1;j<size;j++)
            if (*(p+i)<=*(p+j))
            {
               t=*(p+i);
               *(p+i)=*(p+j);
               *(p+j)=t;
            }
}
#include<iostream.h>
//定義結構
struct student {
    char  name[10];
    float   grade;
};

//更改student資料的grade成員,引數形式為引用 
void change(student &x,float grade) 
{
    x.grade=grade;
}

//更改student資料的grade成員,引數形式為指標 
void change1(student *p,float grade)      
{
    p->grade=grade;
}

//更改student型別的資料,普通引數形式 
void change2(student x,float grade)      
{
    x.grade=grade;
}

//顯示student型別的資料,引數形式為引用
void show(student &x)      
{
    cout<<x.name<<"  "<<x.grade<<endl;
}

//在main()函式中,測試對結構的處理函式
void main()  
{
    student a={"ZhangHua",351.5};

    //顯示a的資料
    show(a);

    //用change修改分數,並顯示
	cout<<"change(student &x,float grade):"<<endl;
    change(a,360);
    show(a);

    //用change1修改分數,並顯示
	cout<<"change1(student *p,float grade):"<<endl;
    change1(&a,375);
    show(a);

    //用change2修改分數,並顯示
	cout<<"change2(student x,float grade):"<<endl;
    change2(a,380.5);
    show(a);
}
#include<iostream.h>
//定義函式計算陣列的和和平均值
void calculate(int a[],int size,int& sum,float& average)
{
    sum=0;
    for (int i=0;i<size;i++) {
        sum+=a[i];
    }
    average=sum/size;
}
//定義顯示陣列的函式
void put_arr(int a[],int size)
{
    for(int i=0;i<size;i++)
        cout<<a[i]<<" ";
    cout<<endl;
}
main()
{
    //宣告陣列並初始化
    int asize,bsize;
    int a[]={2,4,6,1,3,5};
    int b[]={1,3,5,7,9,11,13,15};
    
    //顯示陣列的值
    asize=sizeof(a)/sizeof(int);
	cout<<"put_arr(a,asize):"<<endl;
    put_arr(a,asize);
    bsize=sizeof(b)/sizeof(int);
	cout<<"put_arr(b,bsize):"<<endl;
    put_arr(b,bsize);

    //計算陣列的和和平均值
    float a_ave,b_ave;
    int a_sum,b_sum;
	cout<<"calculate(a,asize,a_sum,a_ave):"<<endl;
    calculate(a,asize,a_sum,a_ave);
    cout<<"a_sum="<<a_sum;
    cout<<" a_ave="<<a_ave<<endl;

	cout<<"calculate(b,bsize,b_sum,b_ave):"<<endl;
    calculate(b,bsize,b_sum,b_ave);
    cout<<"b_sum="<<b_sum;
    cout<<" b_ave="<<b_ave<<endl;
}
#include<iostream.h>

//引數為函式指標的函式
int get_result(int a, int b, int (*sub)(int,int))
{
    int r;
    r=sub(a,b);
    return r;
}
 
//計算最大值
int max(int a, int b)
{
    cout<<"In max"<<endl;
    return((a > b) ? a: b);
}

//計算最小值
int min(int a, int b)
{
    cout<<"In min"<<endl;
    return((a < b) ? a: b);
}

//求和
int sum(int a, int b)
{
    cout<<"In sum"<<endl;
    return(a+b);
}

//測試指向函式的指標
void main(void)
{
    int a,b,result;

    //測試3次
    for (int i=1;i<=3;i++) {
        cout<<"Input a and b :";
        cin>>a>>b;