Step by step

My diary

...

Search

breakinformation. Powered by Blogger.

2018년 10월 16일 화요일

calculation


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
/* calculation */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define MAXSIZE 1000
 
struct _CALCULATOR {
 
    float Number[(MAXSIZE / 2)];
    char Operator[(MAXSIZE) / 2];
};
 
char* String_Tokenize(char* String, char const* _Delimiter);
int Number_in_Calculator(struct _CALCULATOR* _calculate, char* _paper);
int Operator_in_Calculator(struct _CALCULATOR* _calculate, char* _paper);
 
void Nagative_Number_Distinction(struct _CALCULATOR* _calculate, int sign);
float Priority_Operation(struct _CALCULATOR* _calculate, int The_Number_Of, int sign);
 
void print_testing(struct _CALCULATOR* _calculate);
 
int main(void) {
 
    struct _CALCULATOR calculate = { .Number = { 0, },.Operator = { (char0, } };
    char Paper[MAXSIZE] = "- 1 + 2 + 3 * 2 - 1 + 9 / 2 - 10 ";
    char Copy_Paper[MAXSIZE] = { NULL, };
    char Start_Sign = Paper[0];
 
    memcpy(Copy_Paper, Paper, sizeof(Paper));
 
    int Count_Number = 0, Count_Operator = 0;
    Count_Number = Number_in_Calculator(&calculate, Paper);
    Count_Operator = Operator_in_Calculator(&calculate, Copy_Paper);
 
    int Sign = 0;
    if (Start_Sign != '-') Sign = 1;
 
    Nagative_Number_Distinction(&calculate, Sign);
    //print_testing(&calculate);
 
    if (Sign == 0) Sign = 1;
    else Sign = 0;
 
    float result = 0;
    result = Priority_Operation(&calculate, Count_Number, Sign);
 
    printf("%.3f\n", result);
 
    //print_testing(&calculate);
 
    return 0;
}
 
float Priority_Operation(struct _CALCULATOR* _calculate, int The_Number_Of, int sign) {
 
    int index = 0;
    for (index = 0; _calculate->Operator[index] != NULL; index++) {
 
        if (_calculate->Operator[index] == '*') {
 
            _calculate->Number[index - sign] *= _calculate->Number[index - sign + 1];
            _calculate->Number[index - sign + 1= 0;
        }
        else if (_calculate->Operator[index] == '/') {
 
            _calculate->Number[index - sign] /= _calculate->Number[index - sign + 1];
            _calculate->Number[index - sign + 1= 0;
        }
    }
    
    float Calculation_Result = 0.f;
    for (index = 0; index < The_Number_Of; index++) {
 
        Calculation_Result += _calculate->Number[index];
    }
 
    return Calculation_Result;
}
void Nagative_Number_Distinction(struct _CALCULATOR* _calculate, int sign) {
 
    int index = 0;
    for (index = 0; _calculate->Operator[index] != NULL; index++) {
 
        if (_calculate->Operator[index] == '-') {
 
            _calculate->Number[index + sign] *= -1;
        }
    }
 
    return;
}
int Number_in_Calculator(struct _CALCULATOR* _calculate, char* _paper) {
 
    char* Pointer = NULL;
    Pointer = String_Tokenize(_paper, " +-*/=");
 
    int index = 0, The_Number_Of_Count = 0;
    while (Pointer != NULL) {
 
        if (atof(Pointer)) {
 
            _calculate->Number[index++= atof(Pointer);
            The_Number_Of_Count++;
        }
        Pointer = String_Tokenize(NULL" +-*/=");
    }
 
    return The_Number_Of_Count;
}
int Operator_in_Calculator(struct _CALCULATOR* _calculate, char* _paper) {
 
    char* Pointer = NULL;
    Pointer = String_Tokenize(_paper, " 0123456789=");
 
    int index = 0, The_Number_Of_Count = 0;
    while (Pointer != NULL) {
 
        if (*Pointer) {
 
            _calculate->Operator[index++= *Pointer;
            The_Number_Of_Count++;
        }
        Pointer = String_Tokenize(NULL" 0123456789=");
    }
 
    return The_Number_Of_Count;
}
char* String_Tokenize(char* _String, char const* _Delimiter) {
 
    char static* String = NULL;
    char const* Delimiter = NULL;
 
    if (_String == NULL) {
 
        _String = String;
    }
    else {
 
        String = _String;
    }
 
    if (*_String == NULLreturn NULL;
 
    while (*String) {
 
        Delimiter = _Delimiter;
        while (*Delimiter) {
 
            if (*String == *Delimiter) {
 
                *String = NULL;
                String++;
                return _String;
            }
            Delimiter++;
        }
        String++;
    }
 
    return _String;
}
void print_testing(struct _CALCULATOR* _calculate) {
 
    int loop = 0;
    for (loop = 0; _calculate->Number[loop] != 0; loop++) {
 
        printf("%.2f\n", _calculate->Number[loop]);
    }
    for (loop = 0; _calculate->Operator[loop] != 0; loop++) {
 
        printf("%c\n", _calculate->Operator[loop]);
    }
 
    return;
}
cs

0 개의 댓글:

댓글 쓰기