麻烦会的给出代码!谢谢了!最好是VC6编译的!!谢谢了!!!!
C++程序题!急求代码!!!!
- 提问者网友:皆是孤独
- 2021-04-20 23:51
- 五星知识达人网友:执傲
- 2021-04-21 00:59
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
class shape {
public:
enum type { C, R, T };
virtual double area() const = 0;
type getType() const
{
return t;
}
virtual ~shape() { }
protected:
type t;
};
class circle : public shape {
public:
circle( double _x, double _y, double _r )
: x( _x ), y( _y ), r( _r )
{ shape::t = C; }
double area() const
{
return PI * r * r;
}
double getRadius() const
{
return r;
}
private:
double x, y , r;
static const double PI;
};
const double circle::PI = 3.1415926;
class rectangle : public shape {
public:
rectangle( double _x1, double _y1, double _x2, double _y2 )
: x1( _x1 ), y1( _y1 ), x2( _x2 ), y2( _y2 )
{ shape::t = R; }
double area() const
{
return fabs( x1-x2 ) * fabs( y1-y2 );
}
private:
double x1, y1, x2, y2;
};
class triangle : public shape {
public:
triangle( double _x1, double _y1, double _x2, double _y2, double _x3, double _y3 )
: x1( _x1 ), y1( _y1 ), x2( _x2 ), y2( _y2 ), x3( _x3 ), y3( _y3 )
{ shape::t = T; }
double area() const
{
return fabs( x1*y2 + y1*x3 + x2*y3 - x1*y3 - y1*x2 - y2*x3 ) / 2;
}
private:
double x1, y1, x2, y2, x3, y3;
};
int main()
{
vector<shape*> v;
ifstream inFile( "shape.txt" );
while( inFile ) {
char c = inFile.get();
if ( c == 'C' ) {
double x, y, r;
inFile >> x >> y >> r;
v.push_back( new circle( x, y, r ) );
} else if ( c == 'R' ) {
double x1, y1, x2, y2;
inFile >> x1 >> y1 >> x2 >> y2;
v.push_back( new rectangle( x1, y1, x2, y2 ) );
} else if( c == 'T' ) {
double x1, y1, x2, y2, x3, y3;
inFile >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
v.push_back( new triangle( x1, y1, x2, y2, x3, y3 ) );
} else if ( c == 'X' ) {
inFile.close();
}
}
const char* p[] = { "Circle", "Rectangle", "Triangle" };
cout.setf( ios::fixed | ios::left );
cout << setw( 12 ) << "Type" << setw( 20 ) << "Area" << setw( 10 ) << "Radius" << "\n\n";
for ( size_t i = 0; i < v.size(); ++i ) {
shape::type t = v[i]->getType();
cout << setw( 12 ) << p[t] << setw( 20 ) << v[i]->area();
if ( t != shape::C )
cout << setw( 10 ) << "/" << '\n';
else
cout << dynamic_cast<circle*>(v[i])->getRadius() << "\n";
}
}
望采纳
- 1楼网友:杯酒困英雄
- 2021-04-21 02:29
#include<iostream> #include<cmath> using namespace std;
const double PI=3.14;
class Shape { public: char c; virtual double Area() { return 0; } };
class Circle: public Shape { public: double x,y,r; double get() { return r; } double Area() { return PI*r*r; } };
class Rectangle:public Shape { public: double x1,y1,x2,y2; double Area() { return fabs(x1-x2)*fabs(y1-y2); } };
class Triangle:public Shape { public: double x1,y1,x2,y2,x3,y3; double Area() { return fabs(x1*y2+y1*x3+x2*y3-x1*y3-y1*x2-y2*x3)/2; } };
int main() {
Shape *sp; char c; while(cin>>c) { if(c=='X') break; if(c=='C') { Circle cp; cin>>cp.x>>cp.y>>cp.r; sp=&cp; } else if(c=='T') { Triangle tp; cin>>tp.x1>>tp.y1>>tp.x2>>tp.y2>>tp.x3>>tp.y3; sp=&tp; } else if(c=='R') { Rectangle rp; cin>>rp.x1>>rp.y1>>rp.x2>>rp.y2; sp=&rp; } cout<<"面积是:"<<sp->Area()<<endl; } return 0; }