Question

You will write a program that performs the following tasks for a simple mathematical calculator: (1) Open a user-specified fi


a. Invalid calculation type - calculation type is not Math or Trig. b. Invalid operator for Math c. Invalid operation for Tri

this is a C++ program!
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<math.h>
using namespace std;

int main()
{
        ofstream myfile;
        string filename;
        string line;
        char op;
        double value1;
        double value2;
        char dORr;
        double anglevalue;
        double pi = 3.14159265;
        bool isEmpty = false;
        while(1)
        {
                cout << "Enter the name of the File :";
                cin >> filename;
                ifstream myfile (filename);
                if (myfile.is_open())
                {
                        //moving to the last position of the file
                        myfile.seekg(0, ios::end);  
                        
                        //if eod the file is the beginning position, file is empty
                        if (myfile.tellg() == 0) {    
                          cout << "Input file empty" << endl;
                          isEmpty = true;
                        }
                        
                        myfile.seekg(0, ios::beg); 
                        
                    while ( getline (myfile,line) )
                    { 
                        if(isEmpty)
                        {
                                break;
                                }
                                
                        istringstream ss(line); 
                        
                                string type; 
                            ss >> type; 
                                if (type == "Math")
                            {
                                ss >> op;
                                ss >> value1;
                                ss >> value2;
                                
                                if(op == '+')
                                {
                                        cout<< "Add: "<<value1<<op<<value2<<" = "<<value1+value2<<endl;
                                        }
                                        else if(op == '-')
                                        {
                                                cout<< "Sub: "<<value1<<op<<value2<<" = "<<value1-value2<<endl;
                                        }
                                        else if(op == '*')
                                        {
                                                cout<< "Mul: "<<value1<<op<<value2<<" = "<<value1*value2<<endl;
                                        }
                                        else if(op == '/')
                                        {
                                                cout<< "Div: "<<value1<<op<<value2<<" = "<<value1/value2<<endl;
                                        }
                                        else if(op == '%')
                                        {
                                                cout<< "Mod: "<<value1<<op<<value2<<" = "<<(int)value1%(int)value2<<endl;
                                        }
                                        else
                                        {
                                                cout<< "Invalid operator for Math";
                                        }
                                } 
                                else if (type == "Trig")
                            {
                                ss >> op;
                                ss >> dORr;
                                ss >> anglevalue;
                                string angletype;
                                //coverting degree to radian because all the function like Cos(), sin() operates on degree in radian
                                if (dORr == 'd')
                                {
                                        anglevalue= anglevalue * (pi/180); // radian = degree * (pi/180)
                                        angletype="degree";
                                        }
                                        
                                        else if(dORr == 'r'){
                                                angletype="radian";
                                        }
                                        
                                if(op == 's')
                                {
                                        cout << "sin("<<angletype<<"): sin("<<angletype<<") = "<< sin(anglevalue)<<endl;
                                        }
                                        else if(op == 'c')
                                        {
                                                cout << "cos("<<angletype<<"): cos("<<angletype<<") = "<< cos(anglevalue)<<endl;
                                        }
                                        else if(op == 't')
                                        {
                                                cout << "tan("<<angletype<<"): tan("<<angletype<<") = "<< tan(anglevalue)<<endl;
                                        }
                                }
                                else{
                                        cout<< "Invalid calculation type - calculation type is not Math or Trig\n";
                                }
                          
                        }
                    myfile.close();
                }
                else
                {
                        cout << "ERROR : File not found/opening\n";
                }
        }
        

}
Add a comment
Know the answer?
Add Answer to:
this is a C++ program! You will write a program that performs the following tasks for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • **This is for the C Language. Trigonometric Calculator Specification You are required to write a program...

    **This is for the C Language. Trigonometric Calculator Specification You are required to write a program that calculates and displays values of the trigonometric functions sine, cosine, and tangent for user-supplied angular values. The basic program must comply with the following specification. 1. When the program is run, it is to display a short welcome message. TRIG: the trigonometric calculator 2. The program shall display a message prompting the user to enter input from the keyboard. Please input request (h-help,...

  • OUTCOMES After you finish this assignment, you will be able to do the following: Define an...

    OUTCOMES After you finish this assignment, you will be able to do the following: Define an abstract class Create concrete classes from an abstract class Overload an operator Split classes into .h and .cpp files Open files for reading Write to files Use output manipulators such as setw, fixed, and setprecision DESCRIPTION A binary arithmetic operation takes two double operands (left and right) to perform addition, subtraction, multiplication, or division on. For example, 10 + 11 is an addition (+)...

  • Description Write C++ code that correctly input and output information. (Moving data to and from the...

    Description Write C++ code that correctly input and output information. (Moving data to and from the screen and to and from text files. Also inputting predefined functions, etc., from included libraries.) Problem Description Consider a data file that has geometrical angle values in degrees. Angle values may be on one line, several lines, or any other white space separated format. Fig. 1 below shows one possible format, but other formats are possible. 12.9 100.8 270.5 300.6 120.8 There are no...

  • opiond it 0 T2419 (3UN) Write a C++ program which performs +,-,,/and $ on hexadecimal operands....

    opiond it 0 T2419 (3UN) Write a C++ program which performs +,-,,/and $ on hexadecimal operands. The maximum length of any operand or a solution is 40 digits. The input will be in the following format: Opl op op2- There is no space between operands and operator. I Note 5/2-quotient 2, remainder 1 2$3-8 The output should be of the form 2 3-6 Read date from a file Upload the source code, executable and output TEST DATA: AAAA+BBF- BFD+2DE 100...

  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

  • MAT LAB: write a program that can choose from an assortment of calculations and perform the...

    MAT LAB: write a program that can choose from an assortment of calculations and perform the desired physics calculation in order to streamline the design and modelling process. Your function should take three inputs in the following order: var1 – The first number in the calculation you want to perform. var2 – The second number in the calculation you want to perform var3 – The third number in the calculation you want to perform indicator . A variable, that has...

  • Consider the following C++ program. It prints a small table containing sin and cos values from...

    Consider the following C++ program. It prints a small table containing sin and cos values from 0 to 360 degrees. #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int step = 20; cout << setw(10) << "degrees" << setw(10) << "cos" << setw(10) << "sin" << endl; for (int degrees = 0; degrees <= 360; degrees += step) { cout << setw(10) << degrees << setw(10) << setprecision(5) << fixed << cos(degrees) << setw(10) << setprecision(5)...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations must be...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for the list of operations that can be calculated and get the input from the user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations...

  • Write a program to compute the area of a triangle using side-angle-side method and reports the...

    Write a program to compute the area of a triangle using side-angle-side method and reports the area of that triangle (rounded to 2 decimal places). Side-angle-side formula: ???? = 1/ 2 ?? sin(?), where a and b are two sides of the triangle, and C is the included angle. Your program must meet the following criteria to receive full marks: • Randomly generate two values between 5 and 10 (inclusive) for two sides a and b of the triangle, respectively....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT