C++程序整理资料_第1页
C++程序整理资料_第2页
C++程序整理资料_第3页
C++程序整理资料_第4页
C++程序整理资料_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

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

文档简介

SchoolofInformationandEngineering,ShenzhenUniversity12.3根本数据类型5.应用举例[例1]输入矩形的长和宽,求面积#include<iostream.h>voidmain()//操作系统把main函数执行当调用一个函数,要求执行完后返回一个值

{

floatwidth,length,area;

cout<<“\nInputlength:”;cin>>length;

cout<<“Inputwidth:”;cin>>width;

area=length*width;

cout<<“\nArea(length*width)=”<<area<<‘\n’;

}SchoolofInformationandEngineering,ShenzhenUniversity22.3根本数据类型[例2]输入圆的半径,求其周长和面积#include<iostream.h>constfloatPI=3.14159;

voidmain(){

intr;floatarea,length;//设半径为整数

cout<<“\nEntertheradius:”;cin>>r;

area=PI*r*r;//计算面积

length=2*PI*r;//计算周长

cout<<“Area=”<<area<<‘\n’;

cout<<“length=”<<length<<‘\n’;

}SchoolofInformationandEngineering,ShenzhenUniversity3例子输入24小时制的时间,转换并输出12小时制的时间并注明是上午还是下午〔上午用A.M表示,下午用P.M表示〕SchoolofInformationandEngineering,ShenzhenUniversity4#include<iostream.h>voidmain(){

floatx;

cout<<”Enterthehourplease:”;

cin>>x;

if(x>12)

{

cout<<”P.M:”;

x=x-12;

}

elsecout<<”A.M:”;

cout<<x<<‘\n’;}SchoolofInformationandEngineering,ShenzhenUniversity5从键盘上输入3个数A,B,C,

求出三数中最大者并输出#include<iostream.h>voidmain(){intA,B,C,max;cout<<”EnterA,B,C,Please:”;cin>>A>>B>>C;max=A;if(B>max)max=B;if(C>max)max=C;cout<<”MAX(A,B,C):”<<max<<‘\n’;}SchoolofInformationandEngineering,ShenzhenUniversity6编写一个模拟收费程序。设某药店出售A、B、C、D四种药品,单价分别为2元、3.5元、10.8元、5.0元;从键盘上输入药品名称与数量〔盒〕,计算出收费金额并输出。分析问题特点:选择结构的根本形式是每一个判别选择为两个分支;但此例的选择分支中又含有另一个选择结构,此种形式称“选择嵌套”。这就产生了所谓的“垂悬问题”,即如何判别if与else的搭配问题。我们采用“就近匹配”原那么,一个else与离它就近的上一个if匹配。SchoolofInformationandEngineering,ShenzhenUniversity7#include<iostream.h>voidmain(){intN;floattotal;charch;cout<<”EntertheName:”;cin>>ch;cout<<”EntertheNumber(N):”;cin>>N;if(ch==’A’)total=2.0*N;elseif(ch==’B’)total=3.5*N;elseif(ch==’C’)total=10.8*N;elseif(ch==’D’)total=5.0*N;else{total=0.0;cout<<”EnterDataError!\n”;}cout<<”\nTotal=”<<total<<‘\n’;}SchoolofInformationandEngineering,ShenzhenUniversity8switch语句问题提出:if嵌套形式之化简。多嵌套使程序结构的清晰度受到影响,为保障程序结构清晰性和改善可读性而引入。switch〔条件表达式〕//条件表达式和常量表达式的//值应为int或char型{case常量1:S1;break;

//break不能省去

case常量2:S2;break;

……

case常量3:Sk;break;default

:S;

//当以上情况均不满足时的“其它”}SchoolofInformationandEngineering,ShenzhenUniversity9收费程序#include<iostream.h>voidmain(){intN;floattotal;charch;cout<<”EntertheName(A||B||C||D):”;cin>>ch;cout<<”EntertheNumber(N):”;cin>>N;switch(ch){case’A’:total=2.0*N;break;case’B’:total=3.5*N;break;case’C’:total=10.8*N;break;case’D’:total=5.0*N;break;default:total=0.0;}cout<<”\nTotal=”<<total<<‘\n’;}

SchoolofInformationandEngineering,ShenzhenUniversity10在键盘上输入y年m月,判别y年m月有多少天。设y,m输入均符合要求。[方法1]采用if嵌套方法:逐月判别,给出对应月份的天数。缺乏:采用if多层嵌套,可读性较差。其中2月份的处理if〔m==2〕if(((y%4==0)&&(y%100!=0))||(y%400==0))d=29;//是闰年elsed=28;//不是闰年SchoolofInformationandEngineering,ShenzhenUniversity11[方法2]采用switch语句----改善可理解性

#include<iostream.h>voidmain(){inty,m,d;cout<<”Enteryourdata(yyyymm):”;cin>>y>>m;switch(m){case1:case3:case5:case7:case8:case10:case12:d=31;break;case4:case6:case9:case11:d=30;break;case2:if(((y%4==0)&&(y%100!=0))||(y%400==0))d=29;elsed=28;break;}//switch结束

cout<<y<<”Year”<<m<<”month:”<<d<<”Days\n”;}//可理解性好SchoolofInformationandEngineering,ShenzhenUniversity12[方法3]从方法2改进

#include<iostream.h>

voidmain()

{inty,m,d;

cout<<”EnterYourData:”;cin>>y>>m;

if(m==2)

//是2月份的处理

if(((y%4==0)&&(y%100!=0))||(y%400==0))d=29;

elsed=28;

else

//不是2月份的处理

{if((m<=7〕&&(m%2!=0))d=31;elsed=30;

if((m>=8〕&&(m%2==0))d=31;elsed=30;

}

cout<<”Daysof“<<y<<”year”<<m<<”monthis”<<d<<‘\n’;

}

//非2月份处理还可改为:

{if((1<=m)&&(m<=7〕&&(m%2==1))d=31;elsed=30;if((1<=m)&&(m>=8〕&&(m%2==0))d=31;elsed=30;}SchoolofInformationandEngineering,ShenzhenUniversity13选择语句使用中常见的错误1)条件表达式漏加圆括号ifa>b//错应为:if〔a>b〕{t=a;a=b;b=t;}

{t=a;a=b;b=t;}2)错误的缩进形式if〔a>b〕{ 应为:if〔a>b〕t=a;{a=b;t=a;b=t;}a=b; b=t;//缩进不好}SchoolofInformationandEngineering,ShenzhenUniversity143)漏复合语句花括号if(a>b)//错应为: if(a>b)

{t=a;a=b;b=t;}{t=a;a=b;b=t;}

elseelse

a=b;{a=b;

b=b+50; b=b+50;}4)在条件判别式后错误地参加“;”,使成空操作。if〔a>b〕;//本身是一个完整语句,语法没有错。{t=a;a=b;b=t;}//但成了空操作,导致下面else语法错else{a=b;b=b+50;}SchoolofInformationandEngineering,ShenzhenUniversity155)不正确地使用条件表达式if!(a>b) 应为:if〔!〔a>b〕〕{t=a;a=b;b=t;} {t=a;a=b;b=t;}6)复合语句内的简单语句漏分号if(a>b) 应为:if〔a>b〕{t=a;a=b;b=t} {t=a;a=b;b=t;}

7)用逗号取代了分号if(a>b) 应为:if〔a>b〕{t=a,a=b,b=t} {t=a;a=b;b=t;}elseelse{a=b,b=b+50;} {a=b;b=b+50;}//此形式语法没错,因采用//逗号表达式,但移植可能会错SchoolofInformationandEngineering,ShenzhenUniversity16

8)switch语句漏掉break设有inti;cin>>i;//比较一下两种形式的执行结果switch〔i〕应为:switch〔i〕{case0:i++;{case0:i++;break;

case1:i++;case1:i++;break;case2:i++;case2:i++;break;case3:i++;case3:i++;break;default:i=100;default:i=100;}}不管输入i为何值按输入i之值有不同结果结果均为100

SchoolofInformationandEngineering,ShenzhenUniversity17[例1]给定一个正整数n,求出平方值不超过n的正整数并输出。SchoolofInformationandEngineering,ShenzhenUniversity18#include<iostream.h>main(){intn;//用户给定的正整数

intmax;//所求的最大整数

cout<<”Enteranumber”;//用户给定一个自然数

cin>>n;

if(n<=0)cout<<”Inputerror!\n”;//判断用户输入是否合法

else{//利用循环求出平方大于n的最小整数max=1;while〔max*max<=n〕max=max+1;//输出结果cout<<”Themaximumintegeris:”<<max-1<<‘\n’;}}SchoolofInformationandEngineering,ShenzhenUniversity192.do-while循环1)逻辑形式和程序形式例子

[例1]利用牛顿迭代法求一个正整数的平方根迭代公式xn+1=(xn+a/xn)/2;//x为a的开方当|xn+1-xn|<q时,取xn+1为平方根SchoolofInformationandEngineering,ShenzhenUniversity20#include<iostream.h>main(){constfloatEPSILON=1E-5;//精度要求floatnum;//用户输入的整数floatroot,pre;//求出的近似值//给定一个正数cout<<”Enteranumber:”;cin>>num;//判断用户输入的合法性if〔num<0〕cout<<”Inputerror!\n”;else{root=1;//用牛顿法求平方根do{pre=root;root=(num/root+root)/2;}while((pre-root>EPSILON)||(root-pre>EPSILON));cout<<”Therootof“<<num<<”is”<<root<<‘\n’;}}

SchoolofInformationandEngineering,ShenzhenUniversity21程序1//功能:求1到100之间的数字相加之和的精美算法。#include<iostream.h>Voidmain(){constintmax=100;intsum;sum=(max/2)*(1+max);cout<<“Thesummingresultis”<<sum<<‘\n’;}SchoolofInformationandEngineering,ShenzhenUniversity22程序2//求1到100之间数字相加之和的原始算法。#include<iostream.h>Voidmain(){constintmin=1;constintmax=100;intsum;intcnt;sum=0;for(cnt=min;cnt<=max;cnt=cnt+1)sum=sum+cnt;cout<<“Thesummingresultis“<<sum<<‘\n’;}SchoolofComputerandSoftwareEngineering,ShenzhenUniversity23例子:求用户输入的三个数中的最大者#include<iostream.h>

floatmax(float,float);

//函数的引用性声明

voidmain()

{

floati,j,k;

//用户输入的三个数

floattemp;

//临时最大者

cout<<"Inputthreenumbersplease:";

//用户输入三个数

cin>>i>>j>>k;

//找出最大数存放在temp中

temp=max(i,j);

temp=max(temp,k);

cout<<"Themaximumnumberis"<<temp<<endl;

//输出找到的最大数

}

//求两个数的最大值

floatmax(floatx,floaty)

{

floatz;

if(x>=y)z=x;elsez=y;

returnz;

}SchoolofComputerandSoftwareEngineering,ShenzhenUniversity24交换变量值#include<iostream.h>voidswap(intx,inty)

//实现X,Y的值交换{

intt;

cout<<endl<<"X="<<"\tY="<<y<<endl;

t=x;x=y;y=t;

//X,Y的值交换

cout<<endl<<"X="<<x<<"\tY="<<y<<endl;}voidmain(){

inta,b;

cout<<endl<<"Entertwonumbers.\nA=";cin>>a;

cout<<"\nB=";cin>>b;

cout<<"\nBeforeswapping:A="<<a<<"andB="<<b<<endl;

swap(a,b);

cout<<"\nAfterswapping:A="<<a<<"andB="<<b<<endl;

}SchoolofComputerandSoftwareEngineering,ShenzhenUniversity25程序执行结果:

Entertwonumbers.

A=10↙

B=20↙

Beforeswapping:A=10andB=20

X=10

Y=20

X=20

Y=10

Afterswapping:A=10andB=20SchoolofComputerandSoftwareEngineering,ShenzhenUniversity26交换变量值#include<iostream.h>

voidswap(int&x,int&y);

温馨提示

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

评论

0/150

提交评论