没有合适的资源?快使用搜索试试~ 我知道了~
C++课后习题解答.docx
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 91 浏览量
2023-02-27
20:19:42
上传
评论
收藏 32KB DOCX 举报
温馨提示
试读
13页
.
资源推荐
资源详情
资源评论
C++课后习题程序
3-14.用递归的方法编写程序求 N 阶勒让德多项式的值,在主程序中实现输入输出。递归公
式为
1
(n=0)
(n=1)
(n>1)
Pn(x)= x
[(2n-1)x*Pn-1(x)-(n-1)Pn-2(x)]/n
#include<iostream>
using namespace std;
long p(int x,int y)
{ if(y==0)
//Legendre polynomials 函数定义
return 1;
else if(y==1)
return x;
else
return (((2*y-1)*x*p(x,y-1)-(y-1)*p(x,y-2))/y);
}
void main()
{ int a;
//legendre polynomials 函数中 x 的值
//级数 n
int n;
cout<<"请输入 legendre polynomials 函数中 x 的值:";
cin>>a;
cout<<"请输入所要求的多项式级数:";
cin>>n;
p(a,n);
cout<<"n 阶勒让德多项式的值为(当 x="<<a<<"时):";
cout<<p(a,n)<<endl;
//输出多项式的值
}
4-8 定义一个 Dog 类,包含的age、weight 等属性,以及对这些属性操作的方法。实现并测
试这个类。
(第一个程序待调试)
#include <iostream.h>
class Dog
{ public:
Dog (int initialAge = 0, int initialWeight = 5);
~Dog();
int GetAge() { return itsAge;} // inline!
void SetAge (int age) { itsAge = age;} // inline!
int GetWeight() { return itsWeight;} // inline!
void SetWeight (int weight) { itsAge = weight;} // inline!
private:
int itsAge, itsWeight;
};
Dog::Dog(int initialAge, int initialWeight)
{itsAge = initialAge;
itsWeight = initialWeight;
}
Dog::~Dog() //destructor, takes no action
{
}
int main()
{ Dog Jack(2,10);
cout << "Jack is a Dog who is " ;
cout << Jack.GetAge() << " years old and";
cout << Jack.GetWeight() << " pounds weight.\n";
Jack.SetAge(7);
Jack.SetWeight(20);
cout << "Now Jack is " ;
cout << Jack.GetAge() << " years old and";
cout << Jack.GetWeight() << " pounds weight.";
return 0;}
程序二:
#include <cstdlib>
#include<iostream>
using namespace std;
class dog
{ private:
int m_age;
int m_weight;
public:
dog():m_age(5),m_weight(10){}
void setAge(int i){m_age=i;}
void setWeight(int i){m_weight=i;}
int age(){return m_age;}
int weight(){return m_weight;}
};
int main()
{ dog d;
cout<<d.age()<<" "<<d.weight()<<endl;
return 0;
}
4-9 设计并测试一个名为 Rectangle 的矩形类,其属性为矩形的左下角与右上角两个点的坐
标,能计算矩形的面积。
#include <iostream.h>
class Rectangle
{ public:
Rectangle (int top, int left, int bottom, int right);
~Rectangle () {}
int GetTop() const { return itsTop; }
int GetLeft() const { return itsLeft; }
int GetBottom() const { return itsBottom; }
int GetRight() const { return itsRight; }
void SetTop(int top) { itsTop = top; }
void SetLeft (int left) { itsLeft = left; }
void SetBottom (int bottom) { itsBottom = bottom; }
void SetRight (int right) { itsRight = right; }
int GetArea() const;
private:
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
Rectangle::Rectangle(int top, int left, int bottom, int right)
{ itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;
}
int Rectangle::GetArea() const
{ int Width = itsRight-itsLeft;
int Height = itsTop - itsBottom;
return (Width * Height);
}
int main()
{ Rectangle MyRectangle (100, 20, 50, 80 );
int Area = MyRectangle.GetArea();
cout << "Area: " << Area << "\n";
return 0;
}
4-11 定义一个矩形类,有长、宽两个属性,有成员函数计算矩形的面积
#include <iostream.h>
class Rectangle
{ public:
Rectangle(float len, float width)
{Length = len;
Width = width;
}
~Rectangle(){};
float GetArea() { return Length * Width; }
float GetLength() { return Length; }
float GetWidth() { return Width; }
private:
float Length;
float Width;
};
void main()
{ float length, width;
cout << "请输入矩形的长度:";
cin >> length;
cout << "请输入矩形的宽度:";
cin >> width;
Rectangle r(length, width);
cout << "长为" << length << "宽为" << width << "的矩形的面积为:"
<< r.GetArea () << endl;
}
4-12 定义一个"数据类型" datatype 类,能处理包含字符型、整型、浮点型三种类型的数
据,给出其构造函数。
#include <iostream.h>
剩余12页未读,继续阅读
资源评论
xxpr_ybgg
- 粉丝: 6513
- 资源: 3万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功