c++經(jīng)典代碼大全[共125頁(yè)]

上傳人:gfy****yf 文檔編號(hào):155672360 上傳時(shí)間:2022-09-24 格式:DOC 頁(yè)數(shù):65 大?。?.73MB
收藏 版權(quán)申訴 舉報(bào) 下載
c++經(jīng)典代碼大全[共125頁(yè)]_第1頁(yè)
第1頁(yè) / 共65頁(yè)
c++經(jīng)典代碼大全[共125頁(yè)]_第2頁(yè)
第2頁(yè) / 共65頁(yè)
c++經(jīng)典代碼大全[共125頁(yè)]_第3頁(yè)
第3頁(yè) / 共65頁(yè)

下載文檔到電腦,查找使用更方便

12 積分

下載資源

還剩頁(yè)未讀,繼續(xù)閱讀

資源描述:

《c++經(jīng)典代碼大全[共125頁(yè)]》由會(huì)員分享,可在線閱讀,更多相關(guān)《c++經(jīng)典代碼大全[共125頁(yè)](65頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。

1、#include /包含 iostream.h 頭文件 /float 型溢出main() float x=3.5e14; coutx=xendl;/聲明變量,并初始化 coutx*x=x*xendl;int a=010,b=10,c=0X10; coutx*x*x=x*x*xendl;/以十進(jìn)制形式顯示數(shù)據(jù)coutDEC:;cout a=a;cout b=b; #include cout c=cendl; main()/以八進(jìn)制形式顯示數(shù)據(jù) /x,y 為操作數(shù), c 為運(yùn)算符coutOCT:; int x,y,z;coutoct; /指定八進(jìn)制輸出 char c1;cout a=xc1y; /

2、c1cout b=b;cout c=cendl; /多路選擇語(yǔ)句選擇不同表達(dá)式計(jì)算語(yǔ)句switch(c1) /以十六進(jìn)制形式顯示數(shù)據(jù) case +:coutx+y=x+yendl;coutHEX:; break;couthex; /指定十六進(jìn)制輸出 case -:coutx-y=x-yendl;cout a=a; break;cout b=b; case *:coutx*y=x*yendl;cout c=cendl; break;case /:coutx/y=x/yendl;/八、十和十六進(jìn)制數(shù)混合運(yùn)算并輸出 break;couta+b+c=; case %:coutx%y=x%yendl;c

3、outdec; /恢復(fù)十進(jìn)制輸出 break;couta+b+cendl; default :coutWrong !endl; /當(dāng)不符合上述情況時(shí)執(zhí)行本子句/測(cè)試八、十和十六進(jìn)制輸入 couta; coutb;coutc;coutDEC:decendl; /指定十進(jìn)制輸出couta=aendl; #includecoutb=bendl; float x=365.5; /聲明全局變量coutc=cdx; double w=x+y;coutdy; double x=1.414,y=1.732,z=3.14;coutdx+dy=dx+dyendl; coutinner:x=xendl;coutdx

4、-dy=dx-dyendl; coutinner:y=yendl;coutdx*dy=dx*dyendl; coutinner:z=zendl;coutdx/dy=dx/dyendlendl; coutouter:w=wendl;/coutfx%fy=fx%fyendl; Error! cout:x=:xendl; /訪問重名的全局變量/測(cè)試 float 和 double 類型數(shù)據(jù)的有效位 coutouter:x=xendl;fx=10.0;fy=6.0; coutouter:y=yendl;float fz=fx/fy; coutouter:w=wendl;dx=10.0;dy=6.0;do

5、uble dz=dx/dy; /coutinner:z=zendl; 無(wú)效coutfz=; cout:x=:xendl; /訪問重名的全局變量coutsetprecision(20)fx/fy=fzendl; coutdz=; #includemain() coutsetprecision(20)dx/dy=dzendlendl; /顯示 1,2,3.10for(int i=1;i=10;i+)1couti ; int s=0,n=0;cout=1;j-) while(n100);coutj ; couts=sendl;coutendl;/累加鍵盤輸入的數(shù)據(jù)/顯示 1,3,5.9 double

6、 x,sum=0.0;for(int k=1;k=10;k=k+2) do coutk ; coutx=;coutx;sum+=x;/顯示 ABC.Z while(x!=0);for(char c=A;c=Z;c+) coutsum=sumendl;coutc; coutendl;#include/顯示 0,0.1,0.2.1.0 main()for(float x=0;x=1.0;x=x+0.1) coutx ; /計(jì)算和打印打印乘法九九表coutendl; for (int i=1;i=9;i+) couti;/顯示 0,0.1,0.2.1.0 for (int j=1;j=9;j+)fo

7、r(float x1=0;x1=1.0+0.1/2;x1=x1+0.1) coutti*j=i*j;coutx1 ; coutendl;coutendl; /計(jì)算 s=1+2+3.+100int s=0; #includefor(int n=1;n=100;n+) main()s=s+n; couts=sendl; int x,sum=0; /定義標(biāo)號(hào) L1#include L1: coutx; if (x=-1)/計(jì)算 s=1+2+3.+100 goto L2; /無(wú)條件轉(zhuǎn)移語(yǔ)句,轉(zhuǎn)到 L2 語(yǔ)句處int s=0,n=1; elsewhile(n=100) sum+=x;s=s+n; got

8、o L1; /無(wú)條件轉(zhuǎn)移語(yǔ)句,轉(zhuǎn)到 L1 語(yǔ)句處n+; /定義標(biāo)號(hào) L2 L2: coutsum=sumendl;couts=sendl; /累加鍵盤輸入的數(shù)據(jù)double x,sum=0.0; #includecoutx; while(x!=0) /累加鍵盤輸入的數(shù)據(jù)sum+=x; double x,sum=0.0;coutx; coutx;coutsum=sumendl; if (x=0) break; sum+=x;#include main() coutsum=sumendl; /計(jì)算 s=1+2+3.+1002#include max=a0;main() for (i=0;i=4;i

9、+) int i; if (maxai)for (i=1;i=20;i+) max=ai;if (i%3=0) /能被 3 整除的整數(shù), 返回進(jìn)行下次循 index=i;環(huán) continue; couti ; coutnMax=max index=index; coutendl; #include #define size 5#include main()main() /聲明變量/聲明數(shù)組和變量 int i,j;int a5,i,sum; float t,asize;double avg;/從鍵盤上為數(shù)組賦值/從鍵盤上循環(huán)為數(shù)組賦值 for (i=0;isize;i+)for (i=0;i5;

10、i+) coutai=; coutaiai; cinai; /直接顯示數(shù)組元素 /對(duì)數(shù)組按從小到大順序排序couta0a1a2a3a4endl; for (i=0;isize-1;i+)for (j=i+1;jaj)for (i=0;i5;i+) coutai ; t=ai;coutendl; ai=aj;aj=t;/計(jì)算數(shù)組元素之和 ,并顯示計(jì)算結(jié)果 sum=a0+a1+a2+a3+a4;coutsum=sumendl; /顯示排序結(jié)果for (i=0;isize;i+)/利用循環(huán)計(jì)算數(shù)組的累加和 coutai ;for (sum=0,i=0;i5;i+) coutendl;sum+=ai;

11、/輸入要查找的數(shù)據(jù)/顯示累加和及平均值 int value;coutsum=sumendl; int found; /找到為 1,否則為 0avg=sum/5.0; int low,high,mid;coutavg=avgendl; for (i=1;i=3;i+) coutvalue=;#include cinvalue;main() /二分法查找數(shù)組 aint i,max,index,a5; found=0;low=0;/從鍵盤上為數(shù)組賦值 high=size-1;for (i=0;i=4;i+) while(low=high) coutaiai; if (amid=value) foun

12、d=1;/ 利用循環(huán)遍歷數(shù)組,找出最大值的元素及其下標(biāo) break;3 if (amidvalue) coutendl;low=mid+1; elsehigh=mid-1; /找出該數(shù)組的最大元素及其下標(biāo) int h,l,Max=a00;if (found) for (i=0;i2;i+) coutThe valu found for (j=0;j3;j+)at:amid=amidendl; else if (Maxaij) coutThe value is not found!endl; Max=aij; h=i; l=j;#include main() /聲明變量 coutMax:ahl=

13、ahlendl;int i,j; float t,a5; #includemain()/從鍵盤上為數(shù)組賦值 for (i=0;i=4;i+) /聲明字符數(shù)組和變量 char str6;coutaiai; /從鍵盤上輸入字符串coutstr;for (i=0;i=3;i+) coutstrendl;for (j=i+1;j=4;j+)if (ai=aj) /按數(shù)組和下標(biāo)變量?jī)煞N方式顯示字符數(shù)組 coutstrendl;t=ai; for (i=0;i6;i+)ai=aj; coutstri;aj=t; cout=0;i-)for (i=0;i=4;i+) coutstri;coutai ; co

14、utendl;#include /將字符數(shù)組變成大寫字母后輸出main() for (i=0;i=5;i+) stri-=32; /小寫字母轉(zhuǎn)換成大寫字母/聲明二維數(shù)組及變量 coutstrendl; /顯示字符串int a23,i,j; #include/從鍵盤上為數(shù)組 a 賦值 main()for (i=0;i2;i+) for (j=0;j3;j+) /聲明變量和指針變量 int a,b,c,*ip;coutaijaij; /指針變量 ip 指向變量 a a=100;ip=&a; /使指針變量 ip 指向變量 a/顯示數(shù)組 a couta=aendl;for (i=0;i2;i+) co

15、ut*ip=*ipendl;for (j=0;j3;j+) coutip=ipendl;coutaij ; /指針變量 ip 指向變量 b4ip=&b; /使指針變量 ip 指向變量 b cout*ip2=(*ip2)endl;b=200;coutb=bendl; /測(cè)試指針的自增自減運(yùn)算和組合運(yùn)算cout*ip=*ipendl; ip1+;coutip=ipendl; ip2+=4;cout*ip1=(*ip1)endl;/指針變量 ip 指向變量 c cout*ip2=(*ip2)endl;ip=&c; /使指針變量 ip 指向變量 b*ip=a+b; /測(cè)試指針變量之間的關(guān)系運(yùn)算cout

16、c=cip1;cout*ip=*ipendl; coutip1=nendl;coutip=ipendl; coutip2!=NULL=(ip2!=NULL)endl;#include /指針變量之間的減法main() n=ip2-ip1; coutip2-ip1=nendl;/聲明數(shù)組、變量和指針變量 int a23,i,j; #includeint* ip; main()/從鍵盤上為數(shù)組 a 賦值 /聲明字符型數(shù)組和指針變量for (i=0;i2;i+) /為數(shù)組 a 賦值 char str10;for (j=0;j3;j+) char *strip=str;coutaijaij; cout

17、str; /用字符數(shù)組輸入字符串coutstr=strendl;/利用下標(biāo)變量顯示數(shù)組 a coutstrip=stripendl;for (i=0;i2;i+) coutstrip=;for (j=0;jstrip; /用字符指針變量輸入字符串 coutstr=strendl;coutaij ; coutstrip=stripendl;coutendl; /利用指針變量改變其指向字符串的內(nèi)容 *(strip+2)=l;coutstr=strendl;/利用指針變量顯示數(shù)組 a coutstrip=stripendl;ip=&a00;for (i=0;i2;i+) /動(dòng)態(tài)為字符型指針變量分配內(nèi)

18、存for (j=0;j3;j+) strip=new char(100); coutstrip=;coutaijstrip; / 用字符指針變量輸入字符串coutip ; coutstr=strendl;cout*ipendl; coutstrip=stripendl;ip+; #include main() #include / 聲明用于存放運(yùn)動(dòng)員號(hào)碼的數(shù)組main() int h=1001,1002,1003,1004; / 聲明用于存放運(yùn)動(dòng)員成績(jī)的數(shù)組/聲明數(shù)組、變量和指針變量 float x=12.3,13.1,11.9,12.1;int a=1,2,3,4,5,6; /聲明用于存放運(yùn)

19、動(dòng)姓名的字符型指針數(shù)組int *ip1,*ip2; char *p=Wang hua,Zhang jian,Li wei,Hua ming;/i,j,it 是用做循環(huán)控制變量和臨時(shí)變量/測(cè)試指針的賦值運(yùn)算 int i,j,it;ip1=a; /ft 用做暫存變量ip2=ip1; float ft;cout*ip1=(*ip1)endl; /pt 為字符型指針變量用做暫存指針變量5char *pt; coutZbk.pages=Zbk.pagesendl;coutZbk.price=Zbk.priceendl;/用選擇法對(duì)數(shù)組 x 進(jìn)行排序, 并相應(yīng)調(diào)整數(shù)組 h 和 p 中的 cout-endl

20、;數(shù)據(jù)for (i=0;i=3;i+) /對(duì)結(jié)構(gòu)變量的輸入輸出for (j=i+1;j=3;j+) cout=xj) cinWbk.title;ft=xi,xi=xj,xj=ft; coutWbk.author;pt=pi,pi=pj,pj=pt; coutWbk.pages;coutWbk.price;for (i=0;i=3;i+) coutWbk:endl;couthi ,pi ,xiendl; coutWbk.title endl; coutWbk.authorendl;#include coutWbk.pagesendl;main() coutWbk.priceendl; cout-

21、endl;/聲明指針數(shù)組char *colors=Red,Blue,Yellow,Green; /結(jié)構(gòu)變量之間的相互賦值/指向指針的指針變量 books temp;char *pt; temp=Wbk;couttemp:endl;/通過指向指針的變量訪問其指向的內(nèi)容 couttemp.titleendl;pt=colors; couttemp.authorendl;for (int i=0;i=3;i+) couttemp.pagesendl;coutpt=ptendl; couttemp.priceendl;cout*pt=*ptendl; cout*pt=*ptendl;pt+; #inc

22、lude main() #include int i;main() /定義結(jié)構(gòu)類型 struct student /定義結(jié)構(gòu)類型 int num;struct books char name10; float maths;char title20; float physics;char author15; float chemistry;int pages; double total;float price; ; ;/聲明結(jié)構(gòu)數(shù)組 st/聲明結(jié)構(gòu)變量 student st3;struct books Zbk=VC+ ,Zhang,295,35.5;books Wbk; /從鍵盤上為結(jié)構(gòu)數(shù)組輸入

23、值cout num name maths physics chemistry/對(duì)結(jié)構(gòu)變量的輸出 endl;coutZbk:endl; for (i=0;i3;i+)coutZbk.title endl; coutZbk.authorendl; couti+1 ;coutZbk.pagessti.num;coutZbk.pricesti.name;cout-sti.maths;cinsti.physics;/對(duì)結(jié)組成員的運(yùn)算 cinsti.chemistry;Zbk.pages+=10; Zbk.price+=0.5;6/計(jì)算每個(gè)學(xué)生的總成績(jī) for (i=0;i3;i+) /定義結(jié)構(gòu)類型sti

24、.total=sti.maths+sti.physics+sti.chemistry; struct human char name10;/輸出結(jié)構(gòu)數(shù)組各元素的值 int sex;for (i=0;i3;i+) int age; ;coutsti: ;coutsti.numt; /聲明結(jié)構(gòu)變量和結(jié)構(gòu)指針 ,并初始化coutsti.namet; struct human x=WangPing,1,30,*p=&x;coutsti.mathst;coutsti.physicst; /利用結(jié)構(gòu)指針顯示結(jié)構(gòu)中的數(shù)據(jù)coutsti.chemistryt; cout(*p).name=(*p).namee

25、ndl;coutsti.totalendl; cout(*p).sex=(*p).sexendl; cout(*p).age=(*p).ageendl; cout-endl;#includemain() /利用 new 運(yùn)算符為 p 分配內(nèi)存 p=new human;/定義結(jié)構(gòu)類型struct human /從鍵盤上為 p 指向的結(jié)構(gòu)對(duì)象賦值char name10; coutname=;int sex; cinp-name;int age; coutsex=; cinp-sex;coutage=;/聲明結(jié)構(gòu)變量和結(jié)構(gòu)指針變量 ,并初始化 cinp-age;struct human x=Wang

26、Ping,1,30,*p=NULL; cout-endl;/結(jié)構(gòu)指針變量指向?qū)ο?/顯示 p 所指結(jié)構(gòu)對(duì)象的值p=&x; coutname=nameendl;coutsex=sexendl;/顯示結(jié)構(gòu)變量的值 coutage=ageendl;coutx.name=x.nameendl; cout-endl;coutx.sex=x.sexendl;coutx.age=x.ageendl; /顯示結(jié)構(gòu)變量的值coutx.name=x.nameendl;/利用結(jié)構(gòu)指針顯示結(jié)構(gòu)對(duì)象中的數(shù)據(jù) coutx.sex=x.sexendl;cout(*p).name=(*p).nameendl; coutx.a

27、ge=x.ageendl;cout(*p).sex=(*p).sexendl;cout(*p).age=(*p).ageendl; /釋放 p 指向的內(nèi)存coutname=nameendl; delete p;coutsex=sexendl; coutage=ageendl; #includemain()/通過結(jié)構(gòu)指針為結(jié)構(gòu)對(duì)象輸入數(shù)據(jù) cout(*p).name; struct human cout(*p).sex; int sex;cout(*p).age; ;/顯示結(jié)構(gòu)變量的值 /聲明結(jié)構(gòu)數(shù)組和結(jié)構(gòu)指針變量 ,并初始化coutx.name=x.nameendl; humancoutx.s

28、ex=x.sexendl; x=WeiPing,1,30,LiHua,1,25,LiuMin,0,23,*p=NUcoutx.age=x.ageendl; LL;include /用下標(biāo)變量的輸出結(jié)構(gòu)數(shù)組的元素main() for (int i=0;i3;i+)7 main()coutxi.namet; coutxi.sext; /定義 date 結(jié)構(gòu)coutxi.ageendl; struct date cout-endl; int year;int month;/用結(jié)構(gòu)指針輸出結(jié)構(gòu)數(shù)組的元素 int day;for (p=x;p=&x2;p+) ;coutnamet; /定義 baby 結(jié)

29、構(gòu)coutsext; struct baby coutageendl; int num; float weight; date birthday; / date 為結(jié)構(gòu)類型#include ;main() /聲明 baby 結(jié)構(gòu)變量并初始化/定義一個(gè)包含指針成員的結(jié)構(gòu)類型 baby b1=10001,10,2002,12,25;struct test char *str; /下列是 baby 結(jié)構(gòu)變量 b1 的引用。int *ip; coutb1.num=b1.numendl; x; coutb1.weight=b1.weightendl;coutb1.birthday.year=b1.bir

30、thday.yearendl;/使用結(jié)構(gòu)變量 x 中的整型指針 ip coutb1.birthday.month=b1.birthday.monthendl;x.ip=new int; /分配 1 個(gè)單元 coutb1.birthday.day=b1.birthday.dayendl;*(x.ip)=100; cout-endl;coutx.ip:x.ipt*(x.ip)endl;cout-endl; /聲明 baby 結(jié)構(gòu)變量 temp,并進(jìn)行賦值運(yùn)算delete x.ip; baby temp;x.ip=new int5; /分配 5 個(gè)單元 temp=b1;for(int i=0;i5;

31、i+) couttemp.num=temp.numendl;*(x.ip+i)=100+i; couttemp.weight=temp.weightendl;coutx.ip:endl; couttemp.birthday.year=temp.birthday.yearendl;for(i=0;i5;i+) couttemp.birthday.month=temp.birthday.monthendl;coutx.ip+it(*(x.ip+i)endl; couttemp.birthday.day=temp.birthday.dayendl;delete x.ip; cout-endl; #i

32、ncludemain()/使用結(jié)構(gòu)變量 x 中的字符型指針 str x.str=new char(A); /分配 1 個(gè)單元 /定義名為 list 的遞歸結(jié)構(gòu)coutx.str:(*x.str)endl; struct list cout-endl; char name10;delete x.str; int sex;x.str=new char5; /分配多個(gè)單元 int age;*x.str=G; list *next; /成員 next 為指向其自身結(jié)*(x.str+1)=o;構(gòu)的指針*(x.str+2)=o; ;*(x.str+3)=d;*(x.str+4)=0; /使用遞歸結(jié)構(gòu)變量c

33、outx.str:x.strendl; list L1=WeiPing,1,35.5,NULL;delete x.str; coutL1:endl;cout-endl; coutnametL1.nameendl;coutsextL1.sexendl;/在聲明結(jié)構(gòu)變量時(shí)初始化 coutagetL1.ageendl;test y=Very Good!,NULL; coutnexttL1.nextendl;couty.str:y.strendl; couty.ip:y.ipendl; #include main()#include 8int i; int math;/定義名為 student 的遞歸

34、結(jié)構(gòu) int computer;struct student float sum;char name10; student *forw; /forw 成員是前指針int math; student *next; /next 成員是后指針int computer; ;float sum;student *next; /next 成員是指向自身的結(jié)構(gòu)指 /用 student 聲明 3 個(gè)結(jié)構(gòu)指針變量針 struct student *head,*tail,*temp;/申請(qǐng)第 1 塊數(shù)據(jù),并設(shè)置各結(jié)構(gòu)指針的初值/用 student 聲明 3 個(gè)結(jié)構(gòu)指針變量 temp=new struct stud

35、ent; /申請(qǐng)內(nèi)存struct student *head,*tail,*temp; head=temp; / 頭指針tail=head; / 尾指針/申請(qǐng)第 1 塊數(shù)據(jù),并設(shè)置各結(jié)構(gòu)指針的初值 head-forw=NULL;temp=new struct student; /申請(qǐng)內(nèi)存head=temp; / 頭指針 /循環(huán)為鏈表記錄輸入數(shù)據(jù)tail=head; / 尾指針 couttname Math Computerendl;for (i=1;i+) /循環(huán)為鏈表輸入數(shù)據(jù) coutit;couttname Math Computertemp-name;for (i=1;i+) if (t

36、emp-name0!=*)coutitemp-name; cintemp-mathtemp-computer;if (temp-name0!=*) temp-sum=temp-math+temp-computer; temp-next=NULL;cintemp-mathtemp-computer; tail=temp; /設(shè)置鏈表尾指針temp-sum=temp-math+temp-computer; temp-next=NULL; elsetail=temp; /設(shè)置鏈表尾指針 / 以下是輸入結(jié)束處理else delete temp; tail-next=NULL;/ 以下是輸入結(jié)束處理 b

37、reak;delete temp; tail-next=NULL; /為下一個(gè)學(xué)生申請(qǐng)內(nèi)存break; temp-next=new struct student; temp-next-forw=temp; /設(shè)置前指針/為下一個(gè)學(xué)生申請(qǐng)內(nèi)存 temp=temp-next; /使處理指針 temp 指向新temp-next=new struct student;內(nèi)存塊temp=temp-next; / 使處理指針 temp 指向新內(nèi)存 塊 / 將鏈表數(shù)據(jù)從頭到尾打印出來couttail:endl;/將鏈表數(shù)據(jù)從頭到尾打印出來 temp=head;cout-endl; while (temp!=N

38、ULL) temp=head; coutname,math,;while (temp!=NULL) coutcomputer,sumendl;coutname,mathnext;coutcomputer,sumnext; / 將鏈表數(shù)據(jù)從尾到頭打印出來 couthead:endl;#include temp=tail;main() while (temp!=NULL) coutname,math,;int i; coutcomputer,sumforw;struct student char name10; 9#include ux.x=123.456;main() coutux.x=ux.x

39、endl; /輸出聯(lián)合變量 ux 的 x 成員 coutsizeof(ux)=sizeof(ux)endl;int i; /定義聯(lián)合類型 #includeunion utag main()char c; int k; /自定義類型float x; typedef int ARRAY_INT50; int i;ARRAY_INT a; /用自定義類型聲明數(shù)組變量 a/聲明聯(lián)合變量union utag u; /以下為數(shù)組 a 賦值,并打印for (i=0;i50;i+) / 使用聯(lián)合變量中的字符型成員 if (i%10=0) /每 10 個(gè)數(shù)換一次行u.c=*; coutendl;coutu.c=

40、u.cendl; ai=i;coutait;/ 使用聯(lián)合變量中的整型成員 u.k=1000; coutendl;coutu.k=u.kendl; #include/ 使用聯(lián)合變量中的浮點(diǎn)型成員 /定義結(jié)構(gòu)類型u.x=3.1416; struct studentcoutu.x=u.xendl; int num;/聲明聯(lián)合變量時(shí)初始化 char name20;utag u1=A; float grade;/同時(shí)引用聯(lián)合變量的各成員 void main(void)coutu1.c=u1.cendl; coutu1.k=u1.kendl; /聲明數(shù)組coutu1.x=u1.xendl; int i,s

41、ize; char str=This is a string.;#include int int_values = 51, 23, 2, 44, 45,0,11;main() float float_values = 15.1, 13.3, 22.2, 10.4, 1.5; student/定義結(jié)構(gòu)類型,并為聲明的結(jié)構(gòu)變量賦初值 st_arr=101,WangLin,92,102,LiPing,85,103,ZhaoMin,88;struct s_tag short i; /顯示 char 類型數(shù)組元素及其大小float x; size=sizeof(str) / sizeof(char);

42、sx=100,3.1416; coutNumber of elements in str: ;coutsizeendl;/定義聯(lián)合類型,并為聲明的聯(lián)合變量賦初值 for(i=0;isize;i+) union u_tag coutstri;short i; float x; coutendl; ux=1000;/顯示 int 類型數(shù)組元素及其大小/輸出結(jié)構(gòu)類型和結(jié)構(gòu)變量的有關(guān)信息 size=sizeof(int_values) / sizeof(int);coutsizeof(struct s_tag)=sizeof(struct s_tag)endl; coutNumber of eleme

43、nts in int_values: ;coutsx.i=sx.iendl; coutsizeendl;coutsx.x=sx.xendl; for(i=0;isize;i+) coutsizeof(sx)=sizeof(sx)endl; coutint_valuesi ;cout-endl; coutendl;/輸出聯(lián)合類型和聯(lián)合變量的有關(guān)信息coutsizeof(union u_tag)=sizeof(union u_tag)endl; /顯示 float 類型數(shù)組元素及其大小ux.i=200; size=sizeof(float_values) / sizeof(float);coutu

44、x.i=ux.iendl; /輸出聯(lián)合變量 ux 的 i 成員 coutNumber of elements in float_values: ;10coutsizeendl; double x;for(i=0;isize;i+) int i;coutfloat_valuesi ; for (i=0;i=2;i+) coutx=;coutx;coutsgn(x)=sgn(x)endl;/顯示 student 類型數(shù)組元素及其大小 size=sizeof(st_arr) / sizeof(student); coutNumber of elements in st_arr: ; #include

45、coutsizeendl; /函數(shù)原型語(yǔ)句可以在這里for(i=0;isize;i+) /定義 main() 函數(shù)coutst_arri.num ; main()coutst_arri.name ; coutst_arri.gradeendl; /max() 函數(shù)原型聲明語(yǔ)句 float max(float,float);#include /變量聲明語(yǔ)句/add()函數(shù)的定義,其有返回值 float a,b,Max;double add(double x,double y) /輸入?yún)?shù)并計(jì)算double z; couta;coutx+y=zendl; coutb; Max=max(a,b);

46、/調(diào)用 max()函數(shù)coutmax(a,b)=Maxendl;main() /定義 max()函數(shù)double a=0.5,b=1.0; float max(float x,float y) /max()返回值類型為浮點(diǎn)型/以不同參數(shù)形式調(diào)用函數(shù) add() float z;coutadd(1.5,2.5)=add(1.5,2.5)y)?x:y;coutadd(a,b)=add(a,b)endl; return(z);coutadd(2*a,a+b)=add(2*a,a+b)endl; cout-endl;#include/以表達(dá)式方式調(diào)用函數(shù) add() /定義 f() 函數(shù)double c

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

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!