-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmx_calc_test.h
More file actions
194 lines (173 loc) · 4.35 KB
/
mx_calc_test.h
File metadata and controls
194 lines (173 loc) · 4.35 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef __MX_CALC_TEST_H__
#define __MX_CALC_TEST_H__
#include<stdio.h>
#include<stdlib.h>
//global data
void prompt(){
printf(">");
}
int extract(char st1[])
{
char line[512]; // or however large you think these lines will be
char string1[20];
int intval;
int val2;
FILE *in1 = fopen ("file.txt", "rw"); /* open the file for reading */
int cur_line = 0;
while(fgets(line, 512, in1) != NULL) {
/* get a line, up to 512 chars from in. done if NULL */
sscanf (line, "%s %d\n",string1,&intval);
if(strcmp(string1,st1)==0)
{
val2=intval;
// printf("%d",val2);
}
// now you should store or manipulate those strings
// printf("%s,%d",string1,intval);
}
fclose(in1); /* close the file */
return(val2);
}
void clear()
{
int status;
status = remove("file.txt");
printf("Cleared all the data");
}
void inte(char c[],int val) {
FILE *in = fopen ("file.txt", "a+"); /* open the file for reading */
/* "rt" means open the file for reading text */
fprintf(in,"%s %d\n",c,val);
fclose(in);
}
void divide(char st1[], char st2[])
{
int val1 = extract(st1);
int val2 = extract(st2);
if(val2==0)
{
printf("division by 0 is not possible, try again\n");
prompt();
}
else
{
int divval=val1/val2;
printf("Division value %d\n",divval);
prompt();
}
prompt();
}
void mul(char st1[], char st2[])
{
int val1 = extract(st1);
int val2 = extract(st2);
int mulval=val1 * val2;
printf("Multiplied value %d\n",mulval);
prompt();
}
void add(char st1[], char st2[])
{
int val1 = extract(st1);
int val2 = extract(st2);
int addedval=val1 + val2;
printf("Added value %d\n",addedval);
prompt();
}
void sub(char st1[], char st2[])
{
int val1 = extract(st1);
int val2 = extract(st2);
int addedval=val1 - val2;
printf("Substracted value %d\n",addedval);
prompt();
}
void test() {
char st1[10]="m";
int val1 = 24;
char st2[]="n";
int val2=30;
inte(st1,val1);
inte(st2,val2);
add(st1,st2);
}
void parse (const char *cmd) {
int i = 0;
int j = 0;
char supint[10]="integer";
char supadd[10]="add";
char supsub[10]="sub";
char supmul[10]="mul";
char supdiv[10]="divide";
char supexit[10]="exit";
char supclear[10]="clear";
char suphelp[10]="help";
char var[10];
char var1[10];
char var2[10];
int arg;
while(j==0 && i<5) {
if(!strncmp(cmd,supexit,4)) {
j=1; exit(0);
}
else if (!strncmp(cmd,suphelp,4)) {
printf("integer X $X- To enter integer values\n");
printf("add X Y- To Add 2 integer values\n");
printf("Mul X Y- To multiply integer values X and Y\n");
printf("divide X Y- To divide integer values X and Y\n");
printf("clear - To clear data in the variables\n");
printf("exit- To exit the program\n");
j=1;
}
else if (!strncmp(cmd,supclear,4)) {
clear();
j=1;
}
else if (!strncmp(cmd,supmul,3)) {
if(sscanf(cmd+3,"%s %s",var1,var2) > 0) {
mul(var1,var2);
j=1;
}
}
else if (!strncmp(cmd,supdiv,3)) {
if(sscanf(cmd+3,"%s %s",var1,var2) > 0) {
divide(var1,var2);
j=1;
}
}
else if(!strncmp(cmd,supint,7)) {
if(sscanf(cmd+7,"%s %d",var,&arg) > 0) {
inte(var,arg);
j=1;
}
}
else if(!strncmp(cmd,supadd,3)){
if(sscanf(cmd+3,"%s %s",var1,var2) > 0) {
add(var1,var2);
j=1;
}
}
else if(!strncmp(cmd,supsub,3)){
if(sscanf(cmd+3,"%s %s",var1,var2) > 0) {
sub(var1,var2);
j=1;
}
}
else {
printf("Not a Valid Command\n");
prompt();
j=1;
}
} //while loop closing
}
void calctest() {
int i = 0;
char cmd[200];
while(1) {
prompt();
char *test=fgets(cmd,200,stdin);
// printf("%s",test);
parse(test);
// parse(fgets(cmd,200,stdin));
}
}
#endif