基本信息
源码名称:多边形基类.CPP
源码大小:3.60KB
文件格式:.cpp
开发语言:C/C++
更新时间:2022-01-11
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


多边形基类.CPP

设计多边形基类CPolygon及其派生类CTriangle、CRectangel和CCircle类,为各个类设计虚函数Input()、Area()、Print(),实现从数据文件读入数据,从而生成各种多边形对象,并且计算面积和输出对象信息,数据文件格式如下:

    TRIANGLE  底边长   高

RECTANGLE  长   宽

CIRCLE  半径

每一行存储一个对象,第一单词位对象类型,其后是数据,具体实例:

TRIANGLE  30  5

RECTANGLE  12   7

TRIANGLE  20  3



       

int main()
{
FILE* stream;
stream = fopen("1.txt", "r");
if (stream == NULL)
{
cout << "can't open the file." << endl;
return 0;
}
CPolygon* as[3];
char sbuf[100];
int index = 0;
while (fgets(sbuf, 100, stream) != NULL && index < 10)
{
if (strncmp(sbuf, "CIRCLE", 6) == 0)
as[index] = new CCircle();
else if (strncmp(sbuf, "TRIANGLE", 8) == 0)
as[index] = new CTriangle();
else if (strncmp(sbuf, "RECTANGLE", 9) == 0)
as[index] = new CRectangle();
else break;
as[index]->Input(stream);
index ;
}
fclose(stream);
for (int i = 0; i < index; i )
{
as[i]->Show();
cout << endl;
delete as[i];
}
return 0;
}
.
└── 好例子网_课设1.cpp

0 directories, 1 file