A 计算圆面积
题目描述
描述
编写一个圆类Circle,实现半径的输入、面积的计算和输出。要求在类中分别写3个函数实现输入半径、计算面积、输出面积。
输入
输入一行,输入圆的半径(double类型)。
输出
输出一行,输出圆的面积(保留小数点后两位)。
输入样例 1
3
输出样例 1
28.27
提示
1、在输出面积前使用如下语句:cout<
2、使用acos(-1.0)来得到圆周率的值,可将其设置为常量,因此:const double PI=acos(-1.0);需要包含cmath头文件
AC代码
#include <iostream>
#include<cmath>
#include<iomanip>
using namespace std;
const double PI=acos(-1.0);
class Circle{
private:
double r;
public:
void Input();
double s();
void Output();
};
int main() {
Circle c;
c.Input();
c.s();
c.Output();
return 0;
}
void Circle::Input(){
cin>>r;
}
double Circle::s(){
return PI*pow(r,2);
}
void Circle::Output(){
cout<<setiosflags(ios::fixed)<<setprecision(2)<<s();
}
B 过道和栏杆的造价
题目描述
描述
编写一个圆类Circle,实现半径的输入、面积的计算和输出。要求在类中分别写3个函数实现输入半径、计算面积、输出面积。
输入
输入一行,输入圆的半径(double类型)。
输出
输出一行,输出圆的面积(保留小数点后两位)。
输入样例 1
3
输出样例 1
28.27
提示
1、在输出面积前使用如下语句:cout<
2、使用acos(-1.0)来得到圆周率的值,可将其设置为常量,因此:const double PI=acos(-1.0);需要包含cmath头文件
AC代码
#include<iostream>
using namespace std;
class Rectangle
{
private:
double length;
double width;
public:
Rectangle(double Length=10.,double Width=5.);
double Area();
double Perimeter();
};
int main()
{
double Length, Width;
std::cin>>Length>>Width;
Rectangle r1(Length, Width);
Rectangle r2(Length + 3, Width + 3);
cout<<(50 * r2.Perimeter())<<endl;
cout<<(240 * (r2.Area() - r1.Area()))<<endl;
return 0;
}
Rectangle::Rectangle(double Length, double Width)
{
length = Length;
width = Width;
}
double Rectangle::Area()
{
return length