歡迎來到裝配圖網(wǎng)! | 幫助中心 裝配圖網(wǎng)zhuangpeitu.com!
裝配圖網(wǎng)
ImageVerifierCode 換一換
首頁 裝配圖網(wǎng) > 資源分類 > DOC文檔下載  

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

  • 資源ID:155672360       資源大?。?span id="aenu2oo" class="font-tahoma">1.73MB        全文頁數(shù):65頁
  • 資源格式: DOC        下載積分:12積分
快捷下載 游客一鍵下載
會員登錄下載
微信登錄下載
三方登錄下載: 微信開放平臺登錄 支付寶登錄   QQ登錄   微博登錄  
二維碼
微信掃一掃登錄
下載資源需要12積分
郵箱/手機:
溫馨提示:
用戶名和密碼都是您填寫的郵箱或者手機號,方便查詢和重復(fù)下載(系統(tǒng)自動生成)
支付方式: 支付寶    微信支付   
驗證碼:   換一換

 
賬號:
密碼:
驗證碼:   換一換
  忘記密碼?
    
友情提示
2、PDF文件下載后,可能會被瀏覽器默認(rèn)打開,此種情況可以點擊瀏覽器菜單,保存網(wǎng)頁到桌面,就可以正常下載了。
3、本站不支持迅雷下載,請使用電腦自帶的IE瀏覽器,或者360瀏覽器、谷歌瀏覽器下載即可。
4、本站資源下載后的文檔和圖紙-無水印,預(yù)覽文檔經(jīng)過壓縮,下載后原文更清晰。
5、試題試卷類文檔,如果標(biāo)題沒有明確說明有答案則都視為沒有答案,請知曉。

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

#include <iostream.h> /包含 iostream.h 頭文件 /float 型溢出main() float x=3.5e14; cout<<"x="<<x<<endl;/聲明變量,并初始化 cout<<"x*x="<<x*x<<endl;int a=010,b=10,c=0X10; cout<<"x*x*x="<<x*x*x<<endl;/以十進(jìn)制形式顯示數(shù)據(jù)cout<<"DEC:"cout<<" a="<<a;cout<<" b="<<b; #include <iostream.h>cout<<" c="<<c<<endl; main()/以八進(jìn)制形式顯示數(shù)據(jù) /x,y 為操作數(shù), c 為運算符cout<<"OCT:" int x,y,z;cout<<oct; /指定八進(jìn)制輸出 char c1;cout<<" a="<<a; cin>>x>>c1>>y; /c1cout<<" b="<<b;cout<<" c="<<c<<endl; /多路選擇語句選擇不同表達(dá)式計算語句switch(c1) /以十六進(jìn)制形式顯示數(shù)據(jù) case '+':cout<<x<<"+"<<y<<"="<<x+y<<endl;cout<<"HEX:" break;cout<<hex; /指定十六進(jìn)制輸出 case '-':cout<<x<<"-"<<y<<"="<<x-y<<endl;cout<<" a="<<a; break;cout<<" b="<<b; case '*':cout<<x<<"*"<<y<<"="<<x*y<<endl;cout<<" c="<<c<<endl; break;case '/':cout<<x<<"/"<<y<<"="<<x/y<<endl;/八、十和十六進(jìn)制數(shù)混合運算并輸出 break;cout<<"a+b+c=" case '%':cout<<x<<"%"<<y<<"="<<x%y<<endl;cout<<dec; /恢復(fù)十進(jìn)制輸出 break;cout<<a+b+c<<endl; default :cout<<"Wrong !"<<endl; /當(dāng)不符合上述情況時執(zhí)行本子句/測試八、十和十六進(jìn)制輸入 cout<<"DEC:a=" cin>>a; cout<<"OCT:b=" cin>>b;cout<<"HEX:a=" cin>>c;cout<<"DEC:"<<dec<<endl; /指定十進(jìn)制輸出cout<<"a="<<a<<endl; #include<iostream.h>cout<<"b="<<b<<endl; float x=365.5; /聲明全局變量cout<<"c="<<c<<endl; main() int x=1,y=2;cin>>dx; double w=x+y;cout<<"dy=" cin>>dy; double x=1.414,y=1.732,z=3.14;cout<<dx<<"+"<<dy<<"="<<dx+dy<<endl; cout<<"inner:x="<<x<<endl;cout<<dx<<"-"<<dy<<"="<<dx-dy<<endl; cout<<"inner:y="<<y<<endl;cout<<dx<<"*"<<dy<<"="<<dx*dy<<endl; cout<<"inner:z="<<z<<endl;cout<<dx<<"/"<<dy<<"="<<dx/dy<<endl<<endl; cout<<"outer:w="<<w<<endl;/cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl; Error! cout<<":x="<<:x<<endl; /訪問重名的全局變量/測試 float 和 double 類型數(shù)據(jù)的有效位 cout<<"outer:x="<<x<<endl;fx=10.0;fy=6.0; cout<<"outer:y="<<y<<endl;float fz=fx/fy; cout<<"outer:w="<<w<<endl;dx=10.0;dy=6.0;double dz=dx/dy; /cout<<"inner:z="<<z<<endl; 無效cout<<"fz=" cout<<":x="<<:x<<endl; /訪問重名的全局變量cout<<setprecision(20)<<fx<<"/"<<fy<<"="<<fz<<endl; cout<<"dz=" #include<iostream.h>main() cout<<setprecision(20)<<dx<<"/"<<dy<<"="<<dz<<endl<<endl; /顯示 1,2,3.10for(int i=1;i<=10;i+)1cout<<i<<" " int s=0,n=0;cout<<endl; do n+;/顯示 10,9,8.1 s+=n;for(int j=10;j>=1;j-) while(n<100);cout<<j<<" " cout<<"s="<<s<<endl;cout<<endl;/累加鍵盤輸入的數(shù)據(jù)/顯示 1,3,5.9 double x,sum=0.0;for(int k=1;k<=10;k=k+2) do cout<<k<<" " cout<<"x="cout<<endl; cin>>x;sum+=x;/顯示 ABC.Z while(x!=0);for(char c='A'c<='Z'c+) cout<<"sum="<<sum<<endl;cout<<c; cout<<endl;#include<iostream.h>/顯示 0,0.1,0.2.1.0 main()for(float x=0;x<=1.0;x=x+0.1) cout<<x<<" " /計算和打印打印乘法九九表cout<<endl; for (int i=1;i<=9;i+) cout<<i;/顯示 0,0.1,0.2.1.0 for (int j=1;j<=9;j+)for(float x1=0;x1<=1.0+0.1/2;x1=x1+0.1) cout<<'t'<<i<<"*"<<j<<"="<<i*j;cout<<x1<<" " cout<<endl;cout<<endl; /計算 s=1+2+3.+100int s=0; #include<iostream.h>for(int n=1;n<=100;n+) main()s=s+n; cout<<"s="<<s<<endl; int x,sum=0; /定義標(biāo)號 L1#include<iostream.h> L1: cout<<"x="main() cin>>x; if (x=-1)/計算 s=1+2+3.+100 goto L2; /無條件轉(zhuǎn)移語句,轉(zhuǎn)到 L2 語句處int s=0,n=1; elsewhile(n<=100) sum+=x;s=s+n; goto L1; /無條件轉(zhuǎn)移語句,轉(zhuǎn)到 L1 語句處n+; /定義標(biāo)號 L2 L2: cout<<"sum="<<sum<<endl;cout<<"s="<<s<<endl; /累加鍵盤輸入的數(shù)據(jù)double x,sum=0.0; #include<iostream.h>cout<<"x=" main()cin>>x; while(x!=0) /累加鍵盤輸入的數(shù)據(jù)sum+=x; double x,sum=0.0;cout<<"x=" while(1) cin>>x; cout<<"x=" cin>>x;cout<<"sum="<<sum<<endl; if (x<=0) break; sum+=x;#include<iostream.h> main() cout<<"sum="<<sum<<endl; /計算 s=1+2+3.+1002#include<iostream.h> max=a0;main() for (i=0;i<=4;i+) int i; if (max<ai)for (i=1;i<=20;i+) max=ai;if (i%3=0) /能被 3 整除的整數(shù), 返回進(jìn)行下次循 index=i;環(huán) continue; cout<<i<<" " cout<<"nMax="<<max<<" index="<<index; cout<<endl; #include<iostream.h> #define size 5#include<iostream.h> main()main() /聲明變量/聲明數(shù)組和變量 int i,j;int a5,i,sum; float t,asize;double avg;/從鍵盤上為數(shù)組賦值/從鍵盤上循環(huán)為數(shù)組賦值 for (i=0;i<size;i+)for (i=0;i<5;i+) cout<<"a"<<i<<"=" cout<<"a"<<i<<"="cin>>ai; cin>>ai; /直接顯示數(shù)組元素 /對數(shù)組按從小到大順序排序cout<<a0<<a1<<a2<<a3<<a4<<endl; for (i=0;i<size-1;i+)for (j=i+1;j<size;j+)/利用 for 循環(huán)顯示數(shù)組各元素的值 if (ai>aj)for (i=0;i<5;i+) cout<<ai<<" " t=ai;cout<<endl; ai=aj;aj=t;/計算數(shù)組元素之和 ,并顯示計算結(jié)果 sum=a0+a1+a2+a3+a4;cout<<"sum="<<sum<<endl; /顯示排序結(jié)果for (i=0;i<size;i+)/利用循環(huán)計算數(shù)組的累加和 cout<<ai<<" "for (sum=0,i=0;i<5;i+) cout<<endl;sum+=ai;/輸入要查找的數(shù)據(jù)/顯示累加和及平均值 int value;cout<<"sum="<<sum<<endl; int found; /找到為 1,否則為 0avg=sum/5.0; int low,high,mid;cout<<"avg="<<avg<<endl; for (i=1;i<=3;i+) cout<<"value="#include<iostream.h> cin>>value;main() /二分法查找數(shù)組 aint i,max,index,a5; found=0;low=0;/從鍵盤上為數(shù)組賦值 high=size-1;for (i=0;i<=4;i+) while(low<=high) cout<<"a"<<i<<"=" mid=(high+low)/2;cin>>ai; if (amid=value) found=1;/ 利用循環(huán)遍歷數(shù)組,找出最大值的元素及其下標(biāo) break;3 if (amid<value) cout<<endl;low=mid+1; elsehigh=mid-1; /找出該數(shù)組的最大元素及其下標(biāo) int h,l,Max=a00;if (found) for (i=0;i<2;i+) cout<<"The valu found for (j=0;j<3;j+)at:a"<<mid<<"="<<amid<<endl; else if (Max<aij) cout<<"The "<<value<<" is not found!"<<endl; Max=aij; h=i; l=j;#include<iostream.h> main() /聲明變量 cout<<"Max:"<<"a"<<h<<""<<l<<"="<<ahl<<endl;int i,j; float t,a5; #include<iostream.h>main()/從鍵盤上為數(shù)組賦值 for (i=0;i<=4;i+) /聲明字符數(shù)組和變量 char str6;cout<<"a"<<i<<"=" int i;cin>>ai; /從鍵盤上輸入字符串cout<<"str="/對數(shù)組按從大到小順序排序 cin>>str;for (i=0;i<=3;i+) cout<<str<<endl;for (j=i+1;j<=4;j+)if (ai<=aj) /按數(shù)組和下標(biāo)變量兩種方式顯示字符數(shù)組 cout<<str<<endl;t=ai; for (i=0;i<6;i+)ai=aj; cout<<stri;aj=t; cout<<endl;/字符串反向輸出/顯示排序結(jié)果 for (i=5;i>=0;i-)for (i=0;i<=4;i+) cout<<stri;cout<<ai<<" " cout<<endl;#include<iostream.h> /將字符數(shù)組變成大寫字母后輸出main() for (i=0;i<=5;i+) stri-=32; /小寫字母轉(zhuǎn)換成大寫字母/聲明二維數(shù)組及變量 cout<<str<<endl; /顯示字符串int a23,i,j; #include<iostream.h>/從鍵盤上為數(shù)組 a 賦值 main()for (i=0;i<2;i+) for (j=0;j<3;j+) /聲明變量和指針變量 int a,b,c,*ip;cout<<"a"<<i<<""<<j<<"="cin>>aij; /指針變量 ip 指向變量 a a=100;ip=&a; /使指針變量 ip 指向變量 a/顯示數(shù)組 a cout<<"a="<<a<<endl;for (i=0;i<2;i+) cout<<"*ip="<<*ip<<endl;for (j=0;j<3;j+) cout<<"ip="<<ip<<endl;cout<<aij<<" " /指針變量 ip 指向變量 b4ip=&b; /使指針變量 ip 指向變量 b cout<<"*ip2="<<(*ip2)<<endl;b=200;cout<<"b="<<b<<endl; /測試指針的自增自減運算和組合運算cout<<"*ip="<<*ip<<endl; ip1+;cout<<"ip="<<ip<<endl; ip2+=4;cout<<"*ip1="<<(*ip1)<<endl;/指針變量 ip 指向變量 c cout<<"*ip2="<<(*ip2)<<endl;ip=&c; /使指針變量 ip 指向變量 b*ip=a+b; /測試指針變量之間的關(guān)系運算cout<<"c="<<c<<endl; int n=ip2>ip1;cout<<"*ip="<<*ip<<endl; cout<<"ip2>ip1="<<n<<endl;cout<<"ip="<<ip<<endl; cout<<"ip2!=NULL="<<(ip2!=NULL)<<endl;#include<iostream.h> /指針變量之間的減法main() n=ip2-ip1; cout<<"ip2-ip1="<<n<<endl;/聲明數(shù)組、變量和指針變量 int a23,i,j; #include<iostream.h>int* ip; main()/從鍵盤上為數(shù)組 a 賦值 /聲明字符型數(shù)組和指針變量for (i=0;i<2;i+) /為數(shù)組 a 賦值 char str10;for (j=0;j<3;j+) char *strip=str;cout<<"a"<<i<<""<<j<<"=" /輸入輸出cin>>aij; cout<<"str=" cin>>str; /用字符數(shù)組輸入字符串cout<<"str="<<str<<endl;/利用下標(biāo)變量顯示數(shù)組 a cout<<"strip="<<strip<<endl;for (i=0;i<2;i+) cout<<"strip="for (j=0;j<3;j+) cin>>strip; /用字符指針變量輸入字符串 cout<<"str="<<str<<endl;cout<<aij<<" " cout<<"strip="<<strip<<endl;cout<<endl; /利用指針變量改變其指向字符串的內(nèi)容 *(strip+2)='l'cout<<"str="<<str<<endl;/利用指針變量顯示數(shù)組 a cout<<"strip="<<strip<<endl;ip=&a00;for (i=0;i<2;i+) /動態(tài)為字符型指針變量分配內(nèi)存for (j=0;j<3;j+) strip=new char(100); cout<<"strip="cout<<"a"<<i<<""<<j<<"=" cin>>strip; / 用字符指針變量輸入字符串cout<<ip<<" " cout<<"str="<<str<<endl;cout<<*ip<<endl; cout<<"strip="<<strip<<endl;ip+; #include<iostream.h> main() #include<iostream.h> / 聲明用于存放運動員號碼的數(shù)組main() int h=1001,1002,1003,1004; / 聲明用于存放運動員成績的數(shù)組/聲明數(shù)組、變量和指針變量 float x=12.3,13.1,11.9,12.1;int a=1,2,3,4,5,6; /聲明用于存放運動姓名的字符型指針數(shù)組int *ip1,*ip2; char *p="Wang hua","Zhang jian","Li wei","Hua ming"/i,j,it 是用做循環(huán)控制變量和臨時變量/測試指針的賦值運算 int i,j,it;ip1=a; /ft 用做暫存變量ip2=ip1; float ft;cout<<"*ip1="<<(*ip1)<<endl; /pt 為字符型指針變量用做暫存指針變量5char *pt; cout<<"Zbk.pages="<<Zbk.pages<<endl;cout<<"Zbk.price="<<Zbk.price<<endl;/用選擇法對數(shù)組 x 進(jìn)行排序, 并相應(yīng)調(diào)整數(shù)組 h 和 p 中的 cout<<"-"<<endl;數(shù)據(jù)for (i=0;i<=3;i+) /對結(jié)構(gòu)變量的輸入輸出for (j=i+1;j<=3;j+) cout<<"Wbk.title ="if (xi>=xj) cin>>Wbk.title;ft=xi,xi=xj,xj=ft; cout<<"Wbk.author="it=hi,hi=hj,hj=it; cin>>Wbk.author;pt=pi,pi=pj,pj=pt; cout<<"Wbk.pages=" cin>>Wbk.pages;cout<<"Wbk.price="/以下打印排序結(jié)果 cin>>Wbk.price;for (i=0;i<=3;i+) cout<<"Wbk:"<<endl;cout<<hi<<" ,"<<pi<<" ,"<<xi<<endl; cout<<Wbk.title <<endl; cout<<Wbk.author<<endl;#include<iostream.h> cout<<Wbk.pages<<endl;main() cout<<Wbk.price<<endl; cout<<"-"<<endl;/聲明指針數(shù)組char *colors="Red","Blue","Yellow","Green" /結(jié)構(gòu)變量之間的相互賦值/指向指針的指針變量 books temp;char *pt; temp=Wbk;cout<<"temp:"<<endl;/通過指向指針的變量訪問其指向的內(nèi)容 cout<<temp.title<<endl;pt=colors; cout<<temp.author<<endl;for (int i=0;i<=3;i+) cout<<temp.pages<<endl;cout<<"pt="<<pt<<endl; cout<<temp.price<<endl;cout<<"*pt="<<*pt<<endl; cout<<"*pt="<<*pt<<endl;pt+; #include<iostream.h> main() #include<iostream.h> 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ù)組輸入值cout<<" num name maths physics chemistry/對結(jié)構(gòu)變量的輸出 "<<endl;cout<<"Zbk:"<<endl; for (i=0;i<3;i+)cout<<Zbk.title <<endl; cout<<Zbk.author<<endl; cout<<i+1<<" "cout<<Zbk.pages<<endl; cin>>sti.num;cout<<Zbk.price<<endl; cin>>sti.name;cout<<"-"<<endl; cin>>sti.maths;cin>>sti.physics;/對結(jié)組成員的運算 cin>>sti.chemistry;Zbk.pages+=10; Zbk.price+=0.5;6/計算每個學(xué)生的總成績 for (i=0;i<3;i+) /定義結(jié)構(gòu)類型sti.total=sti.maths+sti.physics+sti.chemistry; struct human char name10;/輸出結(jié)構(gòu)數(shù)組各元素的值 int sex;for (i=0;i<3;i+) int age; ;cout<<"st"<<i<<": "cout<<sti.num<<'t' /聲明結(jié)構(gòu)變量和結(jié)構(gòu)指針 ,并初始化cout<<sti.name<<'t' struct human x="WangPing",1,30,*p=&x;cout<<sti.maths<<'t'cout<<sti.physics<<'t' /利用結(jié)構(gòu)指針顯示結(jié)構(gòu)中的數(shù)據(jù)cout<<sti.chemistry<<'t' cout<<"(*p).name="<<(*p).name<<endl;cout<<sti.total<<endl; cout<<"(*p).sex="<<(*p).sex<<endl; cout<<"(*p).age="<<(*p).age<<endl; cout<<"-"<<endl;#include<iostream.h>main() /利用 new 運算符為 p 分配內(nèi)存 p=new human;/定義結(jié)構(gòu)類型struct human /從鍵盤上為 p 指向的結(jié)構(gòu)對象賦值char name10; cout<<"p->name="int sex; cin>>p->name;int age; cout<<"p->sex=" cin>>p->sex;cout<<"p->age="/聲明結(jié)構(gòu)變量和結(jié)構(gòu)指針變量 ,并初始化 cin>>p->age;struct human x="WangPing",1,30,*p=NULL; cout<<"-"<<endl;/結(jié)構(gòu)指針變量指向?qū)ο?/顯示 p 所指結(jié)構(gòu)對象的值p=&x; cout<<"p->name="<<p->name<<endl;cout<<"p->sex="<<p->sex<<endl;/顯示結(jié)構(gòu)變量的值 cout<<"p->age="<<p->age<<endl;cout<<"x.name="<<x.name<<endl; cout<<"-"<<endl;cout<<"x.sex="<<x.sex<<endl;cout<<"x.age="<<x.age<<endl; /顯示結(jié)構(gòu)變量的值cout<<"x.name="<<x.name<<endl;/利用結(jié)構(gòu)指針顯示結(jié)構(gòu)對象中的數(shù)據(jù) cout<<"x.sex="<<x.sex<<endl;cout<<"(*p).name="<<(*p).name<<endl; cout<<"x.age="<<x.age<<endl;cout<<"(*p).sex="<<(*p).sex<<endl;cout<<"(*p).age="<<(*p).age<<endl; /釋放 p 指向的內(nèi)存cout<<"p->name="<<p->name<<endl; delete p;cout<<"p->sex="<<p->sex<<endl; cout<<"p->age="<<p->age<<endl; #include<iostream.h>main()/通過結(jié)構(gòu)指針為結(jié)構(gòu)對象輸入數(shù)據(jù) cout<<"name:" /定義結(jié)構(gòu)類型cin>>(*p).name; struct human cout<<"sex:" char name10;cin>>(*p).sex; int sex;cout<<"age:" int age;cin>>(*p).age; ;/顯示結(jié)構(gòu)變量的值 /聲明結(jié)構(gòu)數(shù)組和結(jié)構(gòu)指針變量 ,并初始化cout<<"x.name="<<x.name<<endl; humancout<<"x.sex="<<x.sex<<endl; x="WeiPing",1,30,"LiHua",1,25,"LiuMin",0,23,*p=NUcout<<"x.age="<<x.age<<endl; LL;include<iostream.h> /用下標(biāo)變量的輸出結(jié)構(gòu)數(shù)組的元素main() for (int i=0;i<3;i+)7 main()cout<<xi.name<<'t' cout<<xi.sex<<'t' /定義 date 結(jié)構(gòu)cout<<xi.age<<endl; struct date cout<<"-"<<endl; int year;int month;/用結(jié)構(gòu)指針輸出結(jié)構(gòu)數(shù)組的元素 int day;for (p=x;p<=&x2;p+) ;cout<<p->name<<'t' /定義 baby 結(jié)構(gòu)cout<<p->sex<<'t' struct baby cout<<p->age<<endl; int num; float weight; date birthday; / date 為結(jié)構(gòu)類型#include<iostream.h> ;main() /聲明 baby 結(jié)構(gòu)變量并初始化/定義一個包含指針成員的結(jié)構(gòu)類型 baby b1=10001,10,2002,12,25;struct test char *str; /下列是 baby 結(jié)構(gòu)變量 b1 的引用。int *ip; cout<<"b1.num="<<b1.num<<endl; x; cout<<"b1.weight="<<b1.weight<<endl;cout<<"b1.birthday.year="<<b1.birthday.year<<endl;/使用結(jié)構(gòu)變量 x 中的整型指針 ip cout<<"b1.birthday.month="<<b1.birthday.month<<endl;x.ip=new int; /分配 1 個單元 cout<<"b1.birthday.day="<<b1.birthday.day<<endl;*(x.ip)=100; cout<<"-"<<endl;cout<<"x.ip:"<<x.ip<<'t'<<*(x.ip)<<endl;cout<<"-"<<endl; /聲明 baby 結(jié)構(gòu)變量 temp,并進(jìn)行賦值運算delete x.ip; baby temp;x.ip=new int5; /分配 5 個單元 temp=b1;for(int i=0;i<5;i+) cout<<"temp.num="<<temp.num<<endl;*(x.ip+i)=100+i; cout<<"temp.weight="<<temp.weight<<endl;cout<<"x.ip:"<<endl; cout<<"temp.birthday.year="<<temp.birthday.year<<endl;for(i=0;i<5;i+) cout<<"temp.birthday.month="<<temp.birthday.month<<endl;cout<<x.ip+i<<'t'<<(*(x.ip+i)<<endl; cout<<"temp.birthday.day="<<temp.birthday.day<<endl;delete x.ip; cout<<"-"<<endl; #include<iostream.h>main()/使用結(jié)構(gòu)變量 x 中的字符型指針 str x.str=new char('A'); /分配 1 個單元 /定義名為 list 的遞歸結(jié)構(gòu)cout<<"x.str:"<<(*x.str)<<endl; struct list cout<<"-"<<endl; char name10;delete x.str; int sex;x.str=new char5; /分配多個單元 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)變量cout<<"x.str:"<<x.str<<endl; list L1="WeiPing",1,35.5,NULL;delete x.str; cout<<"L1:"<<endl;cout<<"-"<<endl; cout<<"namet"<<L1.name<<endl;cout<<"sext"<<L1.sex<<endl;/在聲明結(jié)構(gòu)變量時初始化 cout<<"aget"<<L1.age<<endl;test y="Very Good!",NULL; cout<<"nextt"<<L1.next<<endl;cout<<"y.str:"<<y.str<<endl; cout<<"y.ip:"<<y.ip<<endl; #include<iostream.h> main()#include<iostream.h> 8int i; int math;/定義名為 student 的遞歸結(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 個結(jié)構(gòu)指針變量針 struct student *head,*tail,*temp;/申請第 1 塊數(shù)據(jù),并設(shè)置各結(jié)構(gòu)指針的初值/用 student 聲明 3 個結(jié)構(gòu)指針變量 temp=new struct student; /申請內(nèi)存struct student *head,*tail,*temp; head=temp; / 頭指針tail=head; / 尾指針/申請第 1 塊數(shù)據(jù),并設(shè)置各結(jié)構(gòu)指針的初值 head->forw=NULL;temp=new struct student; /申請內(nèi)存head=temp; / 頭指針 /循環(huán)為鏈表記錄輸入數(shù)據(jù)tail=head; / 尾指針 cout<<"tname Math Computer"<<endl;for (i=1;i+) /循環(huán)為鏈表輸入數(shù)據(jù) cout<<i<<"t"cout<<"tname Math Computer"<<endl; cin>>temp->name;for (i=1;i+) if (temp->name0!='*')cout<<i<<"t" cin>>temp->name; cin>>temp->math>>temp->computer;if (temp->name0!='*') temp->sum=temp->math+temp->computer; temp->next=NULL;cin>>temp->math>>temp->computer; tail=temp; /設(shè)置鏈表尾指針temp->sum=temp->math+temp->computer; temp->next=NULL; elsetail=temp; /設(shè)置鏈表尾指針 / 以下是輸入結(jié)束處理else delete temp; tail->next=NULL;/ 以下是輸入結(jié)束處理 break;delete temp; tail->next=NULL; /為下一個學(xué)生申請內(nèi)存break; temp->next=new struct student; temp->next->forw=temp; /設(shè)置前指針/為下一個學(xué)生申請內(nèi)存 temp=temp->next; /使處理指針 temp 指向新temp->next=new struct student;內(nèi)存塊temp=temp->next; / 使處理指針 temp 指向新內(nèi)存 塊 / 將鏈表數(shù)據(jù)從頭到尾打印出來cout<<"head->tail:"<<endl;/將鏈表數(shù)據(jù)從頭到尾打印出來 temp=head;cout<<"-"<<endl; while (temp!=NULL) temp=head; cout<<temp->name<<","<<temp->math<<","while (temp!=NULL) cout<<temp->computer<<","<<temp->sum<<endl;cout<<temp->name<<","<<temp->math<<"," temp=temp->next;cout<<temp->computer<<","<<temp->sum<<endl; temp=temp->next; / 將鏈表數(shù)據(jù)從尾到頭打印出來 cout<<"tail->head:"<<endl;#include<iostream.h> temp=tail;main() while (temp!=NULL) cout<<temp->name<<","<<temp->math<<","int i; cout<<temp->computer<<","<<temp->sum<<endl;/定義名為 student 的遞歸結(jié)構(gòu) temp=temp->forw;struct student char name10; 9#include<iostream.h> ux.x=123.456;main() cout<<"ux.x="<<ux.x<<endl; /輸出聯(lián)合變量 ux 的 x 成員 cout<<"sizeof(ux)="<<sizeof(ux)<<endl;int i; /定義聯(lián)合類型 #include<iostream.h>union 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;i<50;i+) / 使用聯(lián)合變量中的字符型成員 if (i%10=0) /每 10 個數(shù)換一次行u.c='*' cout<<endl;cout<<"u.c="<<u.c<<endl; ai=i;cout<<ai<<"t"/ 使用聯(lián)合變量中的整型成員 u.k=1000; cout<<endl;cout<<"u.k="<<u.k<<endl; #include<iostream.h>/ 使用聯(lián)合變量中的浮點型成員 /定義結(jié)構(gòu)類型u.x=3.1416; struct studentcout<<"u.x="<<u.x<<endl; int num;/聲明聯(lián)合變量時初始化 char name20;utag u1='A' float grade;/同時引用聯(lián)合變量的各成員 void main(void)cout<<"u1.c="<<u1.c<<endl; cout<<"u1.k="<<u1.k<<endl; /聲明數(shù)組cout<<"u1.x="<<u1.x<<endl; int i,size; char str="This is a string."#include<iostream.h> 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); sx=100,3.1416; cout<<"Number of elements in str: "cout<<size<<endl;/定義聯(lián)合類型,并為聲明的聯(lián)合變量賦初值 for(i=0;i<size;i+) union u_tag cout<<stri;short i; float x; cout<<endl; ux=1000;/顯示 int 類型數(shù)組元素及其大小/輸出結(jié)構(gòu)類型和結(jié)構(gòu)變量的有關(guān)信息 size=sizeof(int_values) / sizeof(int);cout<<"sizeof(struct s_tag)="<<sizeof(struct s_tag)<<endl; cout<<"Number of elements in int_values: "cout<<"sx.i="<<sx.i<<endl; cout<<size<<endl;cout<<"sx.x="<<sx.x<<endl; for(i=0;i<size;i+) cout<<"sizeof(sx)="<<sizeof(sx)<<endl; cout<<int_valuesi<<" "cout<<"-"<<endl; cout<<endl;/輸出聯(lián)合類型和聯(lián)合變量的有關(guān)信息cout<<"sizeof(union u_tag)="<<sizeof(union u_tag)<<endl; /顯示 float 類型數(shù)組元素及其大小ux.i=200; size=sizeof(float_values) / sizeof(float);cout<<"ux.i="<<ux.i<<endl; /輸出聯(lián)合變量 ux 的 i 成員 cout<<"Number of elements in float_values: "10cout<<size<<endl; double x;for(i=0;i<size;i+) int i;cout<<float_valuesi<<" " for (i=0;i<=2;i+) cout<<"x="cout<<endl; cin>>x;cout<<"sgn("<<x<<")="<<sgn(x)<<endl;/顯示 student 類型數(shù)組元素及其大小 size=sizeof(st_arr) / sizeof(student); cout<<"Number of elements in st_arr: " #include<iostream.h>cout<<size<<endl; /函數(shù)原型語句可以在這里for(i=0;i<size;i+) /定義 main() 函數(shù)cout<<st_arri.num<<" " main()cout<<st_arri.name<<" " cout<<st_arri.grade<<endl; /max() 函數(shù)原型聲明語句 float max(float,float);#include<iostream.h> /變量聲明語句/add()函數(shù)的定義,其有返回值 float a,b,Max;double add(double x,double y) /輸入?yún)?shù)并計算double z; cout<<"a="z=x+y; cin>>a;cout<<x<<"+"<<y<<"="<<z<<endl; cout<<"b="return(z); cin>>b; Max=max(a,b); /調(diào)用 max()函數(shù)cout<<"max("<<a<<","<<b<<")="<<Max<<endl;main() /定義 max()函數(shù)double a=0.5,b=1.0; float max(float x,float y) /max()返回值類型為浮點型/以不同參數(shù)形式調(diào)用函數(shù) add() float z;cout<<"add(1.5,2.5)="<<add(1.5,2.5)<<endl; z=(x>y)?x:y;cout<<"add(a,b)="<<add(a,b)<<endl; return(z);cout<<"add(2*a,a+b)="<<add(2*a,a+b)<<endl; cout<<"-"<<endl;#include<iostream.h>/以表達(dá)式方式調(diào)用函數(shù) add() /定義 f() 函數(shù)double c

注意事項

本文(c++經(jīng)典代碼大全[共125頁])為本站會員(gfy****yf)主動上傳,裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對上載內(nèi)容本身不做任何修改或編輯。 若此文所含內(nèi)容侵犯了您的版權(quán)或隱私,請立即通知裝配圖網(wǎng)(點擊聯(lián)系客服),我們立即給予刪除!

溫馨提示:如果因為網(wǎng)速或其他原因下載失敗請重新下載,重復(fù)下載不扣分。




關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

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

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


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