控制台程序求解有理式(带括号,带小数)

来源:互联网 发布:网络交友英语作文题目 编辑:程序博客网 时间:2024/06/05 07:55
// stack_eqution.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;


//单节点定义
typedef struct nnode *PtonNode;
typedef struct nnode
{
float a;
PtonNode next;
}stack1,*s1;


typedef struct onode *PtooNode;
typedef struct onode
{
string a;
PtooNode next;
}stack2,*s2;


//进栈函数
void push(float x, s1 y)
{
s1 p = new stack1;
p->a = x;
p->next = y->next;
y->next = p;
}


void push(string x, s2 y)
{
s2 p = new stack2;
p->a = x;
p->next = y->next;
y->next = p;
}


//出栈函数
float pop(s1 y)
{
s1 z = new stack1;
z = y->next;
y->next = z->next;
float x = z->a;
free(z);
return x;
}


string pop(s2 y)
{
s2 z = new stack2;
z = y->next;
y->next = z->next;
string x = z->a;
free(z);
return x;
}


//string转数字类型(int,float,double)
template <class type>
type stringtonum(const string& str)
{
istringstream s2num(str);
type num;
s2num >> num;
return num;
}


//操作符比较
int value(string cl,string cr)
{
int level;
if (cl == "+" | cl == "-")
{
if (cr == "(")
level = 0;
else if (cr == "*" | cr == "/")
level = -1;
else
level = 1;
}
else if (cl == "*" | cl == "/")
{
if (cr == "(")
level = 0;
else
level = 1;
}
else if (cl == "(")//当cl出现(或者)时,
{
if (cr == ")")
level = 1;
else
level = 0;
}
else
level = 0;
  return level;
}


//进队列函数 
void pushh(string x, s2 y)
{
s2 p = y;
while (p->next != NULL)
{
p = p->next;
}
s2 z = new stack2;
z->a = x;
z->next = p->next;
p->next = z;
}
//多项式字符串入栈
int into_stack(s2 equation, string s)
{
int i = 0;
string str1;
while (s.length() != 0)
{
while (s.at(i) != '+' & s.at(i) != '-' & s.at(i) != '*' & s.at(i) != '/' & s.at(i) != '(' & s.at(i) != ')' & s.at(i) != '=')
{
str1 += s.at(i);
i++;
if (s.at(i) == '+' | s.at(i) == '-' | s.at(i) == '*' | s.at(i) == '/' | s.at(i) == '(' | s.at(i) == ')' | s.at(i) == '=')
{
pushh(str1, equation);
str1 = "";
}
}


if (s.at(i) == '+' | s.at(i) == '-' | s.at(i) == '*' | s.at(i) == '/' | s.at(i) == '(' | s.at(i) == ')' | s.at(i) == '=')
{
str1 = "";
str1 += s.at(i);
pushh(str1, equation);
str1 = "";
if (s.at(i) == '=')
{
str1 += s.at(i);
break;
}
else
{
i++;
}
}
if (str1 == "=")
{
break;
}
}
// if (s.length() == 0)
// {
// cout << "无多项式输入" << endl;
// return 0;
// }
return 1;
}


//内部运算函数
int compute(s2 equ)
{
s1 numhead = new stack1; numhead->next = NULL;
s2 operhead = new stack2; operhead->next = NULL;
if (equ->next == NULL)
{
cout << endl << "无多项式输入" << endl << endl;
return 0;
}
if (equ->next->a == "=")
{
cout << endl << "无多项式输入" << endl << endl;
return 0;
}
string str1 = pop(equ);//用于过程中储存equ中的字符串
float nl, nr;//用于过程中操作数的储存
if (str1 != "+"&str1 != "-"&str1 != "*"&str1 != "/"&str1 != "("&str1 != ")"&str1 != "=")
push(stringtonum<float>(str1), numhead);
else
push(str1, operhead);


while (equ->next->a != "=")
{
str1 = pop(equ);
if (str1 != "+"&str1 != "-"&str1 != "*"&str1 != "/"&str1 != "("&str1 != ")"&str1 != "=")
{
push(stringtonum<float>(str1), numhead);
while (value(operhead->next->a, equ->next->a) == 1)
{
if (operhead->next->a == "(")
{
pop(operhead);
pop(equ);
if (operhead->next == NULL)
break;
continue;
}
nr = pop(numhead);
nl = pop(numhead);
if (operhead->next->a == "+")
{
push(nl+nr, numhead);
}
if (operhead->next->a == "-")
{
push(nl - nr, numhead);
}
if (operhead->next->a == "*")
{
push(nl * nr, numhead);
}
if (operhead->next->a == "/")
{
if (nr == 0)
{
cout << endl << "多项式有误,除数出现0" << endl << endl;
return 0;
}
push(nl / nr, numhead);
}
pop(operhead);
if (operhead->next == NULL)
break;
}


}
else
{
push(str1, operhead);
if (str1 == ")")
pop(operhead);
}
}
cout << numhead->next->a<< endl;
return 0;
}




int _tmain(int argc, _TCHAR* argv[])
{
//多项式输入
cout <<"输入多项式,以=结尾:" << endl;
string str;
getline(cin, str);


//多项式入队列
s2 equ = new stack2;  equ->next = NULL;
into_stack(equ, str);

//多项式运算(使用栈)
compute(equ);
return 0;
}

原创粉丝点击