|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
For this problem, you are going to construct a very simple calculator program in a file called Calculator.cpp that can just add, subtract, multiply, and divide. You will allow the user to enter sets of three "inputs". The first and third input will be numbers, and the middle input will be one of the mathematical operators +, -, *, or / . Depending upon what the user inputs, you will perform the appropriate mathematical operation, using a function. You will write five functions: add, subtract, multiply, divide, and print.
The input lines will look like the following (entered one at a time as shown on the right): 4.2 + 88 321.6 - 95.1 18 * 9 10 / 100 Note: for the last example above, return the value that you would expect from a calculator, which is NOT the result of integer division in C++! Each of the add, subtract, multiply, and divide functions should take two doubles as their parameters, and should return the result of the calculation as a double. The print function should return nothing, and should take the original two operands as doubles, the operator as a char, and the result as a double, and should print the following (corresponding to the examples above): 4.2 + 88 = 92.2 321.6 - 95.1 = 226.5 18 * 9 = 162 10 / 100 = 0.1 If you don't write the functions, you get no points (the goal of this problem is to learn how to write functions...). You must write function prototypes before the main function, and then place the function implementations after main. Not writing prototypes will cost you 10 points. You must write file and function comments as specified in the cl*** coding standards. Not doing that correctly will cost you up to 10 points (if you do no commenting at all). You must have a loop in your main program to allow the user to keep calculating until they want to quit. See the example to the right. Do not call print from within the math functions. Call it from main after returning the math result back from each function. Do not print from within the math functions. Note: The division function needs to be special, since you can get into trouble in one special case. See the example session. Return zero from the function in this case. That isn't really right, but it would be complicated to do otherwise right now... You don't have to worry about trapping errors where the user doesn't enter X op Y expressions, where X and Y are numbers, and op is a single character. You also don't have to trap errors where they enter something other than an integer to continue the program. You may change the prompt or method for continuing the program if you like. But make it obvious and explicit to the user what you want. -------------------------------------------------------------------------------- Problem 2: Calculator Version 2 - real math (60 pts) For this problem make sure that you've got Problem 1 done, since you will start with it for this problem. You're going to add a nicer menu to it, and add some functionality that a calculator really should have... Copy the solution from Problem 1 into a file called Calculator2.cpp. First, you will add a menu of choices for the user. Really, once we learn more about reading, parsing, and manipulating strings, it would be nicer just to allow the user to enter expressions and have the program figure out what to do, but for now, we need to have the user tell us up-front what they are going to enter. So the menu will have three options: 1) enter two argument expression, 2) Enter one argument expression, and 3) Quit program. The user will enter 1, 2, or 3 to choose. See the example to the right. You will write a function to process the menu and user choice. If the user chooses to enter a two-argument expression, then you should read from the console just as in problem 1, and perform exactly the same calculations. However, you will add one more: 5 ^ 3 For which you should print: 5 ^ 3 = 125 So the ^ operator will represent the "power" operator (so the example means "5 to the 3rd power"). You should support any two double operands. A math library function will probably help you here... If the user asks for choice 2 (one argument expression), you should support the following operator-value combinations: S 2 Should result in: The square root of 2 = 1.414 E 5 Should result in: The exponential of 5 = 148.413 and L 2 Should result in: The natural logarithm of 2 is 0.693147 So the operators are the upper case letters S, E, and L, and the arguments follow, and can be appropriate double values. Note that you can't take the square root of a negative number, and you can't take the natural logarithm of any non-positive numbers (including zero). Make sure you protect your program from these conditions. See the figure to the right for a session of my program. You must meet the following architectural requirements: You must have a menu function that presents the menu and get's the user's choice. You should make sure that it will only return valid choices. The main program should ask the user for either the two-operand or one-operand data, and then call one of two processArgs functions (see below). Don't do much processing in main. It should then call one of the two overloaded print functions (see below). You must write two overloaded processArgs functions. One should take the operator and two calculation arguments, and return the result of the calculation through its return parameter. The other should take the operator and one operator argument, and return the result of the calculation through its return parameter. The first version should process two-argument operators, and the second version should process one-argument operators. You must write two overloaded print functions. One version, you already have from problem 1. The second must also be named print, but should take one operator, the operator argument, and the result value, and should print the square root, exponential, or natural logarithm lines, depending on what the operator is. To calculate the powers, square roots, exponential function, and logarithm, you need to call C++ math library functions (look them up). Make sure to check arguments when necessary to ensure that they are valid (i.e., don't allow taking the square root of a negative number). Print appropriate error messages. Never quit the program until the user asks to quit. You must comment all functions correctly. Do***ent purpose, parameters, and return values (if any). Make sure your program input/output is at least as well formatted as in the example session. Sloppy input/output will lose points. |
| Sponsored Links |
|
|
|
|||
|
Are you using C++ or just C?
|
|
|||
|
what are listed below is my program about the homework, but I don't what's wrong with it, it doesn't work/
Code:
#include <iostream>
#include <cmath>
using namespace std;
//declarations of functions
double processArgs(double x, double y);//two operands function
double processArgs(double x);//one operand function
void print(double x,double y);//two operands print functions
void print(double x)// one operands print functions
int processchoice();//a function to process menu or user choice
//main function
int main ()
{
int choice;// menu choice
double x,y,result;
do {
choice=processchoice();
swith(choice)
case 1:result=processArgs(x,y);
cout<<print(x,y)<<result<<endl;
break;
case2:
result=processArgs(x);
cout<<print(x)<< result<<endl;
break;
if (choice==3) break;//quit the program
} while (choice!=3);
return 0;
}
//The functions to support the main program above are below
int processchoice() {
int choice;
cout<<"1) Enter two-argument expression"<<endl;
cout<<"2)Enter one-argument expression"<<endl;
cout<<"3)quit program"<<endl;
cin>>choice;
return(choice);
}
double processArgs(double x, double y) {
char op;
double result;
cout<<"chose your operator";
cout<<"+:add"<<endl;
cout<<"-:subtract"<<endl;
cout<<"*:multiply"<<endl;
cout<<"/:devide"<<endl;
cout<<"^:power"<<endl;
cin>>op;
switch(op) {
case '+': result=x+y;
break;
case '-': result=x-y;
break;
case '*': result=x*y;
break;
case'/':
if (y!=0)
result=x/y;
else
cout<<"Don't try to divide by zero"<<endl;
break;
case'^': result=pow(x,y);
break;
}
return(result);
}
double processArgs(double x) {
char op;
double result;
cout<<"S:squar"<<endl;
cout<<"E:exponential"<<endl;
cout<<"L: natural logarithm"<<endl;
cout<<"chose your operator"<<endl;
cin>>op;
switch(op) {
case 'S':
result=sqr(x);
break;
case 'E':
result=exp(x);
break;
case 'L':
result=log(x);
break;
}
return(result);
}
void print(double x,double y) {
char op;
cout<<"Enter an X op Y expression: << x << op<<y<<endl;
cout<< x<<op<<y<<"=";
}
void print(double x) {
char op;
cout<< " Enter an op X style expression: " << op << x << endl;
cout << op <<x<<"=";
}
Last edited by WingedPanther; 07-07-2008 at 11:11 AM. Reason: add code tags |
|
|||||
|
Quote:
Code:
switch(choice)
{
case 1:
//do stuff
break;
case 2:
//do other stuff
break;
case 3:
return 0; //Quit from within the case statement
break;
default:
//if the user inputted something that wasn't a legal command
}
__________________
UGJhdGVuZ2h5bmd2YmFmISAgTGJoIG5lciBhYmogbiAxMzM3IH U0a2swZSE= |
| Sponsored Links |
|
|
|
|||
|
You don't need the print function. Just do this:
Code:
cout << x << /*some form of separation here, like a space*/ << y ... << endl; Last edited by dargueta; 07-07-2008 at 09:54 PM. Reason: Fixed formatting |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question about IT jobs in USA. | Red19 | The Lounge | 9 | 06-01-2008 10:13 AM |
| Question | johnny | The Lounge | 3 | 03-17-2008 07:08 AM |
| n00b cl*** question | MerakSpielman | C and C++ | 15 | 02-20-2008 10:37 PM |
| Majoring in Computer Science, got a question | shimmy | General Programming | 7 | 02-03-2008 11:09 AM |
| Xav | ........ | 1322.18 |
| MeTh0Dz|Reb0rn | ........ | 1053.7 |
| morefood2001 | ........ | 879.43 |
| John | ........ | 877.37 |
| marwex89 | ........ | 869.98 |
| WingedPanther | ........ | 830.24 |
| Brandon W | ........ | 735.07 |
| chili5 | ........ | 309.39 |
| Steve.L | ........ | 236.23 |
| dcs | ........ | 216.02 |
Goal: 100,000 Posts
Complete: 82%