《面向对象的程序设计》大作业报告
班级:计科 1204 学号:1030412426
姓名 孙彦苹
成绩
(注:按章组织题目)
题目 2.1
内容:输入一个十进制数(正负均可),输出它的二进制形式。
设计思路:分正数,负数;运用位运算;数组。
程序代码:
#include
using namespace std;
void Create(int b[],short s){
int i;
for(i=0;i<16;i++){
b[i]=s&0x01;
s=s>>1;
}
}
void Display(int b[]){
int i;
for(i=15;i>=0;i--)
cout<>s;
if(s>=0){
Create(b,s);
cout<<"数的二进制形式:";
Display(b);
}
else{
s=(s-0x01)^0x7fff;
Create(b,s);
cout<<"数的二进制形式:";
Display(b);
}
}
运行结果:
结论:1 位运算的使用:实现二进制位末置 1,各位取反。
2 数组倒序输出。
题目 2.2
内容:设计一个计算器菜单界面。
设计思路:1 计算器界面:+-*/
2 实现循环:选择操作类型+-*/
程序代码:
#include
using namespace std;
void main(){
double n,i;
char ch;
cout<<"Enter a figure:";
cin>>n;
cout<<"Menu:A(dd) M(inus) P(lus) D(ivid) Q(uit)"<>ch;
while(ch!='Q'){
cout<<"Enter another figure:";
cin>>i;
if(ch=='A')
n+=i;
if(ch=='M')
n-=i;
if(ch=='P')
n*=i;
if(ch=='D')
n/=i;
cout<<"Result= "<>ch;
}
}
运行结果:
结论:循环的控制条件;界面的美化。
题目 3.13
内容:用递归编写函数求 fibonacci 级数,公式为 f(n)=f(n-1)+f(n-2)(n>2),f(1)=f(2)=1
设计思路:1 递归函数 2 函数的调用
程序代码:
#include
using namespace std;
int Fibonacci(int n){
if(n==1||n==2)
return 1;
else
return Fibonacci(n-1)+Fibonacci(n-2);
}
void main(){
int n;
cout<<"请输入要操作的数:";
cin>>n;
cout<
#define Maxsize 10
using namespace std;
template
T Max(T b[],int n){
int i;
T temp;
for(i=0;ib[i+1]){
temp=b[i];
b[i]=b[i+1];
b[i+1]=temp;
}
return b[n-1];
}
void main(){
double a[Maxsize];
int b[Maxsize];
char c[Maxsize];
int n,i;
cout<<"please enter the number of figures 整型数:"<>n;
cout<<"please enter the figures:"<>b[i];
cout<<"The max of the figures:"<>n;
cout<<"please enter the figures:"<>a[i];
cout<<"The max of the figures:"<>n;
cout<<"please enter the figures:"<>c[i];
cout<<"The max of the figures:"<
#define Maxsize 10
using namespace std;
int Max(int b[],int n){
int i;
int temp;
for(i=0;ib[i+1]){
temp=b[i];
b[i]=b[i+1];
b[i+1]=temp;
}
return b[n-1];
}
double Max(double b[],int n){
int i;
double temp;
for(i=0;ib[i+1]){
temp=b[i];
b[i]=b[i+1];
b[i+1]=temp;
}
return b[n-1];
}
char Max(char b[],int n){
int i;
char temp;
for(i=0;ib[i+1]){
temp=b[i];
b[i]=b[i+1];
b[i+1]=temp;
}
return b[n-1];
}
void main(){
double a[Maxsize];
int b[Maxsize];
char c[Maxsize];
int n,i;
cout<<"please enter the number of figures 整型数:"<>n;
cout<<"please enter the figures:"<>b[i];
cout<<"The max of the figures:"<>n;
cout<<"please enter the figures:"<>a[i];
cout<<"The max of the figures:"<>n;
cout<<"please enter the figures:"<>c[i];
cout<<"The max of the figures:"<
#include
class Point{
public:
Point(int a=0,int b=0){
x=a;
y=b;
}
Point(Point &p);
int getx(){return x;}
int gety(){return y;}
private:
int x,y;
};
Point::Point(Point &p){
x=p.x;
y=p.y;
}
class Rectangle{
public:
Rectangle(Point x1,Point x2);
Rectangle(Rectangle &r);
void show_l_w(){
cout<<"the length:"<>n>>m;
Point
b(n,m);