麻烦帮我编个程序:可循环运算四则运算,输入"#"跳出循环.

王朝知道·作者佚名  2010-02-23
窄屏简体版  字體: |||超大  
 
分類: 電腦/網絡 >> 操作系統/系統故障
 
問題描述:

麻烦帮我编个程序:可循环运算四则运算,输入"#"跳出循环.

悬赏分:0 - 离问题结束还有 14 天 23 小时

问题补充:最好用C++ 例如输入式子:

{1+[(2+1)*1]/1}

输出:

{1+[(2+1)*1]/1}=4

參考答案:

// FileName: treemain.cpp

#include <iostream>

#include <string>

#include <stack>

#include "tree.h"

/********** Checks if expression is ok *********/

bool isok(string exp)

{

char check;

int error=0;

int lb=0;

int rb=0;

for(int m=0;m < exp.size(); m++)

{

check = exp[m];

if(IsOperand(check))

{

}

else if(IsOperator(check))

{

if(check == ')')

{

rb++;

if(IsOperator(exp[m+1]) && (exp[m+1]=='+' || exp[m+1]=='-' || exp[m+1]=='*'

|| exp[m+1]=='/' || exp[m+1]=='^' || exp[m+1]==')'))

{

m++;

if(exp[m] == ')')

rb++;

}

else if(IsOperator(exp[m+1]))

error++;

}

else if(check == '(')

{

lb++;

if(IsOperator(exp[m+1]) && exp[m+1] =='(')

{

m++;

lb++;

}

else if(IsOperator(exp[m+1]))

error++;

}

else

{

if(IsOperator(exp[m+1]) && exp[m+1] == '(')

{

m++;

lb++;

}

else if(IsOperator(exp[m+1]))

error++;

}

}

else

error++;

}

if(error == 0 && lb==rb)

return(true);

else

return(false);

}

/*****************************************/

void main()

{

binary_tree etree;

stack<binary_tree> NodeStack;

stack<char> OpStack;

string infix;

char choice = 'y';

char c;

while(choice == 'y' || choice == 'Y')

{

cout << "\n\nIntroduce the Expression (no spaces): ";

cin >> infix;

cout << "----------------------------------------------" << endl;

cout << "Expression: " << infix << endl;

if(isok(infix))

{

for(int i=0; i < infix.size(); i++)

{

c = infix[i];

if(IsOperand(c)) //if operand, create tree and push into NodeStack

{

string tempstring;

tempstring = tempstring + c;

if(i+1 < infix.size() && IsOperand(infix[i+1]))

{

while(i+1 < infix.size() && IsOperand(infix[i+1]))

{

tempstring = tempstring + infix[++i];

}

}

binary_tree temp;

temp.root = build_node(tempstring);

NodeStack.push(temp);

}

//Else if Operator, check precedence and push into OpStack

else if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^')

{

if(OpStack.empty())

OpStack.push(c);

else if(OpStack.top() == '(')

OpStack.push(c);

else if(TakesPrecedence(c, OpStack.top()))

OpStack.push(c);

else

{

while(!OpStack.empty() && TakesPrecedence(OpStack.top(), c))

{

binary_tree temp_tree;

string thisstring="";

thisstring = thisstring + OpStack.top();

OpStack.pop();

etree.root = build_node(thisstring);

copy(temp_tree.root,NodeStack.top().root);

NodeStack.pop();

etree.root->right_child = temp_tree.root;

temp_tree.root = NULL;

copy(temp_tree.root,NodeStack.top().root);

etree.root->left_child = temp_tree.root;

NodeStack.pop();

temp_tree.root = NULL;

copy(temp_tree.root, etree.root);

NodeStack.push(temp_tree);

etree.root = NULL;

}

OpStack.push(c);

}

}

else if(c == '(')

OpStack.push(c);

else if(c == ')')

{

while(OpStack.top() != '(')

{

binary_tree temp_tree;

string thisstring="";

thisstring = thisstring + OpStack.top();

OpStack.pop();

etree.root = build_node(thisstring);

copy(temp_tree.root,NodeStack.top().root);

NodeStack.pop();

etree.root->right_child = temp_tree.root;

temp_tree.root = NULL;

copy(temp_tree.root,NodeStack.top().root);

etree.root->left_child = temp_tree.root;

NodeStack.pop();

temp_tree.root = NULL;

copy(temp_tree.root, etree.root);

NodeStack.push(temp_tree);

etree.root = NULL;

}

OpStack.pop();

}

} // end of for loop

while(!OpStack.empty()) //While OpStack not empty, pop that stack

//and create tree

{

binary_tree temp_tree;

string thisstring="";

thisstring = thisstring + OpStack.top();

OpStack.pop();

etree.root = build_node(thisstring);

copy(temp_tree.root,NodeStack.top().root);

NodeStack.pop();

etree.root->right_child = temp_tree.root;

temp_tree.root = NULL;

copy(temp_tree.root,NodeStack.top().root);

etree.root->left_child = temp_tree.root;

NodeStack.pop();

temp_tree.root = NULL;

copy(temp_tree.root, etree.root);

NodeStack.push(temp_tree);

if(!OpStack.empty())

{

etree.root = NULL;

}

}

cout << "Postfix traversal: ";

etree.print();

cout << endl;

etree.evaluate();

cout << "Result: " << etree.root->data << endl;

cout << "----------------------------------------------" << endl;

cout << "\n\nRun Program again? Enter <y/n> : ";

cin >> choice;

}

else

{

cout << "********************************" << endl;

cout << "ERROR: Invalid Expression" << endl;

cout << "********************************" << endl;

cout << "\n\nRun Program again? Enter <y/n> : ";

cin >> choice;

}

}

} //end of main

--------------------------------------------------------------------------------------------------// FileName: tree.h

#ifndef _TREE_H_

#define _TREE_H_

#include <iostream>

#include <string>

#include <math.h>

#include <sstream>

using namespace std;

// Overloaded function, checks a string for an operator超函数,为一个算子检验一个字符串

bool IsOperator(string mystring)

{

if(mystring == "-" || mystring == "+" || mystring == "/" || mystring == "*" || mystring == "^")

return(true);

else

return(false);

}

// Overloaded function, checks a character for an operator超函数,为一个算子检验一个字符

bool IsOperator(char ops)

{

if(ops == '+' || ops == '-' || ops == '*' || ops == '/' || ops == '^' || ops == '(' || ops == ')')

return(true);

else

return(false);

}

//Binary Tree Definition and Implementation二进制树形网络的定义和执行

class node_type

{

public:

string data;

node_type *left_child;

node_type *right_child;

node_type(string k)

{

data=k;

left_child = NULL;

right_child = NULL;

}

};

class binary_tree

{

public:

node_type *root;

void print(node_type *r); // Postfix traversal后序遍历

binary_tree(void) { root = NULL;}

void print(void) {print (root);}

void evaluate()

{

evaluate(root);

}

void evaluate(node_type* prt) //recursive tree evaluation

{

if(IsOperator(prt->data) && !IsOperator(prt->left_child->data) && !IsOperator(prt->right_child->data))

{

int num = 0;

int num1=atoi(prt->left_child->data.c_str());

int num2=atoi(prt->right_child->data.c_str());

if(prt->data == "+")

num = num1 + num2;

else if(prt->data == "-")

num = num1 - num2;

else if(prt->data == "*")

num = num1 * num2;

else if(prt->data == "/")

num = num1 / num2;

else if(prt->data == "^")

num = pow(num1,num2);

stringstream bob;

bob << num;

string suzzy(bob.str());

prt->data = suzzy;

prt->left_child = NULL;

prt->right_child = NULL;

}

else if(prt->left_child == NULL && prt->right_child == NULL);

else

{

evaluate(prt->left_child);

evaluate(prt->right_child);

evaluate(prt);

}

}

void clear_help(node_type* rt) // destructor

{

if( rt != NULL )

{

clear_help( rt->left_child);

clear_help( rt->right_child);

delete rt;

}

}

void clear()

{

clear_help(root);

}

};

node_type *build_node(string x) //build a new node for the tree

{

node_type *new_node;

new_node = new node_type(x);

return(new_node);

}

void binary_tree::print(node_type *p) //postfix traversal

{

if(p != NULL)

{

print(p->left_child);

print(p->right_child);

cout << p->data << " ";

}

}

bool IsOperand(char ch) //Checks for character to be an operand

{

if ((ch >= '0') && (ch <= '9'))

return true;

else

return false;

}

//Checks Precedence, returns true of A is higher precendence over B

bool TakesPrecedence(char OperatorA, char OperatorB)

{

if (OperatorA == '(')

return false;

else if (OperatorB == '(')

return false;

else if (OperatorB == ')')

return true;

else if ((OperatorA == '^') && (OperatorB == '^'))

return false;

else if (OperatorA == '^')

return true;

else if (OperatorB == '^')

return false;

else if ((OperatorA == '*') || (OperatorA == '/'))

return true;

else if ((OperatorB == '*') || (OperatorB == '/'))

return false;

else

return true;

}

//Creates a copy of a tree passes the roots of the 2 trees, r1 being the root of the tree to be copied to

// and r2 being the root of the tree being copied.

void copy(node_type *&r1, node_type *r2)

{

if(r2 == NULL)

r1 = NULL;

else

{

if(r1 == NULL)

{

r1 = build_node(r2->data);

copy(r1->left_child, r2->left_child);

copy(r1->right_child, r2->right_child);

}

else

{

r1 = build_node(r2->data);

copy(r1->left_child, r2->left_child);

copy(r1->right_child, r2->right_child);

}

}

}

#endif

这是我的课程设计哦 c++6.0的 不要照抄 不然会后悔的

参考资料:我的课程设计哦

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航