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

طلب كود postfix

بدأه سار مع الليل في 6 أبريل 2011 · 4 رد · 937 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

اريد كود infix to postfix

حيث ان طريقة التحويل فاهمها تمام

وكذلك فاهم stack

ولكن حاولت اكتب الكود باستخدام stack ولكن وصلت الى مرحلة لا اعرف ماذا افعل

لذلك اطلب كود جاهز اريد ان اتتبع الكود واعرف طريقة عمل الكود واستنتج الكود بنفسي

فارجو تلبية الطلب

لقد وجت اكواد كثيرة في المنتدى ولكن معظمها لا تعمل وبعضها استنتجت منها بعض الافكار

49940.jpg
#2

اذا كان ما تحتاجه مثال بسيط عن العمليات الاساسيه الاربعه دون اقواس كذلك دون دعم للاعداد السالبه فهذا مثال بسيط لذلك

#include<iostream>

#include<conio.h>

using namespace std;

class Stack

{

private:

	char s[100];

	int depth;

public:

	Stack()

	{

		depth=0;

	}

	void push(char x)

	{

		s[depth++]=x;

	}

	char pop()

	{

		if(depth<0)

		depth=0;

		if(depth>=1)

		return s[--depth];

		else

		return 0;

	}

	bool IsEmpty()

	{

		return (depth==0);

	}

	char head()

	{

		if(depth>0)

			return s[depth-1];

		else

			return 0;

	}

};

void main()

{

	Stack stack;

	char *infix;

	char expression[50];

	cin>>expression;

	infix=expression;

    while(*infix!=0)

	{

		switch(*infix)

		{

		case '+':

			if(stack.head() != 0)

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('+');

			break;

		case '-':

			if(stack.head() != 0)

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('-');

			break;

		case '*':

			if(stack.head()=='*'||stack.head()=='/')

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('*');

			break;

		case '/':

			if(stack.head()=='*'||stack.head()=='/')

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('/');

			break;

		default:

			cout<<(*infix);

		}

		*(infix++);

	}

	while(!stack.IsEmpty())

	cout<<stack.pop();

	getch();

	return;

}
#3

هذا كود عملتة بعد تعب ومحاولات

الكود يشمل العمليات الاساسية وهي +-*/

وإن شاء الله سوف ادخل علية الاقواس

وفي مابعد الاعداد السالبة

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

مارايكم بمحاولتي

#include <cstdlib>
#include <iostream>

using namespace std;
void push(char);
void pop();
char top();
bool isemptoy();
int pretake(char);
char infix[40],postfix[40],stack[40];
int index=-1,j=0,i=0,first,last;
char syl,stc;



int main(int argc, char *argv[])
{
    for(int w=0;w<40;w++)
    {
            infix[w]=' ';
            postfix[w]=' ';
            stack[w]=' ';
    }
    cout<<"Enter infix ex 1+2*3 :\n";
    cin>>infix;
    for(i=0;infix!='='&&infix!='\0';i++)
    {

      if(infix!='+'&&infix!='-'&&infix!='*'&&infix!='/'&&infix!='('&&infix!=')'  )
      postfix[j++]=infix;
      else
      if(isemptoy())
      push(infix);
      else
      {
          syl=pretake(infix);
          stc=pretake(top());
          if(syl>stc)
          push(infix);
          else
          {
          while(!isemptoy()&&stc>=syl)
          {
           postfix[j++]=top();
           pop();
           stc=pretake(top());
           }//end loop
           push(infix);
           }//end else 
      }//end else
      }//end else

      while(!isemptoy())
      {
        postfix[j++]=top();
        pop();
      }                
      postfix[j]='\0';

      cout<<"\n"<<postfix<<endl;


    system("PAUSE");
    return EXIT_SUCCESS;
}

void push(char ch)
{


     stack[++index]=ch;
}
void pop()
{
     index--;
}
char top()
{
     return stack[index];
}
bool isemptoy()
{
     return index==-1;
}

int pretake(char ch)       
{
    switch(ch)
    {
              case '+':return 3;
              case '-':return 3;
              case '*':return 4;
              case '/':return 4;
              default :return 0;
              }
              }

شكراً اخي الغالي Ali Al-Zyoud

جاااااااااري قرائة الكود وترجمتة وتحليلة وفهمة

ثم ارد عليك

تم تعديل هذه المشاركة بواسطة سار مع الليل في 7 أبريل 2011 في 14:06

49940.jpg
#4

اخي الغالي Ali Al-Zyoud شكراً لك كثيررررررررر

الكود حقك تمام وكذلك طريقتة ممتازة بدون لف دوران ووجع دماغ

شكراً لك

ولقد طورت الكود على جميع العمليات وسوف ارفقة إن شاء الله في اقرب وقت

وانا الا ابحث عن كود

infix to prefix

حتى اتسطيع افهم اكثر

تم تعديل هذه المشاركة بواسطة سار مع الليل في 9 أبريل 2011 في 12:48

49940.jpg
#5

الحمدلله بفضل الله ثم من اعضاء المنتدى قدرت ان اعمل كود postfix و prefix

ارجو ان يستفيد منة الاعضاء

وهذه صورة توضيحية للكود

19899603.jpg

شكراً للمنتدى ولاعضاء المنتدى على جميع الشروحات والمساعدة التي ساعدونا بها

هذا الكود يضم جميع العمليات الاساسية وكذلك عملية القوس وعملية الاس

node.h


#ifndef node_h
#define node_h
template <class T>
class node
{
private:
T ch;
node<T>* next;
public:
node();
void set_ch(const T&);
void set_next(node<T>*);
T get_ch()const;
node<T>* get_next()const;
};


#include "node.inl"

#endif

node.inl


template <class T>
node<T>::node()
{
next=NULL;
ch=0;
}

template<class T>
void node<T>::set_ch(const T & in)
{
ch=in;
}
template <class T>
void node<T>::set_next(node<T> *in)
{
next=in;
}
template <class T>
T node<T>::get_ch() const
{
return ch;
}
template <class T>
node<T>* node<T>::get_next() const
{
return next;
}

stack.h


#include <iostream>
#include "node.h"
#ifndef h_stack
#define h_stack
template<class T>
class stack
{
private:
node<T>*topstack;
template<class T>
friend std::ostream& operator << (std::ostream & , stack<T> &);

public:
stack();
void push(const T &);
T top();
void pop();
bool isemptoy();
};//end class
#include"stack.inl"
#endif

stack.inl


#include "stdafx.h"

#include <iostream>

using namespace std;
template <class T>
stack<T>::stack():topstack(NULL)
{}
template <class T>
void stack<T>::push(const T & in)
{
node<T>* add;
add= new node<T>();
add->set_ch(in);
add->set_next(topstack);
topstack=add;
}

template<class T>
bool stack<T>::isemptoy()
{
return topstack==NULL;
}

template<class T>
T stack<T>::top()
{
if(!isemptoy())
return topstack->get_ch();
return 0;
}

template<class T>
void stack<T>::pop()
{
if(!isemptoy())
{
node<T>*tmp;
tmp=topstack;
topstack=topstack->get_next();
delete tmp;
}
}
template<class T>
std::ostream& operator <<(std::ostream &out, stack<T>& st)
{
while(!st.isemptoy())
{
out<<st.top();
st.pop();
}
return out;
}

main


// post_pre_fix.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
#include <cstring>

#include "stack.h"
using namespace std;
template <class T>
void POSTFIX(stack<T> &,char [],char []);
template<class T>
void PREFIX(char[],stack<T> &);
int _tmain(int argc, _TCHAR* argv[])
{
int s;
cout<<"program to (infix to prefix or infix to postfix :\n"
<<"1 - infix to prefix. \n"
<<"2 - infix to postfix. \n"
<<"3 - any number to Exit.\n";
cin>>s;
switch(s)
{
case 1:


{
stack<char> prefix;
char *infix=new char[30];
cout<<"Enter value is infix \n";
cin>>infix;
PREFIX(infix,prefix);
cout<<prefix<<endl;
}
break;
case 2:
{
stack<int>post;
char *infix;
infix=new char[30];
cout<<"Enter value is infix \n";
cin>>infix;

char *postfix;
postfix= new char[strlen(infix)+1];

POSTFIX(post,infix,postfix);
delete []infix;

cout<<postfix<<endl;
}
break;
default:
{}
}//end switch


return 0;
}



template <class T>
void POSTFIX(stack<T> &st,char infix [],char postfix[])
{
int i;
int j=0;
for(i=0;infix!= '\0' ;i++)
{
switch(infix)
{

case '(':
st.push('(');break;

case ')':

while(!st.isemptoy() && st.top() !='(')
{
postfix[j++]=st.top();
st.pop();
}
if(st.top()=='(')
st.pop();
else{}
break;

case '+':
while(!st.isemptoy() && st.top() !='(')
{
postfix[j++]=st.top();
st.pop();
}
st.push('+');
break;

case '-':while( !st.isemptoy() && st.top() !='(')
{
postfix[j++]=st.top();
st.pop();
}
st.push('-');
break;

case'*':
while(st.top() =='*' || st.top() =='/'||st.top()=='^')
{
postfix[j++]=st.top();
st.pop();
}
st.push('*');
break;

case'/':
while(st.top() =='*' || st.top() =='/'||st.top()=='^')
{
postfix[j++]=st.top();
st.pop();
}
st.push('/');
break;

case'^':
st.push('^');
break;

default:
postfix[j++]=infix;
break;
}//end switch
}//end loop
while(!st.isemptoy())
{
postfix[j++]=st.top();
st.pop();
}
postfix[j++]='\0';

}//end function POSTFIX

template<class T>
void PREFIX(char infix_s[],stack<T> &prefix)
{
stack<char>operand;
stack<char>infix;
for(int i=0;infix_s!='\0' && infix_s !='=';i++)
{
infix.push(infix_s);
}

for(;!infix.isemptoy();infix.pop())
{
switch(infix.top())
{
case')':
operand.push(')');
break;

case'(':
while(operand.top()!=')')
{
prefix.push(operand.top());
operand.pop();
}
operand.pop();
break;

case'+':
while(operand.top()=='*' || operand.top()=='/' || operand.top()=='^')
{
prefix.push(operand.top());
operand.pop();
}
operand.push('+');
break;

case'-':
while(operand.top()=='*' || operand.top()=='/' || operand.top()=='^')
{
prefix.push(operand.top());
operand.pop();
}
operand.push('-');
break;

case'*':
while(operand.top()=='^')
{
prefix.push(operand.top());
operand.pop();
}
operand.push('*');
break;

case'/':
while(operand.top()=='^')
{
prefix.push(operand.top());
operand.pop();
}
operand.push('/');
break;

case'^':
operand.push('^');
break;

default:
prefix.push(infix.top());
break;
}//end switch
}//end loop

while(!operand.isemptoy())
{
if(operand.top() !=')')
prefix.push(operand.top());
operand.pop();
}

}//end function prefix


تم تعديل هذه المشاركة بواسطة سار مع الليل في 13 أبريل 2011 في 16:43

1
49940.jpg

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