首页 资讯 论坛 我的社区 搜索 用户

[C/C++]c堆栈,写一个科学计算机

无闻风
发表于 2019-06-27 16:12:25
0
568

// calculator.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include
#include
#include
#include
#include  //判断是否为字符函数的头文件
#define MAXSIZE  100
typedef int elemtype;
typedef double OperandType;

char ch[8] = { '+' , '-' , '*' , '/' ,'(' , ')' , '#' }; //把符号转换成字符数组
int f1[7] = { 3,3,5,5,1,6,0 };                           //栈内元素优先级
int f2[7] = { 2,2,4,4,6,1,0 };                           //栈外元素优先级
int n = 0;

typedef struct sqstack
{//顺序栈结构
	elemtype stack[MAXSIZE];
	int top;
}sqstsck;


//1.把操作符转换为相应数字
elemtype cton(char c)
{
	switch (c)
	{
	case '+':  return 0;
	case '-':  return 1;
	case '*':  return 2;
	case '/':  return 3;
	case '(':  return 4;
	case ')':  return 5;
	default:   return 6;
	}
}



//2.比较字符优先级
char Compare(char c1, char c2)
{
	int i1 = cton(c1);
	int i2 = cton(c2);                 //把字符变成数字
	if (f1[i1]>f2[i2]) return '>';    //通过原来的设定找到优先级
	else if (f1[i1]
最后修改 2019-06-27 16:12:25
0
568
用户评论
一起折腾