Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 12.05.2014, 13:15   #1
Yernar_YNWA
Новичок
Джуниор
 
Регистрация: 12.05.2014
Сообщений: 1
По умолчанию Ошибка в коде.help

Код:
//Infix to postfix
//paranthesized

#include <iostream.h>
#include <stdlib.h>  //for exit()
#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <windows.h>
#define row 7
#define col 5


class revpol{
      int top,rank;
      char *stack_arr,*polish,*infix;
      public:
      int f(char);  //Input precedence function
      int g(char);  //Stack precedence function
      int r(char);  //rank function
      void push(char);
      char pop();
      char *concat(char *,char);
      void suffix();//infix to suffix conversion function
      void printStack();
      void printSuffix();
      void printRank();
      void printInfix(char);
};

void revpol :: printInfix(char ch){
    static int y=row,x=col;
 gotoxy(x,y++);
    cout<<ch;
}

void revpol :: printStack(){
     static int x=col,y=row;
     gotoxy(x+10,y++);
     cout<<stack_arr;
}

void revpol :: printSuffix(){
     static int x=col,y=row;
     gotoxy(x+30,y++);
     cout<<polish;
}

void revpol :: printRank(){
     static int x=col,y=row;
     gotoxy(x+50,y++);
     cout<<rank;
}

int revpol :: f(char inputchar){
     if(inputchar==')')
	  return(0);
     else if(inputchar=='+' || inputchar=='-')
	  return(1);
     else if(inputchar=='*' || inputchar=='/')
	  return(3);
     else if(inputchar=='^')
	  return(6);
     else if(inputchar>='a' || inputchar<='z')
	  return(7);
     else if(inputchar>='A' || inputchar<='Z')
	  return(7);
     else if(inputchar=='(')
	  return(9);
     else
	  return(-99);  //for error
}

int revpol :: g(char inputchar){
     if(inputchar=='(')
	  return(0);
     else if(inputchar=='+' || inputchar=='-')
	  return(2);
     else if(inputchar=='*' || inputchar=='/')
	  return(4);
     else if(inputchar=='^')
	  return(5);
     else if(inputchar>='a' || inputchar<='z')
	  return(8);
     else if(inputchar>='A' || inputchar<='Z')
	  return(8);
     else
	  return(-99);  //for error
}


int revpol :: r(char inputchar){
     if(inputchar=='+' || inputchar=='-' || inputchar=='*' || inputchar=='/'|| inputchar=='^')
	  return(-1);
     else if(inputchar>='a' || inputchar<='z')
	  return(1);
     else if(inputchar>='A' || inputchar<='Z')
	  return(1);
     else
	  return(-99);  //for error
}


void revpol :: push(char charin){
     top++;
     stack_arr[top]=charin;
     stack_arr[top+1]='\0';
}

char revpol :: pop(){
     char popdata;
     popdata=stack_arr[top];
     stack_arr[top]='\0';
     top--;
     return(popdata);
}

char * revpol :: concat(char *polish,char temp){
     int i;
     for(i=0;polish[i]!='\0';i++);
     polish[i]=temp;
     polish[i+1]='\0';
     return(polish);
}

void revpol :: suffix(){
     int i;
     char next,temp;

     initialize the stack
     top=0;
     stack_arr[top]='(';

     Intialize output string and rank count
     polish="";
     rank=0;

       gotoxy(1,1);
       textcolor(LIGHTMAGENTA);
     cprintf("*****INFIX TO POSTFIX (Paranthesized)*****\r\n");
     textcolor(WHITE);
     cprintf("Enter Infix string : ");
     cin>>infix;
     infix=concat(infix,')');

     textcolor(LIGHTBLUE);
     gotoxy(2,5);
     cprintf("INFIX");
     gotoxy(15,5);
     cprintf("STACK");
     gotoxy(35,5);
     cprintf("SUFFIX");
     gotoxy(53,5);
     cprintf("RANK");

     gotoxy(15,6);
     cout<<"(";
     cout<<endl;


     Remove symbols with greater precedence from stack
     for(i=0;infix[i]!='\0';i++){
	 
		 obtain next input symbol
	 next=infix[i];
	 cout<<endl;
	 printInfix(next);

	 if(top<0){
	    cout<<"\n\n\tInvali Infix";
	    getch();
	    exit(1);
	 }
	 while(f(next) < g(stack_arr[top])){
	       temp=pop();
	       polish=concat(polish,temp);
	       rank = rank + r(temp);
	       if(rank<1){
		  cout<<"\n\n\tInvalid Infix";
		  getch();
		  exit(1);
	       }
	 }
	 //Are there matching parentheses?
	 if(f(next) != g(stack_arr[top]))
	     push(next);
	 else
	     temp=pop();  //here value in temp is of no use
	 delay(200);
	 printStack();
	 printSuffix();
	 printRank();
     }


      //Check whether expression is invalid
	  if(rank!=1 || top!=-1){
	     textcolor(RED + BLINK);
	     cprintf("\r\n\r\nInvalid Infix");
	  }
	  else{
	    textcolor(LIGHTGREEN + BLINK);
	    cprintf("\r\n\r\nvalid Infix");
	    textcolor(YELLOW);
	    cprintf("\r\nSuffix : %s",polish);
	  }
	  _setcursortype(_NOCURSOR);
}

void main(){
    
     revpol obj;
     obj.suffix();
     getch();
}

Последний раз редактировалось Stilet; 12.05.2014 в 13:22.
Yernar_YNWA вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Ошибка в коде shadezx Помощь студентам 9 04.12.2013 10:03
Ошибка в коде Dady 1992 Общие вопросы C/C++ 3 25.06.2012 15:38
Где ошибка в этом исходном коде на языке Си? Или ошибка в Excel? ArchiCurtis Помощь студентам 2 07.04.2012 14:16
Ошибка в коде zhilkinson Visual C++ 3 11.02.2012 18:41
Ошибка в коде, ошибка в css или это проблема с совместимостью с браузерами? ankris HTML и CSS 5 23.11.2010 16:58