#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct treenode {
char filename[256];
struct treenode* leftchild;
struct treenode* rightchild;
};
typedef struct treenode TreeNode;
/*
This function returns the index of the last occurence of '.' in
the string str. It returns -1 if str is NULL or str has no '.'
*/
int lastIndexDot(char* str) {
//insert code here, so recognzie the "."
int len;
len=strlen(str);
if( str == NULL)
return 0;
while(--len!=-1){
if( str =='.' &&
return len+1;
}
return len;
}
/*
This function compares the relative ordering of file1 and file2.
The definition of ordering is given in the TMA paper.
It returns -1 if file1 < file2, returns 0 if file1 = file2,
and returns if file1 > file2
*/
int compareFilenames(char* file1, char* file2) {
//insert code here for string compare
if(strcmp(file1,file2)==NULL){
return 0;
else if(strcmp(file1,file2)> 0)
return +1;
else if(strcmp(file1,file2)<0)
return -1;
}
return 0;
}
/*
The function inserts the newfile string into the binary tree pointed
by root. The funtcion returns the root pointer of the binary tree
with the newfile insert. The function does nothing but return NULL
if newfile already exists in the binary tree
*/
TreeNode* insertFilename(TreeNode* root, char* newfile) {
//insert code here
if(root==NULL){
root=(TreeNode*)malloc(sizeof(TreeNode));
root-> strcpy(FileName,newfile);
root-> leftchild =NULL;
root-> rightchild=NULL;
}
else if( compareFilenames(file1, file2)<0){
root-> rightchild=insertFilename(root->rightchild,newfile);
else if( compareFilenames(file1, file2)<0 )
root->leftchild=insertFilename(root-> leftchild,newfile);
return (root);
}
/*
This function prints the content of the binary tree in the inorder
traversals with counters.
*/
int printInorderWithCounter(TreeNode* root, int count) {
return 0;
}
/*
This function prints the content of the binary tree in the inorder
traversals
*/
void printInorder(TreeNode* root) {
if (root == NULL)
return;
printInorder(root->leftchild);
printf("%s\n", root->filename);
printInorder(root->rightchild);
}
int numberOfFiles(TreeNode* root) {
if (root == NULL)
return 0;
return numberOfFiles(root->leftchild) +
numberOfFiles(root->rightchild) + 1;
}
int main()
{
FILE* fp; /* file pointer for popen */
char buffer[256]; /* buffer for holding filename temporary */
TreeNode* root = NULL; /* the root pointer to the binary tree */
/*
declare other necessary variables
*/
fp = _popen("dir /a-d /b", "rt");
if (fp == NULL) {
printf("Error in listing directory\n");
}
/*
make changes to the following code
*/
root= insertFilename( root, newfile);
while (fgets(buffer, 256, fp) != NULL) {
buffer[strlen(buffer)-1] = '\0'; /* removing the ending newline */
printf("%s\n", buffer);
}
}
Thanks hammer
I just have problems with comparefilename function and Lastdotindex. The insertionnode function should work. It is using comparefilename to compare file1 string and file 2 string.
Notice I put insert code on lastindexdo and comparefilename.
Underneath, i write some code myself. I am not sure it is right.