问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

一道C++的题目,马上考试了不会写,帮下忙啦,十分感谢1

发布网友 发布时间:2023-10-19 06:24

我来回答

2个回答

热心网友 时间:2024-12-02 15:00

#include<cmath>
#include<iostream>

using namespace std;

class Point
{
public:
Point(int x = 0, int y = 0):_x(x), _y(y) {};
Point operator+(const Point &p)
{
Point a;
a._x = _x + p._x;
a._y = _y + p._y;
return a;
}
Point operator-(const Point &p)
{
Point a;
a._x = _x - p._x;
a._y = _y - p._y;
return a;
}
int getX() {return _x;}
int getY() {return _y;}
friend double distan(Point, Point);
private:
int _x;
int _y;
};

double distan(Point a, Point b)
{
return sqrt(pow(a._x - b._x, 2.0) + pow(a._y - b._y, 2.0));
}

void main()
{
Point a(1,1), b(3,2), c;
c = b - a;
cout<<c.getX()<<','<<c.getY()<<endl;
c = b + a;
cout<<c.getX()<<','<<c.getY()<<endl;
cout<<distan(a,b)<<endl;
}
两个点要怎么比较大小我不明白,所以没有写>

热心网友 时间:2024-12-02 15:00

#include <iostream>
#include <string.h>
using namespace std;

// Rudimentary string class
class String
{
public:
// constructors
String();
String(const char *const);
String(const String &);
~String();

// overloaded operators
char & operator[](unsigned short offset);
char operator[](unsigned short offset) const;
String operator+(const String&);
void operator+=(const String&);
String & operator= (const String &);

// General accessors
unsigned short GetLen()const
const char * GetString() const

private:
String (unsigned short); // private constructor
char * itsString;
unsigned short itsLen;
};

// default constructor creates string of 0 bytes
String::String()
{
itsString = new char[1];
itsString[0] = '\0';
itsLen=0;
}
String::String(const String & rhs)
{
itsLen=rhs.GetLen();
itsString=new char[itsLen+1];
for(unsigned short i=0;i<itsLen;i++)
itsString[i]=rhs[i];
itsString[itsLen]='\0';
cout<<"the time constructor:"<<endl;
}
// private (helper) constructor, used only by
// class methods for creating a new string of
// required size. Null filled.
String::String(unsigned short len)
{
itsString = new char[len+1];
for (unsigned short i = 0; i<=len; i++)
itsString[i] = '\0';
itsLen=len;
}

// Converts a character array to a String
String::String(const char * const cString)
{
itsLen = strlen(cString);
itsString = new char[itsLen+1];
for (unsigned short i = 0; i<itsLen; i++)
itsString[i] = cString[i];
itsString[itsLen]='\0';

}

// copy constructor

// destructor, frees allocated memory
String::~String ()
{
delete [] itsString;
itsLen = 0;
}

// operator equals, frees existing memory
// then copies string and size
String& String::operator=(const String & rhs)
{
if (this == &rhs)
return *this;
delete [] itsString;
itsLen=rhs.GetLen();
itsString = new char[itsLen+1];
for (unsigned short i = 0; i<itsLen;i++)
itsString[i] = rhs[i];
itsString[itsLen] = '\0';
return *this;
}

//nonconstant offset operator, returns
// reference to character so it can be
// changed!
char & String::operator[](unsigned short offset)
{
if (offset > itsLen)
return itsString[itsLen-1];
else
return itsString[offset];
}

// constant offset operator for use
// on const objects (see copy constructor!)
char String::operator[](unsigned short offset) const
{
if (offset > itsLen)
return itsString[itsLen-1];
else
return itsString[offset];
}

// creates a new string by adding current
// string to rhs
String String::operator+(const String& rhs)
{
unsigned short totalLen = itsLen + rhs.GetLen();
String temp(totalLen);
unsigned short i;
for ( i= 0; i<itsLen; i++)
temp[i] = itsString[i];
for (unsigned short j = 0; j<rhs.GetLen(); j++, i++)
temp[i] = rhs[j];
temp[totalLen]='\0';
return temp;
}

// changes current string, returns nothing
void String::operator+=(const String& rhs)
{
unsigned short rhsLen = rhs.GetLen();
unsigned short totalLen = itsLen + rhsLen;
String temp(totalLen);
unsigned short i;
for (i = 0; i<itsLen; i++)
temp[i] = itsString[i];
for (unsigned short j = 0; j<rhs.GetLen(); j++, i++)
temp[i] = rhs[i-itsLen];
temp[totalLen]='\0';
*this = temp;
}

int main()
{

String s1("initial test");
cout << "S1:\t" << s1.GetString() << endl;

char * temp = "Hello World";
s1 = temp;
cout << "S1:\t" << s1.GetString() << endl;

char tempTwo[20];
strcpy(tempTwo,"; nice to be here!");
s1 += tempTwo;
cout << "tempTwo:\t" << tempTwo << endl;
cout << "S1:\t" << s1.GetString() << endl;

cout << "S1[4]:\t" << s1[4] << endl;
s1[4]='x';
cout << "S1:\t" << s1.GetString() << endl;

cout << "S1[999]:\t" << s1[999] << endl;

String s2(" Another string");
String s3;
s3 = s1+s2;
cout << "S3:\t" << s3.GetString() << endl;

String s4;
s4 = "Why does this work?";
cout << "S4:\t" << s4.GetString() << endl;
string ss("abcdefg");
string &rwjx=ss;

return 0;
}
怎么样,实现了多种运算符的重载.
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
谁能给个单机版的风云之雄霸天下啊?? 求风云雄霸天下PC单机游戏WIN7版 雄霸天下任务指南 开心网001老房子卖了以后家具还有吗? 为什么001开心网买房子组件删除仓库里的东西都没了 请教一下,开心001的开心庄园里面的建材有好多富余的的 除了5元一个卖... 开心网001小号怎么给大号送房子? 开心网001多少级能送别人房子?多少级能接受别人给的房子? 开心网001果实或家具能送人吗 开心网(kaixin001)怎么买外地房子? 长安十二时辰演员表有哪些人? 怎么制作PPT里的柱形图呢?9 打印文件的时候怎么制作框图和图表呢? 贾斯汀比伯的全部歌,都是自己谱的曲吗? ...保险满两年每年交一万,要交五年,如果退保能得多少钱 ...四月份交的保险,交一万多块钱,以后不想交了,请问退保能退多少... justin bieber 的歌的词曲都是自己创作的吗? 一年内第二次修改技巧 Justin bieber的哪些歌是B宝他自己写的啊?? 松下相机SD卡提示需要格式化怎么办?1 如何格式化sd卡2 鱼肚是否属于内脏2 现在新媒体行业很火,在哪学的比较快? 天正给排水T20,怎样批量删除或隐藏选定的图块?求大神指点!1 俄罗斯无线网卡多少钱?我要去南萨哈林,打算带着笔记本去,不知... dnf帝血弑天后期装备和刷图技巧。 心大心细打十二生肖里的一只1 贾斯汀.比伯哪首歌是他自己写,问了很多人都有不一样的回答,我要绝对正... justin bieber贾斯汀比伯有几首自己的歌???... PDF左右上方下载状态图标如何去除?3 这是什么鸟?怎么养要喂什么给它? 一般累犯具有的标准条件是什么 天下足球十大经典足球歌曲673 鱼肚是否属于内脏2 求足球音乐42 山东省莱州的张氏家谱86 山东省莱州市驿道镇古台口村张姓祖籍是哪里,属于张姓中的哪一支...8 统统掠走中统的读音14 山东张姓的来历32 妊娠糖尿病吃什么主食好 寻找祖籍,我姓张祖籍山东莱州府掖县邑北葛溪村 有没有知道张姓族谱的?祖籍山东掖县张家大队(现在的莱州),爷...7 iphone金山电池 点数据功能→显示打开移动数据 关闭移动数据是啥意思... 飞机即将坠毁时,小伙子慌乱中扯开女飞行员的衣服当场呆掉8 3D MAX2016怎么添加骨骼?7 资生堂哪个系列适合26岁使用? 鉴别石头 请问图片是什么石头,说是很硬,切割机都切不动 怎么再申请一个 godaddy域名到期没有续费,后来是赎回的,请问,赎回付款... 小燕子从南方赶来了,为春光增添了许多生趣,我不由得想起了什么...32