perkeyone
something clever
- Reaction score
- 71
edit: solved
void children(int k)
{
if(lcrc[0][k]!="+"||lcrc[0][k]!="-"||lcrc[0][k]!="*"||lcrc[0][k]!="/")
{
lcrc[1][k]=n;
n++;
children(lcrc[[1][int k]-"0");
lcrc[[2][k]=n;
n++;
children(lcrc[[2][k]-"0");
}
else
{
lcrc[1][k]=0;
lcrc[[2][k]=0;
}
}
the reason i didnt give examples of what i am getting is because i couldnt even get it to compile.
char lcrc[3][9], n="1";
#include <iostream.h>
int test = 5; //global
int main()
{
int other = 10 // local
std::cout<<test<<" "<<other;
return 0;
}
char lcrc[3][9], n="1"; //this is my 2d array where all the magic happens
children(lcrc[[2][k]-"0");
//Calvin Perkey
//OLA2
//October 19 2009
#include <iostream>
#include <string>
using namespace std;
char lcrc[3][9]; //this is my 2d array where all the magic happens
int n=1; //this is an index used in the children function
void children(int k)
{ //this function is supposed to fill up the 2nd and 3rd columns of my array
//first it checks if the first column contains an operator character
if(lcrc[0][k]!="+"||lcrc[0][k]!="-"||lcrc[0][k]!="*"||lcrc[0][k]!="/")
{ //if it is an operator then there must be operands, so is puts the next index into left child
//the next index starts as "1" but is increased each time it gets used
//the index is also a character since it is being put in a char array but will never be greater than 9
//since my teacher specified that she would not be using more than 9 characters for input
lcrc[1][k]=itoa(n); //first it sets the left child index
n++;
children(atoi(lcrc[1][k])); //here the function calls itself on the right child of the current index
lcrc[2][k]=n; //the it sets the right child index
n++;
children(atoi(lcrc[1][k])); //here the function calls itself on the right child this time
}
else //if it was not an operation character then there are no children
//so we set them to the character "0"
{
lcrc[1][k]="0";
lcrc[2][k]="0";
}
}
void postfix(int k)
{ //after all the data is put into the array this function outputs the data in postfix order
if(lcrc[1][k]!="0") //if the index has a right child the function calls itself on the right child
{
postfix(atoi(lcrc[1][k]));
}
if(lcrc[2][k]!="0") //if the index has a left child it calls itself on the left child
{
postfix(atoi(lcrc[1][k]));
}
cout<<lcrc[0][k]; //finally after testing both left and right, it outputs the character stored at the index
}
void infix(int k)
{ //this function works similar to the postfix but goes in a different order and adds parenthses
cout<<"(";
if(lcrc[1][k]!="0") //it calls itself for the left child
{
postfix(atoi(lcrc[1][k]));
}
cout<<")"<<lcrc[0][k]<<"("; //it outputs the character at the index
if(lcrc[2][k]!="0")
{
postfix(atoi(lcrc[1][k])); //then it calls itself for the right child
}
cout<<")";
}
int main()
{
cout<<"Input prefix expression.\n"; //promts for input
for(int i=0;i<9;i++)
{
cin.get()>>lcrc[0][i]; //put each character of the input into the first column of the 2d array
}
children(0); //calls the children function which i described above
cout<<"The expression tree is\n"; //this outputs all the data in the array
for(int i=0;i<9;i++)
{
for(int j=0;j<3;j++)
{
cout<<lcrc[j][i]<<" ";
}
cout<<"\n";
}
cout<<"The postfix expression is ";
postfix(0); //this calls the postfix function i described above
cout<<"\nThe infix expression is ";
infix(0); //this calls the infix functionion from above
return 0;
}
//Calvin Perkey
//OLA2
//October 19 2009
#include <iostream>
#include <string>
using namespace std;
char lcrc[3][9],n='1'; //this is my 2d array where all the magic happens
void children(int k)
{ //this function is supposed to fill up the 2nd and 3rd columns of my array
//first it checks if the first column contains an operator character
if(lcrc[0][k]=='+'||lcrc[0][k]=='-'||lcrc[0][k]=='*'||lcrc[0][k]=='/')
{ //if it is an operator then there must be operands, so is puts the next index into left child
//the next index starts as "1" but is increased each time it gets used
//the index is also a character since it is being put in a char array but will never be greater than 9
//since my teacher specified that she would not be using more than 9 characters for input
lcrc[1][k]=n; //first it sets the left child index
n++;
children(lcrc[1][k]-'0'); //here the function calls itself on the right child of the current index
lcrc[2][k]=n; //the it sets the right child index
n++;
children(lcrc[2][k]-'0'); //here the function calls itself on the right child this time
}
else //if it was not an operation character then there are no children
//so we set them to the character '0'
{
lcrc[1][k]='0';
lcrc[2][k]='0';
}
}
void postfix(int k)
{ //after all the data is put into the array this function outputs the data in postfix order
if(lcrc[1][k]!='0') //if the index has a right child the function calls itself on the right child
{
postfix(lcrc[1][k]-'0');
}
if(lcrc[2][k]!='0') //if the index has a left child it calls itself on the left child
{
postfix(lcrc[2][k]-'0');
}
cout<<lcrc[0][k]; //finally after testing both left and right, it outputs the character stored at the index
}
void infix(int k)
{ //this function works similar to the postfix but goes in a different order and adds parenthses
cout<<'(';
if(lcrc[1][k]!='0') //it calls itself for the left child
{
infix(lcrc[1][k]-'0');
}
cout<<')'<<lcrc[0][k]<<'('; //it outputs the character at the index
if(lcrc[2][k]!='0')
{
infix(lcrc[2][k]-'0'); //then it calls itself for the right child
}
cout<<')';
}
int main()
{
string m;
cout<<"Input prefix expression.\n"; //promts for input
cin>>m;
for(int i=0;i<m.size();i++)
{
m[i]>>lcrc[0][i]; //put each character of the input into the first column of the 2d array
}
children(0); //calls the children function which i described above
cout<<"The expression tree is\n"; //this outputs all the data in the array
for(int i=0;i<m.size();i++)
{
for(int j=0;j<3;j++)
{
cout<<lcrc[j][i]<<" ";
}
cout<<"\n";
}
cout<<"The postfix expression is ";
postfix(0); //this calls the postfix function i described above
cout<<"\nThe infix expression is ";
infix(0); //this calls the infix functionion from above
cin>>lcrc[0][0];
return 0;
}