gpt4 book ai didi

c++ - 将两个字符数组相加

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:43:58 24 4
gpt4 key购买 nike

我正在开发一个程序,基本上假设列出冰淇淋及其成分、名称、价格和折扣,直到代码中给出的点我正在尝试使用 2 个不同的冰淇淋名称,如果说用户输入焦糖苹果软糖和巧克力我需要它来给我“焦糖苹果软糖 + 巧克力”,因为我对运算符(operator)的工作方式不熟悉,而且这需要与运算符(operator)一起完成我已经被困了几个小时,我我遇到了类似的问题,但我不知道该怎么做,我考虑过连接字符串,但在打印部分时没有任何意义。

IceCream &operator +(const IceCream &i){
IceCream temp;
temp.name = strncpy(this->name) + strncat(i.name);
if(*this>temp){
delete [] temp.name;
temp.name=new char [strlen(this->name)+1];
strncpy(temp.name,this->name,strlen(this->name)+1);

}
return temp;
}

这是代码的其余部分,我知道我有一些问题,但这是我需要帮助解决的问题,因为我无法理解所有运算符的工作方式。

class IceCream{
private:
char *name;
char ingr[100];
float price;
int discount;

public:
IceCream(){name=new char [0];}
IceCream(char *name=" ", char* ingr=" ", float price=0.0, int discount=0){
this->name=new char[strlen(name)+1];
strcpy(this->name, name);
strcpy(this->ingr,ingr);
this->price=price;
this->discount=discount;
}

IceCream(const IceCream &i){
this->name=new char[strlen(i.name)+1];
strcpy(this->name,i.name);
strcpy(this->ingr,i.ingr);
this->price=i.price;
this->discount=i.discount;
}

~IceCream(){delete [] this->name;}

friend ostream& operator<<(ostream& out, IceCream &i){
if(i.discount>0){
out<<i.name<<": "<<i.ingr<<" "<<i.ingr" "<<"("i.discount")"<<endl;
}
else{
out<<i.name<<": "<<i.ingr<<" "<<i.price" "<<endl;
}
return out;
}


IceCream &operator ++(int){
discount+=5;
return *this;
}

IceCream &operator +(const IceCream &i){
IceCream temp;
temp.name = strncpy(this->name) + strncat(i.name);
if(*this>temp){
delete [] temp.name;
temp.ime=new char [strlen(this->name)+1];
strncpy(temp.name,this->name,strlen(this->name)+1);

}
return temp;
}

最佳答案

你可以改变你的设计并为这个问题做出更好的解决方案:

// DecoratorPattern.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

class IiceCream
{
public:
virtual void Make() = 0;
virtual ~IiceCream() { }

};

class SimpleIceCream: public IiceCream
{
public:
virtual void Make()
{
std::cout<<"\n milk + sugar + Ice cream Powder";
}


};

class IceCreamDecorator: public IiceCream
{

public:
IceCreamDecorator(IiceCream& decorator):m_Decorator(decorator)
{

}

virtual void Make()
{
m_Decorator.Make();
}
private:
IiceCream& m_Decorator;
};

class WithFruits : public IceCreamDecorator
{

public:
WithFruits(IiceCream& decorator):IceCreamDecorator(decorator)
{

}
virtual void Make()
{
IceCreamDecorator::Make();
std::cout<<" + Fruits";
}

};

class WithNuts : public IceCreamDecorator
{

public:
WithNuts(IiceCream& decorator):IceCreamDecorator(decorator)
{

}

virtual void Make()
{
IceCreamDecorator::Make();
std::cout<<" + Nuts";
}

};

class WithWafers : public IceCreamDecorator
{

public:
WithWafers(IiceCream& decorator):IceCreamDecorator(decorator)
{

}

virtual void Make()
{
IceCreamDecorator::Make();
std::cout<<" + Wafers";
}

};

int _tmain(int argc, _TCHAR* argv[])
{
IiceCream* pIceCreamSimple = new SimpleIceCream();
pIceCreamSimple->Make();

IiceCream* pIceCreamFruits = new WithFruits(*pIceCreamSimple);
pIceCreamFruits->Make();

IiceCream* pIceCreamNuts = new WithNuts(*pIceCreamFruits);
pIceCreamNuts->Make();

IiceCream* pIceCreamWafers = new WithWafers(*pIceCreamNuts);
pIceCreamWafers->Make();

delete pIceCreamSimple;
delete pIceCreamFruits;
delete pIceCreamNuts;
delete pIceCreamWafers;

return 0;
}

此解决方案使用 decorator design pattern .

关于c++ - 将两个字符数组相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43057360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com