69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct TreeNode *PtrToNode;
typedef PtrToNode Tree;
struct TreeNode
{
char name[9];
int age;
int networth;
Tree Left;
Tree Right;
};
Tree GetContent(char str[21])
{
Tree temp;
temp = (Tree)malloc(sizeof(struct TreeNode));
int n = strlen(str) - 1;
int i;
int count = 0;
char Age[4];
char Networth[8];
for(i = 0; i < n; i++)
{
if(str[i] != ' ')
{
temp->name[i] = str[i];
printf("temp->name[%d] = %c\n",i,temp->name[i]);//********************for testing
system("pause");
}
else{break;}
}
temp->name[i] = '\0';
printf("temp->name[%d] = %c\n",i,temp->name[i]);//********************for testing
system("pause");
printf("temp->name = %s\n",i, temp->name);//********************for testing
system("pause");
for(++i; i < n; i++)
{
if(str[i] != ' '){Age[count++] = str[i];}
else{break;}
}
Age[count] = '\0';
count = 0;
temp->age = atoi(Age);
for(++i; i < n; i++)
{
Networth[count++] = str[i];
}
Networth[count] = '\0';
temp->networth = atoi(Networth);
temp->Left = NULL;
temp->Right = NULL;
printf("temp->name = %c, temp->age = %d, temp->networth = %d",temp->name,temp->age,temp->networth);//********************for testing
system("pause");
return temp;
}
void AttachTree(Tree Mtree, Tree Atree)
{
if(Mtree == NULL){Mtree = Atree;}
else if(Atree->networth > Mtree->networth)
{
if(Mtree->Left == NULL){Mtree->Left = Atree;}
else{AttachTree(Mtree->Left,Atree);}
}
else if(Atree->networth < Mtree->networth)
{
if(Mtree->Right == NULL){Mtree->Right = Atree;}
else{AttachTree(Mtree->Right,Atree);}
}
else if(Atree->age < Mtree->age)
{
if(Mtree->Left == NULL){Mtree->Left = Atree;}
else{AttachTree(Mtree->Left,Atree);}
}
else if(Atree->age > Mtree->age)
{
if(Mtree->Right == NULL){Mtree->Right = Atree;}
else{AttachTree(Mtree->Right,Atree);}
}
else if(strcmp(Atree->name, Mtree->name) < 0)
{
if(Mtree->Left == NULL){Mtree->Left = Atree;}
else{AttachTree(Mtree->Left,Atree);}
}
else if(strcmp(Atree->name, Mtree->name) > 0)
{
if(Mtree->Right == NULL){Mtree->Right = Atree;}
else{AttachTree(Mtree->Right,Atree);}
}
else
{
printf("Attach tree failed, this two trees are the same.");
system("pause");
}
}
void OutPut(char queries[12], Tree tree)
{
PrintTree(tree);
system("pause");
int M, Amin, Amax, count, i, n;
char num[4];
n = strlen(queries) - 1;
for(i = 0; i < n; i++)
{
if(queries != ' '){num[count++] = queries[i];}
else{break;}
}
num[count] = '\0';
M = atoi(num);
count = 0;
for(++i; i < n; i++)
{
if(queries != ' '){num[count++] = queries[i];}
else{break;}
}
num[count] = '\0';
Amin = atoi(num);
count = 0;
for(++i; i < n; i++)
{
if(queries != ' '){num[count++] = queries[i];}
else{break;}
}
num[count] = '\0';
Amax = atoi(num);
count = 0;
strcpy(num,"");
printf("M = %d, Amin = %d, Amax = %d, n = %d\n",M,Amin,Amax,n);
system("pause");
Travel(tree, Amin, Amax, &count, M);
}
void Travel(Tree tree, int Amin, int Amax, int *count, int M)
{
if(count < M)
{
if(tree->Left != NULL){Travel(tree->Left, Amin, Amax, count, M);}
else
{
if(Amin < tree->age < Amax)
{
TreeOut(tree);
*count++;
}
if(tree->Right != NULL){Travel(tree->Right, Amin, Amax, count, M);}
}
}
}
void TreeOut(Tree tree)
{
FILE* fp;
fp=fopen("C:/Output.txt","w");
fprintf(fp,"%s %d %d\n",tree->name,tree->age,tree->networth);
fclose(fp);
printf("%s %d %d\n",tree->name,tree->age,tree->networth);
}
void PrintTree(Tree tree)//********************for testing
{
printf("%s %d %d\n",tree->name,tree->age,tree->networth);
}
int main()
{
int N, K;
int n, i, j;
int count = 0;
char ch;
char filename[100];
char str[100];
char num[10];
char queries[12];
char content[21];
FILE* fpr;
FILE* fpw;
//printf("\nEnter a filepath/filename:\n");
//gets(filename);
strcpy(filename,"D:/Input.txt");//********************for testing
fpr = fopen(filename,"r");
if(fpr == NULL)//if can't open the file
{
printf("File open error!\n");
system("pause");
return -1;
}
if(fgets(str,100,fpr) != NULL)
{
n = strlen(str) - 1;
if(str[0] == ' ' || str[0] < '0' || str[0]> '9')
{
printf("Can't get N.(Unexpected input in the file)");
puts(str[0]);
system("pause");
return -2;
}
else
{
ch = str[0];
num[count++] = ch;
}
for(i = 1; i < n; i++)
{
ch = str[i];
if(ch != ' ' && '0' <= ch <= '9')
{
num[count++] = ch;
num[count] = '\0';
}
else if(ch == ' '){break;}
else
{
printf("Can't get N.(Unexpected input in the file)");
system("pause");
return -2;
}
}
N = atoi(num);
if(N < 0)
{
printf("The number N is wrong.It shouldn't be %d, please input another N.",N);
system("pause");
}
strcpy(num, "");
count = 0;
ch = str[++i];
if(ch < '0' || ch > '9')
{
printf("Can't get K.(Unexpected input.)");
system("pause");
return -3;
}
for(; i < n; i++)
{
ch = str[i];
if(ch < '0' || ch > '9')
{
printf("Can't get K.(Unexpected input.)");
system("pause");
return -3;
}
else
{
num[count++] = ch;
num[count] = '\0';
}
}
K = atoi(num);
if(K < 0)
{
printf("The number K is wrong.It shouldn't be %d, please input another K.",K);
system("pause");
}
}
else//if the file is empty
{
printf("File read error!(Unexpected end of file)");
system("pause");
return -5;
}
printf("N = %d, K = %d\n",N,K);//********************for testing
Tree tree;
tree = (Tree)malloc(sizeof(struct TreeNode));
for(i = 0; i < N; i++)
{
fgets(content,100,fpr);
AttachTree(tree, GetContent(content));
//puts(content);
//system("pause");
}
PrintTree(tree);//********************for testing
//printf("-----------------------\n");
for(j = 1; j <= K; j++)
{
fpw=fopen("C:/Output.txt","w");
fprintf(fpw,"Case #%d:\n",j);
fclose(fpw);
fgets(queries,20,fpr);
puts(queries);
system("pause");
OutPut(queries, tree);
//system("pause");
}
fclose(fpr);
system("pause");
return 0;
}