将两个字符数组相互添加
Adding two character arrays into one another
我正在开发一个程序,该程序基本上应该列出冰淇淋及其成分,名称,价格和折扣,直到给定的代码中,我正在尝试采用2个不同的冰淇淋名称,如果说用户输入焦糖苹果软糖和巧克力,我需要它给我"焦糖苹果喜悦+巧克力", 而且由于我对运算符的工作方式不熟悉,并且这需要与操作员一起完成,我已经为此卡了几个小时,我遇到了类似的问题,我不知道如何做到这一点,我已经考虑过连接字符串,但当涉及到打印部分时没有任何意义。
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;
}
此解决方案使用装饰器设计模式。
不能使用 +
来连接char
数组。您需要使用 new
为新字符串分配空间,然后复制并连接成该字符串。
IceCream &operator +(const IceCream &i){
IceCream temp;
delete[] temp.name; // Free the string allocated by the default constructor
temp.name = new char[strlen(this->name) + strlen(i.name) + 1];
strcpy(temp.name, this->name);
strcat(temp.name, i.name);
return temp;
}
相关文章:
- C++向数组添加元素并调整数组大小
- 向对象数组 c++ 添加值
- 在 c++ 中将 Char 数组添加到 Char * 变量
- 向动态数组添加内容
- 动态数组添加元素而不创建结构变量/对象
- 将 2D 数组添加到 1D 数组
- 如何使用sprintf将UINT_64的数组添加到一个字符数组中
- 当循环崩溃时,不向数组添加单词
- 如何将对象数组添加到另一个类
- 将字符数组添加到 const string&in C++
- 如何将数组添加到列表中
- 向量数组C++ - 添加元素时的奇怪行为
- 将char数组添加到C 中的字符串
- strncpy向我的char数组添加了额外的字符
- 在C++中向数组添加项的最有效方法
- 将类对象的数组添加到一个主类对象中
- 将BSON数组添加到MongoDB 3.2文档并提取值(MongoCXX 3.2)(C++11)
- 我是否正确地为 2D 数组添加了行与列
- 读取文件内容,存储在数组中,向数组添加更多内容,然后将新数组存储在文件C++中
- C++类对象数组 - 添加新记录