Sunteți pe pagina 1din 37

ACADEMIA DE STUDII ECONOMICE

FACULTATEA DE CIBERNETIC, STATISTIC i INFORMATIC ECONOMIC

STRUCTURI DE DATE PROIECT


AVIOANE

Student : IORDACHE ANDREEA - MIHAELA Grupa : 1053 An: III

Bucureti 2010

CUPRINS:
1.

Introducere:.....................................................................pag.: 3 Structura programului:..................................................pag.: 3 Structurile de date utilizate intern:...............................pag.: 6 Descrierea implementrii :.............................................pag.: 7 Concluzii:.........................................................................pag.: 9

2.

3.

4.

5.

Bibliografie Anexe

AVIOANE

1. Introducere:

Obiectivul proiectului il reprezinta implementarea pe calculator a binecunoscutului joc al avioanelor. Necesitatea proiectului: oferirea copiilor si de ce nu si adultilor a unui joc de destindere, simplu, cu un grad de dificultate scazut dar care dezvolta si spiritul competitiv. Mijloacele de realizare: Limbajul de programare folosit pentru realizarea proiectului este limbajul C. Mediul de dezvoltare a aplicaiei a fost Microsoft Visual C++9.0 2008 iar compilatorul folosit este Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86.

2.

Structura programului:

Programul face referire la structura de date game cu urmtoarea structur:


struct game { player me; player pc; int state; }; player este structura definita de mine astfel: typedef struct { plane planes[3]; int nr_planes; //number of player's planes int guessed; //number of the opponent's planes guessed int my_matrix[10][10]; //the matrix with the place of player's planes int my_matrix_op[10][10]; //the matrix with the place of opponent's planes char *name; int score; }player; Iar structura plane este definita astfel: typedef struct { int state; //0-dead; 1-alive int oriented; //1-up; 2-down; 3-left; 4-right cell head; }plane; //structura avion Structura cell este definita astfel: typedef struct {

int pozX; int pozY; }cell;

La rularea programului se afiseaza un meniu cu obtiuni: 1.START NEW GAME 2.ADD PLANE 3.ADD PLAYER`S NAME 4.HALL OF FAME 5.EXIT GAME 1.START NEW GAME Pentru ca aceasta obtinue sa porneasca jocul trebuie ca jucatorul sa aibe desenate cele 3 avioane.Daca aceasta conditie nu este indeplinita se afiseaza pe ecran measajul din Fig1.

Fig1. Se revine la meniul principal iar jucatorul trebuie sa isi aleaga cele 3 avioane( coordonatele capului avionului si orientare).Pentru acest lucru trebuie sa aleaga obtinuea 2 din meniu, adica ADD PLANE. 2.ADD PLANE Cand se alege aceasta optiunea jucatorul trebuie sa dea coordonatele capului si orientarea.Pentru a alege orientarea avionului sea fiseaza pe ecran un meniu explicativ, dupa care jucatorul alege optiunea sa. Dupa adaugarea avionului se afiseaza un mesaj in care se anunta cate avioane mai are de adaugat jucatorul pentru a putea incepe jocul, dupa care se revine la meniul principal. De aici ciclul se reia pana cand sunt adaugat 3 avioane si jocul poate incepe. Tot aceste ciclu de adaugare avion este prezentat in figura Fig2.

Fig.2 Acum daca este aleasa optiunea 1 din meniu jocul poate incepe.Jocul incepe prin afisarea hartii cu avioanele jucatorului. Dupa care jucatorul este rugat sa aleaga un punct de pe harta oponentului(calculatorului) pentru a vedea daca a lovit o partea dintr-un avion a unui oponent sau chiar capul. Acest lucru este reprezentat in Fig.3.

Dupa ce sunt date coordonatele este afisata o harta in care este afisat punctul lovit cu anumite caractere. XXX coordonatele fac parte dintr-un avion al oponentului (Fig4.), *** coordonatele date nu reprezinta nimic pe harta oponentului, ### coordonatele reprezinta capul unui avion al oponentului.Daca este ghicit capul atunci se afiseaza un mesaj in care se cere sa dam si orientarea avionului. Daca orietarea este cea a avionului oponentului atunci avionul este declarat mort si se afiseaza harta 5

oponentului doar cu avionul ghicit de jucator. Algoritmul se repeta pana cand ori jucatorul ori oponentul a ghicit 3 avioane, numarul tuturor avioanelor unuia dintre jucatori, dupa care se iese din program( ENTER). 3.ADD PLAYER`S NAME Daca se alege aceasta optiune jucatorul poate sa isi introduca numele iar pe parcursul jocului acest numei ii va aparea in mesajele destinate acestuia (ex. ANDREEA`S TURN..., HELLO ANDREEA). Dupa ce numele este introdus prin apasarea tastei ENTER se revine la meniul principal. Acest lucru este reprezentat in Fig5.

Fig5. 4.HALL OF FAME Afiseaza toti jucatorii care au jucat in ordinea descrescatoare a scorurilor. 5.EXIT Daca se alege optiunea y dupa apasarea tastei ENTER se iese din program, iar daca se alege optiunea n atunci se revine la meniul anterior. Acest lucru este prezenat in Fig6 si Fig7.

Fig6.

Fig7.

3. Structurile de date utilizate intern: Structurile de date utilizate intern sunt: fisier, masive (uni si bidimensionale), lista si structurile de tip articol amintite mai sus.

Fisierul pc_data.txt si list.txt este sunt fisier de tip text care au urmatoarele caracteristici: - fiecare linie contine coordonatele capului unui fisier si orientarea sa - saptiul dintre coordonata si orientare estre seminificativ deoarece marceheaza sfarsitul coordonatei si inceputul orientarii. - Ordinea sirurilor de caractere din fisier este neimportanta. Pentru prelucrarea acestui fisier sunt folosite urmatoarele operatii: descrierea fisierului, asignarea fisierului intern la unul extern, deschidere fisier, citire date din fisier si inchidere fisier. Masive Operatiile folosite pentru prelucrarea masivelor sunt: initializare, parcurgere, afisare. Lista simplu inlantuita implementata in acest program are urmatoare structura:
struct information { char *name; int score; }; typedef struct elem{ information info; struct elem *next; }node;

Operatiile care se plica aceste liste simplu inlantuite sunt: creare lista ordonata, afisarea ei, inserare nod respectand criteriul de lista ordonata.

4. Descrierea implementrii
Programul implementeaza functiile: Initializarea matricei g este implementata prin intermediul functiei urmatoare care initializeaza cu 0 toate elementele stucturii game.
void init(Game g) { int i,j; g->state = 0; g->me.nr_planes = 0; //planes nr for player g->me.guessed = 0; //planes guessed by player g->pc.nr_planes = 0; //planes nr for computer g->pc.guessed = 0; //planes guessed by computer for(i=0; i<10; i++) for(j=0; j<10; j++) { g->me.my_matrix[i][j] = 0; g->me.my_matrix_op[i][j] = 0; g->pc.my_matrix[i][j] = 0; g->pc.my_matrix_op[i][j] = 0; } //memory allocation for name g->me.name = (char*)malloc(sizeof(char)); }

Functia print_main_menu afiseaza pe ecran meniul principal dupa care verifica daca jucatorul si-a adaugat numele si afiseaza pe ecran mesajul Welcome, nume_adaugat!, iar daca nu si-a introdus numele inca afiseaza Welcome, PLAYER!:
void print_main_menu() { system("cls"); printf("+--------------------------------- MAIN MENU ----------------------------------+\n"); printf("| 1. START NEW GAME |\n"); printf("| 2. ADD PLANES |\n"); printf("| 3. ADD PLAYER'S NAME |\n"); printf("| 4. HALL OF FAME |\n"); printf("| 5. EXIT GAME |\n"); printf("+------------------------------------------------------------------------------+\n"); if(changed_name == 0) { printf("Welcome, PLAYER!\n"); printf("If you want to change your name, go to PLAYER'S NAME menu and change it.\n\n"); } else { printf("Welcome, %s!\n",g->me.name); } printf("MAKE A CHOICE!\n"); }

Functia main_menu * Daca optiunea utilizatorului este 1: Verifica daca jucatorul are 3 avioane in harta sa daca nu are atunci se apeleaza functia de afisare pe ecran a maeniului principal, dupa care se apeleaza functia main_menu, se revine la meniul principal. Daca jucatorul are 3 avioane atunci : - Se apeleaza functia read_file Aceasta functie verifica daca exista fisierul pc_data.txt in care sunt coordonatele si orientarea avionului oponentului(calculatorului).Daca nu exista afiseaza un mesaj pe ecran. Daca exista atunci il deschide pentru citire. Apoi citeste coordonatele capului si orientarea avioanelor calculatorului. Coordonatele capurilor avioanelor sunt transformate din sir de caractere in numere intregi prin functia head_coord: Acest lucru il face astfel: scade din prima litera din sirul de caractere codul ASCII al lu A.Stii ca aceste coordonate au prima litera intre a si j atunci pentru litera A vom avea 0 (A A=0), pentru B 1 (B A=1) si asa mai departe. In functie de orientarea care o gaseste in fisier pune in matricea avioanelor calculatorului 2 pentru cap si 1 pentru restul avionului(practic deseneaza avioanele). Ii da numele oponentului COMPUTER.

Verifica daca jucatorul si-a adaugat numele daca nu ii da ca nume implicit numele de PLAYER. Afiseaza harta cu avioanele jucatorului Cat timp jocul nu este terminat ( g.state == 0) se afiseaza un mesaj pe ecran prin care i se cere jucatorului sa dea coordonatele punctului in care vrea sa loveasca. Se apeleaza head_coord, pentru transformarea coordonatelor in numere Se verifica daca coordonatele sunt valide cu functia coord_valid. Coordonatele se considera valide daca x si y este intre 0 si 9. Verifica daca coordonatele reprezinta capul unui avion si daca nu pune 1 in matricea jucatorului in care se retin punctele incercate de el daca a lovit o parte a avionului si -1 daca nua lovit nimic, iar daca a lovit capul ii cere jcuatorului sa ii ghiceasca orientarea.Daca a ghicit orientarea atunci avionul este declarat mort. Verifica daca jucatorul a ghicit pana acum 3 avionae daca da atunci se iese din joc daca nu ii cere acestuia sa mai dea coordonate, dupa care vine randul calculatorului care da coordonate. Acesta da coordonate.Se verifica la fel daca a nimerit capul unui avion si urmeaza aceasi pasi ca si in cazul jucatorului numai ca aceste coordonate date de calculator nu sunt introduse de un utilizator ci alege calculatorul random. - Verifica daca sunt introduse 3 avioane. Daca da atunci se afiseaza un mesaj prin care se anunta ca jocul poate incepe si se revine la meniul principal, daca nu se afiseaza un mesaj princ care utilizatorul este anuntat cat avioane mai poate adauga si se revine la meniul principal pentru a alege o alta optiune.

* Daca optiunea utilizatorului este 2:

* Daca optiunea utilizatorului este 3: - I se cere utilizatorului sa isi dea numele. Daca introduce numele atunci aceste este afisat pe ecran, duppa care se revine la meniul principal. * Daca optiunea utilizatorului este 4: - Se pe ecran o lista cu jucatori impreuna cu scorul acestora in ordinea descrescatoare scorurilor. * Daca optiunea utilizatorului este 5: - Se apeleaza exit_menu . Utilizatorul este intrebat daca vrea sa iasa si acest lucru se paote face tastand y sau n. Daca tasteaza orice alt caracter se afiseaza un meniu de erroare si este rugat sa introduca un caracter valid. Daca introduce y atunci se iese din program, daca se tasteaza n atunci se revine la meniul principal.

5. Concluzii:
In urma elaborarii acestui program am ajuns la urmatoarele concluzii: se pot utiliza simultan diferite structuri de date pentru a ajunge la rezultatul dorit. Structura de date masiv ofera rapid si usor accesul la elemente. De aceea pot spune ca pentru acest proiect aceasta structura de date este cea mai eficienta. 9

Bibliografie:
Programarea calculatoarelor Stiinta invatarii unui limbaj de programare Teorie si aplicatii Prof.univ.dr.Ion Gh. ROSCA (coordonator); Prof.univ.dr.Bogdan GHILIC-MICU; Lect.univ.dr.Catalina Lucia COCIANU;Lect.univ.dr.Marian STOICA; Asist.univ.drd.Cristian USCATU

Anexe:
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <ctype.h> #include <time.h> typedef struct { int pozX; int pozY; }cell; //structura celula

typedef struct { int state; //1-dead; 0-alive int oriented; //1-up; 2-down; 3-left; 4-right cell head; }plane; //structura avion typedef struct { plane planes[3]; int nr_planes; //number of player's planes int guessed; //number of the opponent's planes guessed int my_matrix[10][10]; //the matrix with the place of player's planes int my_matrix_op[10][10]; //the matrix with the place of opponent's planes char *name; int score; }player; struct game {

10

player me; player pc; int state; }; typedef struct game *Game; struct information { char *name; int score; }; typedef struct elem{ information info; struct elem *next; }node; void print_main_menu(); void main_menu(); void print_error(); void init(Game g); void start_game(Game g); void submenu(); void print_submenu(); int cap_valid(int x, int y, char op); void add_plane(Game g); int not_nulls(Game g); void intersect(); void head_coord(char coord[4]); void print_matrix_pc(Game g); //global variables Game g; FILE *fin; char coord[4], my_coord[4], pc_coord[4]; int x,y; //the coordinates for the head player int z,t; //the coordinates for the head computer int pc_oriented; int changed_name = 0; int fault; node * add_node(node *rad, information infonou) { node *nou, *p, *q; p =(node*)malloc(sizeof(node)); if ( ((nou=(node *)malloc(sizeof(node)))==NULL) || ( (nou->info.name = (char*)malloc(sizeof(char))) == NULL)) { printf("ERROR: NOT ENOUGH MEMORY AVAILABLE\n"); return NULL; } strcpy(nou->info.name,infonou.name); nou->info.score = infonou.score; nou->next = NULL;

11

for(p = q = rad; p!=NULL && p->info.score > infonou.score; q=p, p=p->next); if(p!=q) { q->next = nou; nou->next = p; } else { nou->next = rad; rad = nou; } return rad; } void print_list(node *rad) { node *p =rad; for(p=rad; p!=NULL; p=p->next) { printf(" } } //reads from file void read_file(FILE *f) { char string[3]; int or,i,j; if((f = fopen("pc_data.txt","rt")) == NULL) { printf("File not found!\n"); exit(1); } for(j = 0; j < 3; j++) { fscanf(f,"%s",&string); head_coord(string); fscanf(f,"%d",&or); switch(or) { case 1: g->pc.my_matrix[x][y] = 2; for(i = y-2; i <= y+2; i++) g->pc.my_matrix[x+1][i] = 1; g->pc.my_matrix[x+2][y] = 1; for(i = y-1; i <= y+1; i++) g->pc.my_matrix[x+3][i] = 1; g->pc.planes[j].oriented = 1; //the plane has orientation up g->pc.planes[j].head.pozX = x; g->pc.planes[j].head.pozY = y; break; case 2: g->pc.my_matrix[x][y] = 2; for(i = y-2; i <= y+2; i++) g->pc.my_matrix[x-1][i] = 1; g->pc.my_matrix[x-2][y] = 1; for(i = y-1; i <= y+1; i++)

%s %d\n",p->info.name, p->info.score);

12

g->pc.my_matrix[x-3][i] = 1; g->pc.planes[j].oriented = 2; //the plane has orientation down g->pc.planes[j].head.pozX = x; g->pc.planes[j].head.pozY = y; break; case 3: g->pc.my_matrix[x][y] = 2; for(i = x-2; i <= x+2; i++) g->pc.my_matrix[i][y+1] = 1; g->pc.my_matrix[x][y+2] = 1; for(i = x-1; i <= x+1; i++) g->pc.my_matrix[i][y+3] = 1; g->pc.planes[j].oriented = 3; //the plane has orientation left g->pc.planes[j].head.pozX = x; g->pc.planes[j].head.pozY = y; break; case 4: g->pc.my_matrix[x][y] = 2; for(i = x-2; i <= x+2; i++) g->pc.my_matrix[i][y-1] = 1; g->pc.my_matrix[x][y-2] = 1; for(i = x-1; i <= x+1; i++) g->pc.my_matrix[i][y-3] = 1; g->pc.planes[j].oriented = 4; //the plane has orientation right g->pc.planes[j].head.pozX = x; g->pc.planes[j].head.pozY = y; break; } } } void head_coord(char coord[4]) { //transforms the coordinates read to be between 0-9 x = toupper(coord[0]) - 'A'; y = -1; if(strlen(coord) == 3 && coord[1] == '1' && coord[2] == '0') y = 9; else if( strlen(coord) == 2) y = coord[1] - '1'; } //prints the map of pc planes void print_matrix_op(int mat[10][10]) { int i,j,k; char letter='A'; printf(" "); for(i=1; i<=10; i++) printf(" %d", i); printf("\n "); for(i=1; i<=10; i++) printf("----"); printf("-\n"); for(i=0;i<10;i++) {

13

printf("%c |",letter); for(j=0; j<10; j++) if(mat[i][j] == 0) printf(" |"); else if(mat[i][j] == -1) printf("***|"); else if(mat[i][j] == 1) printf("XXX|"); else if(mat[i][j] == 2) printf("###|"); printf("\n "); letter++; for(j=1; j<=10; j++) printf("----"); printf("-\n"); } printf("\n"); } //prints the map of pc planes void print_matrix_pc(Game g) { int i,j,k; char letter='A'; printf(" "); for(i=1; i<=10; i++) printf(" %d", i); printf("\n "); for(i=1; i<=10; i++) printf("----"); printf("-\n"); for(i=0;i<10;i++) { printf("%c |",letter); for(j=0; j<10; j++) if(g->pc.my_matrix[i][j] == 0) printf(" |"); else if(g->pc.my_matrix[i][j] == 1) printf("DDD|"); else if(g->pc.my_matrix[i][j] == 2) for(k=0; k<3; k++) if(g->pc.planes[k].head.pozX == i && g>pc.planes[k].head.pozY == j) switch(g->pc.planes[k].oriented) { case 1: printf(" A |"); //orientation up break; case 2:

14

printf(" V |"); break; case 3: printf(" < |"); break; case 4: printf(" > |"); break; } printf("\n "); letter++; for(j=1; j<=10; j++) printf("----"); printf("-\n"); } printf("\n"); } //prints the map of player's planes void print_matrix(Game g) { int i,j,k; char letter='A'; printf(" "); for(i=1; i<=10; i++) printf(" %d", i); printf("\n "); for(i=1; i<=10; i++) printf("----"); printf("-\n"); for(i=0;i<10;i++) { printf("%c |",letter); for(j=0; j<10; j++) if(g->me.my_matrix[i][j] == 0) printf(" |"); else if(g->me.my_matrix[i][j] == 1) printf("DDD|"); else if(g->me.my_matrix[i][j] == 2) for(k=0; k<3; k++) if(g->me.planes[k].head.pozX == i && g>me.planes[k].head.pozY == j) switch(g->me.planes[k].oriented) { case 1: printf(" A |"); //orientation up break; case 2: printf(" V |"); break; case 3: printf(" < |"); break; case 4:

15

printf(" > |"); break; } printf("\n "); letter++; for(j=1; j<=10; j++) printf("----"); printf("-\n"); } printf("\n"); } //the exit menu void exit_menu() { char option; system("cls"); //clears the screen printf("---------------------------------EXIT GAME MENU-----------------------------\n\n"); printf("Are you sure you want to exit? y/n \n"); printf("EXIT? - "); fflush(stdin); scanf("%c", &option); option = toupper(option); switch(option) { case 'Y': printf("You choosed to EXIT the GAME!\nEXITING..."); getch(); exit(0); break; case 'N': printf("You don't want to EXIT the GAME!\nGetting back to MAIN MENU..."); getch(); system("cls"); print_main_menu(); main_menu(); break; default: print_error(); getch(); exit_menu(); break; } } void intersect() { printf("The plane with HEAD = %s is intersecting with other plane(s) present in the map!\n", coord); printf("Please provide an appropriate HEAD and Orientation\n"); printf("Returning to MAIN MENU..."); getch(); system("cls"); print_main_menu(); main_menu(); }

16

int not_nulls(Game g) { int null,i,j; null = 0; for(i =0; i<10; i++) for(j = 0; j<10; j++) if(g->me.my_matrix[i][j] != 0) null++; return null; } //prints an error message if an option doesn't exist void print_error() { printf("ERROR: OPTION UNKNOWN!\n\n"); } //checks if the head of the plane is valid int cap_valid(int x, int y, char op) { int ok = 0; switch(op) { case '1': //orientare sus if( (x>= 0) && (x <= 6) && (y >= 2) && (y <= 7) ) ok = 1; break; case '2': //orientare jos if( (x>= 3) && (x <= 9) && (y >= 2) && (y <= 7) ) ok = 1; break; case '3': //orientare stanga if( (x>= 2) && (x <= 7) && (y >= 0) && (y <= 6) ) ok = 1; break; case '4': //orientare dreapta if( (x>= 2) && (x <= 7) && (y >= 3) && (y <= 9) ) ok = 1; break; } return ok; } void submenu() { char option; int i,j; fflush(stdin); fflush(stdout); printf("OPTION SELECTED: "); scanf("%c", &option); //checking if the head is valid depending orientation

17

if(cap_valid(x, y, option) != 1) //is not valid { if(option == '1') printf("ERROR: We can't place the plane with the HEAD = %s oriented UP\n\n", coord); else if(option == '2') printf("ERROR: We can't place the plane with the HEAD = %s oriented DOWN\n\n", coord); else if(option == '3') printf("ERROR: We can't place the plane with the HEAD = %s oriented LEFT\n\n", coord); else if(option == '4') printf("ERROR: We can't place the plane with the HEAD = %s oriented RIGHT\n\n", coord); printf("Returning to ADD PLANES MENU..."); getch(); add_plane(g); } else { if(not_nulls(g)%10 == 0 && g->me.my_matrix[x][y] == 0 && g->me.nr_planes != 3) { switch(option) { case'1': g->me.my_matrix[x][y] = 2; for(i = y-2; i <= y+2; i++) g->me.my_matrix[x+1][i] = 1; g->me.my_matrix[x+2][y] = 1; for(i = y-1; i <= y+1; i++) g->me.my_matrix[x+3][i] = 1; g->me.planes[g->me.nr_planes].oriented = 1; //the plane has orientation up g->me.planes[g->me.nr_planes].head.pozX = x; g->me.planes[g->me.nr_planes].head.pozY = y; g->me.nr_planes++; if(not_nulls(g)%10 != 0) { g->me.my_matrix[x][y] = 0; for(i = y-2; i <= y+2; i++) g->me.my_matrix[x+1][i] = 0; g->me.my_matrix[x+2][y] = 0; for(i = y-1; i <= y+1; i++) g->me.my_matrix[x+3][i] = 0; g->me.nr_planes--; g->me.planes[g->me.nr_planes].oriented = 0; g->me.planes[g->me.nr_planes].head.pozX = 0; g->me.planes[g->me.nr_planes].head.pozY = 0; intersect(); } break; case '2': g->me.my_matrix[x][y] = 2; for(i = y-2; i <= y+2; i++) g->me.my_matrix[x-1][i] = 1;

18

g->me.my_matrix[x-2][y] = 1; for(i = y-1; i <= y+1; i++) g->me.my_matrix[x-3][i] = 1; g->me.planes[g->me.nr_planes].oriented = 2; //the plane has orientation up g->me.planes[g->me.nr_planes].head.pozX = x; g->me.planes[g->me.nr_planes].head.pozY = y; g->me.nr_planes++; if(not_nulls(g)%10 != 0) { g->me.my_matrix[x][y] = 0; for(i = y-2; i <= y+2; i++) g->me.my_matrix[x-1][i] = 0; g->me.my_matrix[x-2][y] = 0; for(i = y-1; i <= y+1; i++) g->me.my_matrix[x-3][i] = 0; g->me.nr_planes--; g->me.planes[g->me.nr_planes].oriented = 0; //the plane has orientation down g->me.planes[g->me.nr_planes].head.pozX = 0; g->me.planes[g->me.nr_planes].head.pozY = 0; intersect(); } break; case '3': g->me.my_matrix[x][y] = 2; for(i = x-2; i <= x+2; i++) g->me.my_matrix[i][y+1] = 1; g->me.my_matrix[x][y+2] = 1; for(i = x-1; i <= x+1; i++) g->me.my_matrix[i][y+3] = 1; g->me.planes[g->me.nr_planes].oriented = 3; //the plane has orientation left g->me.planes[g->me.nr_planes].head.pozX = x; g->me.planes[g->me.nr_planes].head.pozY = y; g->me.nr_planes++; if(not_nulls(g)%10 != 0) { g->me.my_matrix[x][y] = 0; for(i = x-2; i <= x+2; i++) g->me.my_matrix[i][y+1] = 0; g->me.my_matrix[x][y+2] = 0; for(i = x-1; i <= x+1; i++) g->me.my_matrix[i][y+3] = 0; g->me.nr_planes--; g->me.planes[g->me.nr_planes].oriented = 0; //the plane has orientation right g->me.planes[g->me.nr_planes].head.pozX = 0; g->me.planes[g->me.nr_planes].head.pozY = 0; intersect(); } break; case '4': g->me.my_matrix[x][y] = 2; for(i = x-2; i <= x+2; i++) g->me.my_matrix[i][y-1] = 1; g->me.my_matrix[x][y-2] = 1;

19

for(i = x-1; i <= x+1; i++) g->me.my_matrix[i][y-3] = 1; g->me.planes[g->me.nr_planes].oriented = 4; //the plane has orientation up g->me.planes[g->me.nr_planes].head.pozX = x; g->me.planes[g->me.nr_planes].head.pozY = y; g->me.nr_planes++; if(not_nulls(g)%10 != 0) { g->me.my_matrix[x][y] = 0; for(i = x-2; i <= x+2; i++) g->me.my_matrix[i][y-1] = 0; g->me.my_matrix[x][y-2] = 0; for(i = x-1; i <= x+1; i++) g->me.my_matrix[i][y-3] = 0; g->me.nr_planes--; g->me.planes[g->me.nr_planes].oriented = 0; //the plane has orientation up g->me.planes[g->me.nr_planes].head.pozX = 0; g->me.planes[g->me.nr_planes].head.pozY = 0; intersect(); } break; default: print_error(); printf("Returning to ORIENTATION MENU..."); getch(); system("cls"); printf("HEAD = %s\n\n", coord); print_submenu(); submenu; break; } } else { printf("The plane with HEAD = %s is intersecting with other plane(s) present in the map!\n", coord); printf("Please provide an appropriate HEAD and Orientation\n"); printf("Returning to MAIN MENU..."); getch(); system("cls"); print_main_menu(); main_menu(); } } } //submenu for orientation of the plane void print_submenu() { printf("+-------------------------------- ORIENTATION ---------------------------------+\n"); printf("| 1. HEAD UP |\n"); printf("| 2. HEAD DOWN |\n"); printf("| 3. HEAD LEFT |\n"); printf("| 4. HEAD RIGHT |\n"); printf("+------------------------------------------------------------------------------+\n"); printf("MAKE A CHOICE!\n\n");

20

} //checks if the coordinates are valid int coord_valid(int x, int y) { int ok = 0; if(x >= 0 && x <= 9 && y >= 0 && y <= 9) ok = 1; return ok; } //adds a plane in the game for the player void add_plane(Game g) { system("cls"); printf("---------------------------------ADD PLANES MENU-----------------------------\n\n"); fflush(stdin); fflush(stdout); printf("Please give the HEAD coordinates, in this mode: <LETTER><NUMBER> like A10/a10.\n"); printf("The letters must be between A-J/a-j, the numbers between 1-10!\n\n\n"); printf("HEAD coordinates: "); scanf("%s", &coord); //transforms the coordinates read to be between 0-9 head_coord(coord); //tests if the coordinates read are between [A-J][1-10] or [a-j][1-10] if (coord_valid(x,y) == 0 ) //coordinates are not valid { printf("ERROR: You introduced non-valid coordinates!\nPlease give the coordinates in this format: [A-J][1-10]/[a-j][1-10]\n"); printf("Returning to ADD PLANES MENU...."); getch(); add_plane(g); } else //coordinates are valid { //we print the submenu with the orientation of the plane print_submenu(); submenu(); } } void submenu2() { char option; int i,j, nr; fflush(stdin); for(i=0; i<3; i++) if(g->pc.planes[i].head.pozX == x && g->pc.planes[i].head.pozY == y) nr = i; scanf("%c", &option); switch(option) { case '1':

21

if(g->pc.planes[nr].oriented == 1) { printf("You guessed the plane!\n"); g->me.guessed++; g->pc.my_matrix_op[x][y] = 2; for(i = y-2; i <= y+2; i++) g->pc.my_matrix_op[x+1][i] = 2; g->pc.my_matrix_op[x+2][y] = 2; for(i = y-1; i <= y+1; i++) g->pc.my_matrix_op[x+3][i] = 2; g->me.score = g->me.score + 100; g->me.planes[nr].state=1; } else printf("You don't have luck! Try again next time with the orientation\n"); break; case '2': if(g->pc.planes[nr].oriented == 2) { printf("You guessed the plane!\n"); g->me.guessed++; g->pc.my_matrix_op[x][y] = 2; for(i = y-2; i <= y+2; i++) g->pc.my_matrix_op[x-1][i] = 2; g->pc.my_matrix_op[x-2][y] = 2; for(i = y-1; i <= y+1; i++) g->pc.my_matrix_op[x-3][i] = 2; g->me.score = g->me.score + 100; g->me.planes[nr].state=1; } else printf("You don't have luck! Try again next time with the orientation\n"); break; case '3': if(g->pc.planes[nr].oriented == 3) { printf("You guessed the plane!\n"); g->me.guessed++; g->pc.my_matrix_op[x][y] = 2; for(i = x-2; i <= x+2; i++) g->pc.my_matrix_op[i][y+1] = 2; g->pc.my_matrix_op[x][y+2] = 2; for(i = x-1; i <= x+1; i++) g->pc.my_matrix_op[i][y+3] = 2; g->me.score = g->me.score + 100; g->me.planes[nr].state=1; } else printf("You don't have luck! Try again next time with the orientation\n"); break; case '4': if(g->pc.planes[nr].oriented == 4) { printf("You guessed the plane!\n"); g->me.guessed++; g->pc.my_matrix_op[x][y] = 2; for(i = x-2; i <= x+2; i++) g->pc.my_matrix_op[i][y-1] = 2; g->pc.my_matrix_op[x][y-2] = 2; for(i = x-1; i <= x+1; i++)

22

g->pc.my_matrix_op[i][y-3] = 2; g->me.score = g->me.score + 100; g->me.planes[nr].state=1; } else printf("You don't have luck! Try again next time with the orientation\n"); break; default: print_error(); print_submenu(); submenu2(); break; } } void submenu3() { int option; int i,j, nr; fflush(stdin); for(i=0; i<3; i++) if(g->me.planes[i].head.pozX == x && g->me.planes[i].head.pozY == y) nr = i; option = (rand()%4) + 1; printf("The computer choosed %d for orientation\n\n", option); switch(option) { case 1: if(g->me.planes[nr].oriented == 1) { printf("You guessed the plane!\n"); g->pc.guessed++; g->me.my_matrix_op[x][y] = 2; for(i = y-2; i <= y+2; i++) g->me.my_matrix_op[x+1][i] = 2; g->me.my_matrix_op[x+2][y] = 2; for(i = y-1; i <= y+1; i++) g->me.my_matrix_op[x+3][i] = 2; g->pc.score = g->pc.score + 100; g->pc.planes[nr].state=1; } else printf("You don't have luck! Try again next time with the orientation\n"); break; case 2: if(g->me.planes[nr].oriented == 2) { printf("You guessed the plane!\n"); g->pc.guessed++; g->me.my_matrix_op[x][y] = 2; for(i = y-2; i <= y+2; i++) g->me.my_matrix_op[x-1][i] = 2; g->me.my_matrix_op[x-2][y] = 2; for(i = y-1; i <= y+1; i++) g->me.my_matrix_op[x-3][i] = 2; g->pc.score = g->pc.score + 100; g->pc.planes[nr].state=1; }

23

else printf("You don't have luck! Try again next time with the orientation\n"); break; case 3: if(g->me.planes[nr].oriented == 3) { printf("You guessed the plane!\n"); g->pc.guessed++; g->me.my_matrix_op[x][y] = 2; for(i = x-2; i <= x+2; i++) g->me.my_matrix_op[i][y+1] = 2; g->me.my_matrix_op[x][y+2] = 2; for(i = x-1; i <= x+1; i++) g->me.my_matrix_op[i][y+3] = 2; g->pc.score = g->pc.score + 100; g->pc.planes[nr].state=1; } else printf("You don't have luck! Try again next time with the orientation\n"); break; case 4: if(g->me.planes[nr].oriented == 4) { printf("You guessed the plane!\n"); g->pc.guessed++; g->me.my_matrix_op[x][y] = 2; for(i = x-2; i <= x+2; i++) g->me.my_matrix_op[i][y-1] = 2; g->me.my_matrix_op[x][y-2] = 2; for(i = x-1; i <= x+1; i++) g->me.my_matrix_op[i][y-3] = 2; g->pc.score = g->pc.score + 100; g->pc.planes[nr].state=1; } else printf("You don't have luck! Try again next time with the orientation\n"); break; } } //main menu of the game void main_menu() { char option; char *name; int i=0, nr_cell=0; int j,k; FILE *f; node *raddix = NULL, *p; information inf; fflush(stdin); fflush(stdout); printf("OPTION SELECTED: "); scanf("%c", &option); //reads a character from screen representing the option in the menu //we use switch..case..to test which option was choosed

24

//if a wrong option will be made an error message will be printed and the main menu will be displayed again switch(option) { case '1': system("cls"); printf("---------------------------------START GAME MENU-----------------------------\n\n"); if(g->me.nr_planes!=3) { printf("ERROR: You don't have enough planes added to begin the game!\n"); printf("You need to add %d more planes.\n\n", 3 - g->me.nr_planes); printf("We are getting back to the main menu to add the planes..."); getch(); system("cls"); print_main_menu(); main_menu(); } else { //reading the data used by computer in the game read_file(fin); g->pc.name = "COMPUTER"; if(changed_name == 0) g->me.name = "PLAYER"; printf("HELLO, %s!\n\n", g->me.name); printf("This is the map with your planes!\n\n"); printf("MAP WITH YOUR PLANES:\n"); getch(); print_matrix(g); printf("Press ENTER to begin the game!\n"); getch(); while(g->state == 0) //while the game is not finished { system("cls"); printf("%s's TURN\n\n", g->me.name); print_matrix_op(g->pc.my_matrix_op); printf("Please insert the coordinates!\n"); printf("%s COORDINATES: ",g->me.name); scanf("%s",&my_coord); head_coord(my_coord); if (coord_valid(x,y) == 0 ) //coordinates are not valid { printf("ERROR: You introduced non-valid coordinates!\n"); printf("Please give the coordinates in this format: [A-J][110]/[a-j][1-10]\n"); getch(); } else { if(g->pc.my_matrix[x][y] == 0) { printf("Your coordinates don't belong to a plane.\n"); g->pc.my_matrix_op[x][y] = -1; g->me.score = g->me.score - 10; } else if(g->pc.my_matrix[x][y] == 1)

25

{ printf("Your coordinates are part of a plane.\n"); g->pc.my_matrix_op[x][y] = 1; g->me.score = g->me.score + 25; } else if(g->pc.my_matrix[x][y] == 2) { printf("Your coordinates represent the head of a plane.\n"); printf("Choose the orientation of the plane to be considered dead.\n\n"); print_submenu(); submenu2(); } if(g->me.guessed == 3) { getch(); system("cls"); print_matrix_op(g->pc.my_matrix_op); g->state = 1; f = fopen("list.txt","at"); fprintf(f,"\n%s %d",g->me.name,g->me.score); printf("The game has finished!\n\n"); printf("%s wins the game with score %d!\n", g>me.name, g->me.score); printf("EXITING..."); getch(); exit(0); } print_matrix_op(g->pc.my_matrix_op); printf("Finished turn %s...", g->me.name); getch(); system("cls"); printf("%s's TURN\n\n", g->pc.name); print_matrix_op(g->me.my_matrix_op); printf("%s's COORDINATES: ",g->pc.name); srand(time(0)); x = rand()%10; y = rand()%10; printf("%c%d\n",x+'A',y+1); if(g->me.my_matrix[x][y] == 0) { printf("Your coordinates don't belong to a plane.\n"); g->me.my_matrix_op[x][y] = -1; g->pc.score = g->pc.score - 10; } else if(g->me.my_matrix[x][y] == 1) { printf("Your coordinates are part of a plane.\n"); g->me.my_matrix_op[x][y] = 1; g->pc.score = g->pc.score + 25;

26

} else if(g->me.my_matrix[x][y] == 2) { printf("Your coordinates represent the head of a plane.\n"); printf("Choose the orientation of the plane to be considered dead.\n\n"); print_submenu(); submenu3(); } if(g->pc.guessed == 3) { g->state = 1; getch(); system("cls"); print_matrix_op(g->me.my_matrix_op); f = fopen("list","at"); fprintf(f,"\n%s %d",g->pc.name,g->pc.score); printf("The game has finished!\n\n"); printf("%s wins the game with score %d!\n", g>pc.name, g->pc.score); printf("EXITING..."); getch(); exit(0); } print_matrix_op(g->me.my_matrix_op); printf("Finished turn %s...", g->pc.name); getch(); system("cls"); } } getch(); } break; case '2': system("cls"); if(g->me.nr_planes!=3) { add_plane(g); printf("You need to add %d more planes to begin the game.\n\n", 3 - g>me.nr_planes); printf("Returning to MAIN MENU for adding the planes..."); getch(); print_main_menu(); main_menu(); } else { printf("YOU added the maximum number of planes!\n"); printf("You can play now!\n"); printf("Returning to MAIN MENU..."); getch(); print_main_menu();

27

main_menu(); } break; case '3': system("cls"); printf("---------------------------------PLAYER'S NAME MENU-----------------------------\n\n"); printf("Please write your name below to change it.\n"); printf("PLAYER'S NAME:"); fflush(stdin); name = (char*)malloc(sizeof(char)); gets(name); strcpy(g->me.name,name); changed_name = 1; //the name has changed for(i = 0; i<strlen(g->me.name); i++) g->me.name[i] = toupper(g->me.name[i]); printf("The player's name has changed to %s.\n",g->me.name); printf("Returning to main menu..."); getch(); system("cls"); print_main_menu(); main_menu(); getch(); break; case '4': system("cls"); printf("+--------------------------------HALL OF FAME MENU----------------------------+\n"); inf.name = (char*)malloc(sizeof(char)); if( (fin = fopen("list.txt","rt")) == NULL) { printf("error!\n"); exit(1); }; while(!feof(fin)) { fscanf(fin,"%s",inf.name); fscanf(fin,"%d",&inf.score); raddix = add_node(raddix,inf); } print_list(raddix); fclose(fin); printf("Press ENTER to get back to MAIN MENU..."); getch(); print_main_menu(); main_menu(); free(raddix); break; break; case '5': exit_menu(); break; default: print_error(); printf("Returning to MAIN MENU..."); getch(); print_main_menu(); main_menu(); break;

28

} } //prints on the screen the main menu of the game void print_main_menu() { system("cls"); printf("+--------------------------------- MAIN MENU ----------------------------------+\n"); printf("| 1. START NEW GAME |\n"); printf("| 2. ADD PLANES |\n"); printf("| 3. ADD PLAYER'S NAME |\n"); printf("| 4. HALL OF FAME |\n"); printf("| 5. EXIT GAME |\n"); printf("+------------------------------------------------------------------------------+\n"); if(changed_name == 0) { printf("Welcome, PLAYER!\n"); printf("If you want to change your name, go to PLAYER'S NAME menu and change it.\n\n"); } else { printf("Welcome, %s!\n",g->me.name); } printf("MAKE A CHOICE!\n"); } //initialises the game void init(Game g) { int i,j; g->state = 0;//state of the game is not finished g->me.nr_planes = 0; //planes nr for player g->me.guessed = 0; //planes guessed by player g->me.score = 0; g->pc.score = 0; g->pc.nr_planes = 0; //planes nr for computer g->pc.guessed = 0; //planes guessed by computer for(i=0; i<10; i++) for(j=0; j<10; j++) { g->me.my_matrix[i][j] = 0; g->me.my_matrix_op[i][j] = 0; g->pc.my_matrix[i][j] = 0; g->pc.my_matrix_op[i][j] = 0; } //memory allocation for name g->me.name = (char*)malloc(sizeof(char)); } //main menu of the program int main() {

29

//memory allocation g = (Game)malloc(sizeof(struct game)); //initializing the data used for the game init(g); //printing the main menu print_main_menu(); //the main menu function main_menu(); return 0; }

Anexe:
Codul surs al programului este urmtorul:
#include <stdio.h> #include <malloc.h> #include <string.h> struct information { int key; int position; }; struct BinaryTreeNode {

30

information info; BinaryTreeNode* left; BinaryTreeNode* right; }; struct infoutil { char* type; char* theColumn; }; struct nodel { infoutil inf; nodel* next; }; struct queue { nodel* first; nodel* last; }; BinaryTreeNode* createNode(information inf, BinaryTreeNode* l, BinaryTreeNode* r) { BinaryTreeNode* temp; temp = (BinaryTreeNode*) malloc(sizeof(BinaryTreeNode)); temp->left = l; temp->right = r; temp->info.key = inf.key; temp->info.position = inf.position; return temp; } BinaryTreeNode* insertNode(BinaryTreeNode* root, information info) { BinaryTreeNode* aux; aux = root; if(!root) { aux=createNode(info,NULL,NULL); return aux; } else { //structura repetitiva la infinit, iese prin return while(1){ if(info.key > aux->info.key) { if(aux->right) aux=aux->right; else { aux->right=createNode(info,NULL,NULL); return root; } } else { if(info.key < aux->info.key) { if (aux->left) aux=aux->left; else { aux->left=createNode(info,NULL,NULL); return root; } } else { printf("\n Nod existent"); return root; } } } } } void writeRLeftRight(BinaryTreeNode* root) { if (root) { printf("\nThe key %d has the position %d", root->info.key, root->info.position); writeRLeftRight(root->left);

31

writeRLeftRight(root->right); } } void writeLeftRRight(BinaryTreeNode* root) { if (root) { writeLeftRRight(root->left); printf("\nThe key %d has the position %d", root->info.key, root->info.position); writeLeftRRight(root->right); } } void writeLeftRightR(BinaryTreeNode* root) { if (root) { writeLeftRightR(root->left); writeLeftRightR(root->right); printf("\nThe key %d has the position %d", root->info.key, root->info.position); } } void put(queue* q, infoutil info) { nodel* t = (nodel*)malloc(sizeof(nodel)); t->inf.theColumn = (char*) malloc(strlen(info.theColumn) + 1); strcpy(t->inf.theColumn,info.theColumn); t->inf.type = (char*) malloc(strlen(info.type) + 1); strcpy(t->inf.type,info.type); t->next = NULL; if(q->last) q->last->next = t; else q->first = t; q->last = t; return; } infoutil get(queue* q) { infoutil elem; elem.type = NULL; elem.theColumn = NULL; if(q->first) { nodel* t = q->first; elem.theColumn = (char*) malloc(strlen(t->inf.theColumn)+1); strcpy(elem.theColumn, t->inf.theColumn); elem.type = (char*) malloc(strlen(t->inf.type)+1); strcpy(elem.type, t->inf.type); q->first = q->first->next; free(t); if(!q->first) q->last = 0x00000000; return elem; } else { printf("\n The queue is empty!!!\n"); return elem; } } void printTheQueue(queue* q) { nodel* t; t = q->first; while(t) { printf("\n Coloana %s are tipul %s", t->inf.theColumn, t->inf.type); t = t->next;

32

} } void parseAndExecuteCommandsFile(char*); FILE* createFileComm(char*, queue*); FILE* openFileComm(char* comm, queue* qFStr); void insertValuesComm(char* comm, FILE* f); void updateValuesComm(char* comm); void deleteValuesComm(char* comm); FILE* selectValuesComm(char*); void indexComm(void); int main(int argc, char** argv) { //preluare din linia de comanda a argumentelor char* filePathCommands = NULL; if (argc > 1) { filePathCommands = (char*)malloc((strlen(argv[1])+1)*sizeof(char)); strcpy(filePathCommands, argv[1]); parseAndExecuteCommandsFile(filePathCommands); free(filePathCommands); } else printf("\n Utilizare:\n TheProject01.exe FisierDeComenzi.txt "); return 0; } void parseAndExecuteCommandsFile(char* fPathComm) { FILE* fdata = NULL, *fcommands = NULL, *gdata = NULL; char line[100]; int nrComm = 0; char seps[] = " ;,\t\n"; queue qFileStructure; if ((fcommands = fopen(fPathComm, "r")) != NULL) { while ( fgets( line, 100, fcommands ) != NULL) { //citeste comenzile din fisier linie cu linie //incepe parsarea comanda cu comanda - linie cu linie din fisierul de comenzi nrComm++; printf("Comanda %d este: %s", nrComm, line); if (line != NULL) { if(strncmp(line, "CREATE", 6) == 0) fdata = createFileComm(line, &qFileStructure); else if(strncmp(line, "OPEN", 4) == 0) fdata = openFileComm(line, &qFileStructure); else if(strncmp(line, "INSERT", 6) == 0) insertValuesComm(strncpy(line, &line[7], strlen(line)), fdata); else if(strncmp(line, "UPDATE", 6) == 0) updateValuesComm(strncpy(line, &line[7], strlen(line))); else if(strncmp(line, "DELETE", 6) == 0) deleteValuesComm(strncpy(line, &line[7], strlen(line))); else if(strncmp(line, "INDEX", 5) == 0) indexComm(); else if(strncmp(line, "SELECT", 6) == 0) gdata = selectValuesComm(line); } } fclose(fcommands); } return; } FILE* createFileComm(char* comm, queue* qFStr) { char seps[] = " ;,\t\n"; char *token; int nrTok = 0; char* fileDataName = NULL; FILE* f; infoutil elem; qFStr->first = NULL; qFStr->last = NULL; token = strtok(comm, seps); while (token != NULL) {

33

nrTok++; printf(" %s\n", token); if (nrTok == 2) { fileDataName = (char*)malloc((strlen(token)+1)*sizeof(char)); strcpy(fileDataName, token); f = fopen(fileDataName, "wr"); } else if (nrTok > 2) { if (nrTok % 2) { elem.theColumn = (char*)malloc((strlen(token)+1)*sizeof(char)); strcpy(elem.theColumn, token); } else { elem.type = (char*)malloc((strlen(token)+1)*sizeof(char)); strcpy(elem.type, token); put(qFStr, elem); } } token = strtok(NULL, seps); } return f; } FILE* openFileComm(char* comm, queue* qFStr) { char seps[] = " ;,\t\n"; char *token; int nrTok = 0; char* fileDataName = NULL; FILE* f; infoutil elem; qFStr->first = NULL; qFStr->last = NULL; token = strtok(comm, seps); while (token != NULL) { nrTok++; printf(" %s\n", token); if (nrTok == 2) { fileDataName = (char*)malloc((strlen(token)+1)*sizeof(char)); strcpy(fileDataName, token); f = fopen(fileDataName, "r+"); } else if (nrTok > 2) { if (nrTok % 2) { elem.theColumn = (char*)malloc((strlen(token)+1)*sizeof(char)); strcpy(elem.theColumn, token); } else { elem.type = (char*)malloc((strlen(token)+1)*sizeof(char)); strcpy(elem.type, token); put(qFStr, elem); } } token = strtok(NULL, seps); } return f; } void insertValuesComm(char* comm, FILE* f) { char seps[] = ";\n"; char *token; token = strtok(comm, seps); fputs(token, f); fputs("\n", f); } void updateValuesComm(char* comm) { char seps[] = ";\n"; char separ[]=","; char *token; char token1[100]; token = strtok(comm, seps);

34

strcpy((char *)token1,token); FILE *f,*g; char s[100],s1[100]; char *t,*tt; t = strtok(token,separ); f=fopen("studenti.csv","r"); g=fopen("temp.csv","w"); rewind(f); fgets(s,100,f); while (!feof(f)) { strcpy(s1,s); tt = strtok((char *)s,separ); if (strcmp(tt,t)!=0) fputs(s1,g); else { fputs(token1,g); fprintf(g,"\n"); } fgets(s,100,f); } fputs("\n",g); fclose(f); fclose(g); f=fopen("studenti.csv","w"); g=fopen("temp.csv","r"); rewind(g); rewind(f); fgets(s,100,g); while(!feof(g)) { fputs(s,f); fgets(s,100,g); } fclose(f); fclose(g); indexComm(); } void deleteValuesComm(char* comm) { char seps[] = " ;\n"; char separ[] = ","; FILE *f,*g; char s[100],s1[100]; char *t,*tt; t = strtok(comm,seps); f=fopen("studenti.csv","r"); g=fopen("temp.csv","w"); rewind(f); fgets(s,100,f); while (!feof(f)) { strcpy(s1,s); tt = strtok((char *)s,separ); if (strcmp(tt,t)!=0) fputs(s1,g); fgets(s,100,f); } fputs("\n",g); fclose(f); fclose(g); f=fopen("studenti.csv","w"); g=fopen("temp.csv","r"); rewind(g); rewind(f); fgets(s,100,g); while(!feof(g)) { fputs(s,f); fgets(s,100,g); }

35

fclose(f); fclose(g); indexComm(); } FILE *selectValuesComm(char *comm) { char seps[] = " ;\t\n"; char separ[] = "<>=,"; char separ1[] = ","; char *token, *tt; char cod1[5], cod2[5], cod3[5]; char *token1,*comm1,*token3; char s[100], s1[100]; int nrTok = 0,nrTok3 = 0; int nr=0,nr1=0; int var=0; char* filename = NULL; FILE *f,*g; comm1=(char *)malloc((strlen(comm)+1)*sizeof(char)); strcpy(comm1,comm); token = strtok(comm, seps); while (token != NULL) { nrTok++; if (nrTok==2) { token1 = strtok(token,separ); while (token1 != NULL) { nr++; if (strcmp(token,"Cod")==0) { var=1; if (nr == 2) strcpy(cod1,token1); if (nr == 4) strcpy(cod2,token1); } else if (strcmp(token,"Nume")==0) { var=2; if (nr == 2) strcpy(cod3,token1); } token1 = strtok(NULL,separ); } } token = strtok(NULL,seps); } token3= strtok(comm1,seps); while (token3 != NULL) { nrTok3++; if (nrTok3 == 4) { filename = (char*)malloc((strlen(token3)+1)*sizeof(char)); strcpy(filename, token3); f = fopen(filename,"w"); } token3=strtok(NULL,seps); } if (var==1) { g=fopen("studenti.csv","r+"); fgets(s,100,g); while (!feof(g)) { strcpy(s1,s); tt = strtok((char *)s,separ1); if ((strcmp(tt,cod1)==1) && (strcmp(cod2,tt)==1)) fputs(s1,f); fgets(s,100,g); } fclose(g); } else if (var==2) { g=fopen("studenti.csv","r+");

36

fgets(s,100,g); while (!feof(g)) { strcpy(s1,s); tt = strtok((char *)s,separ1); while (tt!=NULL) { nr1++; if (nr1==2) { if (strcmp(tt,cod3)==1) fputs(s1,f); nr1=-1;} tt = strtok(NULL,separ1); } fgets(s,100,g); } fclose(g); } return f; } void indexComm(void) { FILE *f,*g; int i=0; char s[100]; f=fopen("studenti.csv","r"); g=fopen("index.csv","w"); fgets(s,100,f); while (!feof(f)) { i++; fprintf(g,"%d",i); fprintf(g," "); fputs(s,g); fgets(s,100,f); } fclose(f); fclose(g); }

37

S-ar putea să vă placă și