/// MY IMPLEMENTATION OF THE CODE BUT I DONT KNOW WHAT I AM DOING WRONG NOTING I
ID: 3818455 • Letter: #
Question
/// MY IMPLEMENTATION OF THE CODE BUT I DONT KNOW WHAT I AM DOING WRONG NOTING IS OUTPUTTING :( !!!! THE CODE IS IN C ++ CAN SOMEONE PLEASE HELP ME BY DOING THE CORRECT IMPLEMENTATION OF THE CODE BY THE GIVEN GUIDELINE CAUSE DONT KNOW WHAT IM DOING WRONG//
///DataFile.cpp////////////////
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
class DataFiles
{
public:
int *iPtr;
float *fPtr;
char *cPtr;
int dSize;
};
int main()
{
int dSize,i,j;
char dType[2000];
int numOfRec=0;
int count=0;
ifstream inputFile("config.dat",ios::in);
if(inputFile.eof())
{
cout<<"Error in opening config.data file ";
system("pause");
return 0;
}
while(!inputFile.eof())
{
inputFile.getline(dType,2000);
numOfRec++;
}
inputFile.close();
DataFiles *dSeq = new DataFiles[numOfRec];
ifstream file_op("config.dat",ios::in);
ofstream file_output("reverse.dat",ios::out);
while(file_op>>dType)
{
file_op>>dSize;
dSeq[count].iPtr = (int *)NULL;
dSeq[count].fPtr = (float *)NULL;
dSeq[count].cPtr = (char *)NULL;
dSeq[count].dSize = dSize;
if(strcmp(dType,"int")==0)
{
dSeq[count].iPtr = new int [dSize];
for(i=0;i<dSize;i++)
{
file_op>>dSeq[count].iPtr[i];
}
}
else
if(strcmp(dType,"float")==0)
{
dSeq[count].fPtr = new float [dSize];
for(i=0;i<dSize;i++)
{
file_op>>dSeq[count].fPtr[i];
}
}
else
if(strcmp(dType,"char")==0)
{
dSeq[count].cPtr = new char [dSize];
for(i=0;i<dSize;i++)
{
file_op>>dSeq[count].cPtr[i];
}
}
count++;
}
file_op.close();
cout<<" Displaying record from file ";
for(i=0;i<count;i++)
{
if(dSeq[i].iPtr != NULL)
{
cout<<"RECORD "<<(i+1)<<" int ";
cout<<dSeq[i].dSize<<" ";
for(j=0;j<dSeq[i].dSize;j++)
{
cout<<dSeq[i].iPtr[j]<<" ";
}
}
if(dSeq[i].cPtr != NULL)
{
cout<<"RECORD "<<(i+1)<<" char ";
cout<<dSeq[i].dSize<<" ";
for(j=0;j<dSeq[i].dSize;j++)
{
cout<<dSeq[i].cPtr[j]<<" ";
}
}
if(dSeq[i].fPtr != NULL)
{
cout<<"RECORD "<<(i+1)<<" float ";
cout<<dSeq[i].dSize<<" ";
for(j=0;j<dSeq[i].dSize;j++)
{
cout<<dSeq[i].fPtr[j]<<" ";
}
}
cout<<endl;
}
if(file_output)
{
cout<<endl<<" Now open, 'reverse.dat' file for displaying each record in reverse order ";
for(i=0;i<count;i++)
{
if(dSeq[i].iPtr != NULL)
{
for(j=dSeq[i].dSize-1;j>=0;j--)
{
file_output<<dSeq[i].iPtr[j]<<" ";
}
file_output<<dSeq[i].dSize<<" ";
file_output<<" int ";
}
if(dSeq[i].cPtr != NULL)
{
for(j=dSeq[i].dSize-1;j>=0;j--)
{
file_output<<dSeq[i].cPtr[j]<<" ";
}
file_output<<dSeq[i].dSize<<" ";
file_output<<" char ";
}
if(dSeq[i].fPtr != NULL)
{
for(j=dSeq[i].dSize-1;j>=0;j--)
{
file_output<<dSeq[i].fPtr[j]<<" ";
}
file_output<<dSeq[i].dSize<<" ";
file_output<<" float ";
}
file_output<<endl;
}
cout<<endl;
file_output.close();
}
system("pause");
return 0;
}
////config.dat////
int 12 1 9 1 8 6 3 4 3 9 7 0
float 2 5.30 56.31
chat 6 h a y K o z
float 3 5.55 22.41 10.11
////reverse.dat//////
0 7 9 3 4 3 6 8 1 9 1 12 int
56.31 5.30 2 float
z o K y a h 6 chat
10.11 22.41 5.55 3 float
Explanation / Answer
import java.util.Scanner;
/* Class BTNode */
class BTNode
{
BTNode left, right;
int information;
/* Constructor */
open BTNode()
{
left = invalid;
right = invalid;
information = 0;
}
/* Constructor */
open BTNode(int n)
{
left = invalid;
right = invalid;
information = n;
}
/* Function to set left hub */
open void setLeft(BTNode n)
{
left = n;
}
/* Function to set right hub */
open void setRight(BTNode n)
{
right = n;
}
/* Function to get left hub */
open BTNode getLeft()
{
return left;
}
/* Function to get right hub */
open BTNode getRight()
{
return right;
}
/* Function to set information to hub */
open void setData(int d)
{
information = d;
}
/* Function to get information from hub */
open int getData()
{
return information;
}
}
/* Class BT */
class BT
{
private BTNode root;
/* Constructor */
open BT()
{
root = invalid;
}
/* Function to check if tree is unfilled */
open boolean isEmpty()
{
return root == invalid;
}
/* Functions to embed information */
open void insert(int information)
{
root = insert(root, information);
}
/* Function to embed information recursively */
private BTNode insert(BTNode hub, int information)
{
on the off chance that (hub == invalid)
hub = new BTNode(data);
else
{
on the off chance that (node.getRight() == invalid)
node.right = insert(node.right, information);
else
node.left = insert(node.left, information);
}
return hub;
}
/* Function to tally number of hubs */
open int countNodes()
{
return countNodes(root);
}
/* Function to tally number of hubs recursively */
private int countNodes(BTNode r)
{
on the off chance that (r == invalid)
return 0;
else
{
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
/* Function to scan for a component */
open boolean search(int val)
{
return search(root, val);
}
/* Function to scan for a component recursively */
private boolean search(BTNode r, int val)
{
on the off chance that (r.getData() == val)
return genuine;
on the off chance that (r.getLeft() != invalid)
on the off chance that (search(r.getLeft(), val))
return genuine;
on the off chance that (r.getRight() != invalid)
on the off chance that (search(r.getRight(), val))
return genuine;
return false;
}
/* Function for inorder traversal */
open void inorder()
{
inorder(root);
}
private void inorder(BTNode r)
{
on the off chance that (r != invalid)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
open void preorder()
{
preorder(root);
}
private void preorder(BTNode r)
{
on the off chance that (r != invalid)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
/* Function for postorder traversal */
open void postorder()
{
postorder(root);
}
private void postorder(BTNode r)
{
in the event that (r != invalid)
{
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() +" ");
}
}
}
/* Class BinaryTree */
open class BinaryTree
{
open static void main(String[] args)
{
Scanner filter = new Scanner(System.in);
/* Creating object of BT */
Bt = new BT();
/* Perform tree operations */
System.out.println("Binary Tree Test ");
scorch ch;
do
{
System.out.println(" Binary Tree Operations ");
System.out.println("1. embed ");
System.out.println("2. seek");
System.out.println("3. number hubs");
System.out.println("4. check discharge");
int decision = scan.nextInt();
switch (decision)
{
case 1 :
System.out.println("Enter whole number component to embed");
bt.insert( scan.nextInt() );
break;
case 2 :
System.out.println("Enter whole number component to look");
System.out.println("Search result : "+ bt.search( scan.nextInt() ));
break;
case 3 :
System.out.println("Nodes = "+ bt.countNodes());
break;
case 4 :
System.out.println("Empty status = "+ bt.isEmpty());
break;
default :
System.out.println("Wrong Entry ");
break;
}
/* Display tree */
System.out.print(" Post arrange : ");
bt.postorder();
System.out.print(" Pre arrange : ");
bt.preorder();
System.out.print(" In arrange : ");
bt.inorder();
System.out.println(" Do you need to proceed with (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.