C语言实现计算器

计算器

主要功能

- 实现表达式计算('+' '-' '*' '/' '%' '√' '(' ')')
- 界面
- 多位数运算
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include<math.h>
#include <stdlib.h>


#define OK    1
#define ERROR	 0

/* 定义结点结构体 */
typedef struct node
{
   
	int data;
	struct node* next;
}Node;

/* 定义栈结构体 */
typedef struct stack
{
   
	Node* top;
	int count;
}Stack;

int Priority(char s);//自定义优先级
int InitStack(Stack* S); //初始化栈
int EmptyStack(Stack* S);//判空
int GetTop(Stack* S);    //获取栈顶
int Push(Stack* S, int e);   //进栈
int Pop(Stack* S);           //出栈
void Paint();            //页面绘制
double dis(int x1, int y1, int x2, int y2);

int main()
{
   
	Paint();
}


/* 自定义优先级 */
int Priority(char s)
{
   
	switch (s)
	{
   
	case '(':				//括号优先级最高
		return 3;
	case '*':
	case '/':
	case '%':
	case '^':	            //乘除取余幂次次之
		return 2;
	case '+':
	case '-':				//加减最低
		return 1;
	default:
		return 0;
	}
}


/* 初始化栈 */
int InitStack(Stack* S)
{
   
	S->top = NULL;
	S->count = 0;

	return OK;
}


/* 判空 */
int EmptyStack(Stack* S)
{
   
	return (S->count == 0) ? OK : ERROR;
}


/* 获取栈顶 */
int GetTop(Stack* S)
{
   
	if (NULL == S -> top)
		return ERROR;

	return (S -> top -> data);
}


/* 进栈 */
int Push(Stack* S, int e)
{
   
	Node* p = (Node*)malloc(sizeof(Node));
	if (p == NULL)
	{
   
		return ERROR;
	}
	p->data = e;
	p->next = S->top;
	S->top = p;
	S->count++;

	return OK;
}


/* 出栈 */
int Pop(Stack* S)
{
   
	int e;

	if (NULL == S->top)
		return ERROR;

	Node* p = S->top;
	e = p->data;
	S->top = p->next;
	free(p);
	S->count--;

	return e;
}

void Paint() {
   
	int number;
	// 初始化图形模式
	initgraph(300, 600);
	// 设置背景为深灰色
	setbkcolor(RGB(51, 51, 51));
	int c;
	for (int y = 0; y < 20; y++) {
   
		setcolor(RGB(51, 51, 51));
		line(0, y, 400, y);
	}
	//设置文本颜色
	settextcolor(WHITE);
	//输出文本信息
	settextstyle(20, 10,<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值