system(
"color 0A"
);
/*...ADT RECtangle is
Data:
float length,width;
Operations:
void InitRectangle(struct Rectangle& r,float len, float wid);
float Circumference(struct Rectangle& r);
float Area(struct Rectangle& r);
//r的类型名是struct Rectangle表示一个用户定义的记录(结构)类型 ,
//struct在C语言中必须保留,在C++中可以省略
//该类型包括矩形长度和宽度两个域,用于统一描述抽象数据类型的数据部分
end RECtangle 、
struct Rectangle{
float length,width;//对数据部分的统一描述
};
//初始化矩形数据的函数定义
void InitRectangle(struct Rectangle& r, float len, float wid) {
r.length = len;
r.width = wid; //把wid的值赋值给r.width
}
//求矩形周长和面积函数 矩形的引用参数
float Circumference(struct Rectangle){
return 2*(r.length+r,width);
}
float Area(struct Rectangle& r){
return r.length*r.width;
}
//小知识:函数参数分为:引用参数和值参数,若参数类型和参数名之间使用&则表示该参数定义为引用参数中,
//引用参数在C++ 中可以使用,在C语言中不能使用引用参数,在C语言中指针参数可以实现引用的功能
//采用C++类实现抽象数据类型RECtangle
class RECtangle{
private:
float length,width;
public:
RECtangle(float len, float wid){
lendth = len;
width = wid;
}
float Circumference(void){
return 2*(length+width);
}
float Area(void){
return length*width;
}
};...*/
//完整C++代码
#include<iostream> //在C语言中用include<stdio.h>代替
using namespace std;
struct Rectangle{
float length,width;
};
void InitRectangle(Rectangle& r, float len, float wid); //函数声明
float Circumference(Rectangle& r);
float Area(Rectangle& r);
main(void){
float x,y;
float p,s;
Rectangle a; //定义矩形变量
cout<<"请输入矩形的长和宽"<<endl; //输入提示信息
cin>>x>>y;
InitRectangle(a,x,y);//对矩形a初始化
p = Circumference(a);
s = Area(a);
cout<<endl;
cout<<"矩形的周长为:"<<p<<endl;
cout<<"矩形的面积为:"<<s<<endl;
}
void InitRectangle(Rectangle& r,float len,float wid){
r.length = len;
r.width = wid;
}
float Circumference(Rectangle& r)
{
return 2*(r.length+r.width);
}
float Area(Rectangle& r){
return r.length*r.width;
}
```C