控制台程序求解有理式(带括号,带小数)
来源:互联网 发布:网络交友英语作文题目 编辑:程序博客网 时间: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;
}
//
#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;
}
阅读全文
1 0
- 控制台程序求解有理式(带括号,带小数)
- 控制台程序求解有理式(带括号,适用于int类型)
- 四则运算(带括号)的小程序
- 简单的计算器小程序(不带括号)
- vs2008带参数控制台程序调试方法
- 多数带括号计算
- 带括号的四则运算
- 带括号有负数有小数有表达式的计算器的简单实现
- c#控制台绚丽动画(带背景音乐)
- 【第二版】五子?棋(带AI、悔 棋)程序+源码【控制台版】
- VC6.0 调试带参数的控制台程序
- CodeBlock如何调试带参数的控制台程序
- 带括号带键盘输入事件的计算器
- JavaScript带小数金额计算
- AOJ 435 带小数高精度
- new带括号与不带括号的区别
- [C++] new带括号和不带括号
- [转载]new带括号与不带括号的区别
- Sublime Text 3常用插件安装
- centos 下 安装使用GoogleMock
- GitHub-fork/star/watch/follow
- POJ
- input、textarea标签placeholder兼容IE8浏览器js插件代码
- 控制台程序求解有理式(带括号,带小数)
- C++网站开发MVC框架TreeFrog Framework教程——8.Otama模板
- Windows 7(Win7)下Visual Studio 2012(VS2012)编译jrtplib与MinGW编译jrtplib
- Java 静态工厂模式的使用
- Eclipse Memory Analysis的安装和使用
- name,id,class的关系和区别
- 九大Java性能调试工具,必备至少一款
- 在github上创建一个静态网站
- linux 中mysql 四种启动方式