Infix Conversion Help

Can't import the contents of the textfile to array

Page 1 of 1

5 Replies - 693 Views - Last Post: 22 September 2009 - 11:48 PM Rate Topic: -----

#1 mister1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-September 09

Infix Conversion Help

Post icon  Posted 22 September 2009 - 06:24 PM

Ok, I have to logic down but, I can't figure how to read directly from the text file to the array. The only way it will work is to get the user to type it in. I've been working at this for hours, all I'm getting is brick walls. Any help is greatly appreciated.
// inFix to post fix converter

//This does it correctly but, the User must type in the statement but they are given what to type.

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<vector>
#include<string.h>
#include <stdlib.h>
#include <stdio.h>
#include<stdlib.h>
#include<fstream>



const int size =50;
char infix[size],postfix[size],stack[size];
int top=-1;

int precedence(char ch);   //  get the precedence of the operator
char pop();  // pop an element from the stack
char topelement();  // returns the top element of the stack
void push(char ch);  // pushes an element into the stack

using namespace std;

int main()
{
	
CString readLine("C:\\InFix.txt");

   ifstream inFile("C:\\InFix.txt");
fstream File_op("C:\\InFix.txt");
   fstream Fire_op("C:\\InFix.txt");
 
   
	
	char bh;
	 cout << "Please type The following statement";
	 cout << "\n";
	 
	while (!File_op.eof())
	{
		  //if the statement ends in a charachter and not a blankspace it will print the last characther twice. I am assuming that the last char
		  // will be a blank space.
		  File_op.get(bh);
		 
			cout<<bh;
			Fire_op << bh;

 
		
		  }
					   
			  cout << "\n";
			 cout << "THis will convert what is in InFix.txt to Post Fix";
			 cout << "\n";
	
		  
	ofstream OUTS;
	
	//OUTS.open("C:\\outputs.txt");
	
	
	 char ele,elem,st[2];
	 int prep,pre,popped,j=0,chk=0;
	 strcpy(postfix," ");
	 
	 //cstring
 
//Gets the inFIx typed but this is acutally getting written to the textfile not what you wrote.
	gets(infix);
   
	 for(int i=0;infix[i]!=0;i++)
		  {
		  if(infix[i]!='('&&infix[i]!=')'&&infix[i]!='^'&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&infix[i]!='-')	 
				postfix[j++]=infix[i];
			  else if(infix[i]=='(')
				 {
						 elem=infix[i];
						 push(elem);
					  }
				  else if(infix[i]==')')
					  {
						 while((popped=pop()) != '(')
							 postfix[j++]=popped;
					  }
				  else
					  {
						 elem=infix[i];
						 //stores precedence of operator from infix
						 pre=precedence(elem);
						 ele=topelement();
						 //stores precedence of operator at top stack
						 prep=precedence(ele);
						
						 if(pre > prep)
						   push(elem);										 
						   
						 else
						   {
	while(prep >= pre)
								  {
					   if(ele=='#')
						   break;
				 popped=pop();
				 ele=topelement();
				 postfix[j++]=popped;
				 prep=precedence(ele);
								   }
			 push(elem);
							}
						 }
			 } 
			 
		  while((popped=pop())!='#')
		 postfix[j++]=popped;
		  postfix[j]='\0';
		  
		  
		 cout<<"\n post fix  Version :"<<postfix<<endl;
	  
		  //was able to write to a file but, only overwrote the exisiting file
		  // inFile << "testing! \n";
		   inFile.close();
		   Fire_op.close();
			File_op.close();
		   system("pause");
		   return 0;
}

int precedence(char ch)
{
	   switch(ch)
		  {
			   case '^' : return 5;
			   case '/' : return 4;
			   case '*' : return 4;											
			   case '+' : return 3;
			   case '-' : return 3;
			   case '=' : return 2;			  
			   default  : return 0;
		  }
		  
}

char pop()	  
//pop the element from the stack			
{
	 char ret;
	 if(top!=-1)
	   {  ret =stack[top];
		  top--;
		  return ret;
	   }
	 else
		return '#';
}
						 
char topelement()		
 // return top element from the stack without popping 
{	 
	  char ch;
	  if(top!=-1)
		ch=stack[top];
	  else
		 ch='#';
	   return ch;
}

void push(char ch)		 
 // push an element in the stack
{
	 if(top!=size-1)
		 {
			top++;
			stack[top]= ch;
		 }
}		 
								



Is This A Good Question/Topic? 0
  • +

Replies To: Infix Conversion Help

#2 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Infix Conversion Help

Posted 22 September 2009 - 06:32 PM

You want to get the lines into an array?
getline(variable, sizeof(file to be read));

Ok basicly what you do is getting line by line all the file into a string or array.
After this you could go and break it down while you are still in the loop. Save it into other different array or strings.

Now on this part
   ifstream inFile("C:\\InFix.txt");
fstream File_op("C:\\InFix.txt");//this you dont need it.
   fstream Fire_op("C:\\InFix.txt");//this you dont need it.
 
   

ifstream will open the file to read.
ofstream will open the file to write.
Another way would be
fstream inFile, outFile;
inFile.open(textfile, ios::in);//to read
outFile.open(textfile, ios::out);//to write.


Just an idea hope that helps.
Was This Post Helpful? 1
  • +
  • -

#3 mister1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-September 09

Re: Infix Conversion Help

Posted 22 September 2009 - 06:44 PM

const int size =50;
char infix[size],postfix[size],stack[size];
int top=-1;

int precedence(char ch);   //  get the precedence of the operator
char pop();  // pop an element from the stack
char topelement();  // returns the top element of the stack
void push(char ch);  // pushes an element into the stack

using namespace std;

int main()
{
	


   ifstream inFile("C:\\InFix.txt");
  // fstream File_op("C:\\InFix.txt");
  // fstream Fire_op("C:\\InFix.txt");
 char thing[50];
   
	getline(thing, sizeof("C:\\InFix.txt"));


better yet, any idea on how to copy the contents of my text file to the array infix that I already have declaired?
Was This Post Helpful? 0
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Infix Conversion Help

Posted 22 September 2009 - 06:59 PM

You can change the sizeof for [code] sizeof(inFile)[code]
change the thingfor infix in the getline.

Remember that to read all the file you still need your while.

And that inside that while you could use different whiles to search for specific words or some kind of separation :P
Was This Post Helpful? 0
  • +
  • -

#5 mister1337  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-September 09

Re: Infix Conversion Help

Posted 22 September 2009 - 07:06 PM

I'm totally confused now. O_o
Was This Post Helpful? 0
  • +
  • -

#6 Guest_c.user*


Reputation:

Re: Infix Conversion Help

Posted 22 September 2009 - 11:48 PM

inFile.getline(thing, sizeof thing);

one more way

	char arr[50], *p;
	char ch;

	p = arr;
	while (inFile.get(ch)
		&& p - arr < sizeof arr - 1) {
		*p++ = ch;
		*p = '\0';
	}


Was This Post Helpful? 0

Page 1 of 1