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

Meaningless


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
#include <stdio.h>
 
int main(void) {
 
    char _Array[10= { 0, };
    int inputInteger = 33;
    char symbol = '3';
 
    int loop = 0, index = 0, count = 0;
    for (loop = 1; loop < inputInteger + 1; loop++) {
 
        sprintf(_Array, "%d", loop);
        while (_Array[index]) {
 
            if (_Array[index] == symbol) {
 
                printf("%s ", _Array);
                count++;
            }
            index++;
        }
        index = 0;
    }
    printf("\n\n%d\n", count);
 
    return 0;
}
cs

2018년 10월 15일 월요일

print formatted


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
#include <stdio.h>
#include <stdarg.h>
 
int Print_Formatted (char const* format, ...);
 
int main (void) {
    
    char* Greeting = "Hello, World!!";
    
    printf ("String length : %d\n", Print_Formatted ("%s%c", Greeting, '\n'));
    
    return 0;
}
 
int Print_Formatted (char const* format, ...) {
    
    va_list argument = NULL;
    int String_Length = 0;
    
    va_start (argument, format);
    String_Length = vfprintf (stdout, format, argument);
    va_end (argument);
    
    return String_Length;
}
cs

string tokenize


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
#include <stdio.h>
 
char* String_Tokenize(char* String, char const* _Delimiter);
 
int main(void) {
 
    char String[30= "The Little Prince";
    char* pointer = String_Tokenize(String, " ");
 
    while (pointer != (char)0) {
 
        printf("%s\n", pointer);
        pointer = String_Tokenize((char)0" ");
    }
 
    return 0;
}
 
char* String_Tokenize(char* _String, char const* _Delimiter) {
 
    char static* String = (char)0;
    char const* Delimiter = (char)0;
 
    if (_String == (char)0) {
 
        _String = String;
    }
    else {
 
        String = _String;
    }
 
    if (*_String == (char)0return (char)0;
 
    while (*String) {
 
        Delimiter = _Delimiter;
        while (*Delimiter) {
 
            if (*String == *Delimiter) {
 
                *String = (char)0;
                String++;
                return _String;
            }
            Delimiter++;
        }
        String++;
    }
 
    return _String;
}
cs

string concatenate


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
#include <stdio.h>
 
char* String_Concatenate(char* _Destination, char const* _Source);
 
int main(void) {
 
    char greeting [20= "hello ";
 
    printf("%s\n", String_Concatenate(greeting, "world!!"));
 
    return 0;
}
 
char* String_Concatenate(char* _Destination, char const* _Source) {
 
    int Destination_Index = 0;
    while (_Destination[Destination_Index] != (char0) Destination_Index++;
 
    int Source_Index = 0;
    while (_Source[Source_Index] != (char)0) _Destination[Destination_Index++= _Source[Source_Index++];
 
    _Destination[Destination_Index] = (char)0;
 
    return _Destination;
}
cs

magic square


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
#include <stdio.h>
 
#define MAX_SIZE 4
 
int main (void) {
    
    int array[MAX_SIZE][MAX_SIZE] = { 0, };
    
    // diagonal line
    int minimum = 0, maximum = 0;
    for (minimum = 0, maximum = MAX_SIZE - 1; minimum < MAX_SIZE; minimum++, maximum--) {
        
        array[minimum][minimum] = -1;
        array[minimum][maximum] = -1;
    }
    
    // insert a number
    int increase = 1, decrease = MAX_SIZE * MAX_SIZE;
    int row = 0, column = 0;
    for (column = 0; column < MAX_SIZE; column++) {
        for (row = 0; row < MAX_SIZE; row++) {
            
            if (array[column][row] == -1) array[column][row] = increase;
            else array[column][row] = decrease;
            
            increase++;
            decrease--;
        }
    }
    
    // print
    for (column = 0; column < MAX_SIZE; column++) {
        for (row = 0; row < MAX_SIZE; row++) {
            
            printf ("%d\t", array[column][row]);
        }
        printf ("\n");
    }
    
    return 0;
}
cs

2018년 10월 1일 월요일

bubble sort


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
#include <stdio.h>
 
void bubble_sort (int* array, int count);
void swapping (int* current, int* next);
 
int main (void) {
    
    int numberArray[10= { 84253710169 };
    int loop = 0;
    
    bubble_sort (numberArray, sizeof (numberArray) / sizeof (int));
    
    for (loop = 0; loop < sizeof (numberArray) / sizeof (int); loop++) {
        
        printf("%d ", numberArray[loop]);
    }
    printf("\n");
    
    return 0;
}
 
void bubble_sort (int* array, int count) {
    
    int allElement = 0, partialElement = 0;
    
    for (allElement = 0; allElement < count; allElement++) {
        for (partialElement = 0; partialElement < count - 1; partialElement++) {
            if (array[partialElement] > array[partialElement + 1]) {
                
                swapping (&array[partialElement], &array[partialElement + 1]);
            }
        }
        partialElement++;
    }
    
    return;
}
void swapping (int* current, int* next) {
    
    int temporary = 0;
    
    temporary = *current;
    *current = *next;
    *next = temporary;
    
    return;
}
cs