分享 C语言--四则混合运算(栈应用)(正负数,小数,括号和+-*/的优先级) 请在VS2019运行

本文介绍了如何使用C语言通过栈数据结构实现四则混合运算,包括正负数、小数及括号的处理,详细讨论了运算符的优先级问题。建议在VS2019环境下编译运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

在这里插入代码片
```#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Elemtype char
#define Elemtype1 double

typedef struct Node {
   
	Elemtype data;
	struct Node* pNext;
}Node,*pNode;

typedef struct Stack {
   
	pNode Base;
	pNode top;
	int num;
}Stack;

typedef struct Node1 {
   
	Elemtype1 data;
	struct Node1* pNext;
}Node1, * pNode1;

typedef struct Stack1 {
   
	pNode1 Base;
	pNode1 top;
	int num;
}Stack1;

void Init(Stack* s);
bool Empty_stack(Stack* s);
pNode Buy_node(Stack* s, Elemtype x);
void push_stack(Stack* s,Elemtype x);
void show_stack(Stack* s);
void Get_top(Stack* s, Elemtype* v);
void pop_stack(Stack* s);


void Init1(Stack1* s);
bool Empty_stack1(Stack1* s);
pNode1 Buy_node1(Stack1* s, Elemtype1 x);
void push_stack1(Stack1* s, Elemtype1 x);
void show_stack1(Stack1* s);
void Get_top1(Stack1* s, Elemtype1* v);
void pop_stack1(Stack1* s);



char Lz(char st, char sr1);
char Bat(char a, char bat, char b);
double Bat1(double a, double bat1, double b);
double Numzhuan(char* st1);
double zhuan10(int n);
void Outg(char* str, char* str1);

//
#include"Stackyy.h"

void Init(Stack* s)
{
   
	s->Base = s->top = (pNode)malloc(sizeof(Node));
	if (s->Base == NULL)
		exit(-1);
	s->Base->pNext = NULL;
	s->num = 0;
}
bool Empty_stack(Stack* s)
{
   
	if (s->Base = s->top)
		return true;
	else
		return false;
}
void pop_stack(Stack* s)
{
   
	pNode p = s->top;
	s->top = p->pNext;
	free(p);
	s->num--;
}
pNode Buy_node(Stack* s, Elemtype x)
{
   
	pNode p = (pNode)malloc(sizeof(Node));
	if (p == NULL)
		exit(-1);
	p->data = x;
	p->pNext = NULL;
	return p;
}
void push_stack(Stack* s,Elemtype x)
{
   
	pNode p = Buy_node(s, x);
	p->pNext = s->top;
	s->top = p;
	s->num++;
}
void show_stack(Stack* s)
{
   
	if (s->top == s->Base)
	{
   
		puts("空栈,不能遍历");
		return;
	}
	pNode p = s->top;
	while (p != s->Base)
	{
   
		printf("%c", p->data);
		p = p->pNext;
	}
	puts("");
}
void Get_top(Stack* s, Elemtype *v)
{
   
	if (s->top != s
好的,我可以帮你解决这个问题。你可以使用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++ 集成开发环境中的图形用户界面库,通过鼠标点击计算器按钮来输入数字运算符,且支持括号运算。在这个程序中,我们使用一个计算表达式的值。你可以根据自己的需求来修改这个程序,添加更多的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值