实验六 模块化设计1.docx

上传人:b****3 文档编号:10820275 上传时间:2023-05-27 格式:DOCX 页数:15 大小:31.62KB
下载 相关 举报
实验六 模块化设计1.docx_第1页
第1页 / 共15页
实验六 模块化设计1.docx_第2页
第2页 / 共15页
实验六 模块化设计1.docx_第3页
第3页 / 共15页
实验六 模块化设计1.docx_第4页
第4页 / 共15页
实验六 模块化设计1.docx_第5页
第5页 / 共15页
实验六 模块化设计1.docx_第6页
第6页 / 共15页
实验六 模块化设计1.docx_第7页
第7页 / 共15页
实验六 模块化设计1.docx_第8页
第8页 / 共15页
实验六 模块化设计1.docx_第9页
第9页 / 共15页
实验六 模块化设计1.docx_第10页
第10页 / 共15页
实验六 模块化设计1.docx_第11页
第11页 / 共15页
实验六 模块化设计1.docx_第12页
第12页 / 共15页
实验六 模块化设计1.docx_第13页
第13页 / 共15页
实验六 模块化设计1.docx_第14页
第14页 / 共15页
实验六 模块化设计1.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验六 模块化设计1.docx

《实验六 模块化设计1.docx》由会员分享,可在线阅读,更多相关《实验六 模块化设计1.docx(15页珍藏版)》请在冰点文库上搜索。

实验六 模块化设计1.docx

实验六模块化设计1

实验六模块化设计

实验目的

(1)体验结构化程序设计方法要点,理解模块化设计的思想,划分子模块的原则

(2)掌握is函数的定义和调用

实验内容

任务一改错求PI值error02_02.cpp

打开程序文件error02_02.cpp改正程序中的错误。

根据下式求PI值,直到某一项小于10-6

PI/2=1+1!

/3+2!

/(3*5)+……+n!

/(3*5*…*(2n-1))

正确运行结果

PI=3.14159

#include

intfact(intn);

intmulti(intn);

intmain(void)

{

inti;

doublesum,item,eps;

eps=1e-6;

sum=1;

item=1;

for(i=1;item>=eps;i++){

item=fact(i)/multi(2*i+1);

sum=sum+item;

}/*调试时设置断点*/

printf("PI=%0.5lf\n",sum*2);

return0;

}

intfact(intn)

{

inti;

doubleres;

res=1;

for(i=0;i<=n;i++)

res=res*i;

returnres;/*调试时设置断点*/

}

intmulti(intn)

{

inti;

doubleres;

res=1;

for(i=3;i<=n;i=i+2)

res=res*i;

returnres;/*调试时设置断点*/

}

任务二改错输出逆序数error02_03.cpp

打开程序文件error02_03.cpp改正程序中的错误。

输入2个整数,分别将其逆向输出,要求定义并调用函数fun(n),它的功能是返回n的逆向值

正确运行结果

123-910

123的逆向是321

-910的逆向是-19

#include

intmain(void)

{

intm1,m2;

scanf("%d%d",&m1,&m2);

printf("%d的逆向是%d\t",n,fun(m1));

printf("%d的逆向是%d\n",m,fun(m2));

return0;

}

intfun(intn)

{

intm,y;

y=0;

if(n<0)

m=n;

else

m=-n;

while(m==0){

y=y*10+m/10;

m=m%10;

}

if(n>=0)

returny;

else

return-y;

}

#include

#include"stdlib.h"

intfun(intn);

voidmain(void)

{

intm1,m2;

scanf("%d%d",&m1,&m2);

printf("%d的Ì?

逆?

向¨°是º?

%d\t",m1,fun(m1));

printf("%d的Ì?

逆?

向¨°是º?

%d\n",m2,fun(m2));

system("pause");

}

intfun(intn)

{

intm,y;

y=0;

if(n<0)

m=-n;

elsem=n;

while(m!

=0){

y=y*10+m%10;

m=m/10;

}

if(n>=0)

returny;

else

return-y;

}

任务三改写输出m-n之间所有的素数(ex6_1.cpp)

改写(sample02_07.cpp)保存为ex6_1.cpp:

输出m-n之间所有的素数,调用intisprime(intn)函数,每行输出10个素数。

1.源程序清单:

ex6_1.cpp

2。

实现算法(自然语言描述)

任务四改写打印表格ex6_2.cpp

改写实验五任务四ex5_3.cpp,保存为ex02_07.cpp

(1)增加函数voidPrtLine(intbottom,intheight),已知底长和高,输出一行表格数据。

(2)增加函数voidPrtBody(void)打印表格除表头和标题以外的主体内容部分,调用PrtLine实现。

(3)在main函数中输出表头和标题,调用PrtBody打印表格主体。

源程序清单:

ex6_2.cpp

#include

#include

voidPrtBody(void);

voidPrtLine(intbottom,intheight);

intmain(void)

{printf("木?

材?

尺?

寸ä?

横¨¢截?

面?

积y惯?

性?

力¢|矩?

截?

面?

系¦Ì数ºy\n");

PrtBody();

system("pause");

}

voidPrtBody(void)

{intbottom,height;

doublea,b,c;

bottom=2;

while(bottom<=10)

{for(height=2;height<=12;height=height+2)

{PrtLine(bottom,height);}

bottom=bottom+2;

}

}

voidPrtLine(intbottom,intheight)

{doublea,b,c;

a=bottom*height;

b=bottom*height*height*height/12;

c=bottom*height*height/6;

printf("%d*%d%12.1lf%12.1lf%12.1lf\n",bottom,height,a,b,c);

}

 

任务五编程给小学生出加法考试题ex6_3.cpp

编写一个程序,给学生出一道加法运算题,然后判断学生输入的答案对错与否,按下列要求以循序渐进的方式编程。

程序1通过输入两个加数给学生出一道加法运算题,如果输入答案正确,则显示“Right!

”,否则显示“Notcorrect!

Tryagain!

”,程序结束。

(ex6_3_1.cpp)

程序2通过输入两个加数给学生出一道加法运算题,如果输入答案正确,则显示“Right!

”,否则显示“Notcorrect!

Tryagain!

”,直到做对为止。

(ex6_3_2.cpp)

程序3通过输入两个加数给学生出一道加法运算题,如果输入答案正确,则显示“Right!

”,否则提示重做,显示“Notcorrect!

Tryagain!

”,最多给三次机会,如果三次仍未做对,则显示“Notcorrect!

Youhavetriedthreetimes!

Testover!

”,程序结束。

(ex6_3_3.cpp)

程序4连续做10道题,通过计算机随机产生两个1~10之间的加数给学生出一道加法运算题,如果输入答案正确,则显示“Right!

”,否则显示“Notcorrect!

”,不给机会重做,10道题做完后,按每题10分统计总得分,然后打印出总分和做错的题数。

(ex6_3_4.cpp)

程序5通过计算机随机产生10道四则运算题,两个操作数为1~10之间的随机数,运算类型为随机产生的加、减、乘、整除中的任意一种,如果输入答案正确,则显示“Right!

”,否则显示“Notcorrect!

”,不给机会重做,10道题做完后,按每题10分统计总得分,然后打印出总分和做错题数。

(ex6_3_5.cpp)

【思考题】如果要求将整数之间的四则运算题改为实数之间的四则运算题,那么程序该如何修改呢?

请修改程序,并上机测试程序运行结果。

源程序清单:

(ex6_3_5.cpp)

#include

#include"stdlib.h"

#include"time.h"

voidplus(doublea,doubleb);

voidminus(doublea,doubleb);

voidmultiply(doublea,doubleb);

voiddivide(doublea,doubleb);

intcor=0,incor=0;

voidmain()

{

doublea,b;

inti,sign;

for(i=1;i<=10;i++)

{

srand((unsigned)time(NULL));

a=rand()%1000/100.0;b=rand()%1000/100.0;

sign=rand()%2;

if(sign==1)

{sign=rand()%2;

if(sign==1)

plus(a,b);

else

minus(a,b);

}

else

{

sign=rand()%2;

if(sign==1)

multiply(a,b);

else

divide(a,b);

}

}

printf("Allinall%dcorrect%dincorrect",cor,incor);

system("pause");

}

 

voidplus(doublea,doubleb)

{

doubleans,rans;intk;

printf("%.2f+%.2f=?

\n",a,b);

rans=a+b;

k=rans*100;

rans=k/100.0;

scanf("%lf",&ans);

if(ans==rans)

{printf("right\n");

cor++;}

else{printf("notcorrect\n");incor++;}

}

voidminus(doublea,doubleb)

{

doubleans,rans;intk;

printf("%.2f-%.2f=?

\n",a,b);

rans=a-b;

k=rans*100;

rans=k/100.0;

scanf("%lf",&ans);

if(ans==rans){printf("right\n");cor++;}

else{printf("notcorrect\n");incor++;}

}

voidmultiply(doublea,doubleb)

{

doubleans,rans;intk;

printf("%.2f*%.2f=?

(保留两位小数)\n",a,b);

rans=a*b;

k=rans*100+0.5;

rans=k/100.0;

scanf("%lf",&ans);

if(ans==rans){printf("right\n");cor++;}

else{printf("notcorrect\n");incor++;}

}

voiddivide(doublea,doubleb)

{doubleans,rans;intk;

printf("%.2f/%.2f=?

(保留两位小数)\n",a,b);

rans=a/b;

k=rans*100+0.5;//四舍五入保留两位小数

rans=k/100.0;

scanf("%lf",&ans);

if(ans==rans){printf("right\n");cor++;}

else{printf("notcorrect\n");incor++;}

}

任务六编程菜单程序(ex6_4.cpp)

1.运行程序,了解程序功能

(1)在cmd窗口运行程序graph.exe,在菜单程序中分别输入1、2、3、4、5、6,观察程序的运行结果,输入0程序结束。

(2)下图为graph.exe的程序模块图,打开ex6_4.cpp,文件给出了程序的框架结构,请根据程序的运行过程,确定每一个模块的功能。

2.编写函数,完整程序ex02_10.cpp并上交源程序

打开ex02_10.cpp,文件中已给出了函数displayMenu和processChoice的代码。

请编写6个打印图案的程序,要求输入n,打印n行如graph.exe运行所示的图形,并在main函数中调用函数displayMenu和processChoice完成菜单程序。

3.写出函数printStar_1,printStar_2,printStar_3,printStar_4,printStar_5,printStar_6的算法。

#include

#include

voidprocessChoice(intindex);

voiddisplayMenu();

voidprintStar_1();

voidprintStar_2();

voidprintStar_3();

voidprintStar_4();

voidprintStar_5();

voidprintStar_6();

voidmain()

{intchoice;

while

(1)

{

}

}

voiddisplayMenu()

{

clrscr();

cout<<"**************************"<

cout<<"*1.patternone*"<

cout<<"*2.patterntwo*"<

cout<<"*3.patternthree*"<

cout<<"*4.patternfour*"<

cout<<"*5.patternfive*"<

cout<<"*6.patternsix*"<

cout<<"*0.exit*"<

cout<<"**************************"<

cout<<"pleasechoice(0,1,2,3,4,5,6):

";

}

voidprocessChoice(intindex)

{switch(index)

{case1:

printStar_1();break;

case2:

printStar_2();break;

case3:

printStar_3();break;

case4:

printStar_4();break;

case5:

printStar_5();break;

case6:

printStar_6();break;

}

}

voidprintStar_1()

{inti,j;

cout<<"patternone"<

inti,j;

for(i=1;i<=10;i++)

{for(j=1;j<=i;j++)

printf("*");

printf("\n");

getch();

}

voidprintStar_2()

{inti,j;

cout<<"patterntwo"<

inti,j;

for(i=1;i<=10;i++)

{for(j=10;j>=i;j--)

printf("*");

printf("\n");

getch();

}

voidprintStar_3()

{inti,j,k;

cout<<"patternthree"<

for(i=1;i<=10;i++)

{for(k=0;k

printf("");

for(j=10;j>=i;j--)

printf("*");

printf("\n");

getch();

}

voidprintStar_4()

{inti,j,k;

cout<<"patternfour"<

for(i=1;i<=10;i++)

{for(j=9;j>=i;j--)

printf("");

for(k=1;k<=i;k++)

printf("*");

printf("\n");}

getch();

}

voidprintStar_5()

{inti,j;

cout<<"patternfive"<

getch();

}

voidprintStar_6()

{inti,j;

cout<<"patternsix"<

getch();

}

任务七补充(选做)编程计算折线映射面积ex6_5.cpp

一对实数可以表示平面上的一个点的坐标,一系列的实数对,当这些数对里的第一个数为递增时,把它们表示的平面点连接起来,可以得到一条与x轴同方向的折线。

由一条这种折线、该折线两端引向x轴的垂线,以及x轴本身,能够形成一个封闭区域的边界。

请写一个程序,它接受一系列由标准输入得到的数对,最后计算这些数对所确定的区域的面积。

规定用一对零(0.0,0.0)表示输入的结束。

可先假定输入的数对中x的值总是递增的,y值均不为负。

进一步说,

(1)输入的数对中x的值不是递增的,也可视为输入结束。

(2)如果y值可以为负,如何修改程序。

G

创建程序ex02_11.cpp,设计程序的合理模块结构,编写函数,完成程序并上交源程序

问题分析:

程序结构模块图:

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2