C++经典题目
创始人
2024-04-19 19:00:03
0

目录

P62 3.6 求圆周长面积

P80 3 华氏转摄氏

P80 10 分段函数

P81 21 数列求和

P82 24 打印图形

P229 6 长方体体积

P384 4 printArea


题目来源于C++程序设计(第4版)

P62 3.6 求圆周长面积

设圆半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。

#include
using namespace std;
#define pi 3.14;
int main()
{int r,h;int L, S, S1, V, V1;cin >> r >> h;L = r*2*pi;S = r * r * pi;S1 =  r * r* 4 * pi;V =  r * r * r * pi;V = V * 4 / 3;V1 = S * h;cout << "圆周长L=" << L<

P80 3 华氏转摄氏

输入一个华氏温度,要求输出摄氏温度。公式为:c=5(F-32)/9,输出要有文字说明,取两位小数

#include
#include "string" 
using namespace std;
void main()
{float F,c;cin >> F;c = 5.0 / 9.0 * (F - 32);cout << "c=" <

P80 10 分段函数

有一函数:

编写一程序,输入x,输出y的值。(有一说一,csdn的公式插入很烂)

#include
using namespace std;
int main()
{int x, y;cin >> x ;if (x < 1)y = x;else if (x < 10)y = 2 * x - 1;else y = 3 * x - 11;cout << y;
}

 

P81 21 数列求和

有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13,...

求出这个数列的前20项之和。


#include
#include
using namespace std;
int main()
{double a = 2, b = 3, c = 1, d = 2;double sum=0;for (int i = 1; i < 11; i++){sum += (a / c + b / d);a = a + b;b = a + b;c = c + d;d = c + d;}cout << setiosflags(ios::fixed) << setprecision(2)<

P82 24 打印图形

输出以下图案:

#include
using namespace std;
int main()
{int i, j;for (i = 1; i < 5; i++){for (j = 1; j <= (i - 1) * 2 + 1; j++){cout << "*";}putchar('\n');}i-=2; j-=2;//前面循环出来行数多一次,下一面循环行数少一次,共2次for (; i>0; i--){for (j= (i - 1) * 2 + 1; j>0; j--){cout << "*";}putchar('\n');}}

P229 6 长方体体积

需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、height(高)。要求用成员函数实现以下功能:

(1)由键盘分别输入3个长方柱的长、宽、高;

(2)计算长方柱的体积;

(3)输出3个长方柱的体积。

请编写程序,上机调试并运行

#include
using namespace std;
class A
{
private:int length, width, height,V;
public:void calculateV();void set();void show();
};
void A::set()
{cin >> length >> width >> height;
}
void A::calculateV()
{V=length * width * height;}
void A::show()
{calculateV();cout << "长方柱的体积:" << V;}
int main()
{A a;a.set();a.show();
}

P384 4 printArea

编写一个程序,声明抽象基类Shape,由它派生出来3个派生类Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数printArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。

#include
using namespace std;
class Shape
{
public:virtual double area() const=0;
};class Circle :public Shape
{
private:double pi = 3.14,r;
public :Circle(double r);virtual double area() const;
};double Circle::area() const
{return pi * r * r;
}Circle::Circle(double r)
{this->r = r;
}class Rectangle :public Shape
{
private:double l, w;
public:Rectangle(double a, double b) :l(a), w(b) {}virtual double area() const;};double Rectangle::area() const
{ return l * w;
}class Triangle :public Shape
{
private:double l, h;
public:Triangle(double a, double b) :l(a), h(b) {}virtual double area() const{return l * h * 0.5;}};void printArea(const Shape& s)
{cout << s.area() << endl;
}int main()
{Circle circle(3.0);Triangle triangle(4.0, 8.0);Rectangle rectangle(4.0, 8.0);cout << "area of circle = ";printArea(circle);cout << "area of triangle =";printArea(triangle);cout << "area of rectangle =";printArea(rectangle);
}

 

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...