[Solved] CS135 Project 4

$25

File Name: CS135__Project_4.zip
File Size: 150.72 KB

SKU: [Solved] CS135 – Project 4 Category: Tag:
5/5 - (1 vote)

Project [4]: Interactive Programs

Project Goals

The goals of this project are to:

  1. Get students familiar with switch statements
  2. Get students familiar with writing interactive programs
  3. Get students familiar with character input/output functions

Important Notes:

  1. Formatting: Make sure that you follow the precise recommendations for the output connate and formatting: for example, do not change the text in the first problem to differ from what is in the assignment. Your assignment will be auto-graded and any changes in formatting will result in a loss in the grade.
  2. Comments: Header comments are required on all files and recommended for the rest of the program. Points will be deducted if no header comments are included.
  3. Restriction: The use of goto statements anywhere within this program is prohibited. Points will be deducted if gotois used.

Problem 1

Write an interactive program that implements a simple calculator. The program should allow the user to choose a

binary arithmetic operation and enter two terms to which to apply the operation. The program should then compute the result and display it to the user. Your calculator will have two modes: double-precision mode and integer mode. Double-precision mode will do calculations and output using variables of type double. Integer mode will do calculations and output using variables of type int. Your program should start in double-precision mode.

At the beginning, the program should output the following:

This program implements a calculator.

At every iteration, your program should ask the user for an option as follows:

Options:

  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option:

Below is a description of what the program should do for each of the above options (items underlined are to be entered by the user):

  1. Perform addition. For this option, the program should ask the user to enter two terms to be added, as follows:

In double-precision mode:

Enter first term: 1

Enter second term: 1

The sum is: 2.000000000000000

In integer mode:

Enter first term: 1

Enter second term: 1

The sum is: 2

  1. Perform subtraction. For this operation, the program should ask the user to enter two terms to be subtracted, as follows:

In double-precision mode:

Enter first term: 4

Enter second term: 2

The difference is: 2.000000000000000

In integer mode:

Enter first term: 4

Enter second term: 2

The difference is: 2

  1. Perform multiplication. For this operation, the program should ask the user to enter two terms to be multiplied, as follows:

In double-precision mode:

Enter first term: 2

Enter second term: 3

The product is: 6.000000000000000

In integer mode:

Enter first term: 2

Enter second term: 3

The product is: 6

  1. Perform division. For this operation, the program should ask the user to enter two terms to be divided, as follows:

In double-precision mode:

Enter first term: 5

Enter second term: 2

The quotient is: 2.500000000000000

In integer mode:

Enter first term: 5

Enter second term: 2

The quotient is: 2

If the second term entered by the user is 0, the program should print:

Cannot divide by zero!

  1. Toggle the type of calculator. By default, the program should perform its calculations using doubleprecision. However, if the user chooses this option, the program should change the type of calculator: either from double to int or vis versa.

When switching from double-precision to integers the program should print the follow message:

Calculator now works with integers.

When switching from integers to double-precision the program should print the following message:

Calculator now works with doubles.

  1. The program should end.

Your program should function as follows (items underlined are to be entered by the user):

This program implements a calculator.

Options: 1 addition 2 subtraction

  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option: 1

Enter first term: 1.525

Enter second term: 3

The sum is: 4.525000000000000

Options:

  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option: 2 Enter first term: 4.873

Enter second term: 3.1

The difference is: 1.773000000000000

Options:

  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option: 3

Enter first term: 4.56

Enter second term: 2.13 The product is: 9.712799999999998

Options:

  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option: 4

Enter first term: 5.15

Enter second term: 9.8765 The quotient is: 0.521439781299043

Options:

  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option: 4

Enter first term: 7.13 Enter second term: 0 Cannot divide by zero! Options:

  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option: 18

Invalid Option. Options:

  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program Please enter your option: 5 Calculator now works with integers. Options:
  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option: 1

Enter first term: 7

Enter second term: 9

The sum is: 16

Options:

  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option: 4

Enter first term: 5

Enter second term: 2

The quotient is: 2

Options:

  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option: 5 Calculator now works with doubles.

Options:

  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option: 6

Notes:

  • If the user enters an invalid command, the program should print the menu again and ask for a valid option, as follows:

Invalid Option.

Options:

  • addition
  • subtraction
  • multiplication
  • division
  • toggle calculator type
  • exit program

Please enter your option:

  • For all options, except division, you can assume the user will give valid input
  • When the calculator is in double-precision mode it should output 15 digits after the decimal point.

Save your program as calc.c

Challenge for problem 1 (10 extra credit points):

Turn the calculator into a scientific calculator. Change your calculator as follows:

Options:

  • addition
  • subtraction
  • multiplication
  • division
  • exp(x)
  • log(x)
  • toggle calculator type
  • exit program Please enter your option:
  1. Compute exp(x). For this option, the program should prompt the user to enter a single number as follows:

In double-precision mode:

Enter term: 4

The result of exp(4.000000000000000) is: 54.598150033144236

In integer mode:

Cannot calculate with integers.

  1. Compute log(x). For this option, the program should prompt the user to enter a single number as follows:

In double-precision mode:

Enter term: 4

The result of log(4.000000000000000) is: 1.386294361119891

In integer mode:

Cannot calculate with integers.

This is the natural logarithm, it cannot be used with negative numbers. If the user enters a number that is less than or equal to zero, you program should print: Cannot take the log of that number!

Note:

  • For this program you must use the exp() and log() functions from the math library. To do this:

o Add #include <math.h> in your calc_c.c file o Add -lm to the compilation command: gcc -o calc_c calc_c.c -lm

Save your challenge separately as calc_c.c

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CS135 Project 4
$25