-
Notifications
You must be signed in to change notification settings - Fork 1
/
exam1.c
105 lines (95 loc) · 3.35 KB
/
exam1.c
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
/*
* Thomas Krogh Lohse
* Gruppe A.306
* Datalogi
*/
#include<stdio.h>
#include<math.h>
double run_calculator();
void scan_data(char *operator, double *operand);
void do_next_op(char *operator, double *operand, double *result);
void print_help(void);
int main(void) {
/* Prints the end-result */
printf("\nEnd result of the accumulator = %.2lf\n", run_calculator());
return 0;
}
double run_calculator() {
print_help(); /* Starts with printing the help-menu, showing all commands in the program */
/* Instantiates and declares variables */
char operator;
double operand, result = 0.0;
do { /* Iteratively asks the user to give an input, and acts accordingly */
printf("Accumulator so far = %.2lf\n\n", result);
scan_data(&operator, &operand);
do_next_op(&operator, &operand, &result);
} while(operator != 'q' && operator != 'Q'); /* Keeps asking untill the 'Quit'-input is typed */
return result;
}
void scan_data(char *operator, double *operand) {
printf("Enter an operator and, possibly, an operand: ");
scanf(" %c", operator); /* Scans input as the operator */
switch(*operator) { /* If it is an operator that needs an operand, it continues to the next scanf, which takes the input as the operand */
case '+': case '-': case '*': case '/': case '^':
break;
default:
return;
}
scanf("%lf", operand);
}
void do_next_op(char *operator, double *operand, double *result) {
int i;
switch(*operator) { /* Executes the commands given to the specific operators */
case '+':
*result += *operand;
break;
case '-':
*result -= *operand;
break;
case '*':
*result *= *operand;
break;
case '/':
if (*operand != 0)
*result /= *operand;
break;
case '^':
for(i = 0; i < *operand; i++)
*result *= *result;
break;
case '#':
if (*result >= 0)
*result = sqrt(*result);
break;
case '%':
*result = *result * (-1);
break;
case '!':
if(*result != 0)
*result = 1 / *result;
break;
case 'q': case 'Q':
printf("\nQuitting\n");
break;
case 'h': case 'H':
print_help();
break;
default:
printf("\nType something valid, please.\nType 'h' for help\n");
break;
}
}
void print_help(void) { /* Prints all of the commands in the program */
printf("\nOperators in this program in no particular order:\n");
printf(" '+ <operand>' Adds <operand> to the accumulator\n");
printf(" '- <operand>' Subtracts <operand> from the accumulator\n");
printf(" '* <operand>' Times the accumulator with <operand>\n");
printf(" '/ <operand>' Divides the accumulator with <operand>\n");
printf(" '^ <operand>' Raise accumulator to the power of <operand>\n\n");
printf(" '#' Squares the accumulator\n");
printf(" '%c' Reverses the sign of the accumulator\n", '%');
printf(" '!' Divides the accumulator with 1\n\n");
printf(" 'h' Prints this help-menu\n");
printf(" 'q' Quits the program\n\n");
}