• Lenguaje

    C

  • Descripción

    Calculadora simple de escritorio.
    Hace operaciones de suma, resta, multiplicación, división, raíz cuadrada, porcentaje y operaciones básicas de memoria.

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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* Sintaxis de la linea de comandos para compilar en
 *     Visual Studio: cl calculadora.c /link user32.lib gdi32.lib
 *     Borland C++  : bcc32 -L"[directorio de borland c++]\lib" -I"[directorio de borland c++]\include" -tW calculadora.c
 *     MinGW        : gcc -mwindows -o calculadora.exe calculadora.c
 */

#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>

const char *etiquetas[] =
{
    "\xAC", "CE", "C",
    "MC", "MR", "MS", "M+",
    "7", "8", "9", "\xB8", "\xD6",
    "4", "5", "6", "\xB4", "%",
    "1", "2", "3", "-", "1/x",
    "0", "\xB1", ".", "+", "="
};
static int locaciones[][2] =
{
                {99,  47}, {137,  47}, {175,  47},
    {12,  89}, { 12, 118}, { 12, 147}, { 12, 176},
    {61,  89}, { 99,  89}, {137,  89}, {175,  89}, {213,  89},
    {61, 118}, { 99, 118}, {137, 118}, {175, 118}, {213, 118},
    {61, 147}, { 99, 147}, {137, 147}, {175, 147}, {213, 147},
    {61, 176}, { 99, 176}, {137, 176}, {175, 176}, {213, 176}
};
HWND botones[27], display, displayMemoria;
char *cifra = NULL;
double valor = 0.0, memoria = 0.0, anterior = 0.0;
char operador = '\0';

void cifra_set_text (const char *text)
{
    if (cifra!=NULL)
        free (cifra);
    cifra = text!=NULL? strdup (text): NULL;
}

void setValor (double val)
{
    char text[255];
    valor = val;
    cifra_set_text (NULL);
    if (valor==0.0)
        sprintf (text, "0");
    else if (valor == floor (valor))
        sprintf (text, "%ld", (long)valor);
    else
        sprintf (text, "%.20g", valor);
    SetWindowText (display, text);
}

void setMemoria (double mem)
{
    memoria = mem;
    SetWindowText (displayMemoria, memoria==0.0? "": "M");
}

void mostrarError (void)
{
    SetWindowText (display, "Error");
    cifra_set_text (NULL);
    valor = 0.0;
}

void onButtonPress (int boton)
{
    double temporal;
    char text[255], tecla = etiquetas[boton][0];
    switch (boton)
    {
        //  DEL
        case 0:
            if (cifra==NULL || strlen(cifra)==1 || (cifra[0]=='-' && strlen(cifra)==2) || strcmp (cifra, "0.")==0 || strcmp (cifra, "-0.")==0)
                setValor(0.0);
            else
            {
                cifra[strlen(cifra)-1] = '\0';
                valor = atof (cifra);
                SetWindowText (display, cifra);
            }
            break;
        //  CE
        case 1:
            anterior = 0.0;
            operador = '\0';
        //  C
        case 2:
            setValor (0.0);
            break;
        //  MC
        case 3:
            setMemoria (0.0);
            break;
        //  MR
        case 4:
            setValor (memoria);
            break;
        //  MS
        case 5:
            setMemoria (valor);
            break;
        //  M+
        case 6:
            setMemoria (memoria + valor);
            break;
        //  SQRT
        case 11:
            if (valor >= 0.0)
                setValor (sqrt (valor));
            else
                mostrarError ();
            break;
        //  /
        case 10:
        //  *
        case 15:
        //  %
        case 16:
        //  -
        case 20:
        //  +
        case 25:
        //  =
        case 26:
            temporal = valor;
            switch (operador)
            {
                case '/':
                    if (valor!=0.0)
                        setValor (anterior/valor);
                    else
                        mostrarError ();
                    break;
                case '*': setValor (anterior*valor); break;
                case '%': setValor (anterior*valor/100.0); break;
                case '-': setValor (anterior-valor); break;
                case '+': setValor (anterior+valor); break;
            }
            anterior =
                operador == '/' || operador == '*' ||
                operador == '%' || operador == '-' ||
                operador == '+' ? valor : temporal;
            if (boton == 10)
                operador = '/';
            else if (boton == 15)
                operador = '*';
            else if (boton == 20)
                operador = '-';
            else
                operador = tecla;
            cifra_set_text (NULL);
            break;
        //  1/x
        case 21:
            if (valor!=0.0)
                setValor (1.0/valor);
            else
                mostrarError ();
            break;
        //  +/-
        case 23:
            if (valor!=0.0)
            {
                if (cifra==NULL)
                    setValor(-valor);
                else
                {
                    valor = -valor;
                    if (cifra[0]=='-')
                        sprintf (text, "%s", &cifra[1]);
                    else
                        sprintf (text, "-%s", cifra);
                    cifra_set_text (text);
                    SetWindowText (display, text);
                }
            }
            break;
        //  .
        case 24:
            if (cifra==NULL)
            {
                valor = 0.0;
                cifra_set_text ("0.");
                SetWindowText (display, cifra);
            }
            else if (strchr(cifra, '.')==NULL)
            {
                sprintf (text, "%s.", cifra);
                cifra_set_text (text);
                SetWindowText (display, cifra);
            }
            break;
        //  0 1 2 3 4 5 6 7 8 9
        default:
            if (cifra!=NULL || tecla!='0')
            {
                if (cifra==NULL)
                    sprintf (text, "%c", tecla);
                else
                    sprintf (text, "%s%c", cifra, tecla);
                cifra_set_text (text);
                valor = atof (cifra);
                SetWindowText (display, cifra);
            }
    }
}

LRESULT CALLBACK MainWndProc (HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
    int i, boton;
    HDC hdc;
    PAINTSTRUCT ps;
    HFONT symbolFont, verdanaFont;
    static HBRUSH hbrBackground;
    switch (nMsg)
    {
        case WM_CREATE:
            symbolFont = CreateFont (14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Symbol");
            verdanaFont  = CreateFont (14, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Verdana");
            for (i=0; i<27; i++)
            {
                botones[i] = CreateWindow (
                    "button", etiquetas[i], WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                    locaciones[i][0], locaciones[i][1], 33, 23, hwnd, (HMENU) 1,
                    ((LPCREATESTRUCT) lParam)->hInstance, NULL);
                SendMessage (botones[i], WM_SETFONT, (WPARAM)(i==0 || i==10 || i==11 || i==15? symbolFont: verdanaFont), TRUE);
            }
            display        = CreateWindow ("static", "0", WS_CHILD | WS_VISIBLE | SS_RIGHT  | SS_SUNKEN, 12, 9, 239, 25, hwnd, NULL, ((LPCREATESTRUCT) lParam)->hInstance, NULL);
            displayMemoria = CreateWindow ("static", "" , WS_CHILD | WS_VISIBLE | SS_CENTER | SS_SUNKEN, 61, 47, 33, 23, hwnd, NULL, ((LPCREATESTRUCT) lParam)->hInstance, NULL);          
            SendMessage (display       , WM_SETFONT, (WPARAM)verdanaFont, TRUE);
            SendMessage (displayMemoria, WM_SETFONT, (WPARAM)verdanaFont, TRUE);
            hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
            return 0;
        case WM_DESTROY:
            PostQuitMessage (0);
            return 0;
        case WM_PAINT:
            BeginPaint (hwnd, &ps);
            EndPaint (hwnd, &ps);
            return 0;
        case WM_COMMAND:
            if (LOWORD(wParam) == 1 && HIWORD(wParam) == BN_CLICKED)
                for (i=0; i<27; i++)
                    if ((HWND)lParam==botones[i])
                    {
                        onButtonPress (i);
                        SetFocus (hwnd);
                        break;
                    }
            return 0;
        case WM_CHAR:
            switch (tolower(wParam))
            {
                case '\b': boton= 0; break;
                case 'c': boton= 2; break;
                case '7': boton= 7; break;
                case '8': boton= 8; break;
                case '9': boton= 9; break;
                case '/': boton=10; break;
                case 's': boton=11; break;
                case '4': boton=12; break;
                case '5': boton=13; break;
                case '6': boton=14; break;
                case '*': boton=15; break;
                case 'p':
                case '%': boton=16; break;
                case '1': boton=17; break;
                case '2': boton=18; break;
                case '3': boton=19; break;
                case '-': boton=20; break;
                case 'r': boton=21; break;
                case '0': boton=22; break;
                case '.': boton=24; break;
                case '+': boton=25; break;
                case '\r':
                case '\n':
                case '=': boton=26; break;
                case 27:
                    PostQuitMessage (0);
                    return 0;
                default:
                    return 0;
            }
            onButtonPress (boton);
            return 0;
        case WM_KEYDOWN:
            if (wParam==VK_DELETE)
            {
                onButtonPress (2);
                return 0;
            }
        case WM_CTLCOLORDLG:
            return (LONG)hbrBackground;
        case WM_CTLCOLORSTATIC:
            hdc = (HDC)wParam;
            SetBkMode(hdc, TRANSPARENT);
            return (LONG)hbrBackground;
    }
    return DefWindowProc (hwnd, nMsg, wParam, lParam);
}

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
    HWND calculadora;
    MSG msg;
    WNDCLASSEX wndclass;
    RECT rect;
    HWND desktop = GetDesktopWindow();
    int x = CW_USEDEFAULT, y = CW_USEDEFAULT;
    char *szMainWndClass = strdup ("Calculadora");
    memset (&wndclass, 0, sizeof(WNDCLASSEX));
    wndclass.lpszClassName = szMainWndClass;
    wndclass.cbSize = sizeof(WNDCLASSEX);
    wndclass.lpfnWndProc = MainWndProc;
    wndclass.hInstance = hInst;
    wndclass.hbrBackground = (HBRUSH) COLOR_WINDOW;
    RegisterClassEx (&wndclass);
    if (GetClientRect(desktop, &rect))
    {
        x = (rect.right  - 272) / 2;
        y = (rect.bottom - 242) / 2;
    }
    calculadora = CreateWindow (szMainWndClass, szMainWndClass, WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, x, y, 272, 242, NULL, NULL, hInst, NULL);
    ShowWindow (calculadora, nShow);
    UpdateWindow (calculadora);
    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }
    return msg.wParam;
}