c++程序实例.doc_第1页
c++程序实例.doc_第2页
c++程序实例.doc_第3页
c++程序实例.doc_第4页
c++程序实例.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C+程序设计实例【例3.12】用下面公式求的近似值。/41-1/3+1/5-1/7+直到最后一项的绝对值小于10-7为止。根据给定的算法很容易编写程序如下:. #include. #include. #include. usingnamespacestd;. intmain(). . ints=1;. doublen=1,t=1,pi=0;. while(fabs(t)1e-7). . pi=pi+t;. n=n+2;. s=-s;. t=s/n;. . pi=pi*4;. coutpi=setiosflags(ios:fixed)setprecision(6)piendl;. return0;. 运行结果为pi=3.141592注意:不要把n定义为整型变量,否则在执行“t=s/n;”时,得到t的值为0(原因是两个整数相除)。【例3.13】求Fibonacci数列前40个数。这个数列有如下特点:第1、2个数为1、1。从第3个数开始,每个数是其前面两个数之和。即: F1=1 (n=1) F2=1 (n=2) Fn=Fn-1+Fn-2(n3)这是一个有趣的古典数学问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第3个月后每个月又生一对兔子,假设所有兔子都不死,问每个月的兔子总数为多少?根据给出的每月兔子总数的关系,可编写程序如下:. #include. #include. usingnamespacestd;. intmain(). . longf1,f2;. inti;. f1=f2=1;. for(i=1;i=20;i+). . coutsetw(12)f1setw(12)f2;. /设备输出字段宽度为12,每次输出两个数. if(i%2=0)coutendl;. /每输出完4个数后换行,使每行输出4个数. f1=f1+f2;. /左边的f1代表第3个数,是第12个数之和. f2=f2+f1;. /左边的f2代表第4个数,是第23个数之和. . return0;. 【例3.14】找出100200间的全部素数。编写程序如下:. #include. #include. #include. usingnamespacestd;. intmain(). . intm,k,i,n=0;. boolprime;/定义布尔变量prime. for(m=101;m=200;m=m+2)/判别m是否为素数,m由101变化到200,增量为2. . prime=true;/循环开始时设prime为真,即先认为m为素数. k=int(sqrt(m);/用k代表根号m的整数部分. for(i=2;i=k;i+)/此循环作用是将m被2根号m除,检查是否能整除. if(m%i=0)/如果能整除,表示m不是素数. . prime=false;/使prime变为假. break;/终止执行本循环. . if(prime)/如果m为素数. . coutsetw(5)m;/输出素数m,字段宽度为5. n=n+1;/n用来累计输出素数的个数. . if(n%10=0)coutendl;/输出10个数后换行. . coutendl;/最后执行一次换行. return0;. 【例3.15】译密码。为使电文保密,往往按一定规律将电文转换成密码,收报人再按约定的规律将其译回原文。例如,可以按以下规律将电文变成密码:将字母A变成字母E,a变成e,即变成其后的第4个字母,W变成A,X变成B,Y变成C,Z变成D。见图3.20,字母按上述规律转换,非字母字符不变,如Wonderful!转换为Asrhivjyp!。输入一行字符,要求输出其相应的密码。图 3.20程序如下:. #include. usingnamespacestd;. intmain(). . charc;. while(c=getchar()!=n). . if(c=a&c=A&cZ&cz). c=c-26;. . coutc;. . coutZ| cz) c=c-26;因为所有小写字母都满足“cZ”条件,从而也执行“c=c-26;”语句,这就会出错。因此必须限制其范围为“cZ & cz & cz”即可。计算拉格朗日插值的源程序#include #include #include /#include float Lagrange(float *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;float Lagrange(float *,float *,float,int);yy=Lagrange(x,y,xx,4);/clrscr();printf(x=%f,y=%f/n,xx,yy);getch();编译原理词法分析器c+源程序#include#include#include#include#include#include#include /*头文件*/ void init(); char *DchangeB(char *buf); int search(char *buf,int type,int command); void intdeal(char *buffer); void chardeal(char *buffer); void errordeal(char error,int lineno); void scanner();void init() char *key=,auto,break,case,char,const,continue,default,do,double, else,enum,extern,float,for,goto,if,int,long,register, return,short,signed,sizeof,static,struct,switch,typedef, union,unsigned,void,volatile,while; /*C语言所有关键字/ char *limit= ,(,),-,.,!,+,-,&, *,/,%,+,-,=,=,!=,&,|, =,+=,-=,*=,/=,;,#,_,;/*运算、限界符*/fstream outfile; int i,j;char *c;outfile.open(key.txt,iOS:out);for(i=0;i32;i+)outfilekeyiendl;outfile.close();outfile.open(Limit.txt,ios:out);for(j=0;j38;j+)outfilelimitjendl;c=;outfilec;outfile.close();outfile.open(bsf.txt,ios:out);outfile.close();outfile.open(cs.txt,ios:out);outfile.close();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); /*将字符转化为十进制数*/ if(value=0) binary=new char2;binary0=0;binary1=/0;return(binary);i=0;while(value!=0) tempi+=value%2;value/=2;tempi=/0;binary=new chari+1;for(j=0;j=i-1;j+) binaryj=(char)(tempi-j-1+48); binaryi=/0; return(binary); /*十进制转化为二进制*/int search(char *buf,int type,int command) int number=0; 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);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) tempi+=ch; outfile.get(ch); tempi=/0; i=0; number+; if(strcmp(temp,buf)=0) outfile.close();return number; /*若找到,返回在相应表中的序号*/ else outfile.get(ch); /结束外层while循环 if(command=1) outfile.close( ); return 0; /*找不到,当只需查表,返回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,ios:in);break; case 4: outfile.open(limit.txt,ios:in);break; outfilebuf;outfile.close();return number+1;void intdeal(char *buffer) fstream outfile; int result; result=search(buffer,1,1); /*先查关键字表*/ outfile.open(output.txt,ios:app); if(result!=0) outfilebufferresultendl; /*若找到,写入输出文件*/ else result=search(buffer,2,2); /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/ outfilebufferresultendl; /*写入输出文件*/ outfile.close();void chardeal(char *buffer) fstream outfile; int result; result=search(buffer,1,1); /*先查关键字表*/outfile.open(output.txt,ios:app); if(result!=0) outfilebufferresultendl; /*若找到,写入输出文件*/ else result=search(buffer,2,2); /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/ outfilebufferresultendl; /*写入输出文件*/ outfile.close();void errordeal(char error,int lineno) cout/nerror: error ,linelineno;void scanner() fstream infile,outfile; char filename20; char ch;int err=0; int i=0,line=1; int count,result,errorno=0;char array30; char *word;printf(/n please input the file scanner name:); scanf(%s,filename); err=1; infile.open(filename,ios:nocreate|ios:in); while(! infile) coutcannot open fileendl; printf(please input the file name again:/n);scanf(%s,filename); infile.open(filename,ios:nocreate|ios:in); err+;if(err=3)coutSORROY YOU CANT VUEW THE PRGARME/n; coutTANKE YOU VIEW=A)&(ch=a)&(ch=A)&(ch=a)&(ch=0)&(ch=0&ch=0&ch=9) arrayi+=ch; infile.get(ch); word=new chari+1;memcpy(word,array,i);wordi=/0; intdeal(word); if(ch!=EOF)infile.seekg(-1,ios:cur); else if(ch= )|(ch=/t); /*消除空格符和水平制表符*/else if(ch=/n)line+; /*消除回车并记录行数*/else if(ch=/) /*消除注释*/infile.get(ch);if(ch=) /*判断是否为/=符号*/outfile.open(output.txt,ios:noreplace|ios:app);outfile/=/t/t/t4/t/t/t32/n; outfile.close();else if(ch!=*) /*若为除号,写入输出文件*/ outfile.open(output.txt,ios:noreplace|ios:app);outfile/t/t/t4/t/t/t13/n; outfile.close();outfile.seekg(-1,ios:cur);else if(ch=*) /*若为注释的开始,消除包含在里面的所有字符*/count=0;infile.get(ch);while(count!=2) /*当扫描到*且紧接着下一个字符为/才是注释的结束*/count=0;while(ch!=*)infile.get(ch);count+; infile.get(ch);if(ch=/)count+;elseinfile.get(ch);else if(ch=) /*消除包含在双引号中的字符串常量*/outfile.open(output.txt,ios:noreplace|ios:app); outfilech/t/t/t4/t/t/t37/n;outfile.close();while(ch!=)infile.get(ch); infilech/t/t/t4/t/t/t37/n;infile.close();else /*首字符为其它字符,即运算限界符或非法字符*/ array0=ch; infile.get(ch); /*再读入下一个字符,判断是否为双字符运算、限界符*/ if(ch!=EOF) /*若该字符非文件结束符*/ array1=ch;word=new char3;memcpy(word,array,2);word2=/0; result=search(word,4,1); /*先检索是否为双字符运算、限界符*/if(result=0) /*若不是*/ word=new char2;memcpy(word,array,1);word1=/0;result=search(word,4,1); /*检索是否为单字符运算、限界符*/if(result=0) /*若还不是,则为非法字符*/errordeal(array0,line);errorno+;infile.seekg(-1,ios:cur); else /*若为单字符运算、限界符,写入输出文件并

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论