用c++编写一个基于对象的程序:输入半径,计算圆的周长和面积并输出。实现提示:设计一个圆类,包括至少一个数据成员radius(圆的半径);若干成员函数:计算周长的length( ),计算面积的area( ),输入半径的set(),输出有关数据的show()。设计main()函数,测试程序
时间: 2023-05-28 14:07:40 浏览: 95
#include <stdio.h>
#define PI 3.1415926
//定义圆类
typedef struct Circle{
double radius;
double length;
double area;
void (*set)(struct Circle*, double);
void (*length_calculate)(struct Circle*);
void (*area_calculate)(struct Circle*);
void (*show)(struct Circle*);
}Circle;
//设置半径
void set(Circle* c, double r){
c->radius = r;
}
//计算周长
void length_calculate(Circle* c){
c->length = 2 * PI * c->radius;
}
//计算面积
void area_calculate(Circle* c){
c->area = PI * c->radius * c->radius;
}
//输出信息
void show(Circle* c){
printf("半径为%.2f的圆的周长是%.2f,面积是%.2f\n", c->radius, c->length, c->area);
}
int main(){
Circle c;
c.set = set;
c.length_calculate = length_calculate;
c.area_calculate = area_calculate;
c.show = show;
double r;
printf("请输入圆的半径:");
scanf("%lf", &r);
c.set(&c, r);
c.length_calculate(&c);
c.area_calculate(&c);
c.show(&c);
return 0;
}
阅读全文