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

مشكلة التحويل من postfix to infix

بدأه m1m6 في 21 ديسمبر 2012 · 3 رد · 1,551 مشاهدة · في ارشيف قسم C/C++
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

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

المشكلة في حساب الناتج وايضا بعض التعابير لا يحسبها بشكل صحيح

مثلا التعبير التالي:

c*(a+b)

يحسبه بشكل صحيح

اما التعبير

A*(B*C+D) +E

لا يحسبه بشكل صحيح

كود البرنامج

// convert.h
const char SIZE=100;

class stack
{
public:
	stack();
	bool isempty()const;
	bool isfull()const;
	void push(char num);
	void pop(char &elm);
	void cntres();
	int topnum;
	void print();
	void result(stack &post);
private:
	char data[SIZE];
	int top;
};

// convert.cpp
#include<iostream>
#include"convert.h"
#include<cstddef>
#include<cstring>

using namespace std;

stack::stack()
{
	top=-1;
	topnum=top;
}

bool stack::isempty()const
{
	return top==-1;

}

bool stack::isfull()const
{
	return top==SIZE-1;
}

void stack::push(char elm)
{
if(isfull())
	cout<<"Cannot Add New Item , No Free Space ."<<endl;
else
	{
		top++;
		data[top]=elm;
		topnum=top;
	}

}
void stack::pop(char &elm)
{
	if(isempty())
		cout<<"There's No Data ."<<endl;
	else
	{
		topnum=top;
		elm=data[top];
		top--;

	}
}


void stack::print()
{	int i;
	cout<<"PostFix Formula : ";
	for(i=0;i<=top;i++)
	cout<<data;
}

void stack::result(stack &post)
{
	int i;
	int a,b,c;
	char oper;
	int res=0;
	int loop=strlen(post.data);
	for(i=0;i<=loop;i++)
{
	if(post.data=='+'||post.data=='-'||post.data=='*'||post.data=='/')
	{
		pop(oper);
		a=oper-48;
		pop(oper);
		b=oper-48;
		if (post.data=='+')
			c=a+b;
		else if (post.data=='-')
			c=a-b;
		else if (post.data=='*')
			c=a*b;
		else c=a/b;

		oper=c+48;
		push(oper);

	}
	push(post.data);

}
pop(oper);
res=oper-48;
cout<<res<<endl;

}

// main
#include<iostream>
#include"convert.h"
#include<cstring>

using namespace std;

int main()
{
	stack op;
	stack post;
	stack res;
	char infex[SIZE];
	cout<<"Please Enter Infex Formula :";
	cin.get(infex,SIZE);
	char OpValue;
	char ch;
	int lenght;
	lenght=strlen(infex);
	for(int i=0;i<lenght;i++)
	{
		if(infex=='+'||infex=='-'||infex=='*'||infex=='/'||infex=='('||infex==')')
		{
			if(infex=='*'||infex=='/'||infex=='(')
				op.push(infex);
			else if(infex=='+'||infex=='-')
			{
				if(op.topnum=='*'||op.topnum=='/')
				{
					op.pop(ch);
					OpValue=ch;
					while(ch!='('&& !op.isempty())
					{
						post.push(ch);
						op.pop(ch);

					}
					op.push(infex);
				}
				else
					op.push(infex);
				}
			else if(infex==')')
			{
				op.pop(ch);
				OpValue=ch;
				while(OpValue!='(')
				{
					post.push(OpValue);
					op.pop(ch);
					OpValue=ch;
				}

			}


			}
		else
			post.push(infex);

		}
	while(!op.isempty())
	{
		op.pop(ch);
		OpValue=ch;
		post.push(OpValue);	
	}

	post.print();
	cout<<endl;

	cout<<"RESULT OF INFIX : ";
	res.result(post);

system("pause");
return 0;
}

اتوقع المتغير topnum ممكن سبب المشكلة

حاولت احله لكن ما قدرت .....

تم تعديل هذه المشاركة بواسطة C++er في 21 ديسمبر 2012 في 01:51

#2

السلام عليك....

أخي أقرأ هذا البحث الرائع الى ان اجد الوقت الكافي لنناقش مشكتك سويا: لتحويل التعبيرات من Postfix الى Infix

وفقك الله

تم تعديل هذه المشاركة بواسطة محمد عودة في 22 ديسمبر 2012 في 06:36

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

#3

اخي المشكلة فقط في حساب الناتج

التحويلات يحولها بشكل صحيح

لكن الناتج خاطيء

#4

حد يساعدني يا جماعة

جربت اكثر من مرة اغير فيه وكل مرة يعطين الناتج خطا .....

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