الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مساعدة: برنامجexpresion Evaluation

بدأه fatimah في 29 أبريل 2009 · 2 رد · 630 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكــم ورحمـة الله وبركاتــه ،،

برنامجي سهل ولكن كل ماعدل شي في الكود احصل خطأ

مطلوب مني الاتي

You are required to write a program to implement the EXPRESSION EVALUATION problem above using an appropriate data structure. Your program must include the functionalities to enable the user to evaluate any expression with the following minimum assumptions:-

* Operands may be negative

* Operators must include (^, %, *, /, +, -,)

* Test for division by zero

* Parenthesis Checking

Expression Examples The Evaluation

5 – ( 3 + -2 ) ^ 3 * 5 / 2 2.5

3 * -2 * [ ( 5 – 3 ) * 2 ] ^ 2 / 3 -32

2 + [ 6 – ( 2 + 1 ] ) ^ 2 Wrong Expression

Instructions

----------------

Design a suitable algorithm for the scenario above and implement the algorithm using C++ language. You are to justify the choice of the data structures used in your program. Provide suitable test data and list any assumptions you have made in your system.

والكود اللي وصلت اليه فقط Stack

#include <iostream>
#ifndef STACK
#define STACK
const int STACK_CAPACITY = 128;

typedef int StackElement;
class Stack {
 public:
  /***** Function Members *****/
  /***** Constructor *****/
  Stack();
  /*----------------------------------------------------------
	Construct a Stack object.
	Precondition:  None.
	Postcondition: An empty Stack object has been constructed
		(myTop is initialized to -1 and myArray is an array
		with STACK_CAPACITY elements of type StackElement).
   -----------------------------------------------------------*/
  bool empty() const;
  /*-----------------------------------------------------------
	Check if stack is empty.
	Precondition: None
	Postcondition: Returns true if stack is empty and
		false otherwise.
   -----------------------------------------------------------*/

  void push(const StackElement & value);
  /*-----------------------------------------------------------
	Add a value to a stack.

	Precondition: value is to be added to this stack
	Postcondition: value is added at top of stack provided
		there is space; otherwise, a stack-full message is
		displayed and execution is terminated.
   -----------------------------------------------------------*/

  void display(ostream & out) const;
  /*-----------------------------------------------------------
	Display values stored in the stack.

	Precondition:  ostream out is open.
	Postcondition: Stack's contents, from top down, have
		been output to out.
   -----------------------------------------------------------*/

  StackElement top() const;
  /*-----------------------------------------------------------
	Retrieve value at top of stack (if any).

	Precondition:  Stack is nonempty
	Postcondition: Value at top of stack is returned, unless
		the stack is empty; in that case, an error message is
		displayed and a "garbage value" is returned.
   ----------------------------------------------------------*/

  void pop();
  /*-----------------------------------------------------------
	Remove value at top of stack (if any).

	Precondition:  Stack is nonempty.
	Postcondition: Value at top of stack has been removed,
		unless the stack is empty; in that case, an error
		message is displayed and execution allowed to proceed.
   ----------------------------------------------------------*/

 private:
  /***** Data Members *****/
  StackElement myArray[STACK_CAPACITY];
  int myTop;
}; // end of class declaration

#endif

#include <iostream>
using namespace std;
#include "Stack.h"

//--- Definition of Stack constructor
Stack::Stack(): myTop(-1) {}

//--- Definition of empty()
bool Stack::empty() const {
   return (myTop == -1);
}

//--- Definition of push()
void Stack::push(const StackElement & value) {
   if (myTop < STACK_CAPACITY - 1){
	  ++myTop;
	  myArray[myTop] = value;
   } else {
	  cerr << "*** Stack full -- can't add new value ***\n"
			 << "Must increase value of STACK_CAPACITY in Stack.h\n";
	  exit(1);
   }
}

//--- Definition of display()
void Stack::display(ostream & out) const {
   for (int i = myTop; i >= 0; i--)
	  out << myArray << endl;
}

//--- Definition of top()
StackElement Stack::top() const {
   if ( !empty() )
	  return (myArray[myTop]);
   else
   {
	  cerr << "*** Stack is empty "
			 << " -- returning garbage value ***\n";
	  return *(new StackElement);
   }
}

//--- Definition of pop()
void Stack::pop() {
   if ( !empty() )
		myTop--;
   else
	  cerr <<  "*** Stack is empty -- "
			  << "can't remove a value ***\n";
}

int convert ( stck a){
   char exp[];
   for(int i=0; i< '\0'; i++)
   {cin >> exp;
	if ( char ch == num)
	a.push(ch);

وحاولت انشاء دالة لتحويل infix to posfix

وللتاكد من صحة المعطى والاقواس كلها مغلقة

هل من مساعدة

#2

السلام عليكــم ورحمـة الله وبركاتــه ،،

شوفى البرنامج التالى يمكن يساعدك

تمت كتابته بإستخدام stack الجاهز فى مكتبات STL

وحاولى تحويله بإستخدام Stack

الذى ارفقتيه فى سؤالك


#include <string>
#include <stack>
using namespace std;
stack<char> T;
char oper[7]="+-*/^%";
int is_operator(char c)
{
int t=0;
for(int i=0;i<7;i++)
{
if(oper==c) t++;
}
return t;
}
int precedence(char c)
{
if(c=='*' || c=='/' || c=='^' || c=='%') return 2;
else if(c=='+' || c=='-') return 1;
else
return 0;
}
string infix_to_postfix(string infix)
{
int i;
string postfix="";
for (i=0;i<infix.size();i++)
{
if(is_operator(infix))
{
while (! T.empty() && infix!='(')
{
if(precedence(infix)<precedence(T.top()))
{
postfix+=T.top();
T.pop();
}
else break;
}
T.push(infix);
}
else if(infix=='(')
T.push(infix);
else if(infix==')')
{
while (! T.empty() && T.top()!='(')
{
postfix+=T.top();
T.pop();
}
if(! T.empty()) T.pop();
}
else
postfix+=infix;

}
while (! T.empty())
{
postfix+=T.top();
T.pop();
}
return postfix;
}
int main()
{
string in,po="";
cout<<"Enter the infix : ";
cin>>in;
po=infix_to_postfix(in);
cout<<" The prefix is "<<po<<endl;
return 0;
}
#include <iostream>

وموضوع الأقواس

يمكن عمل عداد يعد الأقواس المفتوحه و آخر يعدالأقواس المغلقه

ويقارن بينهم

تحياتى

تم تعديل هذه المشاركة بواسطة amin_mu123 في 29 أبريل 2009 في 19:08

اللهم صل على سيدنا محمد وعلى آله وصحبه وسلم

#3

ياخي اشكررررررررررك وربي يوفقك دنيا واخرة ويرزقك الفردوس الاعلى

ويرضى عليك ويهديك ويغفر لك ويرحمك وجميع المؤمنين يارب

انا الحين راح اشتغل على الكود السابق واحاول اكمل البرنامج

اشكرك اخي العزيز

واتمنى من كل شخص يستطيع المساعدة بكود لا يبخل علي به لاني احتاج ان ارى اكثر من طريقة عشان افهم واكون شاكرة للجميع المساعدة

ومن فرج عن اخيه المسلم كربة من كرب الدنيا فرج الله عنه كربة من كرب الاخرة

تم تعديل هذه المشاركة بواسطة fatimah في 29 أبريل 2009 في 20:18

مواضيع مشابهة