-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
80 lines (74 loc) · 1.68 KB
/
main.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
#include "predefinitions.h"
#include "read.h"
#include "eval.h"
#include "print.h"
void base_help(){
printf("calculator v0.0.1 powered by Erchius.\n");
}
int help(){
base_help();
printf("usage:\n");
printf("calculator | calculator -i run interactively.\n");
printf("calculator -h print usage information.\n");
printf("calculator -c \"<expression>\" calculate the input expression, then output.\n");
printf("calculator -f \"<file name>\" read from the file, then calculate and output.\n");
return 0;
}
int help( char command ){
switch( command ){
case 'c':
base_help();
printf("calculator -c \"<expression>\" calculate the input expression, then output.\n");
break;
case 'f':
base_help();
printf("calculator -f \"<file name>\" read from the file, then calculate and output.\n");
break;
default:
printf( "Unknown option: -%c, try `calculator -h`.\n", command );
help();
break;
}
return 0;
}
int help( char *str, char* command ){
printf( str, command );
help();
return 0;
}
void compute(){
Token token[100] = {0};
int num_token = read( token );
if( ~num_token )
print( eval( token, num_token ), token, num_token );
else if( num_token ){
printf("error occurred during I/O\n");
exit(1);
}
else{
return;
}
if( feof(fin) )
exit(0);
}
int main(int argc, char const *argv[])
{
switch( argc ){
case 1:
while(1)
compute();
break;
case 2:
if( argv[1][0] != '-' )
return help( "Not understood: %s, try `calculator -h`.", argv[1] );
if( strlen( argv[1] ) != 2 )
return help( "Unknown option: %s, try `calculator -h`.", argv[1] );
switch( argv[1][1] ){
case 'h':
return help();
default :
return help( argv[1][1] );
}
}
return 0;
}