C++經(jīng)典程序代碼大全_第1頁
C++經(jīng)典程序代碼大全_第2頁
C++經(jīng)典程序代碼大全_第3頁
C++經(jīng)典程序代碼大全_第4頁
C++經(jīng)典程序代碼大全_第5頁
已閱讀5頁,還剩114頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

.z.//根據(jù)半徑計算圓的周長和面積#include<iostream.h>constfloatPI=3.1416;//聲明常量(只讀變量)PI為3.1416floatfCir_L(float);//聲明自定義函數(shù)fCir_L()的原型floatfCir_S(float);//聲明自定義函數(shù)fCir_S()的原型//以下是main()函數(shù)main(){floatr,l,s;//聲明3個變量cout<<"r=";//顯示字符串cin>>r; //鍵盤輸入l=fCir_L(r);//計算圓的周長,賦值給變量ls=fCir_S(r);//計算圓的面積,賦值給變量scout<<"l="<<l;//顯示計算結(jié)果cout<<"\ns="<<s;}//定義計算圓的周長的函數(shù)fCir_L()floatfCir_L(float*){floatz=-1.0;//聲明局部變量if(*>=0.0)//如果參數(shù)大于0,則計算圓的周長z=2*PI**;return(z);//返回函數(shù)值}//定義計算圓的面積的函數(shù)fCir_S()floatfCir_S(float*){floatz=-1.0;//聲明局部變量if(*>=0.0)//如果參數(shù)大于0,則計算圓的面積z=PI****;return(z);//返回函數(shù)值}/*Program:P1-2.CPPWrittenby:HapDatewritten:02:11:10*/#include<iostream.h>voidmain(void){doubles1,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(){doubler=1.0;cout<<"r="<<r<<endl;doublel;l=2*3.1416*r;//計算圓的周長,賦值給變量lcout<<"l="<<l<<endl;//顯示圓的周長 doubles=3.1416*r*r;//計算圓的面積,賦值給變量scout<<"s="<<s<<endl;//顯示圓的面積cout<<"r=";//顯示提示輸入的信息cin>>r;//鍵盤輸入l=2*3.1416*r;//計算圓的周長,賦值給變量lcout<<"l="<<l<<endl;//顯示圓的周長s=3.1416*r*r;cout<<"s="<<s<<endl;//顯示圓的面積}#include<iostream.h>//包含iostream.h頭文件voidmain(){//輸出字符常量、變量和字符串charc1='A';cout<<'W';cout<<c1<<endl;cout<<"Thisisatest."<<endl;cout<<""<<endl;//輸出整型常量、變量和表達式intn=100;cout<<10;cout<<n;cout<<2*n<<endl;//輸出整型表達式cout<<""<<endl;//輸出浮點型常量、變量和表達式doublepi=3.1415926,r=10.0,s=pi*r*r;cout<<pi<<endl;cout<<r;cout<<s;cout<<2*r*pi<<endl;//輸出浮點型表達式cout<<""<<endl;//一個cout可以輸出多項數(shù)據(jù)cout<<'W'<<""<<c1<<endl;cout<<"Thisisatest."<<endl;cout<<"pi="<<pi<<"r="<<r<<"s="<<s<<endl;}#include<iostream.h>//包含iostream.h頭文件main(){//輸入輸出字符charc;cin>>c;cout<<"c="<<c<<endl;//輸入輸出整型數(shù)據(jù)intn;cin>>n;cout<<"n="<<n<<endl;//輸入輸出浮點型數(shù)據(jù)double*;cin>>*;cout<<"*="<<*<<endl;//輸入提示cout<<"n=";cin>>n;cout<<"n="<<n<<endl;//多項輸入cout<<"cn*"<<endl;cin>>c>>n>>*;cout<<"c="<<c<<"n="<<n<<"*="<<*<<endl;}#include<iostream.h>//包含iostream.h頭文件main(){//聲明整型變量inta,b;//從鍵盤上為整型變量賦值cout<<"a=";cin>>a;cout<<"b=";cin>>b;//整型數(shù)的算術(shù)運算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;//測試溢出shortn=32767,m;//n取short類型的最大值cout<<"n="<<n<<endl;m=n+1;//引起溢出cout<<"n+1="<<m<<endl;}#include<iostream.h>//包含iostream.h頭文件main(){//聲明變量,并初始化inta=010,b=10,c=0*10;//以十進制形式顯示數(shù)據(jù)cout<<"DEC:";cout<<"a="<<a;cout<<"b="<<b;cout<<"c="<<c<<endl;//以八進制形式顯示數(shù)據(jù)cout<<"OCT:";cout<<oct;//指定八進制輸出cout<<"a="<<a;cout<<"b="<<b;cout<<"c="<<c<<endl;//以十六進制形式顯示數(shù)據(jù)cout<<"HE*:";cout<<he*;//指定十六進制輸出cout<<"a="<<a;cout<<"b="<<b;cout<<"c="<<c<<endl;//八、十和十六進制數(shù)混合運算并輸出cout<<"a+b+c=";cout<<dec;//恢復(fù)十進制輸出cout<<a+b+c<<endl;//測試八、十和十六進制輸入cout<<"DEC:a=";cin>>a;cout<<"OCT:b=";cin>>b;cout<<"HE*: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型變量的聲明、輸入、計算和輸出floatf*,fy;cout<<"f*=";cin>>f*;cout<<"fy=";cin>>fy;cout<<f*<<"+"<<fy<<"="<<f*+fy<<endl;cout<<f*<<"-"<<fy<<"="<<f*-fy<<endl;cout<<f*<<"*"<<fy<<"="<<f**fy<<endl;cout<<f*<<"/"<<fy<<"="<<f*/fy<<endl<<endl;//cout<<f*<<"%"<<fy<<"="<<f*%fy<<endl;Error!//double型變量的聲明、輸入、計算和輸出floatd*,dy;cout<<"d*=";cin>>d*;cout<<"dy=";cin>>dy;cout<<d*<<"+"<<dy<<"="<<d*+dy<<endl;cout<<d*<<"-"<<dy<<"="<<d*-dy<<endl;cout<<d*<<"*"<<dy<<"="<<d**dy<<endl;cout<<d*<<"/"<<dy<<"="<<d*/dy<<endl<<endl;//cout<<f*<<"%"<<fy<<"="<<f*%fy<<endl;Error!//測試float和double類型數(shù)據(jù)的有效位f*=10.0;fy=6.0;floatfz=f*/fy;d*=10.0;dy=6.0;doubledz=d*/dy;cout<<"fz=";cout<<setprecision(20)<<f*<<"/"<<fy<<"="<<fz<<endl;cout<<"dz=";cout<<setprecision(20)<<d*<<"/"<<dy<<"="<<dz<<endl<<endl;;//float型溢出float*=3.5e14;cout<<"*="<<*<<endl;cout<<"***="<<***<<endl;cout<<"*****="<<*****<<endl;}#include<iostream.h>//包含iostream.h頭文件main(){//字符類型變量的聲明charc1='A';charc2;//字符數(shù)據(jù)的運算及輸出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<<"c1c2"<<endl;cin>>c1>>c2;cout<<"c1="<<c1<<"c2="<<c2<<endl;}#include<iostream.h>//包含iostream.h頭文件main(){charc1='\a',TAB='\t';//陣鈴一聲cout<<c1<<endl;//使用水平制表符cout<<1<<TAB<<2<<TAB<<3<<TAB<<4<<endl;//使用雙引號cout<<"Hesaid\"Thankyou\"."<<endl;//使用回車換行cout<<"abc\n"<<"def"<<'\n';}#include<iostream.h>//包含iostream.h頭文件main(){//聲明bool變量,并初始化boolflag1=false,flag2=true;//輸出布爾常量和變量cout<<"false:"<<false<<endl;cout<<"true:"<<true<<endl;cout<<"flag1="<<flag1<<endl;cout<<"flag2="<<flag2<<endl;//布爾變量的賦值和輸出int*=1;flag1=*>0;//存放關(guān)系運算結(jié)果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>constdoublePI=3.1416;//聲明常量(const變量)PI為3.1416main(){//聲明3個變量doubler,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(){//定義枚舉類型,并指定其枚舉元素的值enumcolor{RED=3,YELLOW=6,BLUE=9};//聲明枚舉變量a和b,并為枚舉變量a賦初值enumcolora=RED;colorb;//合法,與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也錯誤!//枚舉變量的關(guān)系運算b=BLUE; //枚舉變量的賦值運算cout<<"a<b="<<(a<b)<<endl;}#include<iostream.h>constdoublePI=3.1416;//聲明常量(const變量)PI為3.1416main(){//聲明3個變量doubler=3,l,s;//計算圓的周長l=2*PI*r;cout<<"l="<<l<<endl;//計算圓的面積s=PI*r*r;cout<<"s="<<s<<endl;//驗證賦值誤差intil,is;il=l;is=s;cout<<"il="<<il<<endl;cout<<"is="<<is<<endl;}#include<iostream.h>main(){//變量聲明charc;double*,y;//測試自增 cout<<"++EandE++:"<<endl;c='B';cout<<"c="<<++c<<endl;//輸出c=Cc='B';cout<<"c="<<c++<<endl;//輸出c=B*=1.5;y=5+++*;//加號后的空格不能少cout<<"y="<<y<<endl;//輸出y=7.5*=1.5;y=5+*++;cout<<"y="<<y<<endl;//輸出y=6.5cout<<""<<endl; //測試自減 cout<<"--EandE--:"<<endl;c='B';cout<<"c="<<--c<<endl;//輸出c=Ac='B';cout<<"c="<<c--<<endl;//輸出c=B*=1.5;y=5+--*;cout<<"y="<<y<<endl;//輸出y=5.5*=1.5;y=5+*--;cout<<"y="<<y<<endl;//輸出y=6.5}#include<iostream.h>main(){inta=3,b=2;//輸出關(guān)系表達式cout<<a<b<<endl;cout<<(a<b)<<(a>b)<<(a>=b)<<(a==b)<<(a!=b)<<endl;boolflag=2*a<b+10;cout<<"flag="<<flag;}#include<iostream.h>main(){floata=3.5,b=2.1,c=0;cout<<"a="<<a<<"b="<<b<<"c="<<c<<endl;//與運算cout<<"a&&b="<<(a&&b)<<endl;//輸出1cout<<"a&&c="<<(a&&c)<<endl;//輸出0//或運算cout<<"a||b="<<(a||b)<<endl;//輸出1cout<<"a||c="<<(a||c)<<endl;//輸出1//非運算cout<<"!a="<<!a<<endl<<"!c="<<!c<<endl;//輸出01//關(guān)系運算和邏輯運算boolflag=a>=0&&a<=5;//變量a在[0,5]區(qū)間內(nèi)cout<<"a=>0&&a<=5="<<flag<<endl;//輸出1//算術(shù)運算、關(guān)系運算和邏輯運算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(){inta=1,b=1,c=3;//顯示a,b,c的值cout<<"a="<<a<<"b="<<b<<"c="<<c<<endl;//計算顯示(1)b+=a+2*c%5;的結(jié)果b+=a+2*c%5; //相當(dāng)于表達式語句b=b+(a+2*c%5);cout<<"(1)b="<<b<<endl;//計算顯示(2)a<<=c-2*b;的結(jié)果a=1,b=1,c=3;a<<=c-2*b; //相當(dāng)于表達式語句a=a<<(c-2*b);cout<<"(2)a="<<a<<endl;//計算顯示(3)a*=b=c=3;的結(jié)果a=1,b=1,c=3;a*=b=c=3; //相當(dāng)于語句組c=3;b=c;a=a*b;cout<<"(3)a="<<a<<"b="<<b<<"c="<<c<<endl;//計算顯示(4)a+=b+=c;的結(jié)果a=1,b=1,c=3;a+=b+=c; //相當(dāng)于語句組b=b+c;a=a+b;cout<<"(4)a="<<a<<"b="<<b<<"c="<<c<<endl;//計算顯示(5)a-=b=++c+2;的結(jié)果a=1,b=1,c=3;a-=b=++c+2; //相當(dāng)于語句組++c;b=b+c+2;a=a-b;cout<<"(5)a="<<a<<"b="<<b<<"c="<<c<<endl;}#include<iostream.h>main(){//用sizeof計算各類種常量的字節(jié)長度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計算各類型變量的字節(jié)長度inti=100;charc='A';float*=3.1416;doublep=0.1;cout<<"sizeof(i)="<<sizeof(i)<<endl;cout<<"sizeof(c)="<<sizeof(c)<<endl;cout<<"sizeof(*)="<<sizeof(*)<<endl;cout<<"sizeof(p)="<<sizeof(p)<<endl;//用sizeof計算表達式的字節(jié)長度cout<<"sizeof(*+1.732)="<<sizeof(*+1.732)<<endl;//用sizeof計算各類型的字節(jié)長度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計算數(shù)組的字節(jié)長度charstr[]="Thisisatest.";inta[10]; double*y[10];cout<<"sizeof(str)="<<sizeof(str)<<endl;cout<<"sizeof(a)="<<sizeof(a)<<endl;cout<<"sizeof(*y)="<<sizeof(*y)<<endl;//用sizeof計算自定義類型的長度structst{shortnum;floatmath_grade;floatChinese_grade;floatsum_grade;};ststudent1;cout<<"sizeof(st)="<<sizeof(st)<<endl;cout<<"sizeof(student1)="<<sizeof(student1)<<endl;}#include<iostream.h>main(){//聲明變量語句中使用順序運算int*,y;//計算中使用順序運算*=50;y=(*=*-5,*/5);cout<<"*="<<*<<endl;cout<<"y="<<y<<endl;}#include<iostream.h>main(){//測試表達式類型的轉(zhuǎn)換intn=100,m;double*=3.791,y;cout<<"n**="<<n**<<endl;//賦值類型轉(zhuǎn)換m=*;y=n;cout<<"m="<<m<<endl;cout<<"y="<<y<<endl;//強制類型轉(zhuǎn)換cout<<"int(*)="<<int(*)<<endl;cout<<"(int)*="<<(int)*<<endl;cout<<"int(1.732+*)="<<int(1.732+*)<<endl;cout<<"(int)1.732+*="<<(int)1.723+*<<endl;cout<<"double(100)="<<double(100)<<endl;}#include<iostream.h>main(){floata,b,s;cout<<"ab"<<endl;cin>>a>>b; //利用cin從鍵盤上為變量a,b賦值s=a;if(a<b){s=b;//if語句中只有這一個語句,可省略花括號}s=s*s;//變量s中保存a,b中較大的一個數(shù)的平方cout<<"s="<<s;}#include<iostream.h>main(){int*,y;cout<<"*=";cin>>*;if(*<=0){//滿足條件執(zhí)行y=2**;cout<<"y="<<y;//輸出結(jié)果}else{//不滿足條件執(zhí)行y=***;cout<<"y="<<y;//輸出結(jié)果}}#include<iostream.h>main(){inta,b,c;intsmallest;cout<<"abc"<<endl;cin>>a>>b>>c;if(a<=b)//外層條件語句{if(a<=c)//內(nèi)層條件語句smallest=a;elsesmallest=c;}else{if(b<=c)//內(nèi)層條件語句smallest=b;elsesmallest=c;}cout<<"Smallest="<<smallest<<endl;}#include<iostream.h>main(){intscore;//從鍵盤上輸入分數(shù)cout<<"score=";cin>>score;//用帶elseif的條件語句判斷處理if(score<0||score>100){cout<<"Thescoreisoutofrange!"<<endl;}elseif(score>=90)cout<<"YourgradeisaA."<<endl;elseif(score>=80)cout<<"YourgradeisaB."<<endl;elseif(score>=70)cout<<"YourgradeisaC."<<endl;elseif(score>=60)cout<<"YourgradeisaD."<<endl;elsecout<<"YourgradeisaE."<<endl;}#include<iostream.h>main(){intn;cout<<"n=";cin>>n;if(n>=0&&n<=100&&n%2==0)cout<<"n="<<n<<endl;elsecout<<"The"<<n<<"isoutofrange!"<<endl;}#include<iostream.h>main(){inta,b,Ma*;//輸入數(shù)據(jù)cout<<"a=";cin>>a;cout<<"b=";cin>>b;//找出較大值Ma*=a>b"a:b;cout<<"Ma*="<<Ma*<<endl;}#include<iostream.h>main(){inta,b;//輸入數(shù)據(jù)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;}elsecout<<b<<"doesnotdivide"<<a<<endl;}#include<iostream.h>main(){//*,y為操作數(shù),c為運算符int*,y,z;charc1;cin>>*>>c1>>y;//c1//多路選擇語句選擇不同表達式計算語句switch(c1){case'+':cout<<*<<"+"<<y<<"="<<*+y<<endl;break;case'-':cout<<*<<"-"<<y<<"="<<*-y<<endl;break;case'*':cout<<*<<"*"<<y<<"="<<**y<<endl;break;case'/':cout<<*<<"/"<<y<<"="<<*/y<<endl;break;case'%':cout<<*<<"%"<<y<<"="<<*%y<<endl;break;default:cout<<"Wrong!"<<endl;//當(dāng)不符合上述情況時執(zhí)行本子句}}#include<iostream.h>float*=365.5;//聲明全局變量main(){int*=1,y=2;doublew=*+y;{double*=1.414,y=1.732,z=3.14;cout<<"inner:*="<<*<<endl;cout<<"inner:y="<<y<<endl;cout<<"inner:z="<<z<<endl;cout<<"outer:w="<<w<<endl;cout<<"::*="<<::*<<endl;//重名的全局變量}cout<<"outer:*="<<*<<endl;cout<<"outer:y="<<y<<endl;cout<<"outer:w="<<w<<endl;//cout<<"inner:z="<<z<<endl;無效cout<<"::*="<<::*<<endl;//重名的全局變量}#include<iostream.h>main(){//顯示1,2,3...10for(inti=1;i<=10;i++)cout<<i<<"";cout<<endl;//顯示10,9,8...1for(intj=10;j>=1;j--)cout<<j<<"";cout<<endl;//顯示1,3,5...9for(intk=1;k<=10;k=k+2)cout<<k<<"";cout<<endl;//顯示ABC...Zfor(charc='A';c<='Z';c++)cout<<c;cout<<endl;//顯示0,0.1,0.2...1.0for(float*=0;*<=1.0;*=*+0.1)cout<<*<<"";cout<<endl;//顯示0,0.1,0.2...1.0for(float*1=0;*1<=1.0+0.1/2;*1=*1+0.1)cout<<*1<<"";cout<<endl;//計算s=1+2+3...+100ints=0;for(intn=1;n<=100;n++)s=s+n;cout<<"s="<<s<<endl;}#include<iostream.h>main(){//計算s=1+2+3...+100ints=0,n=1;while(n<=100){s=s+n;n++;}cout<<"s="<<s<<endl;//累加鍵盤輸入的數(shù)據(jù)double*,sum=0.0;cout<<"*=";cin>>*;while(*!=0){sum+=*;cout<<"*=";cin>>*;}cout<<"sum="<<sum<<endl;}#include<iostream.h>main(){//計算s=1+2+3...+100ints=0,n=0;do{n++;s+=n;}while(n<100);cout<<"s="<<s<<endl;//累加鍵盤輸入的數(shù)據(jù)double*,sum=0.0;do{cout<<"*=";cin>>*;sum+=*;}while(*!=0);cout<<"sum="<<sum<<endl;}#include<iostream.h>main(){//計算和打印打印乘法九九表for(inti=1;i<=9;i++){cout<<i;for(intj=1;j<=9;j++)cout<<'\t'<<i<<"*"<<j<<"="<<i*j;cout<<endl;}}#include<iostream.h>main(){int*,sum=0;//定義標號L1L1:cout<<"*=";cin>>*;if(*==-1)gotoL2;//無條件轉(zhuǎn)移語句,轉(zhuǎn)到L2語句處elsesum+=*;gotoL1;//無條件轉(zhuǎn)移語句,轉(zhuǎn)到L1語句處//定義標號L2L2:cout<<"sum="<<sum<<endl;}#include<iostream.h>main(){//累加鍵盤輸入的數(shù)據(jù)double*,sum=0.0;while(1){cout<<"*=";cin>>*;if(*<=0)break;sum+=*;}cout<<"sum="<<sum<<endl;}#include<iostream.h>main(){inti;for(i=1;i<=20;i++){if(i%3==0)//能被3整除的整數(shù),返回進展下次循環(huán)continue;cout<<i<<"";}cout<<endl;}#include<iostream.h>main(){//聲明數(shù)組和變量inta[5],i,sum;doubleavg;//從鍵盤上循環(huán)為數(shù)組賦值for(i=0;i<5;i++){cout<<"a["<<i<<"]=";cin>>a[i];}//直接顯示數(shù)組元素cout<<a[0]<<a[1]<<a[2]<<a[3]<<a[4]<<endl;//利用for循環(huán)顯示數(shù)組各元素的值for(i=0;i<5;i++)cout<<a[i]<<"";cout<<endl;//計算數(shù)組元素之和,并顯示計算結(jié)果sum=a[0]+a[1]+a[2]+a[3]+a[4];cout<<"sum="<<sum<<endl;//利用循環(huán)計算數(shù)組的累加和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(){inti,ma*,inde*,a[5];//從鍵盤上為數(shù)組賦值for(i=0;i<=4;i++){cout<<"a["<<i<<"]=";cin>>a[i];}//利用循環(huán)遍歷數(shù)組,找出最大值的元素及其下標ma*=a[0];for(i=0;i<=4;i++){if(ma*<a[i]){ma*=a[i];inde*=i;}}cout<<"\nMa*="<<ma*<<"inde*="<<inde*;}#include<iostream.h>#definesize5main(){//聲明變量inti,j;floatt,a[size];//從鍵盤上為數(shù)組賦值for(i=0;i<size;i++){cout<<"a["<<i<<"]=";cin>>a[i];}//對數(shù)組按從小到大順序排序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;}//顯示排序結(jié)果for(i=0;i<size;i++)cout<<a[i]<<"";cout<<endl;//輸入要查找的數(shù)據(jù)intvalue;intfound;//找到為1,否則為0int low,high,mid;for(i=1;i<=3;i++){cout<<"value=";cin>>value;//二分法查找數(shù)組afound=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;elsehigh=mid-1;}if(found)cout<<"Thevalufoundat:a["<<mid<<"]="<<a[mid]<<endl;elsecout<<"The"<<value<<"isnotfound!"<<endl;}}#include<iostream.h>main(){ //聲明變量inti,j;floatt,a[5];//從鍵盤上為數(shù)組賦值for(i=0;i<=4;i++){cout<<"a["<<i<<"]=";cin>>a[i];}//對數(shù)組按從大到小順序排序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;}//顯示排序結(jié)果for(i=0;i<=4;i++)cout<<a[i]<<"";}#include<iostream.h>main(){//聲明二維數(shù)組及變量inta[2][3],i,j;//從鍵盤上為數(shù)組a賦值for(i=0;i<2;i++)for(j=0;j<3;j++){cout<<"a["<<i<<"]["<<j<<"]=";cin>>a[i][j];}//顯示數(shù)組afor(i=0;i<2;i++){for(j=0;j<3;j++){cout<<a[i][j]<<"";}cout<<endl;}//找出該數(shù)組的最大元素及其下標inth,l,Ma*=a[0][0];for(i=0;i<2;i++){for(j=0;j<3;j++){if(Ma*<a[i][j]){Ma*=a[i][j];h=i;l=j;}}}cout<<"Ma*:"<<"a["<<h<<"]["<<l<<"]="<<a[h][l]<<endl;}#include<iostream.h>main(){//聲明字符數(shù)組和變量charstr[6];inti;//從鍵盤上輸入字符串cout<<"str=";cin>>str;cout<<str<<endl;//按數(shù)組和下標變量兩種方式顯示字符數(shù)組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;//將字符數(shù)組變成大寫字母后輸出for(i=0;i<=5;i++)str[i]-=32; //小寫字母轉(zhuǎn)換成大寫字母cout<<str<<endl; //顯示字符串}#include<iostream.h>main(){//聲明變量和指針變量inta,b,c,*ip;//指針變量ip指向變量aa=100;ip=&a;//使指針變量ip指向變量acout<<"a="<<a<<endl;cout<<"*ip="<<*ip<<endl;cout<<"ip="<<ip<<endl;//指針變量ip指向變量bip=&b;//使指針變量ip指向變量bb=200;cout<<"b="<<b<<endl;cout<<"*ip="<<*ip<<endl;cout<<"ip="<<ip<<endl;//指針變量ip指向變量cip=&c;//使指針變量ip指向變量b*ip=a+b;cout<<"c="<<c<<endl;cout<<"*ip="<<*ip<<endl;cout<<"ip="<<ip<<endl;}#include<iostream.h>main(){//聲明數(shù)組、變量和指針變量inta[2][3],i,j;int*ip;//從鍵盤上為數(shù)組a賦值for(i=0;i<2;i++)//為數(shù)組a賦值for(j=0;j<3;j++){cout<<"a["<<i<<"]["<<j<<"]=";cin>>a[i][j];}//利用下標變量顯示數(shù)組afor(i=0;i<2;i++){for(j=0;j<3;j++){cout<<a[i][j]<<"";}cout<<endl;}//利用指針變量顯示數(shù)組aip=&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(){//聲明數(shù)組、變量和指針變量inta[]={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;//測試指針變量之間的關(guān)系運算intn=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(){//聲明字符型數(shù)組和指針變量charstr[10];char*strip=str;//輸入輸出cout<<"str=";cin>>str;//用字符數(shù)組輸入字符串cout<<"str="<<str<<endl;cout<<"strip="<<strip<<endl;cout<<"strip=";cin>>strip;//用字符指針變量輸入字符串cout<<"str="<<str<<endl;cout<<"strip="<<strip<<endl;//利用指針變量改變其指向字符串的內(nèi)容*(strip+2)='l';cout<<"str="<<str<<endl;cout<<"strip="<<strip<<endl;//動態(tài)為字符型指針變量分配內(nèi)存strip=newchar(100);cout<<"strip=";cin>>strip;//用字符指針變量輸入字符串cout<<"str="<<str<<endl;cout<<"strip="<<strip<<endl;}#include<iostream.h>main(){//聲明用于存放運發(fā)動的數(shù)組inth[]={1001,1002,1003,1004};//聲明用于存放運發(fā)動成績的數(shù)組float*[]={12.3,13.1,11.9,12.1};//聲明用于存放運動**的字符型指針數(shù)組char*p[]={"Wanghua","Zhangjian","Liwei","Huaming"};//i,j,it是用做循環(huán)控制變量和臨時變量inti,j,it;//ft用做暫存變量floatft;//pt為字符型指針變量用做暫存指針變量char*pt;//用選擇法對數(shù)組*進展排序,并相應(yīng)調(diào)整數(shù)組h和p中的數(shù)據(jù)for(i=0;i<=3;i++)for(j=i+1;j<=3;j++)if(*[i]>=*[j]){ft=*[i],*[i]=*[j],*[j]=ft;it=h[i],h[i]=h[j],h[j]=it;pt=p[i],p[i]=p[j],p[j]=pt;}//以下打印排序結(jié)果for(i=0;i<=3;i++)cout<<h[i]<<","<<p[i]<<","<<*[i]<<endl;}#include<iostream.h>main(){//聲明指針數(shù)組char*colors[]={"Red","Blue","Yellow","Green"};//指向指針的指針變量char**pt;//通過指向指針的變量其指向的內(nèi)容pt=colors;for(inti=0;i<=3;i++){cout<<"pt="<<pt<<endl;cout<<"*pt="<<*pt<<endl;cout<<"**pt="<<**pt<<endl;pt++;}}#include<iostream.h>main(){//定義構(gòu)造類型structbooks{chartitle[20];charauthor[15];intpages;floatprice;};//聲明構(gòu)造變量structbooksZbk={"VC++","Zhang",295,35.5};booksWbk;//對構(gòu)造變量的輸出cout<<"Zbk:"<<endl;cout<<Zbk.title<<endl;cout<<Zbk.author<<endl;cout<<Zbk.pages<<endl;cout<<Zbk.price<<endl;cout<<""<<endl;//對構(gòu)造成員的運算Zbk.pages+=10;Zbk.price+=0.5;cout<<"Zbk.pages="<<Zbk.pages<<endl;cout<<"Zbk.price="<<Zbk.price<<endl;cout<<""<<endl;//對構(gòu)造變量的輸入輸出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;//構(gòu)造變量之間的相互賦值bookstemp;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(){inti;//定義構(gòu)造類型structstudent{intnum;charname[10];floatmaths;floatphysics;floatchemistry;doubletotal;};//聲明構(gòu)造數(shù)組ststudentst[3];//從鍵盤上為構(gòu)造數(shù)組輸入值cout<<"numnamemathsphysicschemistry"<<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;}//計算每個學(xué)生的總成績for(i=0;i<3;i++)st[i].total=st[i].maths+st[i].physics+st[i].chemistry;//輸出構(gòu)造數(shù)組各元素的值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(){//定義構(gòu)造類型structhuman{charname[10];intse*;intage;};//聲明構(gòu)造變量和構(gòu)造指針變量,并初始化structhuman*={"WangPing",1,30},*p=NULL;//構(gòu)造指針變量指向?qū)ο髉=&*;//顯示構(gòu)造變量的值cout<<"*.name="<<*.name<<endl;cout<<"*.se*="<<*.se*<<endl;cout<<"*.age="<<*.age<<endl;//利用構(gòu)造指針顯示構(gòu)造對象中的數(shù)據(jù)cout<<"(*p).name="<<(*p).name<<endl;cout<<"(*p).se*="<<(*p).se*<<endl;cout<<"(*p).age="<<(*p).age<<endl;cout<<"p->name="<<p->name<<endl;cout<<"p->se*="<<p->se*<<endl;cout<<"p->age="<<p->age<<endl;//通過構(gòu)造指針為構(gòu)造對象輸入數(shù)據(jù)cout<<"name:";cin>>(*p).name;cout<<"se*:";cin>>(*p).se*;cout<<"age:";cin>>(*p).age;//顯示構(gòu)造變量的值cout<<"*.name="<<*.name<<endl;cout<<"*.se*="<<*.se*<<endl;cout<<"*.age="<<*.age<<endl;}include<iostream.h>main(){//定義構(gòu)造類型structhuman{charname[10];intse*;intage;};//聲明構(gòu)造變量和構(gòu)造指針,并初始化structhuman*={"WangPing",1,30},*p=&*;//利用構(gòu)造指針顯示構(gòu)造中的數(shù)據(jù)cout<<"(*p).name="<<(*p).name<<endl;cout<<"(*p).se*="<<(*p).se*<<endl;cout<<"(*p).age="<<(*p).age<<endl;cout<<""<<endl;//利用new運算符為p分配內(nèi)存p=newhuman;//從鍵盤上為p指向的構(gòu)造對象賦值cout<<"p->name=";cin>>p->name;cout<<"p->se*=";cin>>p->se*;cout<<"p->age=";cin>>p->age;cout<<""<<endl;//顯示p所指構(gòu)造對象的值cout<<"p->name="<<p->name<<endl;cout<<"p->se*="<<p->se*<<endl;cout<<"p->age="<<p->age<<endl;cout<<""<<endl;//顯示構(gòu)造變量的值cout<<"*.name="<<*.name<<endl;cout<<"*.se*="<<*.se*<<endl;cout<<"*.age="<<*.age<<endl;//釋放p指向的內(nèi)存deletep;}#include<iostream.h>main(){//定義構(gòu)造類型structhuman{charname[10];intse*;intage;};//聲明構(gòu)造數(shù)組和構(gòu)造指針變量,并初始化human*[]={{"WeiPing",1,30},{"LiHua",1,25},{"LiuMin",0,23}},*p=NULL;//用下標變量的輸出構(gòu)造數(shù)組的元素for(inti=0;i<3;i++){cout<<*[i].name<<'\t';cout<<*[i].se*<<'\t';cout<<*[i].age<<endl;}cout<<""<<endl;//用構(gòu)造指針輸出構(gòu)造數(shù)組的元素for(p=*;p<=&*[2

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論