使用C语言编写的计算器

运行环境:C++
使用编译器:Visual Studio 2022
使用编码集:UTF-8

这是一个简易的计算器,可以用来计算整数的加减乘除
使用到的内容:
1.栈的相关知识。
2.字符串算式转为数字算式
3.中序转后序
4.后序表达式的计算

输入见下方示例测试数据,输出为浮点型小数。

测试数据:
测试数据1:
-3+(-3*24)
测试数据2:
6+24*{34+[22*8-(66*2)]}-6
测试数据3:
-30+8*56-9

#include<stdio.h>
#include<iostream>
using namespace std;
//结构体
typedef struct ElemType {
	char a;
	int flag1=1;
	float temp_float;
}ElemType;
//节点
typedef struct Sqstack {
	ElemType data;
	struct Sqstack* next;
}Sqstack, LNode, * SqList, * LinkList;

//栈操作
//初始化栈
void InitSqStack(SqList& S) {
	S = NULL;
}
//入栈
void Push(SqList& S, ElemType e) {
	SqList P;
	P = new Sqstack;
	P->data = e;
	P->next = S;
	S = P;
}
//出栈
int Pop(SqList& S, ElemType& e) {
	if (!S) {
		return 0;
	}
	else {
		e = S->data;
		S = S->next;
		return 1;
	}
}
int Pops(SqList& S) {
	if (!S) {
		return 0;
	}
	else {
		S = S->next;
		return 1;
	}
}
//查看栈顶元素
int get_top(SqList S, ElemType& e) {
	if (!S) {
		return 0;
	}
	else {
		e = S->data;
		return 1;
	}
}
//栈的正序化
void Chang_SqList(SqList& S, SqList& ST)
{
	ElemType e;
	while (S) {
		Pop(S, e);
		Push(ST, e);
	}
}
//连续压入栈,io输入以回车结束
void Insert_Long(SqList& S) {
	ElemType e;
	while ((e.a = getchar()) != '\n') {
		Push(S, e);
	}
}

//队列操作
//初始化线性表
void InitLNode(LinkList& L) {
	L = new LNode;
	L->next = NULL;
}
//头插法
int Insert_Head_LNode(LinkList& L, ElemType e) {
	LinkList P;
	P = new LNode;
	P->data = e;
	P->next = L->next;
	L->next;
	return 0;
}

//方法操作
int is_int(char e) {
	if (e >= '0' && e <= '9') {
		return 1;
	}
	else
		return 0;
}
int is_operator(char e) {
	switch (e) {
	case '+':return 1;
	case '-':return 2;
	case '*':return 3;
	case '/':return 4;
	case '(':return 5;
	case '[':return 6;
	case '{':return 7;
	case ')':return 8;
	case ']':return 9;
	case'}':return 10;
	default:return 0;
	}
}
int Take_the_negative(SqList S) {
	ElemType e;
	int temp1,temp2,temp_int1, temp_int2;
	temp_int1= Pop(S, e);
	temp1 = e.temp_float;
	temp_int2 = Pop(S, e);
	temp2 = e.temp_float;
	if ((temp_int2==0||(temp2>4&&temp2<8))&&temp1==2)
		return 1;
	else
		return 0;
}
//字符数字化
void Digitization_of_characters(SqList &S,SqList &ST) {
	SqList STR;
	ElemType e, f;
	int temp_flag1 = 0;
	float number = 0.0;
	InitSqStack(STR);
	while (S) {
		Pop(S, e);
		if (is_int(e.a)) {
			number = number * 10.0 + e.a - '0';
			temp_flag1 = 1;
		}
		else {
			if (temp_flag1 == 1) {
				if (Take_the_negative(STR)) {
					Pops(ST);
					e.flag1 = 1;
					e.temp_float = -number;
				}
				else
					e.temp_float = number;
				Push(ST, e);
			}
		}
		if (is_operator(e.a)) {
			f.temp_float = is_operator(e.a);
			Push(STR, f);
			number = 0.0;
			e.flag1 = 0;
			Push(ST, e);
			e.flag1 = 1;
			temp_flag1 = 0;
		}
	}
	if (number) {
		e.temp_float = number;
		Push(ST, e);
	}
	Chang_SqList(ST, S);
}
int Operator_comparison(ElemType e,ElemType f) {
	int flag = 0;
	if ((e.temp_float > 7 && e.temp_float < 11))
		flag = e.temp_float;
	if (((e.temp_float < 5 && e.temp_float>0 && f.temp_float < 5 && f.temp_float>0) && (f.temp_float - e.temp_float > 0)))
		flag = 1;
	if((e.temp_float < 3 && e.temp_float>0 && f.temp_float < 3 && f.temp_float>0))
		flag = 1;
	if ((e.temp_float < 5 && e.temp_float>2 && f.temp_float < 5 && f.temp_float>2))
		flag = 1;
	return flag;
}
void Print_Control(SqList S) {
	ElemType e;
	while (S) {
		Pop(S, e);
		if (e.flag1 == 1)
			cout << e.temp_float;
		else
			cout << e.a;
	}
}
void Add_an_operand_identifier(SqList& S, SqList& ST) {

	ElemType e;
	while (S) {
		Pop(S, e);
		if (!e.flag1)
			e.temp_float = is_operator(e.a);
		Push(ST, e);
	}
	Chang_SqList(ST, S);
}
void The_infix_is_converted_to_a_suffix(SqList &S,SqList&ST,SqList&STR) {
	ElemType e,f;
	int temp_flag1 = 0;
	
	while (S) {
		Pop(S, e);
		if (e.flag1 == 1) {
			Push(ST, e);
		}
		if (e.flag1 == 0) {
			get_top(STR, f);
			if ((temp_flag1 = Operator_comparison(e, f)) && get_top(STR, f)) {
				if (temp_flag1 == 1) {
					Pop(STR, f);
					Push(ST, f);
					Push(STR, e);
					
				}
				else
				{

					while (f.temp_float != (temp_flag1 - 3)) {
						Pop(STR, f);
						if (f.temp_float < 5 && f.temp_float  > 0)
							Push(ST, f);
					}
				}
			}
			else {
				Push(STR, e);
			}
		}
	}
	while (STR) {
		Pop(STR, e);
		Push(ST, e);
	}




	Chang_SqList(ST, S);
}
float operation(ElemType e,float float1,float float2) {
	int flag = is_operator(e.a);
	switch (flag ){
	case 1:return  (float1+float2);
	case 2:return  (float1 - float2);
	case 3:return  (float1 *float2);
	case 4:return  (float1 / float2);
	}
	

}




int main() {
	ElemType e,f,g;
	SqList S,ST,STR;
	int temp_flag1=0;
	float temp_float1, temp_float2,temp_float3;
	InitSqStack(S);//初始化栈S
	InitSqStack(ST);//初始化栈ST
	InitSqStack(STR);//初始化栈STR
	Insert_Long(ST);//io输入
	Chang_SqList(ST, S);//正序化
	Digitization_of_characters(S, ST);//字符数字化
	Add_an_operand_identifier( S, ST);
	The_infix_is_converted_to_a_suffix(S,ST,STR);
	while (S) {
		Pop(S, e);
		if (e.flag1)
			Push(ST, e);
		else {
			 g= e;
			 Pop(ST, e);
			 temp_float1 = e.temp_float;
			 Pop(ST, e);
			 temp_float2 = e.temp_float;
			 f.temp_float=operation(g, temp_float2, temp_float1);
			 Push(ST, f);
		}
			
	}
	cout << f.temp_float;
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值