forked from patrikarlos/np_assignment1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
152 lines (121 loc) · 4.18 KB
/
main.cpp
File metadata and controls
152 lines (121 loc) · 4.18 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Include the calcLib header file, using <> as its a library and not just a object file we link. */
#include <calcLib.h>
/*
The aim with this code, is to give you examples HOW to grab a random arithmetic operator, matching values and perform a
reference calculation, so that you can eventually compare what came from the other side.
It also shows how to decode a string into different variables.
You are free to reuse relevant parts, as the assignment is ABOUT the communication, which is omitted from the example.
*/
/* Std start to main, argc holds the number of arguments provided to the executable, and *argv[] an
array of strings/chars with the arguments (as strings).
*/
int main(int argc, char *argv[]){
/* Initialize the library, this is needed for this library. */
initCalcLib();
char *ptr;
ptr=randomType(); // Get a random arithemtic operator.
double f1,f2,fresult;
int i1,i2,iresult;
/*
printf("ptr = %p, \t", ptr );
printf("string = %s, \n", ptr );
*/
// printf("Int\t");
i1=randomInt();
i2=randomInt();
// printf("Float\t");
f1=randomFloat();
f2=randomFloat();
printf(" Int Values: %d %d \n",i1,i2);
printf("Float Values: %8.8g %8.8g \n",f1,f2);
/* Act differently depending on what operator you got, judge type by first char in string. If 'f' then a float */
if(ptr[0]=='f'){
/* At this point, ptr holds operator, f1 and f2 the operands. Now we work to determine the reference result. */
if(strcmp(ptr,"fadd")==0){
fresult=f1+f2;
} else if (strcmp(ptr, "fsub")==0){
fresult=f1-f2;
} else if (strcmp(ptr, "fmul")==0){
fresult=f1*f2;
} else if (strcmp(ptr, "fdiv")==0){
fresult=f1/f2;
}
printf("%s %8.8g %8.8g = %8.8g\n",ptr,f1,f2,fresult);
} else {
if(strcmp(ptr,"add")==0){
iresult=i1+i2;
} else if (strcmp(ptr, "sub")==0){
iresult=i1-i2;
} else if (strcmp(ptr, "mul")==0){
iresult=i1*i2;
} else if (strcmp(ptr, "div")==0){
iresult=i1/i2;
}
printf("%s %d %d = %d \n",ptr,i1,i2,iresult);
}
/* This section shows how to read a line from stdin, process and do a similar operation as above. */
char *lineBuffer=NULL;
size_t lenBuffer=0;
ssize_t nread=0;
printf("Print a command: ");
nread=getline(&lineBuffer,&lenBuffer,stdin);
if (nread == -1 ) {
printf("getline failed.\n");
exit(1);
}
printf("got:> %s \n",lineBuffer);
int rv;
char command[10];
rv=0;
rv=sscanf(lineBuffer,"%s",command);
if (rv == EOF ) {
printf("Sscanf failed.\n");
free(lineBuffer); // This is needed for the getline() as it will allocate memory (if the provided buffer is NUL).
exit(1);
}
printf("Command: |%s|\n",command);
if(command[0]=='f'){
printf("Float\t");
rv=sscanf(lineBuffer,"%s %lg %lg",command,&f1,&f2);
if (rv == EOF ) {
printf("Sscanf failed.\n");
free(lineBuffer); // This is needed for the getline() as it will allocate memory (if the provided buffer is NUL).
exit(1);
}
if(strcmp(command,"fadd")==0){
fresult=f1+f2;
} else if (strcmp(command, "fsub")==0){
fresult=f1-f2;
} else if (strcmp(command, "fmul")==0){
fresult=f1*f2;
} else if (strcmp(command, "fdiv")==0){
fresult=f1/f2;
}
printf("%s %8.8g %8.8g = %8.8g\n",command,f1,f2,fresult);
} else {
printf("Int\t");
rv=sscanf(lineBuffer,"%s %d %d",command,&i1,&i2);
if (rv == EOF ) {
printf("Sscanf failed.\n");
free(lineBuffer); // This is needed for the getline() as it will allocate memory (if the provided buffer is NUL).
exit(1);
}
if(strcmp(command,"add")==0){
iresult=i1+i2;
} else if (strcmp(command, "sub")==0){
iresult=i1-i2;
printf("[%s %d %d = %d ]\n",command,i1,i2,iresult);
} else if (strcmp(command, "mul")==0){
iresult=i1*i2;
} else if (strcmp(command, "div")==0){
iresult=i1/i2;
} else {
printf("No match\n");
}
printf("%s %d %d = %d \n",command,i1,i2,iresult);
}
free(lineBuffer); // This is needed for the getline() as it will allocate memory (if the provided buffer is NUL).
}