1. 程式人生 > >字串求值(完整版)

字串求值(完整版)

程式碼如下:

#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<math.h>
#include<Windows.h>

typedef struct StackNode1
{
	char character;
	struct StackNode1 *next;
}LinkStack1;

typedef struct StackNode2
{
	double data;
	struct StackNode2 *next;
}LinkStack2;

void InitStack1(LinkStack1 **Operator);
void InitStack2(LinkStack2 **Operand);
void Push1(char ch, LinkStack1 **Operator);
void Push2(double ch, LinkStack2 **Operand);
char Pop1(LinkStack1 **Operator);
double Pop2(LinkStack2 **Operand);
bool isEmpty(LinkStack1 *Operator);
char getTop(LinkStack1 *Operator);
char Priority(char ch, char ch2);
double Generate(double a, char ch, double b);

int main()
{
	LinkStack1 *Operator;
	LinkStack2 *Operand;
	char ch2, judge, c;
	char sentence[1000];
	double sum, sum1, sum2, a, b, result;
	int k, count = 0;

	InitStack1(&Operator);
	InitStack2(&Operand);
	Push1('=', &Operator);

	scanf("%s", sentence);
	while (!isEmpty(Operator))
	{
		if (sentence[count] >= '0' && sentence[count] <= '9')
		{
			sum = sum1 = sum2 = 0.0;
			k = 0;
			while (sentence[count] >= '0' && sentence[count] <= '9')
			{
				sum1 = sum1 * 10 + (sentence[count] - '0');
				count++;
			}
			if (sentence[count] == '.')
			{
				count++;
				while (sentence[count] >= '0' && sentence[count] <= '9')
				{
					sum2 = sum2 * 10 + (sentence[count] - '0');
					k++;
					count++;
				}
			}
			sum = sum1 + sum2 / pow(10, k);
			Push2(sum, &Operand);
		}
		else
		{
			judge = Priority(sentence[count], getTop(Operator));
			if (judge == '=')
			{
				c = Pop1(&Operator);
				count++;
			}
			else if (judge == '>')
			{
				Push1(sentence[count], &Operator);
				count++;
			}
			else
			{
				a = Pop2(&Operand);
				b = Pop2(&Operand);
				ch2 = Pop1(&Operator);
				result = Generate(a, ch2, b);	 //運算順序為b對a
				Push2(result, &Operand);
			}
		}
	}

	printf("%lf\n", Pop2(&Operand));
	system("pause");
	return 0;
}

void InitStack1(LinkStack1 **Operator)
{
	*Operator = NULL;
	return;
}

void InitStack2(LinkStack2 **Operand)
{
	*Operand = NULL;
	return;
}

void Push1(char ch, LinkStack1 **Operator)
{
	LinkStack1 *p;
	p = (LinkStack1 *)(malloc(sizeof(LinkStack1)));
	p->character = ch;
	p->next = *Operator;
	*Operator = p;
}

void Push2(double ch, LinkStack2 **Operand)
{
	LinkStack2 *p;
	p = (LinkStack2 *)(malloc(sizeof(LinkStack2)));
	p->data = ch;
	p->next = *Operand;
	*Operand = p;
}

char Pop1(LinkStack1 **Operator)
{
	LinkStack1 *p;
	char ch;
	p = *Operator;
	(*Operator) = (*Operator)->next;
	ch = p->character;
	free(p);
	return ch;
}

double Pop2(LinkStack2 **Operand)
{
	LinkStack2 *p;
	double ch;
	p = *Operand;
	(*Operand) = (*Operand)->next;
	ch = p->data;
	free(p);
	return ch;
}

char getTop(LinkStack1 *Operator)
{
	return Operator->character;
}

bool isEmpty(LinkStack1 *Operator)
{
	if (Operator == NULL)
		return true;
	else
		return false;
}

char Priority(char ch, char ch2)  //ch2為棧中元素,ch為棧外元素
{
	switch (ch)
	{
	case '+':
	case '-':
		if (ch2 == '*' || ch2 == '/' || ch2 == '+' || ch2 == '-')
			return '<';
		else if (ch2 == '(' || ch2 == '=')
			return '>';
	case '*':
	case '/':
		if (ch2 == '+' || ch2 == '-' || ch2 == '(' || ch2 == '=')
			return '>';
		else if (ch2 == '*' || ch2 == '/')
			return '<';
	case '(':
		return '>';
	case ')':
		if (ch2 == '(')
			return '=';
		else return '<';
	case '=':
		if (ch2 == '=')
			return '=';
		else return '<';
	}
}

double Generate(double a, char ch, double b)
{
	switch (ch)
	{
	case '+': return b + a;
	case '-': return b - a;
	case '*': return b * a;
	case '/': return b / a;
	}
}