目录
P62 3.6 求圆周长面积
P80 3 华氏转摄氏
P80 10 分段函数
P81 21 数列求和
P82 24 打印图形
P229 6 长方体体积
P384 4 printArea
题目来源于C++程序设计(第4版)
设圆半径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<
输入一个华氏温度,要求输出摄氏温度。公式为: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=" <
有一函数:
编写一程序,输入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;
}
有一分数序列: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)<
输出以下图案:
#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');}}
需要求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();
}
编写一个程序,声明抽象基类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);
}