Created
November 4, 2013 17:57
-
-
Save morganwilde/7306609 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Dovile Mikenaite, Ekonometrija, 2 grupe : | |
2. Su pasirinktais įrašais realizuoti ADT steko operacijas dinaminio dvikrypčio sąrašo pagrindu. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
//#include <conio.h> | |
//typedef int bool; | |
#define true 1 | |
#define false 0 | |
typedef struct{ | |
char pavadinimas[20]; | |
char autorius[20]; | |
int metai; | |
int puslapiu; | |
int versta; | |
//bool versta; | |
char stilius; | |
} duomenys; | |
typedef struct S{ | |
duomenys duom; | |
struct S *sekantis; | |
struct S *buves ; | |
} S; | |
void Initialize(S**); | |
void Push(S**, duomenys x); | |
void Pop(S**); | |
void Top(S**); | |
void Kill(S**); | |
void Print(S*); | |
void SkaitytiDuomenis(S **virsune){ | |
duomenys data; | |
FILE *file; | |
file = fopen("knygos.txt", "r"); | |
if (file) { | |
int i = 0; | |
int duomenu_kiekis = 6; | |
char tmp; | |
while(i < duomenu_kiekis){ | |
fscanf(file,"%s", data.pavadinimas); | |
fscanf(file,"%s", data.autorius); | |
fscanf(file,"%d", &data.metai); | |
fscanf(file,"%d", &data.puslapiu); | |
fscanf(file,"%d", &data.versta); | |
fscanf(file, "%c", &tmp); | |
fscanf(file,"%c", &data.stilius); | |
Push(virsune, data); | |
i++; | |
} | |
fclose( file ); | |
} | |
printf("Duomenys nuskaityti \n\n"); | |
} | |
int main() { | |
S *virsune; | |
int x; | |
int z; | |
while(x != 8){ | |
// prepare storage for Push | |
char | |
knygosPavadinimas[20], | |
knygosAutorius[20], | |
knygosStilius; | |
int | |
knygosMetai, | |
knygosPuslapiu, | |
knygosVersta; | |
// data pointer | |
duomenys duomenysNauji; | |
// UI | |
switch (x) { | |
case 1: | |
printf("\n\n"); | |
Initialize(&virsune); | |
printf("\n\n"); | |
x=0; | |
break; | |
case 2: | |
printf("\n\n"); | |
SkaitytiDuomenis(&virsune); | |
printf("\n\n"); | |
x=0; | |
break; | |
case 3: | |
printf("\n\n"); | |
Pop(&virsune); | |
printf("\n\n"); | |
x=0; | |
break; | |
case 4: | |
printf("\n\n"); | |
Top(&virsune); | |
printf("\n\n"); | |
x=0; | |
break; | |
case 5: | |
printf("\n\n"); | |
Print(virsune); | |
printf("\n\n"); | |
x=0; | |
break; | |
case 6: | |
printf("\n\n"); | |
Kill(&virsune); | |
printf("\n\n"); | |
x=0; | |
break; | |
case 7: | |
printf("\n\n"); | |
printf("Ivesti duomenis:\n"); | |
// name | |
printf("Knygos pavadinimas: "); | |
scanf("%s", knygosPavadinimas); | |
// author | |
printf("Knygos autorius: "); | |
scanf("%s", knygosAutorius); | |
// style | |
printf("Knygos stilius (B arba G): "); | |
scanf(" %c", &knygosStilius); | |
// year | |
printf("Knygos leidimo metai: "); | |
scanf("%d", &knygosMetai); | |
// pages | |
printf("Knyga turi puslapiu: "); | |
scanf("%d", &knygosPuslapiu); | |
// translated | |
printf("Knyga versta? (1 - taip, 0 - ne): "); | |
scanf("%d", &knygosVersta); | |
// Save everything | |
strcpy(duomenysNauji.pavadinimas, knygosPavadinimas); | |
strcpy(duomenysNauji.autorius, knygosAutorius); | |
duomenysNauji.stilius = knygosStilius; | |
duomenysNauji.metai = knygosMetai; | |
duomenysNauji.puslapiu = knygosPuslapiu; | |
duomenysNauji.versta = knygosVersta; | |
Push(&virsune, duomenysNauji); | |
printf("\n\n"); | |
x=0; | |
break; | |
default: | |
printf("1. Inicializuoti steka\n"); | |
printf("2. Nuskaityti duomenis\n"); | |
printf("3. Ismesti paskutini elementa\n"); | |
printf("4. Atspausdinti virsune\n"); | |
printf("5. Spausdinti steka\n"); | |
printf("6. Sunaikinti steka\n"); | |
printf("7. Ivesti naujus duomenis\n"); | |
printf("8. Uzdaryti\n"); | |
scanf("%d", &x); | |
break; | |
} | |
} | |
return 0; | |
} | |
void Initialize(S **pb) | |
{ | |
*pb = NULL; | |
printf("Stekas inicializuotas.\n"); | |
} | |
void Push(S **pb, duomenys x){ | |
S *temp; | |
if (*pb == NULL) | |
{ | |
temp = (S *) malloc(sizeof(S)); | |
temp -> duom = x; | |
temp -> sekantis = NULL; | |
temp -> buves = NULL; | |
*pb = temp; | |
} | |
else | |
{ | |
temp = (S *) malloc(sizeof(S)); | |
temp -> duom = x; | |
temp -> sekantis = NULL; | |
(*pb) -> sekantis = temp; | |
temp -> buves = *pb; | |
*pb = temp; | |
} | |
} | |
void Pop(S **pb){ | |
S *temp; | |
if (*pb == NULL) | |
{ | |
printf("Stekas yra tuscias.\n"); | |
} | |
else | |
{ | |
if((*pb)->buves == NULL){ | |
free(*pb); | |
*pb = NULL; | |
} | |
else | |
{ | |
temp = (*pb) -> buves; | |
(*pb) -> buves->sekantis = NULL; | |
free (*pb); | |
(*pb) = temp; | |
} | |
printf("Elementas pasalintas.\n"); | |
} | |
} | |
void Top(S **pb){ | |
if (*pb == NULL) | |
{ | |
printf("Stekas yra tuscias.\n"); | |
//getch(); | |
}else{ | |
printf("Virsutinis steko elementas: \n\n"); | |
printf(" Pavadinimas: %s\n Autorius: %s\n Metai: %d\n Puslapiu: %d\n Ar versta? %d\n Stilius: %c\n\n", (*pb)->duom.pavadinimas, (*pb)->duom.autorius, (*pb)->duom.metai, (*pb)->duom.puslapiu, (*pb)->duom.versta, (*pb)->duom.stilius); | |
} | |
} | |
void Kill(S **pb){ | |
S *temp; | |
if (*pb == NULL) | |
{ | |
printf("Stekas yra tuscias.\n"); | |
} | |
else | |
{ | |
while (*pb != NULL) | |
{ | |
temp = *pb; | |
if ( temp -> buves != NULL) | |
{ | |
temp = temp -> buves; | |
temp -> sekantis = NULL; | |
free ( *pb ); | |
*pb = temp; | |
} | |
else | |
{ | |
free ( *pb ); | |
*pb = NULL; | |
} | |
} | |
printf("Stekas sunaikintas, atmintis atlaisvinta.\n"); | |
} | |
} | |
void Print(S *pb){ | |
if ( pb == NULL) | |
printf("Stekas tuscias.\n"); | |
else | |
while (pb != NULL) | |
{ | |
printf(" Pavadinimas: %s\n Autorius: %s\n Metai: %d\n Puslapiu: %d\n Ar versta? %d\n Stilius: %c\n\n", (pb)->duom.pavadinimas, (pb)->duom.autorius, (pb)->duom.metai, (pb)->duom.puslapiu, (pb)->duom.versta, (pb)->duom.stilius); | |
pb = pb -> buves; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment