-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
127 lines (92 loc) · 2.52 KB
/
test.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#define __USE_POSIX199309
#include <sys/time.h>
#include <sys/vfs.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <scan.h>
using std::string;
#define success 0
#define TB 1
//***************************************************************************
// Variable Provider
//***************************************************************************
class VariableProvider
{
public:
VariableProvider() {};
virtual ~VariableProvider() {};
int calcExpression(const char* expression);
int calc(const char* op, int left, int right);
};
//***************************************************************************
// Calculate Expression
// calc expressions like: "2 + 4"
//***************************************************************************
int VariableProvider::calcExpression(const char* expression)
{
Scan* scan;
int result = 0;
char op[100]; *op = 0;
if (!expression || !*expression)
return 0;
scan = new Scan(expression, no);
// left expression
if (scan->eat(yes) != success || !scan->isNum())
{
delete scan;
return result;
}
result = scan->lastInt();
while (scan->eat() == success && scan->isOperator())
{
strcpy(op, scan->lastIdent());
if (scan->eat(yes) != success || !scan->isNum())
{
delete scan;
return result;
}
result = calc(op, result, scan->lastInt());
}
delete scan;
return result;
}
int VariableProvider::calc(const char* op, int left, int right)
{
int result = left;
if (*op == '+')
result += right;
else if (*op == '-')
result -= right;
else if (*op == '*')
result *= right;
else if (*op == ':')
result /= right;
return result;
}
//***************************************************************************
// tescht
//***************************************************************************
#include <cstdlib>
#include <iostream>
int main(int argc, char** argv)
{
int res = 0;
VariableProvider v;
// res = v.calcExpression(argv[1]);
// printf("result = %d\n", res);
int x = 250;
int width = 400;
int dspWidth = 1360;
int themeWidth = 1600;
double xScale = (double)dspWidth / (double)themeWidth;
int xDsp = x * xScale;
int widthDsp = width * xScale;
printf("%d / %d = %.2f \n", dspWidth, themeWidth, xScale);
printf("%d / %.2f = %d \n", width, xScale, widthDsp);
return 0;
}