实现简单带+、-、*、/、%、()的计算器

#include<iostream>
#include<vector>
#include<string>
#include<stack>
using namespace std;
int chartoint(char a)
{
    switch(a)
    {
        case '+':
        case '-':return 0;
        case '*':
        case '/':
        case '%':return 1;
        case '(':return 2;
        case ')':return 3;
        default:return 4;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解决这个问题。你可以使用Dev-C++集成开发环境中的图形用户界面库来创建一个简单计算器程序。以下是一个简单的示例代码,可以实现基本的数学计算和括号运算: ```c++ #include <graphics.h> #include <conio.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> #define MaxSize 100 int stack[MaxSize]; int top = -1; // 定义计算器按键的位置和尺寸 int button_left = 50; int button_top = 100; int button_width = 50; int button_height = 50; void push(int x) { if (top == MaxSize - 1) { printf("Stack is full!\n"); } else { top++; stack[top] = x; } } int pop() { if (top == -1) { printf("Stack is empty!\n"); return -1; } else { int x = stack[top]; top--; return x; } } void draw_button(char* text, int x, int y) { setcolor(WHITE); rectangle(x, y, x + button_width, y + button_height); outtextxy(x + 10, y + 10, text); } void draw_digit_buttons() { char text[2]; int x = button_left; int y = button_top; for (int i = 0; i <= 9; i++) { text[0] = '0' + i; text[1] = '\0'; draw_button(text, x, y); x += button_width; if ((i + 1) % 3 == 0) { x = button_left; y += button_height; } } } void draw_operator_buttons() { draw_button("+", button_left + button_width * 3, button_top); draw_button("-", button_left + button_width * 3, button_top + button_height); draw_button("*", button_left + button_width * 3, button_top + button_height * 2); draw_button("/", button_left + button_width * 3, button_top + button_height * 3); draw_button("(", button_left + button_width * 4, button_top); draw_button(")", button_left + button_width * 4, button_top + button_height); } void draw_equal_button() { draw_button("=", button_left + button_width * 2, button_top + button_height * 4); } void draw_clear_button() { draw_button("C", button_left + button_width * 3, button_top + button_height * 4); } void draw_all_buttons() { draw_digit_buttons(); draw_operator_buttons(); draw_equal_button(); draw_clear_button(); } void evaluate_expression(char* expression) { int len = strlen(expression); int i = 0; while (i < len) { if (isdigit(expression[i])) { int num = 0; while (i < len && isdigit(expression[i])) { num = num * 10 + (expression[i] - '0'); i++; } push(num); } else if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] == '/') { int b = pop(); int a = pop(); int result = 0; switch (expression[i]) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; } push(result); i++; } else if (expression[i] == '(') { push(-1); i++; } else if (expression[i] == ')') { int result = 0; while (stack[top] != -1) { result += pop(); } pop(); push(result); i++; } else { i++; } } printf("%s = %d\n", expression, pop()); } int main() { int gd = DETECT, gm; initgraph(&gd, &gm, ""); draw_all_buttons(); char expression[MaxSize]; int pos = 0; while (true) { if (kbhit()) { char ch = getch(); if (ch == 'C' || ch == 'c') { // 清空输入 pos = 0; setcolor(BLACK); rectangle(button_left, button_top - 20, button_left + button_width * 5, button_top - 5); continue; } else if (ch == '=') { // 计算表达式 expression[pos] = '\0'; evaluate_expression(expression); pos = 0; setcolor(BLACK); rectangle(button_left, button_top - 20, button_left + button_width * 5, button_top - 5); continue; } // 显示输入的字符 setcolor(WHITE); outtextxy(button_left + pos * button_width, button_top - 20, &ch); // 添加到表达式中 expression[pos++] = ch; } } closegraph(); return 0; } ``` 这个程序使用了 Dev-C++ 集成开发环境中的图形用户界面库,通过鼠标点击计算器按钮来输入数字和运算符,并且支持括号运算。在这个程序中,我们使用一个栈来计算表达式的值。你可以根据自己的需求来修改这个程序,并添加更多的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值