标题:[原创]前缀表达式求值
只看楼主
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
 问题点数:0 回复次数:0 
[原创]前缀表达式求值

MABfCHYZ.rar (7.62 KB) 前缀表达式求值




// exp.h

#include<iostream>
#include<stack>
#include<string>
#include<vector>
#include<cstddef>
using namespace std;

void Insertvector(string& expection,vector<string> *pvec);

double Exp(vector<string> *pvec);

// exp.cpp

#include "exp.h"

void Insertvector(string& expection,vector<string> *pvec)
{
string::size_type pos = 0,prev_pos = 0;
while((pos = expection.find_first_of(' ',prev_pos))!= string::npos)
{
pvec->push_back(expection.substr(prev_pos,pos-prev_pos));
++pos;
prev_pos = pos;
}
pvec->push_back(expection.substr(prev_pos,pos-prev_pos));
}

double Exp(vector<string> *pvec)
{
stack<string> s_tack;/*主栈*/
stack<double> temp_stack;/*辅佐栈*/

for(vector<string>::iterator it = pvec->begin();
it != pvec->end(); ++it)
s_tack.push(*it);
/*拿 + - 3 2 1 为例子 这时候栈顶是 1*/

bool operflag = false;
while(!s_tack.empty())
{
while(1)
{

string sValue = s_tack.top();

if(sValue != "+" && sValue != "-" && sValue !="*" && sValue != "/")
{

double sum = 0.0;/* 用来保存sValue转换的数字 */
double psum = 0.0;/*小数点后面的数 例如 1.123,那么psum = 123*/
double dsum = 1.0;/*例如 1.123 那么 dsum = 1000,用来除 psum */
int pcount = 0; /*用来防止有两个小数点*/
int count = 0; /*用来计算小数点后的位数*/
bool flag = false; /*用来判断当前的数字是否在小数点前,若是就false*/


for(size_t i=0;i<sValue.size();i++)
{
if(sValue[i]!='.'&& flag == false)
{
if(i== 0 && sValue[i]=='0')
throw string("the number is wrong");

if(sValue[i]>='0'&&sValue[i]<='9')
{
sum = sum*10+(sValue[i]-'0');
}
else
throw string("the number is wrong!");

}
else if(flag == true)
{
psum = psum*10+(sValue[i]-'0');
count++;
}
if(sValue[i]=='.'&&pcount<1)
{
if(i>0)
{
flag=true; pcount++;
}
else
throw string("the number is wrong!");/*如果第一个位是小数点就抛出异常*/
}
else if(sValue[i]=='.'&&pcount==1)/*如果有两个小数点就抛出异常*/
throw string("the number is wrong!");
}

for(int i=0;i<count;i++)
dsum *= 10;

sum+= psum/dsum;

temp_stack.push(sum);/*分别压入 1 2 3*/
}
else{
operflag = true;
break;
}

s_tack.pop();
}
/*这个时候s_tack的栈顶是 3*/
if(operflag == true)
{
string oper = s_tack.top();/* - */
char op = oper[0];
s_tack.pop();
switch(op)
{
case '+':
{
double num1 = temp_stack.top();/*出 3*/
temp_stack.pop();
double num2 = temp_stack.top();/*出 2*/
temp_stack.pop();
temp_stack.push(num1+num2);/*压入 1,这个时候栈顶是 1 ,s_tack再出栈,符号就是+号,那么再出 1 1,最后结果就是2*/
break;
}
case '-':
{
double num1 = temp_stack.top();
temp_stack.pop();
double num2 = temp_stack.top();
temp_stack.pop();
temp_stack.push(num1-num2);
break;
}
case '*':
{
double num1 = temp_stack.top();
temp_stack.pop();
double num2 = temp_stack.top();
temp_stack.pop();
temp_stack.push(num1*num2);
break;
}
case '/':
{
double num1 = temp_stack.top();
temp_stack.pop();
double num2 = temp_stack.top();
temp_stack.pop();
temp_stack.push(num1/num2);
break;
}
}
operflag = false;
}
}

double result = temp_stack.top();
temp_stack.pop();
return result;
}



// main

#include "exp.h"

int main()
{
vector<string> *pvec = new vector<string>;
string expection;

while(1)
{
cout<<"input the expection:( # for exit)"<<'\n';
getline(cin,expection,'\n');
if(expection =="#")
break;
Insertvector(expection,pvec);
try{
cout<<Exp(pvec)<<endl;
}
catch(string s)
{
cout<<s<<endl;
}
pvec->clear();
cout<<'\n';
}

delete pvec;
}



input the expection:( # for exit)
+ * 4 - 5 3 1
9

input the expection:( # for exit)

[此贴子已经被作者于2006-1-18 19:42:43编辑过]

搜索更多相关主题的帖子: 前缀 string 求值 vector include 
2006-01-18 19:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-43347-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.370719 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved