devc++编写界面计算器有+-*/和括号
时间: 2023-10-17 20:11:44 浏览: 158
VC++ 简易计算器 实现的功能有+,-,*,/,三角函数(sin,cos,tan)等一些基本的运算。
好的,我可以帮你解决这个问题。你可以使用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++ 集成开发环境中的图形用户界面库,通过鼠标点击计算器按钮来输入数字和运算符,并且支持括号运算。在这个程序中,我们使用一个栈来计算表达式的值。你可以根据自己的需求来修改这个程序,并添加更多的功能。
阅读全文