两个C++程序练习,麻烦给出代码,谢谢

2025-04-10 23:38:41
推荐回答(1个)
回答1:

第一题:
#include
using namespace std;
class String
{
public:String(void);
String(int n);
String(char *a);
bool setStr(char *a);
bool setStrLen(int n);
int getStrLen(void);
bool getStr(char * a);
~String();
protected:char * data;
};
String::String(void)
{
data = new char[1];
data[0] = '\0';
}
String::String(int n)
{
data = new char[n+1];
data[0] = '\0';
}
String::String(char *a)
{
int n = strlen(a);
data = new char[n+1];
for ( int i=0; i < n; i++)
data[i] = a[i];
}
bool String::setStr(char *a)
{
int n = strlen(a);
if ( strlen(data) != n)
{
delete[] data;
data = new char[n+1];
}
for ( int i=0; i < n; i++)
data[i] = a[i];
data[i] = '\0';
return 1;
}
bool String::setStrLen(int n)
{
bool flage;
if ( strlen(data) != n)
{
int i,j;
j = strlen (data);
char *temp = new char[j+1];
for ( i= 0; i <=j; i++)
temp[i] = data[i];
delete[] data;
if (data = new char[n+1])
{
for (i =0; i < n && i < j; i++)
data[i] = temp[i];
data[n] = 0;
flage = 1;
}
else flage = 0;
delete[] temp;
}
else flage = 1;
return flage;
}
int String::getStrLen(void)
{
int i = 0;
while ( data[i] !='\0' ) i++;
return i;
}
bool String::getStr(char *a)
{
for ( int i = 0,j = this->getStrLen(); i <= j; i++)
a[i] = data [i] ;
return 1;
}
String::~String()
{
delete[] data;
}

int main()
{
String str;
char *a = "Hello!";
str.setStr(a);
char *b;
b = new char[str.getStrLen()];
str.getStr(b);
cout << b << endl;
str.setStrLen\(4);
str.getStr(b);
cout << b << endl;
str.setStr(a);
str.getStr(b);
cout << b << endl;
return 0;
}
测试可行
第二题:
#include
#include
using namespace std;
#define PI 3.1415926
class Circle
{
public:Circle(void): x(0),y(0),r(0){};
Circle(double x,double y): x(x),y(y),r(0){};
Circle(double r): x(0),y(0),r(r){};
Circle(double x,double y,double r): x(x),y(y),r(r){};
~Circle() {};
void setP(double x,double y);
void setX(double x);
void setY(double y);
void setR(double r);
void set(double x,double y,double r);
double getX(void);
double getY(void);
double getR(void);
double getArea(void);
double getCirLen(void);
bool contain(double x,double y);
void show(void);
protected:double x;
double y;
double r;
};
void Circle::setP(double x,double y)
{
this->x = x;
this->y = y;
}
void Circle::setX(double x)
{
this->x = x;
}
void Circle::setY(double y)
{
this->y = y;
}
void Circle::setR(double r)
{
this->r = r;
}
void Circle::set(double x,double y,double r)
{
this->setP(x,y);
this->setR(r);
}
double Circle::getX(void)
{
return x;
}
double Circle::getY(void)
{
return y;
}
double Circle::getR(void)
{
return r;
}
double Circle::getArea(void)
{
return r*r*PI;
}
double Circle::getCirLen(void)
{
return r*PI*2;
}
bool Circle::contain(double x,double y)
{
double len = sqrt( (this->x -x) * (this->x -x) + (this->y -y) * (this->y -y) );
if (len <= this->r) return 1;
else return 0;
}
void Circle::show(void)
{
cout << "圆心:(" <getX() << "," << this->getY() << ")" << endl;
cout << "半径:" << this->getR() << endl;
cout << "面积:" << this->getArea() << endl;
cout << "周长:" << this->getCirLen() << endl;
}
int main()
{
Circle c(1,1,1);
c.show();
c.setX(2);
c.show();
c.setY(3);
c.show();
c.setR(4);
c.show();
c.setP(1,1);
c.show();
c.set(5,5,5);
c.show();
cout << "该圆" << ( c.contain(3,3)?"":"不" )<< "包含点(3,3)。" << endl;
return 0;
}
测试可行。
由于考虑到规范,把需要建立的函数都写出来了,还有测试用main函数,所以较长。

弄了好久了,分给我!