Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include<stdlib.h>
- #include <string.h>
- typedef struct Team {
- char Name[100];
- char City[100];
- int Wins;
- int Loses;
- int Draws;
- int Points;
- struct Team*Left;
- struct Team*Right;
- struct Team*Parent;
- }Team;
- void InitTeam(Team*T)
- {
- T->Left=NULL;
- T->Right=NULL;
- T->Parent=NULL;
- }
- typedef struct TeamTree
- {
- Team* Root;
- }TeamTree;
- void InitTree(TeamTree*R)
- {
- R->Root=NULL;
- }
- void print(Team*T)
- {
- if(T!=NULL)
- printf("\nName: %s\nCity: %s\nWins: %d\nLoses: %d\nDraws: %d\nPoints: %d",T->Name,T->City,T->Wins,T->Loses,T->Draws,T->Points);
- }
- void Add(TeamTree*R,Team*N,Team*Current=NULL) {
- if(R->Root==NULL)
- {
- R->Root=N;
- return;
- }
- if(Current==NULL) Current=R->Root;
- int c=strcmp(Current->Name,N->Name);
- if (c==0)
- Current=N;
- if(c==-1)
- {
- if(Current->Right!=NULL)
- Add(R,N,Current->Right);
- else
- Current->Right=N;
- }
- if(c==1)
- {
- if(Current->Left!=NULL)
- Add(R,N,Current->Left);
- else
- Current->Left=N;
- }
- }
- Team* Find(TeamTree*R,char TeamName[],Team*Current=NULL)
- {
- if(R==NULL) return NULL;
- if(R->Root==NULL)
- return NULL;
- if(Current==NULL) Current=R->Root;
- int c=strcmp(Current->Name,TeamName);
- if (c==0)
- return Current;
- if(c==1)
- {
- if(Current->Left!=NULL)
- return Find(R,TeamName,Current->Left);
- else
- return NULL;
- }
- if(c==-1)
- {
- if(Current->Right!=NULL)
- return Find(R,TeamName,Current->Right);
- else
- return NULL;
- }
- }
- void Load(TeamTree*R, char FileName[])
- {
- if(R==NULL) return;
- FILE* fp;
- fp = fopen(FileName, "r");
- if(fp==NULL)
- {
- printf("\nCouldn't open the file: %s", FileName);
- return;
- }
- char name[100]; char city[100];int wins;int loses;int draws;int points;
- while(1)
- {
- fscanf(fp, "%s%s%d%d%d%d", name, city,wins,loses,draws,points);
- if(strcmp(name, "NULL")!=0 && strcmp(city, "NULL")!=0)
- {
- Team* N = (Team*)malloc(sizeof(Team));
- InitTeam(N);
- strcpy(N->Name,name); strcpy(N->City,city);N->Wins=wins;N->Loses=loses;N->Draws=draws;N->Points=points;
- Add(R, N);
- }
- else
- break;
- }
- fclose(fp);
- }
- void SaveTeam(Team*Node, FILE* fp)
- {
- if(fp==NULL)
- return;
- if(Node!=NULL)
- {
- fprintf(fp, "%s%s%d%d%d%d", Node->Name, Node->City,Node->Wins,Node->Loses,Node->Draws,Node->Points);
- if(Node->Left!=NULL)
- SaveTeam(Node->Left, fp);
- if(Node->Right!=NULL)
- SaveTeam(Node->Right, fp);
- }
- }
- void Save(TeamTree*R, char FileName[])
- {
- if(R==NULL) return;
- FILE *ptr;
- ptr = fopen(FileName, "w");
- if(ptr==NULL)
- {
- printf("\nCouldn't open the file: %s", FileName);
- return;
- }
- SaveTeam(R->Root,ptr);
- fputs("NULL\n", ptr);
- fputs("NULL", ptr);
- fputs("NULL",ptr);
- fputs("NULL",ptr);
- fputs("NULL",ptr);
- fputs("NULL",ptr);
- fclose(ptr);
- }
- void InOrder(Team*Node,void(*f)(Team*))
- {
- if(Node->Left!=NULL)
- InOrder(Node->Left,f);
- if(Node!=NULL)
- f(Node);
- if(Node->Right!=NULL)
- InOrder(Node->Right,f);
- }
- int main() {
- TeamTree Tree;
- InitTree(&Tree);
- TeamTree*ptr=&Tree;
- Team*Eagles=(Team*)malloc(sizeof(Team));InitTeam(Eagles);
- strcpy(Eagles->Name,"Eagles");strcpy(Eagles->City,"Manchester");Eagles->Wins=2;Eagles->Loses=2;Eagles->Draws=1;Eagles->Points=28;
- Team*Lions=(Team*)malloc(sizeof(Team));InitTeam(Lions);
- strcpy(Lions->Name,"Lions");strcpy(Lions->City,"Paris");Lions->Wins=5;Lions->Loses=1;Lions->Draws=0;Lions->Points=34;
- Team*Crocodiles=(Team*)malloc(sizeof(Team));InitTeam(Crocodiles);
- strcpy(Crocodiles->Name,"Crocodiles");strcpy(Crocodiles->City,"Ottawa");Crocodiles->Wins=4;Crocodiles->Loses=4;Crocodiles->Draws=2;Crocodiles->Points=21;
- Team*Elephants=(Team*)malloc(sizeof(Team));InitTeam(Elephants);
- strcpy(Elephants->Name,"Elephants");strcpy(Elephants->City,"Munchen");Elephants->Wins=3;Elephants->Loses=2;Elephants->Draws=3;Elephants->Points=24;
- Add(ptr,Eagles);
- Add(ptr,Lions);
- Add(ptr,Crocodiles);
- Add(ptr,Elephants);
- Team*T=Find(ptr,"Lions");
- if(T!=NULL)
- print(T);
- printf("\n");
- printf("\nInOrder: ");
- void(*f_ptr)(Team*); f_ptr=print;
- InOrder(Tree.Root,f_ptr);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement