Using GETCHAR and PUTCHAR only write a C program to perform simple C arithmetic
ID: 3679933 • Letter: U
Question
Using GETCHAR and PUTCHAR only write a C program to perform simple C arithmetic calculations. The user is to enter a simple expression (integer operator integer) such as 15 + 30. The program is to extract the 2 operands and the operator, perform the indicated calculation and display the result. For example 15 + 30 = 45 Operators should include + , - , * , / , and %. (Include an error message if the user inputs any other operators rather than the 5 ones mentioned above.^^) Operands are positive integers , no sign. Use getchar to input the expression Allow for variable spacing before the first operands and between operators and operands ( 0, 1, or more spaces) Allow the user the option of entering another expression Code a function for each operator to do the appropriate arithmetic operations (5 functions) No global variables, No string handling functions, No arrays. May only use input and output functions such as getchar and putchar Input: interactive. Allow the user to enter multiple expressions. (Loop of Y/N). Output: Echo the input ( both user prompts and the user entered data ) Print the whole expression , with the result or an appropriate error message. Be sure output data shows all valid and invalid test data and how your program handled it. Make sure to include a loop that gives the user to continue the program as many time as they wish and exit when they want to, For example: (Do you wish to continue? Press 'Y' for yes and 'N' to exit. Also in error checks make sure to give error messages if the user enters a negative value or character. (You can use printf ( ) to show the error messages but preferred use would be putchar) For example: 'You entered a negative value. Please try again...' and for characters: 'You entered a character, please enter a integer or actual numbers instead'. Make sure to also include operations which gives out decimal values, such as ' 7 / 3 = 2.33'. Also make sure the program echoes back if user uses the exact amount of spaces . Such as '15 + 30 = 45' or similar to that. **Do not ask the user questions like 'Enter you first and second operand' It's a calculator program. The user is only suppossed to enter '15 + 30' then the program will echo it back and the output will be like '15 + 30 = 45' and so on. I'm attaching the input as reference. (Please don't copy past other Chegg users answers as I checked all of them and they don't match with the conditions mentioned above, please read the conditions carefully and only reply back with the proper code that uses all the conditions mentioned above, Thank you.) Input :
15 + 30
15 + 30 = 45
Do you wish to continue? Press Y for yes and N to exit. Y
7 / 3
7 / 3 = 2.33
Do you wish to continue? Press Y for yes and N to exit. Y
-1 * 1
-1 * 1
Error. The program only accepts positive integers. Please try again.
Do you wish to continue? Press Y for yes and N to exit. Y
5 % t
5 % t
Error. You have entered a character. Please enter an integer instead.
Do you wish to continue? Press Y for yes and N to exit. Y
15 - 30
15 - 30 = -15
Do you wish to continue? Press Y for yes and N to exit. Y
5 / 0
5 / 0 = DNE
Do you wish to continue? Press Y for yes and N to exit. Y
0 / 3
0 / 3 = 0
Do you wish to continue? Press Y for yes and N to exit. N
---exits here----
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int main()
{
int ch;
int input1, input2, input3;
while(1){
printf(" Please enter a calculation to be made. ");
while((ch = getchar()) != ' '){
if (ch == ' ' || ch == ' ') {
ch = '';
continue; /*Ignore spaces*/
}
else if (isdigit(ch)) {
if (ch >= 0) {
input1 = input1 * 10 + (ch - '0');
}
else {
printf(" Error: number is not positive or zero. ");
}
}
else if (ispunct(ch)) {
if (ch == '+' || ch == '*' || ch == '%' || ch == '-' || ch == '/') {
input2 = ch;
}
else {
printf(" Error: input is not one of the allowed mathematical operators. ");
}
}
else if (isdigit(ch)) {
if (ch >= 0) {
input3 = input3 * 10 + (ch - '0');
}
else {
printf(" Error: number is not positive or zero. ");
}
}
}
switch(input2){
case '+':printf("%d %c %d =%d",input1,input2,input3,(input1+input3));break;
case '-':printf("%d %c %d =%d",input1,input2,input3,(input1-input3));break;
case '*':printf("%d %c %d =%d",input1,input2,input3,(input1*input3));break;
case '/':printf("%d %c %d =%lf",input1,input2,input3,((double)input1/((double)input3)));break;
case '%':printf("%d %c %d =%d",input1,input2,input3,(input1%input3));break;
}
printf("%d",input1);
putchar(input1);
putchar(input2);
putchar(input3);
printf("Do ypu wish to continue?Press Y for yes and N to exit ");
if((ch =getchar())=='N'){
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.