Answer to Question #331620 in C++ for khan

Question #331620

BinaryStore Calculator

A BinaryStore calculator will store bytes “stored in strings” with their addresses, i.e., at each address in the BinaryStore there is a stored Byte. Each address will be 4 characters string and each byte will be 8 characters strings.

Create a class BinaryStore and class Byte that offer following overloaded operators.

class offers the following methods:

 an overloaded +=operator that will add the address in the list of BinaryStore

 an overloaded + that will add two string bytes for Byte Class

 an overloaded - that will subtract two string bytesByte Class

 an overloaded || operator that will give the string which is bit by bit logical or of act two string bytes for Byte Class

 an overloaded && operator that will give the string which is bit by bit logical and of act two string bytesf or Byte Class

 an overloaded == operator that will give bool value if they are equal or notfor Byte Class



1
Expert's answer
2022-04-21T07:02:54-0400



#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include <map>//for save adress (binary tree-key string size 4 charackter)
#include <cstdint>
/*
IDE Vsual Studio 2022
Testing for Windows 11 System
*/


//class Byte
using namespace std;
class Byte
{
private:
    char  byte[9];//
public:
    Byte()
    {
        //set all bit to '0'
        for (int i = 0; i < 8; i++)
        {
            byte[i] = '0';
        }
        byte[8] = '\0';//null terminal
    }
    Byte(const char* bts)
    {
        strcpy(byte, bts);
    }
    operator const char* ()const
    {
        return byte;
    }
    Byte(const Byte& b)
    {
        strcpy(this->byte, (const char*)b);
    }
    Byte operator=(const Byte& b)
    {
        strcpy(this->byte, (const char*)b);
    }


    //Overload stream out operator
    friend ostream& operator<<(ostream& out, const Byte& bt);
    //Add two Bytes (string bytes)
    friend Byte operator+(Byte& b1, Byte& b2);
    //Subtract
    friend Byte operator-(Byte& b1, Byte& b2);
    //
    friend Byte operator||(Byte& b1, Byte& b2);
    friend Byte operator&&(Byte& b1, Byte& b2);
    friend bool operator==(Byte& b1, Byte& b2);
};
//Out
ostream& operator<<(ostream& out, const Byte& bt)
{
    out << (const char*)(bt);
    return out;
}
//
Byte operator+(Byte& b1, Byte& b2)
{
    unsigned rem = 0;
    Byte nw;
    for (int i = 7; i >= 0; i--)
    {
        unsigned a = b1.byte[i] - '0';
        unsigned b = b2.byte[i]-'0';
        unsigned ans = a + b+rem;
        if (ans >2)
        {
            rem = 1;
            ans = 1;
        }
        if (ans == 0)
            nw.byte[i] = '0';
        else
            nw.byte[i] = '1';
    }
    return nw;
}
Byte operator-(Byte& b1, Byte& b2)
{
    unsigned rem = 0;
    Byte nw;
    for (int i = 7; i >= 0; i--)
    {
        unsigned a = b1.byte[i] - '0';
        unsigned b = b2.byte[i] - '0';
        unsigned ans = a - b;
        if (ans <0)
        {
            for(int j=i;j>=0;j--)
                if (b1.byte[j] == '1')
                {
                    b1.byte[j] = '0';
                    break;
                }
            ans = 1;
        }
        if (ans == 0)
            nw.byte[i] = '0';
        else
            nw.byte[i] = '1';
    }
    return nw;
}
Byte operator||(Byte& b1, Byte& b2)
{
    Byte nw;
    for (int i = 7; i >= 0; i--)
    {
        bool a = b1.byte[i] - '0';
        bool b = b2.byte[i] - '0';
        bool ans = a||b;
        if (ans == 0)
            nw.byte[i] = '0';
        else
            nw.byte[i] = '1';
    }
    return nw;
}
//and operation
Byte operator&&(Byte& b1, Byte& b2)
{
    Byte nw;
    for (int i = 5; i >= 0; i--)
    {
        bool a = b1.byte[i] - '0';
        bool b = b2.byte[i] - '0';
        bool ans = a && b;
        if (ans == 0)
            nw.byte[i] = '0';
        else
            nw.byte[i] = '1';
    }
    return nw;
}


bool operator==(Byte& b1, Byte& b2)
{
    for (int i = 7; i >= 0; i--)
    {
        if (b1.byte[i] != b2.byte[i])
            return false;
    }
    return true;
}
//class BinaryStore
class BinaryStore
{
private:
    map<string, Byte*>mByte;
public:
    BinaryStore()
    {
        //null
    }
    BinaryStore operator+=(string adres)
    {
        mByte[adres]=new Byte;//add adres
      return (*this);
    }
    void AddToKey(string key, Byte& b)
    {
            *(mByte[key]) = b;
    }
    ~BinaryStore()
    {
        mByte.clear();//alloc memory
    }


};


int main()
{
    Byte ba("10101011");
    Byte bb("11000000");
    BinaryStore bs;
    bs+="0001";
    bs+="0012";
    cout << "ba=" << ba << endl;
    cout << "bb=" << bb << endl;
    cout << "ba+bb="<<(ba + bb) << endl;
    cout << "ba-bb="<<(ba - bb) << endl;
    cout << "ba||bb="<<(ba || bb) << endl;
    cout << "ba&&bb="<<(ba && bb) << endl;
    if (ba == bb)
    {
        cout << ba << "==" << bb << endl;
    }
    else
        cout << ba << "!=" << bb << endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS