南昌大学实验报告
---(3)程序控制结构
学生姓名: 学 号: 专业班级: 实验类型:□ 验证 □ 综合 ■ 设计 □ 创新 实验日期: 2017.11.3 实验成绩:
一、实验目的
(1)熟练应用输入输出语句设计简单的顺序结构程序。
(2)熟练应用关系表达式和逻辑表达式作为程序的控制条件,理解判断条件与程序流程的关系。
(3)熟练掌握while, do-while 和for三种循环语句的语法规则
二、实验内容
(1)输入圆的半径和圆柱的高,输出圆的周长和圆柱体的体积。
xylogx?e?sinz?x(2)计算的值(x=1,y=1,z=90)。
(3)输入3个整数,按照从大到小输出
(4)分别让x=0,1,4,5,6,10,20,30,40,50,100,200,500,计算下列函数的结果:
?1x?5且x?0?x?x?1?f(x)??x2?610?x?100且x?20
?1x取其他值??(5)把一个十进制整数转换成八进制或十六进制输出。 (6)输入n,求和:s??(?1)i?1ni?1*i!
(7)猴子摘下若干桃子,每天吃一半多1个(例如有20个,则吃11个),到第10天吃前发现只剩1个,编程求出摘了多少个桃子。
三、实验要求
1、需写出设计说明; 2、设计实现代码及说明 3、运行结果;
四、主要实验步骤
1.在编辑器中编写代码
2.用g++编译器编译,debug 3.运行
五、实验数据及处理结果
#include
using namespace std;
double r ; double area; double h;
double volume;
cout<<\ cin>>r>>h; area = PI*r*r; volume = area*h;
cout<<\
cout<<\
return 0; }
------------------------------------------------------------------------------------------------------
#include
int main() {
using namespace std; int x =1; int y = 1; int z = 90;
double res = log(x) + sqrt(exp(x)) + sin(z) + pow(x,y); cout< return 0; } 第二题主要就是一个C++函数库的使用。 ------------------------------------------------------------------------------------------------------ #include void disPlayInOrder(int a,int b,int c); int main() { using namespace std; int n1,n2,n3; cout<<\ cout<<\ cin>>n1>>n2>>n3; if(n1>n2 && n1>n3) disPlayInOrder(n1,n2,n3); else if(n2>n1 && n2 >n3) disPlayInOrder(n2,n1,n3); else disPlayInOrder(n3,n1,n2); return 0; } void disPlayInOrder(int a,int b,int c){ std::cout std::cout<<\ else std::cout<<\} ------------------------------------------------------------------------------------------------------ #include double f(int x); int main() { using namespace std; int x[]={0,1,4,5,6,10,20,30,40,50,100,200,500}; for(int c:x) cout<<\ return 0; } double f(int x){ double res; if(x!=0 && x<5) res = 1.0/x + x + 1; //这里写成了+1,与实际表达式结果可能又出入 else if(x!=20 && x<100 && x>=10) res = x*x + 6; else res = 1; return res; } 本题在最开始编译的时候出现了错误,因为代码中使用了C++11中才有的for-each循环,在编译命令中显示指出所用的C++标准就通过了编译。 ------------------------------------------------------------------------------------------------------ #include int main() { using namespace std; vector cout<<\ cin>>num; int numCopy = num; //change it to 8-based number while(num!=0){ v.push_back(num%8); num /= 8; } //print the result cout<<\ while(!v.empty()){ cout< cout< int tmp = numCopy; if(tmp<10) x = tmp +'0'; else x = tmp + ('A'-10); v2.push_back(x); numCopy /= 16; } //print the result cout<<\ while(!v2.empty()){ cout< cout<