Question
Asked Feb 14, 2020
6 views

You are to write a C++ program that performs basic arithmetic operations including addition, subtraction,
multiplication, division, and modulus. Normally, when we work with calculators, we would enter the expression
and press the "=" button to see the result. In this project, it will be done differently. The expressions are
provided an input file and your calculator is supposed to read them from there and perform the calculation.

 

I input.txt - Notepad
File Edit Format View
Help
100
10
109.5 +
25
25.5 /
1.0
2
10
10
ab
ab
100
10
-10
10.5
1000
1000
1.05
1000
1000
1000
1000
1000
help_outline

Image Transcriptionclose

I input.txt - Notepad File Edit Format View Help 100 10 109.5 + 25 25.5 / 1.0 2 10 10 ab ab 100 10 -10 10.5 1000 1000 1.05 1000 1000 1000 1000 1000

fullscreen
check_circle

Expert Answer

Approach

Approach –

  • Create an object for a file.
  • Open a file with the help of an object.
  • Declare two variables for the operands.
  • Declare a character for an operator.
  • Use a loop to read a file.
  • Inside the loop, use the switch to perform various operations for different operators.
  • Close the file.
Program –

#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{
    fstream f; // file object
    f.open("file.txt"); // file opened
    int x, y; // to store operands from the file
    char c; // to store operators from the file
    
    // loop to read the file to the end
    while(!f.eof())
    {
        f >> x; // getting first operator from the file
        f >> c; // getting the operand from the file
        f >> y; // getting second operator from the file
        
        // conditions for the operands to operate
        switch (c)
        {
            case '+': 
                cout << x + y << endl; 
                break; 
            case '-':
                cout << x - y  << endl;
          &nbs...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q:

A: C++ program to Book Airline Tickets. The Destination is Chicago, Miami, and Portland.   Variable in ...

question_answer

Q: Create a Crow’s Foot notation ERD to support the following business operations: A friend of yours ha...

A: The following figure illustrates the ER Diagram for given business rules in Crow’s Foot notation:

question_answer

Q: In each pair, the order of the quantifiers is reversed but everything else is the same. For each pai...

A: (1) The aim is to decide whether the statements hold the same or opposite values of fact. There is a...

question_answer

Q: A Virtual Private Network (VPN) creates a(n)_______     a. Insecure link between the cloud and t...

A: VPNs is the acronym for Virtual Private Network. It is a type of network which is designed with the ...

question_answer

Q: Express -1.5 in IEEE 32-bit floating-point format.

A: Representing -1.5 in IEEE 32-bit floating-point format:The general representation of 32-bit floating...

question_answer

Q: 9. Implement an array with values 1, 5, 14, 23, 45, 52, 58, 81, 82 91. a) Create a getindex( functio...

A: Solving the given problem by using C++, as no programming language is specified.   The below given C...

question_answer

Q: Do the following program in Java Eclipse. Java Class that can be used to create a Fruit object as de...

A: The java program and approach are given below:Note: Run the same code on Eclipse.Approach: Creating ...

question_answer

Q: INTERNAL CONTROLDuring the past year, Howard Industries has experiencedexcessive employee turnover i...

A: a.One of the control concern is given below:Inner control:Separate the task of programming and lapto...

question_answer

Q: Create the table that results from applying a UNION relational operator to the tables shown in Figur...

A: Table: A table is a logical structure that represents an entity state. It is one of the components o...