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

برنامج يوجد الناتج من postfix معادله

مغلق
بدأه Mrs.n في 28 أكتوبر 2004 · 16 رد · 965 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

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

فكرة البرنامج

ندخل المعادلهstring والمعادله المدخله هي postfix

ونستخدم pop و push

لحساب الناتج

مع الانتباه بانه يجيب تحويل الارقام من string الى int

هذا هوه الحل

#include <iostream>
#include <cstring>
#include <cstdio>
#include <conio.h>
#include <cstdlib>
#include <ctype.h>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
struct NodeRec
{
	int eval;
	NodeRec* next;
};
void token(NodeRec* temp, char b[4]);
void push(NodeRec* top,NodeRec* temp);
int pop(NodeRec* top);
void result(char a[50]);
void main ()
{
	char a[50];
	result(a);

	getch();
}

//----------------fun puch---------------------------
void push(NodeRec* top,NodeRec* temp)
{ 
   

	if(top==NULL)

  top=temp;
	else
	{
  temp->next=top;
  top=temp;
	}


}
//------------fun top-------------------------------
int pop(NodeRec* top)
{
	int eval;
    NodeRec* curn;
	curn=top;
	if(top==NULL)
  cout<<"STACK IS EMPTY";
	else
	{
  eval=top->eval;
     top=top->next;
  delete curn;
	}
	return eval;

}
//--------------fun result--------------------------
void result(char a[50])
{
	char b[4];
	int l,j=0,x,i=0;
    NodeRec* top;
	top=new NodeRec;
    NodeRec* temp;
    top=NULL;
    

    cout<<"enter expression in postfix but after number space:";
	gets(a);
	l=strlen(a);
	while(i<l+1)
	{
  temp=new NodeRec;

  temp->eval=0;
        temp->next=NULL;
  if(a =='+')
  {
 	 x=pop(top);
 	 x+=pop(top);
 	 temp->eval=x;
    push(top,temp);
    i++;
  }
       else if(a =='-')
          {
 	 x=pop(top);
 	 x-=pop(top);
 	 temp->eval=x;
    push(top,temp);
    i++;
  }
        else if(a =='*')
 	 {
 	 x=pop(top);
 	 x*=pop(top);
 	 temp->eval=x;
    push(top,temp);
    i++;
  }
     else if(a =='/')
  {
 	 x=pop(top);
 	 x/=pop(top);
    push(top,temp);
    i++;
  }
     
    else
    {
  
     while((a !=' ') && (a !='+') && (a!='-') && (a!='*') && (a!='/'))
  {
 	 b[j]=a;
 	 j++;
 	 i++;
  }
  b[j]='\0';
     token(temp,b);
  //if ((a !='+') && (a!='-') && (a!='*') && (a!='/'))
      push(top,temp);
   	 strcpy(b," ");
   	 j=0;
  i++;
    }
    
	} 
    cout<<endl<<"result= "<<top->eval;
}
//-----------------fun token----------------  // لتحويل من سترينق الى انتيقر
void token(NodeRec* temp, char b[4])
{
	char *y;
	int n;
	y=new char;
	y=strtok(b,'\0');
	n=atoi(y);
	temp->eval=n;
}
//------------------------------------------------------
#2
plz help me
algoritm:  To evaluat an expression written in postfix form:
 - Read the entered expression as string
 - Each time you find adigit, push it into the stack
 - Each time you find an operation, pop tow digits form stack top and compute the result
- Push the result into the stack

Example: evaluating: 2 12 6 4 + - *
   2:push 2 into the stack
 12: push 12 into the stack
6: push 6 into the stack
 4:push 4 into the stack
+:
  -pop 4 and 6
 - cmpute 4+6,and push 10 into the stack
-:
  - pop 10 and 12
  - cmpute 12-10,and push 2 into the stack
*:
  -pop 2 and 2
  - cmpute 2*2,and push 4 into the stack

4:(at the top of the stack)is the answer
#3

plz someone help me

this my homework

look the code where

aproblem

#4

محاوله ثانيه لعتديل البرنامج

ولكن فاشله زي اللي قبلها

اتمنى اي شخص يساعدني

#include <iostream>
#include <cstring>
#include <cstdio>
#include <conio.h>
#include <cstdlib>
#include <ctype.h>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
struct NodeRec
{
	int eval;
	NodeRec* next;
};
void token(int n, char b[4]);
void push(NodeRec*top,int x);
int pop(NodeRec* top);
int result(char a[50]);
void main ()
{
	char a[50];
     int x;
  cout<<"enter expression in postfix but after number space:";
   gets(a);
   x=result(a);
   cout<<endl<<"result= "<<x;

	getch();
}

//----------------fun puch---------------------------
void push(NodeRec* top,int x)
{ 
	NodeRec* temp;
   temp=new NodeRec;
   temp->eval=x;
   temp->next=NULL;

	if(top==NULL)

  top=temp;
	else
	{
  temp->next=top;
  top=temp;
	}


}
//------------fun top-------------------------------
int pop(NodeRec* top)
{
	int eval;
    NodeRec* curn;
	curn=top;
	if(top==NULL)
  cout<<"STACK IS EMPTY";
	else
	{
  eval=top->eval;
     top=top->next;
  delete curn;
	}
	return eval;

}
//--------------fun ans--------------------------
int result(char a[50])
{
	char b[4];
	int l,j=0,x,z,y,i=0;
    NodeRec* top;


    
    top=NULL;
    

    
	l=strlen(a);
	do

	{
  

	if(	(a !='+') || (a!='-') || (a!='*') || (a!='/'))
        
	{	if(a =='+')
  {
 	 x=pop(top);
 	 z=pop(top);
 	 y=x+z;
 	 
      push(top,y);
      i++;
  }
       else if(a =='-')
          {
 	 x=pop(top);
 	 z=pop(top);
 	 y=z-x;
 	 
    push(top,y);
    i++;
  }
        else if(a =='*')
 	 {
    x=pop(top);
 	 z=pop(top);
 	 y=x*z;
  
    push(top,y);
    i++;
  }
     else if(a =='/')
  {	x=pop(top);
 	 z=pop(top);
 	 y=x/z;
  
    push(top,y);
    i++;
	}}
     
    else
    {
  
     while(  (a >=0) ||(a<=9) )
  {
      if((a !=' '))
      {b[j]=a;
       j++;
    i++;}
 	 else 
 	 {	b[j]=NULL;
         token(y,b);
 	 push(top,y);
        strcpy(b," ");
     j=0;
 	 }
      
    
  }


    }
    
	}while(i<l+1); 
    
	return pop(top);
}
//-----------------fun token----------------
void token(int n, char b[4])
{
	char *y;

	y=new char;
	y=strtok(b,NULL);
	n=atoi(y);
;
}
//------------------------------------------------------
#5

للرفع لمن يستطيع المساعدة .

banner_60_468.gif

NOTHING IS IMPOSSIBLE

#6

السلام عليكم

الحقيقة حاولت مساعدتك ولكن باءت محاولتي بالفشل

ولكن فكر معي لماذا لاتقرأ حرف حرف حتى \n اي حتى يضغط enter واختبر الذي قرأته فأن كان عدا فانتظر واقرا واذا كان عدد فحاول تجميع هذا العدد أي قراءة وحدة عدد وان كان فراغ تجاهله واقرا الذي بعده وهكذا للوصول للعملية ثم التنفيذ:

دقيقة

هذا كود طلبمني مره في مادة مترجمات المهم حول المطلوب ولكن استخدمت static وليس dynimac ولاحظ الكود سهل بس مشكلتي مايقرا وحدة عدد مثل 15 بل يقرا 1 فقط

يمكن يفيدك

الكود

اللهم علمنا ما ينفعنا وأنفعنا بما علمتنا أنك أنت العليم الحكيم

سبحان الله وبحمده سبحان الله العظيم

لاحول ولاقوة الا بالله( كنز من كنوز الجنة ).

#7

البرنامج نسيت

اخذت التعديل وبحاول اساعدك بس هل استطيع الله اعلم.

StackExprtion.cpp

اللهم علمنا ما ينفعنا وأنفعنا بما علمتنا أنك أنت العليم الحكيم

سبحان الله وبحمده سبحان الله العظيم

لاحول ولاقوة الا بالله( كنز من كنوز الجنة ).

#8

اشكرك السهم الناري

لمحاولت مساعدتك للي

سوف اقراء برنامجك اذا انحلت مشكلتي ساخبرك

تقبل مني جزيل الشكر

لانك بالفعل بردك جعلتني امسك بخيط النجاة

فهذا البرنامج سوف اختبر فيه يوم الاحد

#9

السهم الناري قرات برنامج

ولكن حله مو ب linked list:(

انتظر محاولاتك يااااااااااااااارب ينحل معك

#10

السلام عليكم

صحيح البرنامج اللي اعطيتك ليس list ولكن حاولت اساعدك ولكن معليش فشلولكن ان شاء الله نحاول

اليك هذاه المحاولة حاول التعديل فيها يمكن تضبط معنا يالله نحاول ونحاول لاتيأس من رحمت الله.

الله يوفقك باختبارك

البرنامج بعد تعديل بسيط بس فيه مشكلة وين مادري:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <conio.h>
#include <cstdlib>
#include <ctype.h>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
struct NodeRec
{
int eval;
NodeRec* next;
};
void token(int n, char b[4]);
void push(NodeRec*top,int x);
int pop(NodeRec* top);
int result(char a[50]);
void main ()
{

	char a[50];
    int x;
	NodeRec * N;
//	N = new NodeRec;
//	N->eval = 10;
//	N->next = NULL;

	N = NULL;



 cout<<"enter expression in postfix but after number space:";
 char ch = getchar();

 while( ch != '\n' )
 {


	if( (ch == ' ')  )
	{
  ch = getchar();  
	}
  else if( (ch >= '0') && (ch <= '9') )
  {
     int s = 0;

   while( (ch >= '0') && (ch <= '9') /*(ch != ' ') && (ch != '\t') */ )
   {
    s +=  ( s*10 ) + ( ch-'0' );
    ch = getchar();
   }
    push(N ,s);
    ungetc(ch,stdin);	

  }
  else if(  (ch == '+') || (ch == '-') || (ch == '*') || (ch == '/') )
  {
   int a,b;

   a = pop(N);
   b = pop(N);

   switch(ch)
   {
   case '+':
     push(N,a+b);
    break;
   case '-':
     push(N,a-b);
    break;
   case '*':
     push(N,a*b);
    break;
   case '/':
    push(N,a/b);
    break;

   }
   
  }

  //ch = getchar();
 }
  //gets(a);
  //x=result(a);
  //cout<<endl<<"result= "<<x;

 cout << pop(N) << "\n\n\n";

getch();
}


//----------------fun puch---------------------------
void push(NodeRec* top,int x)
{ 
NodeRec* temp;
  temp=new NodeRec;
  temp->eval=x;
  temp->next=NULL;

if(top==NULL)
 top=temp;
else
{
 temp->next=top;
 top=temp;
}


}
//------------fun top-------------------------------
int pop(NodeRec* top)
{
int eval;
   NodeRec* curn;
curn=top;
if(top==NULL)
 cout<<"STACK IS EMPTY";
else
{
 eval=top->eval;
    top=top->next;
 delete curn;
}
return eval;

}
//--------------fun ans--------------------------
int result(char a[50])
{
char b[4];
int l,j=0,x,z,y,i=0;
   NodeRec* top;   
   top=NULL;
   

   
l=strlen(a);
do

{


if( (a !='+') || (a!='-') || (a!='*') || (a!='/'))
       
{ if(a =='+')
 {
  x=pop(top);
  z=pop(top);
  y=x+z;
  
     push(top,y);
     i++;
 }
      else if(a =='-')
         {
  x=pop(top);
  z=pop(top);
  y=z-x;
  
   push(top,y);
   i++;
 }
       else if(a =='*')
  {
   x=pop(top);
  z=pop(top);
  y=x*z;

   push(top,y);
   i++;
 }
    else if(a =='/')
 { x=pop(top);
  z=pop(top);
  y=x/z;

   push(top,y);
   i++;
}}
    
   else
   {

    while(  (a >=0) ||(a<=9) )
 {
     if((a !=' '))
     {b[j]=a;
      j++;
   i++;}
  else 
  { b[j]=NULL;
        token(y,b);
  push(top,y);
       strcpy(b," ");
    j=0;
  }
     
   
 }


   }
   
}while(i<l+1); 
   
return pop(top);
}
//-----------------fun token----------------
void token(int n, char b[4])
{
char *y;

y=new char;
y=strtok(b,NULL);
n=atoi(y);
;
}
//------------------------------------------------------

اللهم علمنا ما ينفعنا وأنفعنا بما علمتنا أنك أنت العليم الحكيم

سبحان الله وبحمده سبحان الله العظيم

لاحول ولاقوة الا بالله( كنز من كنوز الجنة ).

#11

مشكور

واشكر كل من حاول يساعدني لانه اختباري فيه بكره

وانا بحاول لانه جالس اسبوعين احاول احله

والله يعين

انتظرك يالسهم وانتظر

كل من يقدر يساعدني

#12

حليته بس ما يحسب الا one digit

فياريت تساعدوني لتحويله لاكثر من digit

#include <iostream>
#include <cstring>
#include <cstdio>
#include <conio.h>
#include <cstdlib>
#include <ctype.h>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
struct NodeRec
{
	char eval;
	NodeRec* next;
};
//void token(int n, char b[4]);
void push(NodeRec*& top,char);
char pop(NodeRec*& top);
int result(char a[50]);
void main ()
{
	char a[50];
    int x;
	int l,flag=0;

  cout<<"Enter the postfix :";
   gets(a);
   l=strlen(a);
	/*for(int n=0;n<=l;n++)
      {
      if(((a[n]>='0')&&(a[n]<='9'))||
    (a[n]=='-')||
    (a[n]=='=')||
    (a[n]=='+')||
    (a[n]=='*')||
    (a[n]=='/')||
    (a[n]==' '))
   continue;
  

      else
   {cout<<"\n";
   flag=1;
   cout<<"error";
   break;
   }
}*/
//if(flag!=1)
 x=result(a);
cout<<endl<<"result= "<<x;



	getch();
}

//----------------fun puch---------------------------
void push(NodeRec*& top,char x)
{ 
	NodeRec* temp;
   temp=new NodeRec;
   temp->eval=x;
   temp->next=NULL;

	if(top==NULL)

  top=temp;
	else
	{
  temp->next=top;
  top=temp;
	}


}
//------------fun top-------------------------------
char pop(NodeRec*& top)
{
	char eval;
    NodeRec* curn;
	curn=top;
	if(top==NULL)
  cout<<"STACK IS EMPTY";
	else
	{
  eval=top->eval;
     top=top->next;
  delete curn;
	}
	return eval;

}
//--------------fun ans--------------------------
int result(char a[50])
{
	char x,z;
	int l,y,i=0;
    NodeRec* top;

    
    top=NULL;
    

    
	l=strlen(a);

	for(i=0;i<=l;i++)

	{
  

   if(	(a =='+') || (a=='-') || (a=='*') || (a=='/'))
        
   {     x=pop(top);
 	 z=pop(top);
     if(a =='+')
     
 	 
 	 y=(x-'0')+(z-'0');
 	 
      
     
    
           else if(a =='-')
         
 	 
 	 y=(x-'0')-(z-'0');
 	 
     
     
   
    
            else if(a =='*')
  
 	 
    y=(x-'0')*(z-'0');
 	 
   
    
  
        else if(a =='/')
     
 	 
 	 y=(x-'0')/(z-'0');
 	 
      
    
     push(top,(y+48));
   }
   
     
    else
    

     push(top,a);}
    
    
	return y;
}
#13

السلام عليكم

الحمد لله الذي علمنا مالم نكن نعلم

أبشر أخي mrs.n لقد وجدت الحل أخيرا ولكن حقيقة شكلي تأخرت عليك بس لم أفكر به الايوم الثلاثاء وقد كتبت الحل ووضعته مرفقا هنا وهو بلغة السي غيره الى لغة ++c وانت تعرف ذلك بدل free delete وبدل malloc new

هذه المشاركة في يوم الثلاثاء المبارك من رمضان تاريخ 19 / 9 /1425

وبهذه الليلة فجعت بخبر وفاة الامير زايد فرحمه الله رحمة واسعة وأعزي كافة الشعب الاماراتي الشقيق.

الكود

لاتنسني من دعائك الصلاح وحقيقة لولا الشغل اللي اشغلني لساعدتكقبل يوم الاحد

اللهم علمنا ما ينفعنا وأنفعنا بما علمتنا أنك أنت العليم الحكيم

سبحان الله وبحمده سبحان الله العظيم

لاحول ولاقوة الا بالله( كنز من كنوز الجنة ).

#14

معليش البرنامج هو:

Linkedlistwithpostifix.cpp

اللهم علمنا ما ينفعنا وأنفعنا بما علمتنا أنك أنت العليم الحكيم

سبحان الله وبحمده سبحان الله العظيم

لاحول ولاقوة الا بالله( كنز من كنوز الجنة ).

#15

السلام عليكم الكود السابق به خطأ بسيطوعدلته واليك الكود بعد التعديل .

LINKED_LIST_STAK_POSTIFEX.cpp

اللهم علمنا ما ينفعنا وأنفعنا بما علمتنا أنك أنت العليم الحكيم

سبحان الله وبحمده سبحان الله العظيم

لاحول ولاقوة الا بالله( كنز من كنوز الجنة ).

#16

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

مشكور يالسهم

بصراحه تعتبتك معي الاختبار يوم الاحد اللغيته واختبارتنا في شي ثاني لانه للاسف مافي احد عرف يحله صح

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

والله يوفقك دنيا واخره

#17

السلام عليكم

اخي العزيز mrs.n هلابك .

بالتوفيق باختبارك .

الكود موجود وصحيح انا جربته حاول التأكد منه فالحل أن شاء الله يأخذ عدد كامل ويقرأه ويعمل له push & pop بشكل صحيح ولاحظ التغييرات .

جرب الكود ورد علي اذا فيه خطأ .

اللهم علمنا ما ينفعنا وأنفعنا بما علمتنا أنك أنت العليم الحكيم

سبحان الله وبحمده سبحان الله العظيم

لاحول ولاقوة الا بالله( كنز من كنوز الجنة ).

هذا الموضوع مغلق.

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