• 简易计算器(API)


    自己做一个ico文件就行了


    #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);//计算器运算程序
    };

    #endif


    //{{NO_DEPENDENCIES}}


    // Microsoft Visual C++ 生成的包含文件。
    // 供 Resource.rc 使用
    //
    #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;//类声明

    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>;
    };

    //链栈类模板,无头结点链表
    template<typename T>class Stack{
    private:
    Node<T>*top;//栈顶指针
    public:
    Stack(){ top = NULL; }
    ~Stack();
    void Push(const T&data);//压栈
    T Pop();//弹出
    T GetTop();//取栈顶元素
    void MakeEmpty();//清空栈
    bool IsEmpty(){ return top == NULL; }
    };

    template<typename T>Stack<T>::~Stack(){ MakeEmpty(); }

    template<typename T>void Stack<T>::Push(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>::Pop(){
    assert(!IsEmpty());
    Node<T>*temp = top;
    T data = temp->info;
    top = top->link;//丢弃栈顶结点
    delete temp;//释放栈顶结点
    return 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] = '\0';
    Nstack.Push(atof(str2));
    j = -1;
    }
    switch (str1[k]){
    case'+':case'-':
    while (!Ostack.IsEmpty() && Ostack.GetTop() != '('){//*************注意
    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;
    }

    //主函数,测试,下面是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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] = '\0';
    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] != '\0'; i++){
    buf[i] = '\0';
    }
    }
    break;
    case IDC_BUTTON21:{
    if (i > 0){
    SendDlgItemMessage(hwnd, IDC_LIST1, LB_RESETCONTENT, 0, 0);
    buf[--i] = '\0';
    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

    /////////////////////////////////////////////////////////////////////////////
    // 中文(简体,中国) 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 // 中文(简体,中国) resources
    /////////////////////////////////////////////////////////////////////////////


    /////////////////////////////////////////////////////////////////////////////
    // 英语(美国) 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"
    "\0"
    END

    3 TEXTINCLUDE
    BEGIN
    "\r\n"
    "\0"
    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, "楷体", 700, 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 // 英语(美国) resources
    /////////////////////////////////////////////////////////////////////////////

    #ifndef APSTUDIO_INVOKED
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 3 resource.
    //


    /////////////////////////////////////////////////////////////////////////////
    #endif // not APSTUDIO_INVOKED

  • 相关阅读:
    Eclipse的下载与安装
    IntelliJ IDEA(2018)安装详解
    关于Idea中的web.xml 自动生成模板问题
    基于ssm框架的web.xml配置及解析
    Mybatis分页插件PageHelper的配置与基本使用
    基于maven的ssm框架pom.xml的基本配置及解析
    python去除字符串中的特殊字符(爬虫存储数据时会遇到不能作为文件名的字符串)
    快速指数算法
    伪随机序列
    django中Queryset的删除问题、分页问题
  • 原文地址:https://www.cnblogs.com/caiyonghai/p/3900578.html
Copyright © 2020-2023  润新知