上海交通大学c++课件3.ppt

  1. 1、本文档共81页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
上海交通大学c课件3

例题3.5(1)改进程序 #include iostream #include cmath using namespace std; int main( ) { float a,b,c,x1,x2,discrim; coutInput a b c values:endl; cinabc; //1 2 1 discrim = b * b - 4 * a * c; if (discrim 0) coutThe equation has no real roots!endl; else { x1=(-b+sqrt(b*b-4*a*c))/(2*a); x2=(-b-sqrt(b*b-4*a*c))/(2*a); coutx1=x1endl; coutx2=x2endl; } return 0; } 例题3.5(2)改进程序 #include iostream #include cmath using namespace std; int main( ) { float a,b,c,x1,x2,discrim; coutInput a b c values:endl; cinabc; //1 2 1 discrim = b * b - 4 * a * c; if (discrim 0) coutThe equation has no real roots!endl; else if (discrim==0) // (double root)1 2 1 (different root)1 3 2 coutThere is a double root :-b/(2 * a)endl; else { x1=(-b+sqrt(b*b-4*a*c))/(2*a); x2=(-b-sqrt(b*b-4*a*c))/(2*a); coutThe solutions: x1=x1 x2=x2endl; } return 0; } * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 图3.18 图3.19 * 例: 求部分级数和 【例3.13】计算sin(x)的值,公式为: 当第n项的绝对值小于10-5时结束。 分析:关键是找部分级数和的通项,如下表示: ti+2= -1*ti*x*x/((i+1)*(i+2)) i=1,3,5,7… t=x; while(fabs(t)=1e-5) { sinx+=t; t=-t*x*x/((i+1)*(i+2)); i=i+2; } 例3.12 用下面公式求π的近似值。π/4≈1-1/3+1/5-1/7+…直到最后一项的绝对值小于10-7为止。 根据给定的算法很容易编写程序如下: #include iostream #include iomanip #include cmath using namespace std; int main( ) {int s=1; double n=1,t=1,pi=0; //注意:这里的double数据类型 while((fabs(t))1e-7) {pi=pi+t; n=n+2; 3.14 编写循环结构的程序 s=-s; t=s/n; } pi=pi*4; cout″pi=″setiosflags(ios∷fixed)setprecision(6)piendl; return 0; } 运行结果为 pi=3.141592 注意: 不要把n定义为整型变量,否则在执行“t=s/n;”时,得到t的值为0(原因是两个整数相除)。 * 例:3-(2)乘法口诀表 //3-2-(1).cpp #include iostream using namespace std; void main() { cout\t 九九乘法表endl; cout\t -----------endl; for(int i=1;i=9;i++) { for(int j=1;j=i;j++) couti×j=i*j\t; coutendl;

文档评论(0)

shuwkb + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档