1. 绪论(算法和数据结构笔记)

1. 绪论

● 抽象数据类型ADT的定义

/* 描述一种抽象数据类型(ADT)的模板:

ADT <抽象数据类型名> is

    Data:

        <数据描述>

    Operations:

        <操作声明>

ADT <抽象数据类型名> is

*/

 

/*抽象数据类型:

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);    //求面积

end RECtangle

*/

 

//具体矩形ADT的实现:

#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); /*函数声明*/

 

void main(void) {

    float x, y; //用于从键盘上输入一个矩形的长和宽

    float p, s; //用于保存矩形的周长和面积

    Rectangle a; //定义一个矩形变量

    cout<<"请输入一个矩形的长和宽!"<<endl; //输出提示信息

    cin>>x>>y; //输入矩形的长和宽

    InitRectangle(a,x,y); //对矩形a进行初始化

    p=Circumference(a); //计算矩形a的周长

    s=Area(a); //计算矩形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;

}

1. 绪论(算法和数据结构笔记)