C語言期末復(fù)習(xí)專題講義_第1頁(yè)
C語言期末復(fù)習(xí)專題講義_第2頁(yè)
C語言期末復(fù)習(xí)專題講義_第3頁(yè)
C語言期末復(fù)習(xí)專題講義_第4頁(yè)
C語言期末復(fù)習(xí)專題講義_第5頁(yè)
已閱讀5頁(yè),還剩13頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

C語言期末復(fù)習(xí)1.1程序設(shè)計(jì)概述Hello,World!#include<stdio.h>#include<iostream>using

namespacestd;

int

main(){printf("Hello");cout<<

''

<<

"World"

<<

"!"

<<endl;}2用兩種方法求π#include<math.h>#include<stdlib.h>#include<iostream>using

namespacestd;

double

Gregory(){

doublepi=

0.0;

ints=

1,n=

1;

while(true){

doublet=

double(s)/n;

if(fabs(t)<

1e-6)break;pi+=t;s=

-s;n+=

2;}

returnpi*

4;}double

MonteCarlo(){

intcount=

0,N=

1000000;

for(inti=

0;i<N;++i){

doublex=

double(rand())/RAND_MAX;

doubley=

double(rand())/RAND_MAX;

if(x*x+y*y<=

1.0)++count;}

return

double(count)/N*

4;}int

main(){cout<<

"Gregory:"

<<Gregory()<<endl;srand(0);cout<<

"MonteCarlo0:"

<<MonteCarlo()<<endl;srand(1);cout<<

"MonteCarlo1:"

<<MonteCarlo()<<endl;}Gregory:3.14159MonteCarlo0:3.14164MonteCarlo1:3.139092024/3/1031.2指針與結(jié)構(gòu)體指針是什么#define_USE_MATH_DEFINES#include<math.h>#include<stdio.h>

int

main(){

charc=

'a';inti=

int(8.1);doubled(M_PI);

char

*cp=

&c;int

*ip=

&i;double

*dp(&d);printf("Variables:%c%d%g\n",c,i,d);printf("Pointedvariables:%c%d%g\n",*cp,*ip,*dp);printf("Pointers:%d%d%d\n",cp,ip,dp);printf("Addedpointers:%d%d%d\n",cp+

1,ip+

1,dp+

1);}Variables:a83.14159Pointedvariables:a83.14159Pointers:445616744561524456136Addedpointers:445616844561564456144Variables:a83.14159Pointedvariables:a83.14159Pointers:563427956342645634248Addedpointers:5634280563426856342562024/3/104交換變量與交換指針#include<stdio.h>

int

main(){

inta=

3,b=

5;

int

*pa=

&a,*pb=

&b;printf("a\tb\t*pa\t*pb\tpa\tpb\n");printf("%d\t%d\t%d\t%d\t%d\t%d\n",a,b,*pa,*pb,pa,pb);{

inttmp=

*pb;*pb=

*pa;*pa=tmp;printf("%d\t%d\t%d\t%d\t%d\t%d\n",a,b,*pa,*pb,pa,pb);}{

int

*pt=pb;pb=pa;pa=pt;printf("%d\t%d\t%d\t%d\t%d\t%d\n",a,b,*pa,*pb,pa,pb);}}ab*pa*pbpapb3535963348896334765353963348896334765335963347696334882024/3/105指針和數(shù)組的關(guān)系#include<stdlib.h>#include<iostream>using

namespacestd;

int

max_element(inta[10]){

intm=a[0];

for(inti=

1;i<

10;++i)

if(a[i]>m)m=a[i];

returnm;}int

max_element(int

*a,intlen){

intm=a[0];

for(inti=

1;i<len;++i)

if(a[i]>m)m=a[i];

returnm;}int

main(){

inta[10]={1,2,3,4,5,0,0,0,0,0};

intN=

5;

int

*pa=(int*)malloc(N*

sizeof(int));

for(inti=

0;i<N;++i)pa[i]=i+

1;cout<<max_element(a)<<

''

<<max_element(a,10)<<

''

<<max_element(pa)<<

''

<<max_element(pa,N)<<endl;free(pa);}5567871041052024/3/106動(dòng)態(tài)內(nèi)存管理C語言malloc,free,realloc只分配或釋放內(nèi)存C++語言new,delete分配內(nèi)存并調(diào)用構(gòu)造函數(shù)、調(diào)用析構(gòu)函數(shù)并釋放內(nèi)存絕不能混用!int

*pa=(int*)malloc(N*

sizeof(int));for(inti=

0;i<N;++i)pa[i]=i+

1;free(pa);int

*pa=

new

int[N];for(inti=

0;i<N;++i)pa[i]=i+

1;delete[]pa;2024/3/107指向指針的指針#include<stdlib.h>#include<iostream>using

namespacestd;

void

ErrorMsg(const

char*msg){cerr<<msg<<endl;exit(EXIT_FAILURE);}int

main(intargc,char

**argv){

if(argc<

4)ErrorMsg("Argumenterror!");

introws=atoi(argv[1]);intcols=atoi(argv[2]);

int

**a=

new

int*[rows];

for(intr=

0;r<rows;++r)a[r]=

new

int[cols];

for(intr=

0;r<rows;++r)for(intc=

0;c<cols;++c)a[r][c]=r+c;

for(intr=

0;r<rows;++r)for(intc=

0;c<cols;++c)cout<<a[r][c]<<(c==cols-

1

?

'\n'

:

'');cout<<argv[3]<<endl;

for(intr=

0;r<rows;++r)delete[]a[r];

delete[]a;}>pointpointer.exeArgumenterror!>pointpointer.exe53done012123234345456done2024/3/108結(jié)構(gòu)體及其指針#include<stdio.h>

typedef

intAge;typedefAge*pAge;typedef

charName[20];typedef

struct{

intyear,month,day;}Date;typedef

structStudent{Namename;Ageage;Datebirthday;Student*nextStudent;}*pStudent,StudentArray[4];

int

main(){StudentArraystudents={{"Zhao",19,1996,5,23},{"Qian",18,1997,7,19},{"Sun",20,1995,10,12},{"Li",17,1998,1,5}};

for(inti=

0;i<

4;++i){students[i].nextStudent=(i==

0

?

NULL

:(i==

1

?students+

2

:(i==

2

?students:students+

1)));}pStudentps=

&students[3];

while(ps){pAgepa=

&(ps->age);printf("%s%d(%d-%d-%d)\n",ps->name,*pa,ps->birthday.year,ps->birthday.month,ps->birthday.day);ps=ps->nextStudent;}}Li17(1998-1-5)Qian18(1997-7-19)Sun20(1995-10-12)Zhao19(1996-5-23)2024/3/1091.3文件操作標(biāo)準(zhǔn)輸入輸出Inputtwointegers:34Inputoperator:Inputerror!Inputtwointegers:34Inputoperator:+Result:7int

main(){

inta,b,res;charc;printf("Inputtwointegers:");scanf("%d%d",&a,&b);printf("Inputoperator:");c=getchar();

switch(c){

case

'+':res=a+b;break;

case

'-':res=a-b;break;

default:ErrorMsg("Inputerror!");}printf("Result:%d\n",res);}此處參加getchar();或者fflush(stdin);2024/3/1010標(biāo)準(zhǔn)輸入輸出C語言scanf,printfscanf("%d%d",&a,&b);printf("%d%d",a,b);C++語言cin,coutcin>>a>>b;cout<<a<<''<<b;慎用scanf!必須提供正確的指針,且保證存儲(chǔ)空間慎用字符串輸入!2024/3/1011按行讀取文本文件#include<stdio.h>#include<memory.h>

bool

readline(FILE

*fp,char

**line,int

*len){

intc=fgetc(fp);

if(c==EOF)return

false;

inti=

0;

do{

if(c==

'\r'){c=fgetc(fp);

if(c!=EOF&&c!=

'\n')fseek(fp,-1,1);

break;}

else

if(c==

'\n')break;

else{(*line)[i++]=c;

if(i>=

*len){

*len+=

80;

char

*newline=

new

char[*len];memcpy(newline,*line,i*sizeof(char));

delete[](*line);

*line=newline;}}}while((c=fgetc(fp))!=EOF);(*line)[i]=

'\0';return

true;}int

main(){

FILE

*fp=fopen("test.txt","rt");

intno=

1,len=

80;

char

*line=

new

char[len];

while(readline(fp,&line,&len))printf("Line%d:%s\n",no++,line);

delete[]line;fclose(fp);}Line1:ThisisthefirstlineLine2:Thesecond!!!Line3:Line4:AveryveryverylonglinefinallyendsLine5:Thelastline2024/3/10121.4函數(shù)與模塊化程序設(shè)計(jì)局部變量與全局變量#include<stdio.h>

intcount;void

func1(){

intcount;

for(count=

0;count<

10;++count)putchar('.');}void

func2(){

inttemp=count;func1();printf("Thecountis%d\n",temp);}int

main(){count=

100;func2();}Thecountis1002024/3/1013引用參數(shù)C++等語言提供的功能,C語言不支持引用參數(shù)傳遞的是“地址”而不是“值”,因此引用參數(shù)使得對(duì)參數(shù)“值”的修改可以回傳C語言的“傳指針”可實(shí)現(xiàn)類似功能,但代碼繁瑣#include<stdio.h>

void

chValue(inti){i=

3;}void

chPointer(int

*i){*i=

5;}void

chRef(int

&i){i=

7;}int

main(){

inta=

1;chValue(a);printf("%d\n",a);chPointer(&a);printf("%d\n",a);chRef(a);printf("%d\n",a);}傳值:修改無法回傳傳指針〔特殊的值〕傳引用:修改可以回傳2024/3/1014引用參數(shù)的用法實(shí)現(xiàn)“多輸出”防止大型結(jié)構(gòu)體的復(fù)制void

MaxMin(int

*p,intlen,int

&maxValue,int

&minValue){

//輸入數(shù)組p,其長(zhǎng)度為len

//以引用參數(shù)返回?cái)?shù)組中的最大值和最小值}const

intN=

10000;structBigStruct{

doublearray[N];};double

MaxValue(BigStruct&a){

doublem=a.array[0];

for(inti=

1;i<N;++i)if(a.array[i]>m)m=a.array[i];

returnm;}2024/3/1015指針的引用參數(shù)#include<stdio.h>

void

InitArray(int

*&p,intlen){

if(p)delete[]p;p=

new

int[len];}void

DestoryArray(int

*&p){

delete[]p;p=

NULL;}int

main(){

int

*p=

NULL;intN=

100;InitArray(p,N);printf("Pointer:%d",p);

for(inti=

0;i<N;++i)p[i]=i;

intsum=

0;for(inti=

0;i<N;++i)sum+=p[i];DestoryArray(p);printf("Pointer:%dSum:%d\n",p,sum);}Pointer:6849840Pointer:0Sum:49502024/3/1016模塊

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝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. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論