⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplecalc.c

📁 uclinux 下的一个计算器程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <string.h>#include <stdlib.h>#include <stdio.h>#include <math.h>#include "nano-X.h"#include "emtk.h"#include "laf/laf.h"#include "i18n.h"#include "stack.h"#ifdef LAUNCHER_SUPPORT    #include "deskmsg.h"#endif#ifdef FONT_TABLE    #include "font_table.h"#else    #define lookupFont(a, b) (FONT1)    #define init_font_table(a)    #define unload_font_table()#endif#if !defined(MERGE_EMTK_LAUNCHER)#define APP_X 0#define APP_Y 0#define APP_W 320#define APP_H 240#endif// 13 digits + decimal point + '\0'#define MAX_DISPLAY_DIGITS 15#define NULL_DISPLAY "0."typedef enum { OP_NOOP, OP_ADD, OP_SUB, OP_MUL, OP_DIV } Operation;typedef enum { REG_X, REG_Y } RegState;typedef enum { NUM0,    NUM1,       NUM2,       NUM3,               NUM4,    NUM5,       NUM6,       NUM7,               NUM8,    NUM9,       MEMCLEAR,   MEMRELEASE,               MEMSUB,  MEMADD,     DOT,        EQ,               DIV,     MUL,        ADD,        SUB,               SIGN,    ALLCLEAR,   CLEAR,      PERCENT,               SQRT,    BACK,             } ButtonDefn;typedef struct {    double value;    Operation op;} Register;static double memory;static Register *current_register;static Stack    *stack;static int  app_window;static int  display_id, exponent_id, memory_id;static char buffer[MAX_DISPLAY_DIGITS];static gboolean decimal;static int position;#ifndef WIDTH#define WIDTH           32#endif#define PADDING         2#define ROW(x)          30 + (WIDTH + PADDING) * x#define COL(x)          10 + (WIDTH + PADDING) * xtypedef struct _btn {    int     row, col;    int     w, h;    char    *on;    char    *off;    ButtonDefn action;    MWKEY   key;    char    *str;} btndef;static btndef buttondefs[] ={#ifndef __uClinux__    { 1, 0, WIDTH, WIDTH, "ac.png",   "ac.png",   ALLCLEAR,         0,  "AC"},    { 2, 0, WIDTH, WIDTH, "sign.png", "sign.png", SIGN,             0,  "+/-" },    { 3, 0, WIDTH, WIDTH, "back.png", "back.png", BACK,             0,  "<-" },    { 4, 0, WIDTH, WIDTH, "c.png",    "c.png",    CLEAR,            0,  "C" },    { 0, 1, WIDTH, WIDTH, "madd.png", "madd.png", MEMADD,           0,  "M+" },    { 1, 1, WIDTH, WIDTH, "7.png",    "7.png",    NUM7,             '7', "7" },    { 2, 1, WIDTH, WIDTH, "4.png",    "4.png",    NUM4,             '4', "4" },    { 3, 1, WIDTH, WIDTH, "1.png",    "1.png",    NUM1,             '1', "1" },    { 4, 1, WIDTH * 2 + 2, WIDTH, "0.png",    "0.png", NUM0,        '0', "0"},    { 0, 2, WIDTH, WIDTH, "msub.png", "msub.png", MEMSUB,           0 , "M-"},    { 1, 2, WIDTH, WIDTH, "8.png",      "8.png",    NUM8,           '8', "8" },    { 2, 2, WIDTH, WIDTH, "5.png",      "5.png",    NUM5,           '5', "5" },    { 3, 2, WIDTH, WIDTH, "2.png",      "2.png",    NUM2,           '2', "2" },    { 0, 3, WIDTH, WIDTH, "mc.png",     "mc.png",   MEMCLEAR,       0, "MC" },    { 1, 3, WIDTH, WIDTH, "9.png",      "9.png",    NUM9,           '9', "9" },    { 2, 3, WIDTH, WIDTH, "6.png",      "6.png",    NUM6,           '6', "6" },    { 3, 3, WIDTH, WIDTH, "3.png",      "3.png",    NUM3,           '3', "3" },    { 4, 3, WIDTH, WIDTH, "dot.png",    "dor.png",  DOT,            '.', "." },    { 0, 4, WIDTH, WIDTH, "mr.png",     "mr.png",   MEMRELEASE,     0, "MR" },    { 1, 4, WIDTH, WIDTH, "percent.png", "percent.png", PERCENT,    '%', "%" },    { 2, 4, WIDTH, WIDTH, "mul.png",    "mul.png",  MUL,            '*', "*" },    { 3, 4, WIDTH, WIDTH * 2 + 2, "add.png", "add.png", ADD,        '+', "+"},    { 1, 5, WIDTH, WIDTH, "sqrt.png",   "sqrt.png", SQRT,           0, "SQRT" },    { 2, 5, WIDTH, WIDTH, "divide.png", "divide.png", DIV,          '/', "/" },    { 3, 5, WIDTH, WIDTH, "sub.png",    "sub.png",    SUB,          '-', "-" },    { 4, 5, WIDTH, WIDTH, "equal.png",  "equal.png",  EQ,           '=', "=" },    { 0, 0, 0, 0, NULL, NULL, 0 },#else    { 1, 0, WIDTH,     WIDTH,     "ac.png",      "ac.png",      ALLCLEAR,   'h',            "AC"},    { 2, 0, WIDTH,     WIDTH,     "sign.png",    "sign.png",    SIGN,       'm',            "+/-" },    { 3, 0, WIDTH,     WIDTH,     "back.png",    "back.png",    BACK,       MWKEY_DELETE,   "<-" },    { 4, 0, WIDTH,     WIDTH,     "c.png",       "c.png",       CLEAR,      'n',            "C" },    { 0, 1, WIDTH,     WIDTH,     "madd.png",    "madd.png",    MEMADD,     'v',            "M+" },    { 1, 1, WIDTH,     WIDTH,     "7.png",       "7.png",       NUM7,       'u',            "7" },    { 2, 1, WIDTH,     WIDTH,     "4.png",       "4.png",       NUM4,       'r',            "4" },    { 3, 1, WIDTH,     WIDTH,     "1.png",       "1.png",       NUM1,       'q',            "1" },    { 4, 1, WIDTH*2+2, WIDTH,     "0.png",       "0.png",       NUM0,       'p',            "0"},    { 0, 2, WIDTH,     WIDTH,     "msub.png",    "msub.png",    MEMSUB,     'b',            "M-"},    { 1, 2, WIDTH,     WIDTH,     "8.png",       "8.png",       NUM8,       'i',            "8" },    { 2, 2, WIDTH,     WIDTH,     "5.png",       "5.png",       NUM5,       't',            "5" },    { 3, 2, WIDTH,     WIDTH,     "2.png",       "2.png",       NUM2,       'w',            "2" },    { 0, 3, WIDTH,     WIDTH,     "mc.png",      "mc.png",      MEMCLEAR,   'g',            "MC" },    { 1, 3, WIDTH,     WIDTH,     "9.png",       "9.png",       NUM9,       'o',            "9" },    { 2, 3, WIDTH,     WIDTH,     "6.png",       "6.png",       NUM6,       'y',            "6" },    { 3, 3, WIDTH,     WIDTH,     "3.png",       "3.png",       NUM3,       'e',            "3" },    { 4, 3, WIDTH,     WIDTH,     "dot.png",     "dor.png",     DOT,        MWKEY_PAGEDOWN, "." },    { 0, 4, WIDTH,     WIDTH,     "mr.png",      "mr.png",      MEMRELEASE, 'f',            "MR" },    { 1, 4, WIDTH,     WIDTH,     "percent.png", "percent.png", PERCENT,    'G',            "%" },    { 2, 4, WIDTH,     WIDTH,     "mul.png",     "mul.png",     MUL,        'l',            "*" },    { 3, 4, WIDTH,     WIDTH*2+2, "add.png",     "add.png",     ADD,        'j',            "+"},    { 1, 5, WIDTH,     WIDTH,     "sqrt.png",    "sqrt.png",    SQRT,       'z',            "SQRT" },    { 2, 5, WIDTH,     WIDTH,     "divide.png",  "divide.png",  DIV,        ' ',            "/" },    { 3, 5, WIDTH,     WIDTH,     "sub.png",     "sub.png",     SUB,        'k',            "-" },    { 4, 5, WIDTH,     WIDTH,     "equal.png",   "equal.png",   EQ,         MWKEY_ENTER,    "=" },    { 0, 0, 0, 0, NULL, NULL, 0 },#endif};static int buttons[sizeof(buttondefs) / sizeof(btndef) - 1];static Register *register_new(){    return g_new0(Register, 1);}static double getDouble() {    double value;        value = atof(buffer);        return value;}static void updateDisplay() {    char *disp;    if (!strchr(buffer, '.'))        disp = g_strconcat(buffer, ".", NULL);    else        disp = g_strdup(buffer);            emtk_label_set_string(display_id, disp, strlen(disp));        g_free(disp);}static void insertChar(char ch) {    if (position < 13)        buffer[position++] = ch;}static void setDisplay(double num) {    char *str, *exp;    int length;        if (isfinite(num)) {         str = g_strdup_printf("%.13g", num);        exp = strchr(str, 'e');                memset(buffer, '\0', MAX_DISPLAY_DIGITS);                length = (exp) ? exp - str : strlen(str);                strncpy(buffer, str, length);                if (exp) {            exp++;            if (*exp == '+') exp++;            emtk_label_set_string(exponent_id, exp, strlen(exp));            emtk_label_show(exponent_id);        } else {            emtk_label_set_string(exponent_id, "", 0);        }        g_free(str);    } else {        sprintf(buffer, "Error");    }}static void clearBuffer() {    position = 0;    memset(buffer, '\0', MAX_DISPLAY_DIGITS);    buffer[0] = '0';    buffer[1] = '.';}static void all_clear() {    Register *reg;    while ((reg = stack_pop(stack)) != NULL) { g_free(reg); };        decimal = FALSE;        current_register = register_new();    stack_push(stack, current_register);        emtk_label_hide(exponent_id);    clearBuffer();}static double evaluate() {    Register *reg;    void *data;    double result = 0.0;        while ((data = stack_pop(stack)) != NULL) {        reg = (Register *) data;        switch(reg->op) {            case OP_ADD:                result = reg->value + result;                break;            case OP_SUB:                result = reg->value - result;                break;            case OP_MUL:                result = reg->value * result;                break;            case OP_DIV:                result = reg->value / result;                break;            case OP_NOOP:                result = reg->value;                break;            default:                printf("unknown op\n");        }        //printf("%d, %g, %g\n", reg->op, reg->value, result);        g_free(reg);    }        current_register = register_new();    stack_push(stack, current_register);    current_register->value = result;    return result;    }static void btnHandler(int bid, void *data) {    int action = (int) data;    double temp;    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -