Using GETCHAR and PUTCHAR ONLY ( Do not use Scanf and Printf) write a C program
ID: 3680227 • Letter: U
Question
Using GETCHAR and PUTCHAR ONLY ( Do not use Scanf and Printf) 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 if they choose.
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.
Output of Division and Mod should be in the form of a double or float if the answer requires a decimal place. Example 3/2 should output 1.5.
input: Allow the user to enter multiple expressions. (Loop of Y/N).
Output: Echo the input ( both user prompts and the user entered data including any user entered spaces ) Print the whole expression , with the result or an appropriate error message to the right of the expression. 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 ONLY TO SHOW ERROR MESSAGES, NOTHING ELSE!! but preferred use would be putchar. For example: 'You entered a negative value. Please try again...
Examples of User input and how output should appear:
Please enter a Calculation Now:
(user input): 1+1
(output): 1+1 = 2
(output): Would you like to Enter another calculation (Y/N)?
(user input): Y
(user input): 1- 6
(output): 1- 6 = -5
(output): Would you like to Enter another equation (Y/N)?
(user input): Y
(user input): -1 + 2
(output): -1 + 2 = Invalid Input - Must be a Positive Integer
(output): Would you like to Enter another equation (Y/N)?
(user input): Y
(user input): 2* b
(output): 2* b = Invalid Input - Must include Integers only
(output): Would you like to Enter another equation (Y/N)?
(user input): Y
(user input): 3 /2
(output): 3 /2 = 1.5 /* output can be a float or double*/
(output): Would you like to Enter another equation (Y/N)?
(user input): N
(output): Ending Program Now
Explanation / Answer
Using GETCHAR and PUTCHAR ONLY ( Cannot use Scanf and Printf) 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 ONLY TO SHOW ERROR MESSAGES, NOTHING ELSE!!) 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 supposed 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:
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
-5 * 1
-5 * 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 (' ' spaces)
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----
So far I have worked on it as much as possible but this is where I'm stuck and completely lost. Please guide me what to do next and what to include where. Any help would be appreciated. Thank you. If someone can take their time and post a full working code, that would be a life saver as this code for me is due by this Tuesday. Thanks in advance.
The other way of executing the program might be like this :
using no global variable declaration :
a program that receives user input, does a mathematical calculation and outputs the result. The program consists of rules such as no use of string handling functions, no arrays or no global variables.
For example, the user enters:
9+8, the output should be 9+8=17
9 + 9, the output should be 9+9=18
*9, output should be error
2 1/3, output should be an error because there should not be a space between 2 and 1
6 % 3, output should be 6%3=0
9 -, output should be an error.
I'm having a few problems, but the major ones are:
1) I am using a global variable for the operator value. I have tried creating a function to handle this and I am receiving a '0' value for the operator, thus receiving a result of '0' when I try to implement the function.
2) The program doesn't evaluate and expression like:
9 - 9
Entering this expression will output an Error message when it shouldn't.
3) After entering an expression such as:
*9
Error.
Please enter an equation for evaluation
3+8
at this point the output will read
9+3=12
Would you like to continue? Y or N: y
The program will close.
My code (with global variable) is as follows:
Code:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int Convert(char);
char Oper = '/0';
int Operator(char);
int main(void){
int Num1=0;
int Num2=0;
char answer='y';
// char Oper='/0';
char ch;
int Result=0;
do{
printf("Please enter an equation for evaluation. ");
while(((ch=getchar())!=EOF)&&(ch!=' ')){
if((ch==' ')||(ch==' ')){
ch='/0';
continue;
}
else if(!isdigit(ch)){
ch=0;
printf("Error. ");
break;
}
else if(isdigit(ch)){
Num1=Convert(ch);
}
ch=getchar();
if(isdigit(ch)){
Num2=Convert(ch);
}
else if(!isdigit(ch)&&(ch==' ')){
ch=0;
printf("Error4. ");
break;
// ch=getchar();
// putchar(ch);
}
else if(!isdigit(ch)&&(ch!=EOF)){
continue;
}
if(Oper=='+')
Result=Num1+Num2;
else if(Oper=='-')
Result=Num1-Num2;
else if(Oper=='*')
Result=Num1*Num2;
else if(Oper=='/')
Result=Num1/Num2;
else if(Oper=='%')
Result=Num1%Num2;
printf("%d%c%d=%d ",Num1, Oper, Num2, Result);
printf("Would you like to continue? Y or N:");
scanf ("%c%*c", &answer);
if(answer=='n'||answer=='N'){
printf("Thanks. Goodbye.");
exit(1);
}
}
}while((answer=='y')||(answer=='Y'));
return 0;
}
int Convert(char c){
int ch;
int sum=c-'0';
// char Operand='/0';
while(((ch=getchar())!=' ')&&(ch!=' ')){
if(!isdigit(ch)){
Oper=ch;
if(ch==' '||ch=='/t')
ch='/0';
break;
}
sum=sum*10+(ch-'0');
//printf("Error9.");
}
return sum;
}
//int Operator(char c){
// char Oper='/0';
// if(c=='+')
// Oper='+';
// else if(c=='-')
// Oper='-';
// else if(c=='*')
// Oper='*';
// else if(c=='/')
// Oper='/';
// else if(c=='%')
// Oper='%';
// return Oper;
//
//}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int Convert(char);
char Oper = '/0';
int Operator(char);
int main(void){
int Num1=0;
int Num2=0;
char answer='y';
// char Oper='/0';
char ch;
int Result=0;
do{
printf("Please enter an equation for evaluation. ");
while(((ch=getchar())!=EOF)&&(ch!=' ')){
if((ch==' ')||(ch==' ')){
ch='/0';
continue;
}
else if(!isdigit(ch)){
ch=0;
printf("Error. ");
break;
}
else if(isdigit(ch)){
Num1=Convert(ch);
}
ch=getchar();
if(isdigit(ch)){
Num2=Convert(ch);
}
else if(!isdigit(ch)&&(ch==' ')){
ch=0;
printf("Error4. ");
break;
// ch=getchar();
// putchar(ch);
}
else if(!isdigit(ch)&&(ch!=EOF)){
continue;
}
if(Oper=='+')
Result=Num1+Num2;
else if(Oper=='-')
Result=Num1-Num2;
else if(Oper=='*')
Result=Num1*Num2;
else if(Oper=='/')
Result=Num1/Num2;
else if(Oper=='%')
Result=Num1%Num2;
printf("%d%c%d=%d ",Num1, Oper, Num2, Result);
printf("Would you like to continue? Y or N:");
scanf ("%c%*c", &answer);
if(answer=='n'||answer=='N'){
printf("Thanks. Goodbye.");
exit(1);
}
}
}while((answer=='y')||(answer=='Y'));
return 0;
}
int Convert(char c){
int ch;
int sum=c-'0';
// char Operand='/0';
while(((ch=getchar())!=' ')&&(ch!=' ')){
if(!isdigit(ch)){
Oper=ch;
if(ch==' '||ch=='/t')
ch='/0';
break;
}
sum=sum*10+(ch-'0');
//printf("Error9.");
}
return sum;
}
//int Operator(char c){
// char Oper='/0';
// if(c=='+')
// Oper='+';
// else if(c=='-')
// Oper='-';
// else if(c=='*')
// Oper='*';
// else if(c=='/')
// Oper='/';
// else if(c=='%')
// Oper='%';
// return Oper;
//
//}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.