将两个字符数组相互添加

Adding two character arrays into one another

本文关键字:数组 添加 字符 两个      更新时间:2023-10-16

我正在开发一个程序,该程序基本上应该列出冰淇淋及其成分,名称,价格和折扣,直到给定的代码中,我正在尝试采用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;
}