版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、C+程序設(shè)計實例【例3.12】用下面公式求的近似值。/41-1/3+1/5-1/7+直到最后一項的絕對值小于10-7為止。根據(jù)給定的算法很容易編寫程序如下:. #include <iostream>. #include <iomanip>. #include <cmath>. using namespace std;. int main( ). . int s=1;. double n=1,t=1,pi=0;. while(fabs(t)>1e-7). . pi=
2、pi+t;. n=n+2;. s=-s;. t=s/n;. . pi=pi*4;. cout<<"pi="<<setiosflags(ios:fixed)<<setprecision(6)<<pi<<endl;. return 0;. 運行結(jié)果為pi=3.141592注意:不要把n定義為整型變量,否則在執(zhí)行“t=s/n;”時,得到t的值為0(原因是兩個整數(shù)相除)。【例3.13】求Fibonacci數(shù)列前40個數(shù)。這個數(shù)列有如下特點:第1、2個數(shù)為1、1。從第3個數(shù)開始,每個數(shù)是其前面兩個數(shù)之和。即:
3、60; F1=1 (n=1) F2=1 (n=2) Fn=Fn-1+Fn-2(n3)這是一個有趣的古典數(shù)學(xué)問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第3個月后每個月又生一對兔子,假設(shè)所有兔子都不死,問每個月的兔子總數(shù)為多少?根據(jù)給出的每月兔子總數(shù)的關(guān)系,可編寫程序如下:1 / 17. #include <iostream>. #include <iomanip>. using namespace std;. int
4、160;main( ). . long f1,f2;. int i;. f1=f2=1;. for(i=1;i<=20;i+). . cout<<setw(12)<<f1<<setw(12)<<f2;. /設(shè)備輸出字段寬度為12,每次輸出兩個數(shù). if(i%2=0) cout<<endl;. /每輸出完4個數(shù)后換行,使每行輸出4個數(shù). f1=f1+f2;. /左邊的f1代表第3個數(shù),是第12個數(shù)之和. f2=f2+f1;. /左邊的f2代表第4個數(shù),是第23個數(shù)之和. . return
5、160;0;. 【例3.14】找出100200間的全部素數(shù)。編寫程序如下:. #include <iostream>. #include <cmath>. #include <iomanip>. using namespace std;. int main( ). . int m,k,i,n=0;. bool prime;/定義布爾變量prime. for(m=101;m<=200;m=m+2) /判別m是否為素數(shù),m由101變化到200,增量為2. .
6、 prime=true;/循環(huán)開始時設(shè)prime為真,即先認為m為素數(shù). k=int(sqrt(m); /用k代表根號m的整數(shù)部分. for(i=2;i<=k;i+) /此循環(huán)作用是將m被2根號m除,檢查是否能整除. if(m%i=0) /如果能整除,表示m不是素數(shù). . prime=false; /使prime變?yōu)榧? break; /終止執(zhí)行本循環(huán). . if (prime)/如果m為素數(shù). . cout<<setw(5)<<m; /輸出素數(shù)m,字段寬度為5. n=n+1; /n用
7、來累計輸出素數(shù)的個數(shù). . if(n%10=0) cout<<endl; /輸出10個數(shù)后換行. . cout<<endl;/最后執(zhí)行一次換行. return 0;. 【例3.15】譯密碼。為使電文保密,往往按一定規(guī)律將電文轉(zhuǎn)換成密碼,收報人再按約定的規(guī)律將其譯回原文。例如,可以按以下規(guī)律將電文變成密碼:將字母A變成字母E,a變成e,即變成其后的第4個字母,W變成A,X變成B,Y變成C,Z變成D。見圖3.20,字母按上述規(guī)律轉(zhuǎn)換,非字母字符不變,如"Wonderful!"轉(zhuǎn)換為"Asrhivjyp!"
8、。輸入一行字符,要求輸出其相應(yīng)的密碼。圖 3.20程序如下:. #include <iostream>. using namespace std;. int main( ). . char c;. while (c=getchar( )!='n'). . if(c>='a' && c<='z') | (c>='A' && c<
9、='Z'). . c=c+4;. if(c>'Z' && c<='Z'+4 | c>'z'). c=c-26;. . cout<<c;. . cout<<endl;. return 0;. 運行結(jié)果如下:I am going to Beijing!M eq ksmrk xs Fimnmrk!while語句中括號內(nèi)的表達式有3個作用:· 從鍵盤讀入一個字符,這是用getchar函數(shù)實現(xiàn)的;· 將讀入的字符賦
10、給字符變量c;· 判別這個字符是否為'n'(即換行符)。如果是換行符就執(zhí)行while語句中的復(fù)合語句(即花括號內(nèi)的語句),對輸入的非換行符的字符進行轉(zhuǎn)換處理。按前面分析的思路對輸入的字符進行處理,有一點請讀者注意,內(nèi)嵌的if語句不能寫成: if (c>'Z'| c>'z') c=c-26;因為所有小寫字母都滿足“c>'Z'”條件,從而也執(zhí)行“c=c-26;”語句,這就會出錯。因此必須限制其范圍為“c>'Z' && c<=&
11、#39;Z'+4”,即原字母為'W'到'Z',在此范圍以外的不是原大寫字母WZ,不應(yīng)按此規(guī)律轉(zhuǎn)換。請考慮:為什么對小寫字母不按此處理,即寫成c>'z' && c<='z'+4而只須寫成“c>'z'”即可。 計算拉格朗日插值的源程序#include <stdio.h>#include <conio.h>#include <stdlib.h>/#include <alloc.h>float Lagrange(float
12、*x,float *y,float xx,int n)int i,j;float *a,yy=0.0;a=(float *)malloc(n*sizeof(float);for(i=0;i<=n-1;i+)ai=yi;for(j=0;j<=n-1;j+)if(j!=i)ai*=(xx-xj)/(xi-xj);yy+=ai;free(a);return yy;void main()float x4=0.56160,0.56280,0.56401,0.56521;float y4=0.82741,0.82659,0.82577,0.82495;float xx=0.5635,yy;fl
13、oat Lagrange(float *,float *,float,int);yy=Lagrange(x,y,xx,4);/clrscr();printf("x=%f,y=%f/n",xx,yy);getch(); 編譯原理詞法分析器c+源程序#include<iostream.h>#include<fstream.h>#include<stdlib.h>#include<stdio.h>#include<string.h>#include<conio.h>#include<proce
14、ss.h> /*頭文件*/ void init(); char *DchangeB(char *buf); int search(char *buf,int type,int command); void intdeal(char *buffer); void chardeal(char *buffer); void errordeal(c
15、har error,int lineno); void scanner();void init() char *key="","auto","break","case","char","const","continue","default","do","double",
16、; "else","enum","extern","float","for","goto","if","int","long","register",
17、; "return","short","signed","sizeof","static","struct","switch","typedef", "u
18、nion","unsigned","void","volatile","while" /*C語言所有關(guān)鍵字/ char *limit=" ","(",")","","","->",".","!","+","-",&quo
19、t;&","", "*","/","%","+","-","<<",">>","<","<=",">",
20、">=","=","!=","&&","|", "=","+=","-=","*=","/=",",","",""
21、,"","#","_","'"/*運算、限界符*/ fstream outfile; int i,j; char *c; outfile.open("key.txt",iOS:out); for(i=0;i<32;i+) outfile<<keyi<<endl; outfile.close(); outfile.open("Limi
22、t.txt",ios:out); for(j=0;j<38;j+) outfile<<limitj<<endl; c="" outfile<<c; outfile.close(); outfile.open("bsf.txt",ios:out); outfile.close(); outfile.open("cs.txt",ios:out); outfile.close();
23、0;outfile.open("output.txt",ios:out); outfile.close();char *DchangeB(char *buf) int temp20; char *binary; int value=0,i=0,j; for(i=0;bufi!='/0'i+) value=value*10+(bufi-48); &
24、#160; /*將字符轉(zhuǎn)化為十進制數(shù)*/ if(value=0) binary=new char2; binary0='0' binary1='/0' return(binary); i=0; while(value!=0) tempi+=value%2; value
25、/=2; tempi='/0' binary=new chari+1; for(j=0;j<=i-1;j+) binaryj=(char)(tempi-j-1+48); binaryi='/0' return(binary); /*十進制轉(zhuǎn)化為二進制*/int search(char *buf,int type,int command) int number=0
26、; fstream outfile; char ch; char temp30; int i=0; switch(type) case 1: outfile.open("key.txt",ios:in);break; case 2: outfile.open("bsf.txt",ios:in);
27、break; case 3: outfile.open("cs.txt",ios:in);break; case 4: outfile.open("limit.txt",ios:in);break; outfile.get(ch); while(ch!=EOF) while(ch!='/n')
28、 tempi+=ch; outfile.get(ch); tempi='/0'
29、; i=0; number+; if(strcmp(temp,buf)=0) outfile.close(); return number;
30、160; /*若找到,返回在相應(yīng)表中的序號*/ else outfile.get(ch);
31、160; /結(jié)束外層while循環(huán) if(command=1) outfile.close( ); return 0;
32、 /*找不到,當(dāng)只需查表,返回0,否則還需造表*/ switch(type) case 1: outfile.open("key.txt",ios:in);break; case 2: outfile.open("bsf.txt",ios:in);break; case 3: outfile.open("cs.txt",i
33、os:in);break; case 4: outfile.open("limit.txt",ios:in);break; outfile<<buf;outfile.close(); return number+1;void intdeal(char *buffer) fstream outfile; int result; result=search(buffer,1,1);
34、; /*先查關(guān)鍵字表*/ outfile.open("output.txt",ios:app); if(result!=0) outfile<<buffer<<result<<endl; /*若找到,寫入輸出文件*/ else
35、60; result=search(buffer,2,2); /*若找不到,則非關(guān)鍵字,查標(biāo)識符表,還找不到則造入標(biāo)識符表*/ outfile<<buffer<<result<<endl;
36、 /*寫入輸出文件*/ outfile.close();void chardeal(char *buffer) fstream outfile; int result;
37、 result=search(buffer,1,1); /*先查關(guān)鍵字表*/ outfile.open("output.txt",ios:app); if(result!=0) outfile<<buffer<<result<<endl; /*若找到,寫入輸出文件*/ &
38、#160; else result=search(buffer,2,2); /*若找不到,則非關(guān)鍵字,查標(biāo)識符表,還找不到則造入標(biāo)識符表*/ outfile<<buffer<<result<<endl;
39、; /*寫入輸出文件*/ outfile.close();void errordeal(char error,int lineno) cout<<"/nerror: "<<
40、error<<" ,line"<<lineno;void scanner() fstream infile,outfile; char filename20; char ch; int err=0; int i=0,line=1; int count,result,errorno=0; char array30; ch
41、ar *word; printf("/n please input the file scanner name:"); scanf("%s",filename); err=1; infile.open(filename,ios:nocreate|ios:in); while(! infile) &
42、#160; cout<<"cannot open file"<<endl; printf("please input the file name again:/n"); scanf("%s",filename); infile.open(filename,ios:nocreate|ios:
43、in); err+; if(err=3) cout<<"SORROY YOU CAN'T VUEW THE PRGARME/n" cout<<"TANKE YOU VIEW"<<endl; exit(0); &
44、#160; infile.get(ch); while(ch!=EOF) /*按字符依次掃描源程序,直至結(jié)束*/ i=0; if(ch>='
45、A')&&(ch<='Z')|(ch>='a')&&(ch<='z')|(ch='_') /*以字母開頭*/ while(ch>=
46、39;A')&&(ch<='Z')|(ch>='a')&&(ch<='z')|(ch='_')|(ch>='0')&&(ch<='9')
47、160; arrayi+=ch; infile.get(ch); word=new chari+1;
48、60;memcpy(word,array,i); wordi='/0' intdeal(word); if(ch!=EOF) infile.seekg(-1,ios:cur);
49、160; else if(ch>='0'&&ch<='9') /*以數(shù)字開頭*/ while(ch>='
50、;0'&&ch<='9') arrayi+=ch; infi
51、le.get(ch); word=new chari+1; memcpy(word,array,i); wordi='/0'
52、60; intdeal(word); if(ch!=EOF) infile.seekg(-1,ios:cur); else if(ch=' ')|(ch='/t')
53、0; /*消除空格符和水平制表符*/ else if(ch='/n') line+; /*消除回車并記錄行數(shù)*/ else if(ch='/')
54、0; /*消除注釋*/ infile.get(ch); if(ch='=') /*判斷是否為/=符號*/ outfile.open(&
55、quot;output.txt",ios:noreplace|ios:app); outfile<<"/=/t/t/t4/t/t/t32/n" outfile.close(); else if(ch!='*')
56、 /*若為除號,寫入輸出文件*/ outfile.open("output.txt",ios:noreplace|ios:app); &
57、#160; outfile<<"/t/t/t4/t/t/t13/n" outfile.close(); outfile.seekg(-1,ios:cur); else if(ch='*')
58、; /*若為注釋的開始,消除包含在里面的所有字符*/ count=0; infile.get(ch); while(count!=2) &
59、#160; /*當(dāng)掃描到*且緊接著下一個字符為/才是注釋的結(jié)束*/ count=0; while(ch!='*') infile.get(ch); count+; infile.get(ch);&
60、#160; if(ch='/') count+; else infile.get(ch); else if(ch='"') &
61、#160; /*消除包含在雙引號中的字符串常量*/ outfile.open("output.txt",ios:noreplace|ios:app); outfile<<ch<<"/t/t/t4/t/t/t37/n" outfile.close(); &
62、#160; while(ch!='"') infile.get(ch); infile<<ch<<"/t/t/t4/t/t/t37/n" infile.close(); else
63、; /*首字符為其它字符,即運算限界符或非法字符*/ array0=ch; infile.get(ch); /*再讀入下一個字符,判斷是否為雙字符運算、限界符*/
64、60; if(ch!=EOF) /*若該字符非文件結(jié)束符*/
65、; array1=ch; word=new char3; memcpy(word,array,2); word2='/0' result=search(word,4,1); /*先檢索是否為雙字符運算、限界
66、符*/ if(result=0) /*若不是*/ &
67、#160; word=new char2; memcpy(word,array,1); word1='/0' result=search(word,4,1); /*檢索是否為單字符運算、限界符*/ if(result=0
68、) /*若還不是,則為非法字符*/
69、 errordeal(array0,line); errorno+; infile.seekg(-1,ios:cur);
70、160; else /*若為單字符運算、限界符,寫入輸出文件并將掃描文件指針回退一個字符*/ outfile.open("output.txt",ios:noreplace|ios:app); outfile<<word<<"/t/t/t4/t/t/t"<<result<<"/t"<<endl; outfile.close();
溫馨提示
- 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. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 人教版(2019)必修第二冊Unit 5 Music Assessing Your Progress 教案
- 第四單元認識圖形(教學(xué)設(shè)計)-2023-2024學(xué)年一年級下冊數(shù)學(xué)青島版
- DB14-T 2766-2023 口岸傳染病防控人員防護規(guī)范
- 冬季施工安全管理方案
- 豬場產(chǎn)房49天精細化管理操作技術(shù)
- 3個人合伙做餐飲協(xié)議書范文
- 職測數(shù)量關(guān)系:利用整除法解數(shù)量關(guān)系題
- 2025屆高考語文復(fù)習(xí):鑒賞詩歌的煉字藝術(shù)
- 有效數(shù)字修約
- 【創(chuàng)投報告】派拉軟件(一體化零信任身份安全產(chǎn)品及服務(wù)提供商,上海派拉軟件股份有限公司)創(chuàng)投信息
- 火車購票系統(tǒng)UML類圖_時序圖_狀態(tài)圖_協(xié)作圖_活動圖_對象圖__用例圖.doc
- 新粵教版三年級下冊科學(xué)課件 第6課《我們需要食物》
- 這個星期天你忙不忙PPT課件
- 平行四邊形面積說課PPT課件
- 藏醫(yī)護理ppt課件
- 礦業(yè)權(quán)評估準(zhǔn)則全
- 衛(wèi)生系列高級職稱院內(nèi)申報匯報PPT模板
- 常見醫(yī)療器械不良事件臨床表現(xiàn)匯總表
- (最新整理)恢復(fù)生態(tài)學(xué)與生態(tài)工程講義
- 聲明書模板財務(wù)收支專項審計管理層聲明書
- 年產(chǎn)12萬噸鎳鐵項目可行性研究報告
評論
0/150
提交評論