功能需求
1、设置运算符栈和运算数栈辅助分析算符优先关系
2、在读入表达式的字符序列的同时,完成运算符和运算数(整数)的识别处理,以及相应的运算
3、在识别出运算数的同时,要将其字符序列形式转换成整数形式
4、在程序的适当位置输出运算符栈、运算数栈、输入字符和主要操作的内容
代码实现:
1、头文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<math.h>
#include<Windows.h>
#include <graphics.h>
#pragma comment(lib, “winmm.lib”)
#pragma comment(lib,“Msimg32.lib”)
#include “mmsystem.h”
#include <easyx.h>
#include
using namespace std;
#define INITSIZE 40
#define INCREMENT 10
#define OK 1
#define ERROR 0
#define max 201
typedef char ElemType;
typedef double SElemType;
typedef int Status;
//1
typedef struct
{
ElemType *base;
ElemType *top;
int StackSize;
}CSqStack;
//2
typedef struct
{
SElemType *base;
SElemType top;
int StackSize;
}ISqStack;
//1
Status CInitStack(CSqStack &S)
{
S.base = (ElemType)malloc(sizeof(ElemType)INITSIZE);
if (!S.base)
{
exit(-1);
}
S.top = S.base;
S.StackSize = INITSIZE;
return OK;
}
//2
Status IInitStack(ISqStack &S)
{
S.base = (SElemType)malloc(sizeof(SElemType)*INITSIZE);
if (!S.base)
{
exit(-1);
}
S.top = S.base;
S.StackSize = INITSIZE;
return OK;
}
//1
Status CPushStack(CSqStack &S,ElemType e)
{
if (S.top - S.base >= S.StackSize)
{
S.base = (ElemType*)realloc(S.base, sizeof(ElemType)*(S.StackSize + INCREMENT));
if (!S.base)
{
return ERROR;
}
S.top = S.base + S.StackSize;
S.StackSize += INCREMENT;
}
*S.top++ = e;
return OK;
}
//2
Status IPushStack(ISqStack &S, SElemType e)
{
if (S.top - S.base >= S.StackSize)
{
S.base = (SElemType*)realloc(S.base, sizeof(SElemType)*(S.StackSize + INCREMENT));
if (!S.base)
{
return ERROR;
}
S.top = S.base + S.StackSize;
S.StackSize += INCREMENT;
}
*S.top++ = e;
return OK;
}
//1
int CStackLength(CSqStack &S)
{
return(S.top - S.base);
}
//2
int IStackLength(ISqStack &S)
{
return(S.top - S.base);
}
//1
int CPopStack(CSqStack &S, ElemType &e)
{
if (S.top == S.base)
{
return ERROR;
}
e = *--S.top;
return OK;
}
//2
int IPopStack(ISqStack &S, SElemType &e)
{
if (S.top == S.base)
{
return ERROR;
}
e = *--S.top;
return OK;
}
//获取光标
void gotoxy(int x, int y)
{
COORD pos = { x,y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
// 获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos);
//两个参数分别是指定哪个窗体,具体位置
}
//隐藏关标
void HideCursor()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &cci);
}
//颜色变化
int color(int c)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
return 0;
}
//神兽保佑,代码无bug
void yt_show();
int make_line();
int explain_show();
void calculator_show();
void Init_calculator();
void over_show();
void Init_show();
void calculate();
int Judge_bracket(int i);
int Judge_bit(int n);
void ZswitchH(CSqStack S);
2、主函数
#include"resource.h"
char s[max];
int s1[max];
int A[max];
char Hz[max];
//中缀转后缀
void ZswitchH(CSqStack S)//90*2+70-4*(3+2)/3
{
int i = 0, j = 0;
ElemType e;
for (; i<strlen(s); i++)
{
if (s[i]-48 >= 0)
{
Hz[j++] = s[i];
}
else if (s[i] == '+' || s[i] == '-')
{
if (!CStackLength(S))
{
CPushStack(S, s[i]);
}
else
{
do
{
CPopStack(S, e);
if (e == '(')
{
CPushStack(S, e);
}
else
{
Hz[j++] = e;
}
} while (CStackLength(S) && e != '(');
CPushStack(S, s[i]);
}
}
else if (s[i] == ')')
{
CPopStack(S, e);
while (e != '(')
{
Hz[j++] = e;
CPopStack(S, e);
}
}
else if (s[i] == '*' || s[i] == '/' || s[i] == '(')
{
CPushStack(S, s[i]);
}
else
{
printf("输入格式错误");
return;
}
}
while (CStackLength(S))
{
CPopStack(S, e);
Hz[j++] = e;
}
}
int Judge_bit(int n)
{
int t = 0;
while (n != 0)
{
t++;
n /= 10;
}
return t;
}
int Judge_bracket(int i)
{
int j = i;
int t = 0,n=0;
if (s1[j] == -1)
{
while (s1[j] == -1)
{
j++;
t++;
}
}
if (t >= 2 )
{
n = t-1;
}
return n;
}
void calculate()//90*2+70-4*(3+2)/3
{ //902*70+432+3/*-
ISqStack S;//s1
IInitStack(S);//2*(6+2*(3+5*(6+7)))
int e;//(((5+6)*7+3)*2+6)*2
int t,m;
int i = 0, j = 0;
double q, w;
double sum;
while (Hz[j] != '\0')
{
m=Judge_bracket(i);
if (s1[i] != -1&&s1[i]!=0)
{
e = s1[i];
IPushStack(S, e);
t = Judge_bit(s1[i]);
i++;
j += t;
}
else
{
if (m != 0)
{
i += m;
}
if (Hz[j] - 48 >= 0 && Hz[j] - 48 <= 9)
{
i++;
}
else
if (Hz[j] == '+')
{
IPopStack(S, q);
IPopStack(S, w);
sum = q + w ;
IPushStack(S, sum);
j++;
i++;
}
else
if (Hz[j] == '-')
{
IPopStack(S, q);
IPopStack(S, w);
sum = w - q ;
IPushStack(S, sum);
j++;
i++;
}
else
if (Hz[j] == '*')
{
IPopStack(S, q);
IPopStack(S, w);
sum = w *q;
IPushStack(S, sum);
j++;
i++;
}
else
if (Hz[j] == '/')
{
IPopStack(S, q);
IPopStack(S, w);
sum = w /q ;
IPushStack(S, sum);
i++;
j++;
}
}
}
IPopStack(S, sum);
printf("%lf",sum);
}
//void Init_show()
//{
// initgraph(960, 640);
// HWND hwnd = GetHWnd();
// SetWindowText(hwnd, L"简易计算器 初级版");
// setbkmode(TRANSPARENT);
// IMAGE img;
// loadimage(&img, L"keji.jpg", 0, 0);
// //putimage(0, 0,400,626, &img,0,0);
// putimage(150, 150, &img);
// //putimage(0, 0, &img);
// setfillcolor(BLUE);
// solidrectangle(0,0,960,150);
// settextcolor(RED);
// settextstyle(15,10,L"楷体");
// outtextxy(300,10,L"简易计算器使用说明");
// settextcolor(GREEN);
// outtextxy(10, 30, L"从键盘上输入计算的式子(注意:此计算器只能计算加减乘除加小括号的计算)");
// outtextxy(10, 60, L"以中缀表达式输入,格式不能输入错误 输入示例:90*2+70-4*(3+2)/3 ");
// outtextxy(10, 90, L"结果精确至小数点后6位 输出结果:254.333333");
// setfillcolor(BLUE);
// solidrectangle(0, 450, 960, 640);
// outtextxy(10, 500, L"输入位置:");
// outtextxy(10,600,L"结果输出:");
// settextstyle(10,500,L"宋体");
//}
void Init_show()
{
color(10);
gotoxy(25, 10);
printf("欢 迎 使 用 简 易 计 算 器");
gotoxy(30, 15);
printf("1、使 用 说 明");
gotoxy(30, 20);
printf("2、进 入 使 用");
color(8);
gotoxy(20, 5);
printf("(从键盘上输入数字1 数字2 点击enter进入功能)");
color(11);
for (int i = 0; i <= 28; i ++)
{
gotoxy(10, i);
printf("■");
gotoxy(70, i);
printf("■");
}
for (int i = 10; i <= 70; i+=2)
{
gotoxy(i, 28);
printf("■");
gotoxy(i, 0);
printf("■");
}
gotoxy(30, 25);
}
void over_show()
{
system("cls");
HideCursor();
color(12);
for (int i = 0; i <= 20; i++)
{
gotoxy(10, i);
printf("■");
gotoxy(50, i);
printf("■");
}
for (int i = 10; i <= 50; i += 2)
{
gotoxy(i, 20);
printf("■");
gotoxy(i, 0);
printf("■");
}
gotoxy(20, 10);
printf("感谢使用");
gotoxy(20, 12);
printf("拜拜(输入任意键退出)");
getch();
exit(0);
}
void Init_calculator()
{
//cin >> s;//90*2+70-4*(3+2)/3
CSqStack S;
CInitStack(S);
memset(Hz, 0, sizeof(char));
memset(s1, 0, sizeof(int));
ZswitchH(S);
int i, j, t = 0, k, sum = 0, q;
int count = 0;
for (i = 0; i < strlen(s); i++)//90
{
if (s[i] >= 48 && s[i] - 48 <= 9)
{
t++;
}
else if (t != 0 && (s[i] - 48 < 0))
{
q = t - 1;
for (j = 0; j < t; j++)
{
sum += (s[j + i - t] - 48)*pow(10, q--);
}
s1[count++] = sum;//90
sum = 0;
t = 0;
i = i - t - 1;//1
}
else
{
s1[count++] = -1;
}
}
sum = 0;
q = t - 1;
for (j = 0; j < t; j++)
{
sum += (s[j + i - t] - 48)*pow(10, q--);
}
s1[count] = sum;//90*2+70-4*(3+2)/3
calculate();
}
void calculator_show()
{
color(11);
for (int i = 0; i <= 20; i++)
{
gotoxy(10, i);
printf("■");
gotoxy(80, i);
printf("■");
}
for (int i = 10; i <= 80; i += 2)
{
gotoxy(i, 20);
printf("■");
gotoxy(i, 0);
printf("■");
}
color(4);
gotoxy(35, 2);
printf("欢迎使用简易计算器");
color(8);
gotoxy(12, 5);
printf("输入表达式:");
gotoxy(12, 10);
printf("结果为:");
color(12);
gotoxy(25, 5);
cin >> s;
gotoxy(25, 10);
Init_calculator();
memset(Hz, 0, sizeof(char));
memset(s1, 0, sizeof(int));
memset(s, 0, sizeof(char));
fflush(stdin);
}
int explain_show()
{
int w=0;
color(11);
for (int i = 0; i <= 15; i++)
{
gotoxy(10, i);
printf("■");
gotoxy(80, i);
printf("■");
}
for (int i = 10; i <= 80; i += 2)
{
gotoxy(i, 15);
printf("■");
gotoxy(i, 0);
printf("■");
}
color(10);
gotoxy(25, 2);
printf("使用说明");
gotoxy(12, 5);
printf("1、输入数字正负都可,格式为中缀表达式");
gotoxy(12, 7);
printf("2、输入长度不可超过200");
gotoxy(12, 9);
printf("3、此计算器只能计算加减乘除,无其他计算功能");
gotoxy(12, 11);
printf("4、最高数字大小不可超过int形范围");
gotoxy(12, 13);
printf("5、精确度为小数点后6位");
gotoxy(12, 20);
printf("输入数字1返回,其他退出");
cin >> w;
if (w != 1)
{
over_show();
}
return w;
}
int make_line()
{
int p;
gotoxy(25, 15);
printf("是否继续进行计算:(1继续/2退出/3返回)");
cin >> p;
if (p == 2)
{
over_show();
}
return p;
}
void yt_show()
{
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━━━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* █████━█████ ┃+
* ┃ ┃ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━━┓ ┏━┛
* ┃ ┃
* ┃ ┃ + + + +
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ + 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃ +
* ┃ ┗━━━┓ + +
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━━━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +*/
}
int main()
{
//HideCursor();
yt_show();
ex:
system("cls");
Init_show();
int f;
while (1)
{
int w;
cin >> f;
switch (f)
{
case 1:
system("cls");
w=explain_show();
if (w == 1)
{
goto ex;
}
break;
case 2:
cu:
int p;
system("cls");
calculator_show();
p=make_line();
if (p == 1)
{
goto cu;
}
else
if (p == 3)
{
goto ex;
}
break;
}
}
return 0;
}