Demo entry 6657836

nothing

   

Submitted by Matrix on Nov 05, 2017 at 05:09
Language: C++. Code size: 6.8 kB.

#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;

#define STACK_INT_SIZE 100
#define FULL_STACK (STACK_INT_SIZE - 1)  
#define EMPTY_STACK (-1)
#define L "log"
#define l "ln"
#define c "cos"
#define s "sin"
#define HIGHER 1;
#define LOWER (-1);

typedef struct Op_Stack {
	char *Array;
	int top;
}Op_Stack;

void InitStack(Op_Stack *S) {
	S->top = -1;
	S->Array = (char*)malloc((STACK_INT_SIZE) * sizeof(char));
}

void DisposeStack(Op_Stack *S) {
	if (S != NULL) {
		free(S->Array);
		free(S);
		S = NULL;
	}
}

int IsEmpty(Op_Stack *S) {
	return S->top == EMPTY_STACK;
}

int IsFull(Op_Stack *S) {
	return S->top == FULL_STACK;
}

void GetTop(Op_Stack *S, char &e) {
	if (IsEmpty(S)) { cout << "GetTop error!" << endl; }
	else { e = S->Array[S->top]; }
}

void Push(Op_Stack *S, char e) {
	if (!IsFull(S)) {
		S->top++;
		S->Array[S->top] = e;
		//cout << "Push successfully" << endl;
	}
	else { cout << "Char Push error!"<<endl; }
}

void Pop(Op_Stack *S, char &e) {
	if (IsEmpty(S)) { cout << "Op Pop error!" << endl; }
	else
	{
		e = S->Array[S->top];
		S->top--;
		//cout << "Pop successfully" << endl;
	}
}


typedef struct Num_Stack {
	float *Array;
	int top;
}Num_Stack;

void InitStack(Num_Stack *S) {
	S->top = -1;
	S->Array = (float*)malloc((STACK_INT_SIZE) * sizeof(float));
}

void DisposeStack(Num_Stack *S) {
	if (S != NULL) {
		free(S->Array);
		free(S);
		S = NULL;
	}
}

int IsEmpty(Num_Stack *S) {
	return S->top == EMPTY_STACK;
}

int IsFull(Num_Stack *S) {
	return S->top == FULL_STACK;
}

void GetTop(Num_Stack *S, float &e) {
	if (IsEmpty(S)) { cout << "GetTop error!" << endl; }
	else { e = S->Array[S->top]; }
}

void Push(Num_Stack *S, float e) {
	if (!IsFull(S)) {
		S->top++;
		S->Array[S->top] = e;
		//cout << "Push successfully" << endl;
	}
	else { cout << " Num Push error!"<<endl; }
}

void Pop(Num_Stack *S, float &e) {
	if (IsEmpty(S)) { cout << "Num Pop error!" << endl; }
	else
	{
		e = S->Array[S->top];
		S->top--;
		//cout << "Pop successfully" << endl;
	}
}

//比较两字符串是否相等
int Compare_String(char s1[], char s2[]) {
	int i = 0;
	while (s1[i] != '\0' && s1[i] == s2[i] ){ i++; }
	if (s1[i] == '\0') return 1;
	else return 0;
}

//比较运算符a的优先级是否大于b,a是栈顶元素,b是ch存储的运算符
int Compare_Priority(char a,char b) {
	switch (a)
	{
	case '+':case '-':
		switch (b)
		{
		case '+':case '-':case '=':case ')': return HIGHER;
		case '*':case '/':case 's':case 'L':case 'l':case 'c':case '(':case '^': return LOWER;
			break;
		}
	case '*':case '/':
		switch (b) {
		case '+':case '-':case '*':case '/':case '=':case ')': return HIGHER;
		case 's':case 'L':case 'l':case 'c':case '(':case '^': return LOWER;
			break;
		}
	case 's':case 'L':case 'l':case 'c':case '^':
		switch (b) {
		case '+':case '-':case '=':case ')':case '*':case '/': return HIGHER;
		case 's':case 'L':case 'l':case 'c':case '(':case '^': return LOWER;
			break;
		}
	case '(':
		switch (b) {
		case ')': return 0;
		case '+':case '-':case '=':case '*':case '/':case 's':case 'L':case 'l':case 'c':case '(':case '^': return LOWER;
			break;
		}
	case ')':
		switch (b) {
		case ')':case '+':case '-':case '=':case '*':case '/':case 's':case 'L':case 'l':case 'c':case '(':case '^': return HIGHER;
			break;
		}
	case '#': return LOWER; break;
	
	default:cout << "The expression is wrong!" << endl;
		break;
	}
}

//将sin,cos,ln.log转换成易存储的符号
void Transfer(char &ch,char *temp) {
	switch (ch) {
	case 'l':
		temp[0] = 'l';
		cin >> temp[1];
		temp[2] = '\0';
		if (Compare_String(temp, l)) {
			ch = 'l';
		}
		else{
			cin >> temp[2];
			temp[3] = '\0';
			if (Compare_String(temp, L)) {
				ch = 'L';
			}
		else {//如果符号出错    
			cout << "The operator has been wrong!" << endl;
			system("pause");
			exit(0);
			}
		}
		break;

	case 's':
		temp[0] = 's';
		cin >> temp[1];
		cin >> temp[2];
		temp[3] = '\0';
		if (Compare_String(temp, s)) {
			ch = 's';
		}
		else {//如果符号出错    
			cout << "The operator has been wrong!" << endl;
			system("pause");
			exit(0);
		}
		break;

	case 'c':
		temp[0] = 'c';
		cin >> temp[1];
		cin >> temp[2];
		temp[3] = '\0';
		if (Compare_String(temp, c)) {
			ch = 'c';
		}
		else {//如果符号出错    
			cout << "The operator has been wrong!" << endl;
			system("pause");
			exit(0);
		}
	}
}

int main() {
	char ch;//ch用来读入表达式的字符
	float num;//用于暂时存放操作数
	float num1, num2;//用于存放从操作数栈中取得的两个操作数
	char ope;//用于存放当前S2栈顶的运算符
	int p = 10;//用于充当10的倍数
	Num_Stack *S1;//操作数栈
	S1 = (Num_Stack*)malloc(sizeof(Num_Stack));//初始化S1
	InitStack(S1);
	Op_Stack *S2;//运算符栈
	S2 = (Op_Stack*)malloc(sizeof(Op_Stack));//初始化S2
	InitStack(S2);
	Push(S2, '#');//将'#'置于栈底
	//对数函数的输入形式为log(a)b【a为底数,b为真数】
	cout << "注意事项:对数函数的输入形式为log(a)b【a为底数,b为真数】" << endl;
	cout << "please input the expression:";
	cin >> ch;
	GetTop(S2, ope);
	while (ope != '=')
	{
		if (ch >= '0' && ch <= '9')//读入数字
		{
				num = ch - 48;
				cin >> ch;
				while (ch >= '0'&&ch <= '9') {
					num = num * 10 + (ch - 48);
					cin >> ch;
				}
				if (ch == '.') {//有小数点时的情况    
					cin >> ch;
					p = 10;
					while (ch >= '0'&&ch <= '9') {
						num = num + (float)(ch - 48) / p;
						cin >> ch;
						p = p * 10;
					}
				}
			Push(S1, num);//当读到的字符不是数字字符时,将操作数压入栈S1
		}
		else {
			if (ch == 's' || ch == 'l' || ch == 'c') {////将sin,cos,ln.log转换成易存储的符号
				char temp[4];//用于暂时存放sin,cos,ln,log运算符
				Transfer(ch, temp);
			}
			switch (Compare_Priority(ope, ch)) {
			case 1:Pop(S2, ope);//如果栈顶符号的优先级大于ch的,就先处理栈顶符号,再用ch和栈顶符号比较
				switch (ope) {
				case '+':Pop(S1, num1), Pop(S1, num2);
					Push(S1, num1 + num2);
					break;
				case '-':Pop(S1, num1), Pop(S1, num2);
					Push(S1, num2 - num1);
					break;
				case '*':Pop(S1, num1), Pop(S1, num2);
					Push(S1, num1 * num2);
					break;
				case '/':Pop(S1, num1), Pop(S1, num2);
					Push(S1, num2 / num1);
					break;
				case 'l':Pop(S1, num1);
					Push(S1, log(num1));
					break;
				case 'L':Pop(S1, num1), Pop(S1, num2);
					Push(S1, log(num1) / log(num2));
					break;
				case 's':Pop(S1, num1);
					Push(S1, sin(num1));
					break;
				case 'c':Pop(S1, num1);
					Push(S1, cos(num1));
					break;
				case '^':Pop(S1, num1), Pop(S1, num2);
					Push(S1, pow(num2, num1));
					break;
				}
				break;
			case -1://如果栈顶符号的优先级小于ch的,就将ch存储的符号推入栈S2中
				Push(S2, ch);
				ch = getchar();
				break;
			case 0://当'('遇到')'时,从栈顶弹出'('
				Pop(S2, ope);
				ch = getchar();
				break;
			}
			GetTop(S2, ope);
		}

	}
	GetTop(S1, num);
	cout << "The result is:" << num << endl;
	DisposeStack(S1);
	DisposeStack(S2);
	system("pause");
	return 0;
}

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).