Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define INSERT "insert"
- #define DELETE "delete"
- #define FIND "find"
- #define MIN "min"
- #define MAX "max"
- #define INORDER "inorder"
- #define BLACK 'B'
- #define RED 'R'
- int N = 0;
- FILE* file;
- //zwraca 0 - sukces 1 - blad
- int openFile(char* fileName);
- //zwraca 0 - sukces 1 - plik nie zostal zainicjalizowany
- int closeFile();
- //zwraca odczytana komende
- char* readCommand();
- //zwraca argument przy komendzie
- int readNumber();
- int invokeCommand();
- //zwraca 0 - sukces 1 - blad
- int insertValue(int value);
- //zwraca 0 - sukces 1 - blad
- int deleteValue(int value);
- //zwraca 0 - sukces 1 - blad
- int findValue(int value);
- void maxValue();
- void minValue();
- void inOrder();
- int main(int argc, char **argv)
- {
- int i = 0;
- char* command;
- if(argc != 2)
- {
- printf("Nieprawidłowa ilosc argumentow\n");
- return 0;
- }
- if(openFile(argv[1]) == 1)
- {
- printf("Proba otwarcia pliku zakonczona niepowodzeniem\n");
- return 0;
- }
- N = readNumber();
- for ( i = 0; i < N; i++ )
- {
- command = readCommand();
- if(invokeCommand(command) == 1)
- {
- printf("Plik wejsciowy zawiera nieprawidowe dane\n");
- return 0;
- }
- }
- if(closeFile() == 1)
- {
- printf("Niepowodzenie podczas zamykania pliku\n");
- }
- return 0;
- }
- int openFile(char* fileName)
- {
- if ((file = fopen(fileName, "r")) == NULL)
- {
- printf ("Nie mogę otworzyć pliku. \n");
- return 1;
- }
- return 0;
- }
- int closeFile()
- {
- if(file != NULL)
- {
- if(fclose(file) == EOF)
- {
- return 1;
- }
- return 0;
- }
- else
- {
- return 1;
- }
- }
- char* readCommand()
- {
- char command[20];
- char* resultCommand;
- fscanf(file, "%s", command);
- if((resultCommand = (char*)malloc(sizeof(char) + sizeof(char) * strlen(command))) == NULL)
- {
- printf("Nie udalo sie zarezerwowac miejsca, wcisnij ENTER");
- getchar();
- return NULL;
- }
- else
- {
- strcpy( resultCommand, command );
- return resultCommand;
- }
- return NULL;
- }
- int readNumber()
- {
- int argument = 0;
- if(fscanf(file, "%d", &argument) == EOF)
- {
- printf("Blad odczytu argumentu. Dalsze dzialanie programu moze przyniesc nieoczekiwany efekt\n");
- }
- return argument;
- }
- int invokeCommand(char* command)
- {
- int argument;
- if(strcmp(command, INSERT) == 0)
- {
- argument = readNumber();
- insertValue(argument);
- return 0;
- }
- if(strcmp(command, DELETE) == 0)
- {
- argument = readNumber();
- deleteValue(argument);
- return 0;
- }
- if(strcmp(command, FIND) == 0)
- {
- argument = readNumber();
- findValue(argument);
- return 0;
- }
- if(strcmp(command, MIN) == 0)
- {
- minValue();
- return 0;
- }
- if(strcmp(command, MAX) == 0)
- {
- maxValue();
- return 0;
- }
- if(strcmp(command, INORDER) == 0)
- {
- inOrder();
- return 0;
- }
- return 1;
- }
- int insertValue(int value)
- {
- printf("insertValue %d \n", value);
- return 0;
- }
- int deleteValue(int value)
- {
- printf("deleteValue %d \n", value);
- return 0;
- }
- int findValue(int value)
- {
- printf("findValue %d \n", value);
- return 0;
- }
- void maxValue()
- {
- printf("maxValue\n");
- }
- void minValue()
- {
- printf("minValue\n");
- }
- void inOrder()
- {
- printf("inOrder\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement