Question

In: Computer Science

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 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.

Source Code:

#include
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

#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 temp; // create new variable
        temp.v = o.v + this->v; // assign value to it
        return temp; // return
}

Val operator*(const Val& o) const {
       // FILL IN
       Val temp;
        temp.v = o.v * this->v;
        return temp;
}

Val operator!() const {
// FILL IN
    Val temp; // create new variable
        int r,rev = 0;
        int num = v; // create dup of this->v
       while(num > 0){ // reverse
            r = num % 10;
           rev = rev*10 + r;
           num = num/10;   
       }
       temp.v = rev; // assign reverse to v
        return temp;
}
};

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;
}

/* OUTPUT */

/* PLEASE UPVOTE */


Related Solutions

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...
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...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must create a list. The list is composed of integer double pairs. There is one pair per line. The integers are in sequence (0, 1, 2, 3, ...) beginning with zero and ending with some random value between 512 and 1024. The doubles should be random values between 50.000 and 90.000. The doubles only have 3 decimal places. The file should look like this (of...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown below.Please note that commented out behavior must be implemented in x86 assembly language. There is no standard, portable way to perform some of these actions in C++. #include void main() { // Use registers for these in your x86 assembly language program // Only use the .data segment for string (character array) variables int eax; int esi; int ecx; int edi; // Loop the...
Program in C Make both a SOURCE AND HEADER FILE WITH FUNCTIONS to run the program....
Program in C Make both a SOURCE AND HEADER FILE WITH FUNCTIONS to run the program. Input data from csv file. Two files. grades.c and grades.h Thank you data.csv file Mark Prest,37468,36,113,In Person Andy Joe,6785923,19,98,Online Caden Miller,237741,20,70.1,In Person Luke Jr,2347878,18,45.9,In Online Ally Rogers,8467483,30,89.99,Online Maya Jank,5674930,30,90,In Person Expected output Name: Mark Prest ID: 37468 Age: 36 Grade: 113.00 Attending: In Person Name: Andy Joe ID: 6785923 Age: 19 Grade: 98.00 Attending: Online Name: Caden Miller ID: 237741 Age: 20 Grade: 70.10...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT