Question

In: Computer Science

Use a switch statement on a char variable to write a function that's a simple calculator...

Use a switch statement on a char variable to write a function that's a simple calculator that keeps running waiting for input

Should have a do while loop that continually loops the function, prompting for a new input and new operator, until the calculator returns 0, in which case it will close.

Solutions

Expert Solution

As the programming language is not mentioned, it is done C language

Source Code:

#include<stdio.h> // header file for input output functions
main() { // main function
   int s,total; // declaring required variables
   char cha;
   do{ // do while starting
       printf(" Enter a number: "); // asking for a number
       scanf("%d",&s); // storing it into s every time
       printf(" Enter a operator(+,-,*,/): "); // asking for a operator
       scanf(" %c",&cha); // stroring into cha variable
       switch(cha){ // switch statement testing the operator
           case '+': total+=s; // if operator is + then add s to previous total
           break;
           case '-': total-=s; // if - substract sfrom total
           break;
           case '*': total*=s; // if * multiply s with total
           break;
           case '/': total/=s;// if / divide s by total
           break;
           default: printf(" Enter a valid operator!!");
       }
       printf(" Total is: %d ",total); // priting the total in each round
   }while(total!=0); // until total becomes 0
}// end of main

OUTPUT:

The below image is the output of the above code. It is run until the value becomes 0.


Related Solutions

WRITTEN IN C PLEASE. Write a switch statement that tests the value of the char variable...
WRITTEN IN C PLEASE. Write a switch statement that tests the value of the char variable response and performs the following actions: if response is y, the message Your request is being processed is printed if response is n, the message Thank you anyway for your consideration is printed if response is h, the message Sorry, no help is currently available is printed for any other value of response, the message Invalid entry; please try again is printed
Use a switch statement to write a function that returns TRUE if a character is a...
Use a switch statement to write a function that returns TRUE if a character is a consonant and returns FALSE otherwise.
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
Write a function that uses a local char queue and a local char stack to determine...
Write a function that uses a local char queue and a local char stack to determine if its string parameter is a palindrome. Your solution will look like: #include <stack> #include <queue> ... bool isPalindrome(const string& candidate) { stack<char> s; queue<char> q; //add only upper case letters and digits to the stack and the queue //if a letter is not upper case then convert it to upper case   } Note: I need help to write out this problem in C++...
Calculator in JavaScript. No use of switch statements or CSS. Project Standards: Students will use click...
Calculator in JavaScript. No use of switch statements or CSS. Project Standards: Students will use click events to capture user input. Students will use variables to store information needed by their application and keep track of their program’s state. Students will use conditionals to control project flow. Project Task You will be building a simple calculator in the browser. It should be able to add, subtract, divide, and multiply. Your program should do the following: Display a standard calculator to...
Problem set Rewrite the following if statement as an equivalent switch statement. The variable digit is...
Problem set Rewrite the following if statement as an equivalent switch statement. The variable digit is of type int. if (digit == 0) value = 3; else if (digit == 1) value = 3; else if (digit == 2) value = 6; else if (digit == 3)     value = 9; The decision table below shows fines imposed for speeding violations. Write a code segment that assigns the correct fine to type double variable fine based on the value of...
Write a function int strlen(char s1[]) which returns the length of the char array s1.
Write a function int strlen(char s1[]) which returns the length of the char array s1.
A Switch/case statement allows multiway branching based on the value of an integer variable. In the...
A Switch/case statement allows multiway branching based on the value of an integer variable. In the following example, the switch variable s is specified to be assumed to be one of three values ​​of [0, 2], and a different action for each case becomes: case 0: a = a - 1; break; case 1: a = a + 2; break; case 2: b = 3 * b; break; Shows how such statements can be compiled into MiniMIPS assembly instructions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT