1. 程式人生 > >C語言極簡版計算器

C語言極簡版計算器

用c寫的簡單的計算器

介面如下圖所示:

感興趣可點選下載:

原始碼如下:

#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include <stdio.h>
#include <stdlib.h>

BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
        HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
		HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
    }

    return FALSE;
}

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
		HWND hwndcombo1 = GetDlgItem(hwnd,IDC_COMBO1);
		ComboBox_InsertString(hwndcombo1,-1,TEXT("+"));
		ComboBox_InsertString(hwndcombo1,-1,TEXT("-"));
		ComboBox_InsertString(hwndcombo1,-1,TEXT("*"));		
		ComboBox_InsertString(hwndcombo1,-1,TEXT("/"));	
    return TRUE;
}

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
    switch(id)
    {
        case IDC_OK:
		{
			TCHAR str1[256];
			TCHAR str2[256];
			GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));
			GetDlgItemText(hwnd,IDC_EDIT2,str2,sizeof(str2));
			int i1=atoi(str1);
			int i2=atoi(str2);
			int i3=0;
			HWND hwndCombo1 = GetDlgItem(hwnd,IDC_COMBO1);
			int curIndex = ComboBox_GetCurSel(hwndCombo1);
			switch(curIndex)
			{
				case 0:
				{
					i3=i1+i2;
				}
				break;
				case 1:
				{
					i3=i1-i2;
				}
				break;
				case 2:
				{
					i3=i1*i2;
				}
				break;
				case 3:
				{
					i3=i1/i2;
				}
				break;
			}
			TCHAR str3[256];
			itoa(i3,str3,10);
			SetDlgItemText(hwnd,IDC_EDIT3,str3);
		}
		break;
        default:
		break;
    }
}

void Main_OnClose(HWND hwnd)
{
    EndDialog(hwnd, 0);
}