Question

In: Computer Science

You are given a source file in your work area for this assignment. It consists of...

You are given a source file in your work area for this assignment.

It consists of a declaration for a Val node. There are declarations for three overloaded operator functions, which you must fill in: operator+, operator*, and operator!.

operator+ should implement addition. operator* should implement multiplication.

operator! should reverse the digits of its operand (i.e., of "this") and return the reversed integer. So for example !123 should return 321.

It should be straightforward to reverse an integer. You can extract the least significant digit by using % 10. You can remove the least significant digit by dividing by 10. Other approaches are possible as well.

These operator functions should not be a lot of code. The addition and multiplication can be written in 1-2 lines, and the reverse function within about 10 lines.

***************************************************************************************************************

THIS IS THE GIVEN PROGRAM WHICH I HAVE TO FILL IN.

********************************************************************************************************************

#include <iostream>
using namespace std;

class Val {
   int   v;
public:
   Val(int v=0) : v(v) {}

   friend ostream& operator<<(ostream& out, const Val& v) {
       out << v.v;
       return out;
   }

   Val operator+(const Val& o) const {
       // FILL IN


   }

   Val operator*(const Val& o) const {

       // FILL IN
   }

   Val operator!() const {

       // FILL IN
   }
};

int main()
{
   Val values[] = { 2, 44, 19, 4391 };
   const int nv = sizeof(values)/sizeof(values[0]);

   for( int i=0; i<nv; i++ ) {
       cout << values[i] << endl;
       cout << "!" << values[i] << " == " << !values[i] << endl;

       for( int j = i+1; j < nv; j++ ) {
           cout << values[j] << endl;
           cout << values[i] << " + " << values[j] << " == "
               << values[i] + values[j] << endl;
           cout << values[i] << " * " << values[j] << " == "
               << values[i] * values[j] << endl;
       }
   }

   return 0;
}

Solutions

Expert Solution

CODE TEXT :

#include <iostream>

using namespace std;

class Val {

int v;

public:

Val(int v=0) : v(v) {}

friend ostream& operator<<(ostream& out, const Val& v) {

out << v.v;

return out;

}

Val operator+(const Val& o) const {

return Val(v+o.v);

}

Val operator*(const Val& o) const {

        return Val(v+o.v);

}

Val operator!() const {

     int rev = 0, temp = v;

     while(temp){

         rev = (10*rev) + (temp%10);

         temp = temp/10;

     }

     return Val(rev);

}

};

int main()

{

Val values[] = { 2, 44, 19, 4391 };

const int nv = sizeof(values)/sizeof(values[0]);

for( int i=0; i<nv; i++ ) {

cout << values[i] << endl;

cout << "!" << values[i] << " == " << !values[i] << endl;

for( int j = i+1; j < nv; j++ ) {

cout << values[j] << endl;

cout << values[i] << " + " << values[j] << " == "

<< values[i] + values[j] << endl;

cout << values[i] << " * " << values[j] << " == "

<< values[i] * values[j] << endl;

}

}

return 0;

}

CODE SCREENSHOT :

OUTPUT:

Hope this helps. Please do upvote!


Related Solutions

Write this program in C++ You are given a source file in your work area for...
Write this program in C++ You are given a source file in your work area for this assignment. It consists of a declaration for a Val node. There are declarations for three overloaded operator functions, which you must fill in: operator+, operator*, and operator!. operator+ should implement addition. operator* should implement multiplication. operator! should reverse the digits of its operand (i.e., of "this") and return the reversed integer. So for example !123 should return 321. It should be straightforward to...
In this assignment you will be given an input file containing a series of universities along...
In this assignment you will be given an input file containing a series of universities along with that universities' location and rating. You must write a program that uses user defined functions to output a list of all school's above a certain rating to the terminal. Your program should do the following: - Prompt the user for the name of the input file to open - Prompt the user for the rating threshold ( Note: we print if the rating...
/* WordList source file * * *   This file will contain the function definitions you will...
/* WordList source file * * *   This file will contain the function definitions you will implement. *   The function signitures may NOT be changed. You may create your own *   helper functions and include them in this file. * *   In addition to the specific instructions for each function, no function *   should cause any memory leaks or alias m_list in any way that would result *   in undefined behavior. * *   Topics: Multilevel Pointers, Dynamic Allocation, Classes *...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Assignment Requirements Write a python application that consists of two .py files. One file is a...
Assignment Requirements Write a python application that consists of two .py files. One file is a module that contains functions used by the main program. NOTE: Please name your module file: asgn4_module.py The first function that is defined in the module is named is_field_blank and it receives a string and checks to see whether or not it is blank. If so, it returns True, if not it return false. The second function that is defined in the module is named...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT