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

مساعدة في الكسور والأعداد السالبة في التحويل من infix الى postfix

بدأه Deadly Arrow في 2 يوليو 2010 · 1 رد · 1,196 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

جمعة مباركة

طلب مننا في الجامعة تصميم آلة حاسبة علمية .. وبعد البحث والمحاولة أتضح لي أنه أفضل طريقة لعملها هو عبر الـ Stack

استطعت التحصل على خوارزمية للتحويل الى postfix الى انه كان ينقصها مراعاة الأعداد السالبة والكسور ..

بعد التلاعب في الشفيرة المصدرية والمحاولة ، استطعت احتواء الكسور عبر حصر الرقم بين []

مثلا عند تحويل

3*5.99

تظهر [3][5.99]*

لكن المشكلة الحقيقة التي تواجهني ولم أستطع أن أجد لها حلاً ، هي في حالة تكرار الإشارة في العدد ومراعاة الأعداد السالبة

فمثلا

---5+-+3

لم أستطع تحويلها .. بصورة صحيحة

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

عموماً هذا هو الكود

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define MAX 20
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

// a code that takes an string like
// (3*5)-11*2-11
// and converts it into
//('3'*'5')-'11'*'2'-'11'

// to discover a number
// it's next either by
//)
// +,-,%,*,^
// \0
// it's previous either by
//a[0]
// +,-,%,*,^
// (

// if it imposes both conditions it should be a number

// for new operation make new file

char s[20]; //holds digits for more than 1 space
char all[100]; // taken expressions
char transformed [150];

void append(char* s, char c);





int extractdigit(char str[],int location);

void transform(char str[])
{


          char news[1000];
int i=0;

    while (str!='\0')
    {

if (isdigit(str))
{

    i+=extractdigit(str,i);
    continue;

}
{
printf("%c\n",str);

append(all,str);

i++;

     }
    }


}



void append(char* s, char c)
{
        int len = strlen(s);
        s[len] = c;
        s[len+1] = '\0';
}




int extractdigit(char str[],int location)
{
// tf9el al digits
// dertmines where does the digits start from & return them in
// global arrays s
 //s[10]=0;



        int i=location;

    int x=0;
    printf("\'");
    append(all,'[');
//   all = strcat( all, "_" );
    while(isdigit(str)||str=='.')

    {
      s[x]=str;
  printf("%c",s[x]);
//  addtostring(all,s[x]);
append(all,s[x]);
//all = strcat( all, s[x] );
    i++;
      x++;

    }
//addtostring(all,'_');
append(all,']');
printf("\'\n");

return x;

}



char stack[MAX];

int top=-1;

char pop();

void push(char item);

int prcd(char symbol)

{

switch(symbol)

{

case '+':

case '-':return 2;

break;

case '*':

case '/':return 4;

break;

case '^':

case '$':return 6;

break;

case '(':

case ')':

case '#':return 1;

break;

}

}

int isoperator(char symbol)

{

switch(symbol)

{

case '+':

case '-':

case '*':

case '/':

case '^':

case '$':

case '(':

case ')':return 1;

break;

default:return 0;

}

}

void convertip(char infix[],char postfix[])

{

int i,symbol,j=0;

stack[++top]='#';

for(i=0;i<strlen(infix);i++) //for all array

{

symbol=infix; // to test the symbol

if(isoperator(symbol)==0)

{

postfix[j]=symbol;

j++;

}

else{

if(symbol=='(')push(symbol);

else if(symbol==')')

{

while(stack[top]!='(')

{

postfix[j]=pop();

j++;

}

pop();//pop out (.

}

else{

if(prcd(symbol)>prcd(stack[top]))

push(symbol);

else{

while(prcd(symbol)<=prcd(stack[top]))

{

postfix[j]=pop();

j++;

}

push(symbol);

}//end of else.

}//end of else.

}//end of else.

}//end of for.

while(stack[top]!='#')

{

postfix[j]=pop();

j++;

}

postfix[j]='\0';//null terminate string.

}

int main()

{

char infix[20],postfix[20];

//clrscr();
printf("Enter the valid infix string:\n");

gets(infix);
transform(infix);

convertip(all,postfix);

printf("The corresponding postfix string is:\n");

puts(postfix);

getch();

}

void push(char item)

{

top++;

stack[top]=item;

}

char pop()

{

char a;

a=stack[top];

top--;

return a;

}
#2

راجع كتاب Beginning Visual C++ 2008 الفصل السادس جزء Implementing a Calculator، فى نسخة الـ PDF ستجدها فى الصفحه 335.

و الله ولى التوفيق

مدونتي: C++ Tips and Tricks

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…