Simple Calculator (API)

Source: Internet
Author: User

Do an ico file on your own.

#ifndef Calculator_h

#define Calculator_h

#include "Stack.h"
#include <iostream>
#include <string>

using namespace Std;

Class calculator{
Private
Stack<double> Nstack;
Stack<char> Ostack;
Public
Double A;
Calculator (): A (0) {};
~calculator () {};
void Clear ();
void Compute (char Opr);
void Gettwonumdouble (double &num1, double &num2);
void Gettwonumint (int &num1, int &num2);
void Cal (string);//Calculator Operation program
};

#endif

{{No_dependencies}}


Included files generated by Microsoft Visual C + +.
For resource.rc use
//
#define IDD_START 102
#define IDI_ICON1 103
#define IDC_BUTTON1 1000
#define IDC_BUTTON2 1001
#define IDC_BUTTON3 1002
#define IDC_BUTTON4 1003
#define IDC_BUTTON5 1004
#define IDC_BUTTON6 1005
#define IDC_BUTTON7 1006
#define IDC_BUTTON8 1007
#define IDC_BUTTON9 1008
#define IDC_BUTTON10 1009
#define IDC_BUTTON11 1010
#define IDC_BUTTON12 1011
#define IDC_BUTTON13 1012
#define IDC_BUTTON14 1013
#define IDC_BUTTON15 1014
#define IDC_BUTTON16 1015
#define IDC_BUTTON17 1016
#define IDC_BUTTON18 1017
#define IDC_LIST1 1018
#define IDC_BUTTON19 1019
#define IDC_BUTTON20 1020
#define IDC_BUTTON21 1021

Next default values for new objects
//
#ifdef apstudio_invoked
#ifndef Apstudio_readonly_symbols
#define _APS_NEXT_RESOURCE_VALUE 104
#define _aps_next_command_value 40003
#define _aps_next_control_value 1022
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif

#ifndef Stack_h
#define Stack_h

#include <cassert>
#include <iostream>
using namespace Std;

Template<typename T>class stack;//class declaration

Template<typename T>class node{
Private
T info;
node<t>*link;
Public
Node (T data = 0, Node<t>*next = NULL) {
info = data;
link = next;
}
Friend Class stack<t>;
};

Chain Stack class template, headless node linked list
Template<typename T>class stack{
Private
node<t>*top;//Stack Top pointer
Public
Stack () {top = NULL;}
~stack ();
void Push (const t&data);//Press Stack
T pop ();//Popup
T GetTop ();//Take the top element of the stack
void Makeempty ();//emptying stack
BOOL IsEmpty () {return top = = NULL;}
};

Template<typename T>stack<t>::~stack () {makeempty ();}

Template<typename t>void stack<t>::P ush (const t&data) {
top = new node<t> (data, top);
}

Template<typename t>void Stack<t>::makeempty () {
node<t>*temp;
while (top! = NULL) {
temp = top;
top = top->link;
Delete temp;
}
}

Template<typename t>t Stack<t>::P op () {
ASSERT (! IsEmpty ());
Node<t>*temp = top;
T data = temp->info;
top = top->link;//Drop stack top node
Delete temp;//release stack top node
Return data;//back to stack top data
}

Template<typename t>t Stack<t>::gettop () {
ASSERT (! IsEmpty ());
Return top->info;
}
#endif

#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <sstream>
#include "Calculator.h"
using namespace Std;

void Calculator::clear () {
Nstack.makeempty ();
Ostack.makeempty ();
}

void Calculator::gettwonumdouble (Double & Num1, double & Num2) {
NUM1 = Nstack.pop ();
Num2 = Nstack.pop ();
}

void Calculator::gettwonumint (int &num1, int &num2) {
NUM1 = Nstack.pop ();
Num2 = Nstack.pop ();
}

void Calculator::compute (char Opr) {
char Opr1 = OPR;
Double Num1, Num2;
int Num3, NUM4;
if (Opr! = ' = ' &&opr! = '% ') gettwonumdouble (NUM1, Num2);
else if (Opr! = ' = ') gettwonumint (Num3, NUM4);
Switch (OPR) {
Case ' + ': Nstack.push (Num2 + Num1); Break
Case '-': Nstack.push (NUM2-NUM1); Break
Case ' * ': Nstack.push (NUM2*NUM1); Break
Case '/': Nstack.push (NUM2/NUM1); Break
Case '% ': Nstack.push (NUM4%NUM3); Break
Case ' = ': a = Nstack.pop (); Break
}
}
void calculator::cal (String str) {
BOOL B1 = true;
bool B2 = true;
Char CH2, str2[50];
string str1 = str;
int K, j =-1;
Nstack.push (0);
for (k = 0; k <= (int) str.length ()-1; k++) {
if ((str1[k] >= ' 0 ' &&str1[k] <= ' 9 ') | | str1[k] = = '. ') {
j + +;
STR2[J] = str1[k];
}
else{
if (J >= 0) {
Str2[j + 1] = ' + ';
Nstack.push (Atof (str2));
j =-1;
}
Switch (Str1[k]) {
Case ' + ': Case '-':
while (! Ostack.isempty () && ostack.gettop ()! = ' (') {//************* note
CH2 = Ostack.pop ();
Compute (CH2);
}
Ostack.push (Str1[k]);
Break
Case ' * ': Case '/': Case '% ':
while (! Ostack.isempty () && b1&&ostack.gettop ()! = ' (') {
CH2 = Ostack.pop ();
if (CH2 = = ' * ' | | ch2 = = '/' | | ch2 = = '% ')
Compute (CH2);
else{
Ostack.push (CH2);
B1 = false;
}
}
Ostack.push (Str1[k]);
B1 = true;
Break
Case ' = ':
while (! Ostack.isempty ()) {
CH2 = Ostack.pop ();
Compute (CH2);
}
Compute (Str1[k]);
Break
Case ' (': Ostack. Push (Str1[k]); Break
Case ') ':
Char STR3 = Ostack.gettop ();
while (STR3! = ' (') {
STR3 = Ostack.pop ();
Compute (STR3);
STR3 = Ostack.gettop ();
}
Ostack.pop ();
Break
}
}
}
}

#include <iostream>
#include <string>
#include <sstream>
#include "Calculator.h"
using namespace Std;

#include <windows.h>
#include "resource.h"

Char buf1[20];

String doubletostring (double D) {
String str;
StringStream SS;
SS << D;
SS >> Str;
return str;
}

Main function, test, below is API
const char g_szclassname[] = "Mywindowclass";
Hbrush g_hbrbackground = CreateSolidBrush (RGB (0, 0, 0));

Char buf[100];
int i = 0;

BOOL CALLBACK Aboutdlgproc (HWND hwnd, UINT Message, WPARAM WPARAM, LPARAM LPARAM)
{
Switch (Message)
{
Case WM_INITDIALOG:
SendMessage (hwnd, Wm_seticon, Icon_small, (LPARAM) LoadIcon (GetModuleHandle (NULL), Makeintresource (Idi_icon1)));
SendMessage (hwnd, Wm_seticon, Icon_big, (LPARAM) LoadIcon (GetModuleHandle (NULL), Makeintresource (Idi_icon1)));
Case WM_CTLCOLORDLG:
Return (LONG) G_hbrbackground;
Case wm_ctlcolorstatic:{
HDC hdcstatic = (hdc) WParam;
SetTextColor (hdcstatic, RGB (255, 255, 255));
SetBkMode (hdcstatic, TRANSPARENT);
Return (LONG) G_hbrbackground;
//}
Break
return TRUE;
Case WM_COMMAND:
Switch (LOWORD (wParam))
{
Case idc_button1:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' 1 ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button2:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' 2 ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button3:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' 3 ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button4:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' 4 ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button5:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' 5 ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button6:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' 6 ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button7:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' 7 ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button8:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' 8 ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button9:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' 9 ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button10:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' 0 ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button11:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = '. ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button12:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' = ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
i = 0;
Calculator Calcul;
Calcul.cal (BUF);
String str1 = Doubletostring (CALCUL.A);
for (int i = 0; i< (int) str1.size (); i++) {
Buf1[i] = Str1[i];
}
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf1);
}
Break
Case idc_button13:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' + ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button14:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = '-';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button15:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' * ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button16:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = '/';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button17:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ' (';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button18:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = ') ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button19:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[i] = '% ';
Buf[++i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
Break
Case idc_button20:{
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
i = 0;
for (int i = 0; Buf[i]! = ' + '; i++) {
Buf[i] = ' + ';
}
}
Break
Case idc_button21:{
if (i > 0) {
SendDlgItemMessage (hwnd, IDC_LIST1, lb_resetcontent, 0, 0);
Buf[--i] = ' + ';
SendDlgItemMessage (hwnd, IDC_LIST1, lb_addstring, 0, (LPARAM) buf);
}
}
Break
}
Break
Case WM_CLOSE:
EndDialog (hwnd, 0);
Default
return FALSE;
}
return TRUE;
}

int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance,lpstr lpcmdline, int ncmdshow)
{
Return DialogBox (HINSTANCE, Makeintresource (Idd_start), NULL, Aboutdlgproc);
}

Microsoft Visual C + + generated resource script.
//
#include "resource.h"

#define Apstudio_readonly_symbols
/////////////////////////////////////////////////////////////////////////////
//
Generated from the Textinclude 2 resource.
//
#ifndef __borlandc__
#include "Winres.h"
#endif

/////////////////////////////////////////////////////////////////////////////
#undef Apstudio_readonly_symbols

/////////////////////////////////////////////////////////////////////////////
Chinese (Simplified, China) resources

#if!defined (afx_resource_dll) | | Defined (AFX_TARG_CHS)
LANGUAGE Lang_chinese, sublang_chinese_simplified

/////////////////////////////////////////////////////////////////////////////
//
Icon
//

Icon with lowest ID value placed first to ensure application icon
Remains consistent on all systems.
Idi_icon1 ICON "E:\\cailu.ico"
#endif//Chinese (Simplified, China) resources
/////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
English (United States) resources

#if!defined (afx_resource_dll) | | Defined (Afx_targ_enu)
LANGUAGE Lang_english, Sublang_english_us

#ifdef apstudio_invoked
/////////////////////////////////////////////////////////////////////////////
//
Textinclude
//

1 textinclude
BEGIN
"Resource.h\0"
END

2 Textinclude
BEGIN
"#ifndef __borlandc__\r\n"
"#include" "Winres.h" "\ r \ n"
"#endif \ r \ n"
"The"
END

3 Textinclude
BEGIN
"\ r \ n"
"The"
END

#endif//apstudio_invoked


/////////////////////////////////////////////////////////////////////////////
//
Dialog
//

Idd_start Dialogex 0, 0, 301, 160
STYLE Ds_setfont | Ds_modalframe | Ds_3dlook | Ws_minimizebox | Ws_popup | ws_caption | Ws_sysmenu
ExStyle Ws_ex_clientedge
CAPTION "My Calculator"
FONT 18, "Italic", 1, 0x0
BEGIN
Pushbutton "1", idc_button1,0,40,50,40
Pushbutton "2", idc_button2,0,80,50,40
Pushbutton "3", idc_button3,0,120,50,40
Pushbutton "4", idc_button4,50,40,50,40
Pushbutton "5", idc_button5,50,80,50,40
Pushbutton "6", idc_button6,50,120,50,40
Pushbutton "7", idc_button7,100,40,50,40
Pushbutton "8", idc_button8,100,80,50,40
Pushbutton "9", idc_button9,100,120,50,40
Pushbutton "0", idc_button10,150,40,50,40
Pushbutton ".", idc_button11,150,80,50,40
Pushbutton "=", idc_button12,150,120,50,40
Pushbutton "+", idc_button13,200,40,50,30
Pushbutton "-", idc_button14,200,70,50,30
Pushbutton "*", idc_button15,200,100,50,30
Pushbutton "/", idc_button16,200,130,50,30
Pushbutton "(", idc_button17,250,40,25,30
Pushbutton ")", idc_button18,276,40,25,30
Pushbutton "%", idc_button19,251,70,50,30
Pushbutton "Clear-all", idc_button20,251,100,50,30
Pushbutton "Clear-single", idc_button21,251,130,50,30
LISTBOX Idc_list1,0,0,400,40,lbs_nointegralheight | Lbs_extendedsel | Ws_vscroll | Ws_tabstop
END


/////////////////////////////////////////////////////////////////////////////
//
Designinfo
//

#ifdef apstudio_invoked
Guidelines Designinfo
BEGIN
Idd_start, DIALOG
BEGIN
END
END
#endif//apstudio_invoked

#endif//English (United States) resources
/////////////////////////////////////////////////////////////////////////////

#ifndef apstudio_invoked
/////////////////////////////////////////////////////////////////////////////
//
Generated from the Textinclude 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif//Not apstudio_invoked

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.