Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <string.h>
- #include <stdio.h>
- typedef enum{anuala,bianuala,perena}tip_planta;
- typedef enum{primavara,vara,toamna}perioada_de_inflorire;
- typedef struct nod_a{
- char tip_p[50];
- float pret;
- tip_planta tp;
- perioada_de_inflorire pi;
- struct nod_a *st, *dr;
- }nod;
- nod *root=NULL;
- nod *adauga(nod *t, char nume[50], tip_planta tp, float p, perioada_de_inflorire pi)
- {
- if(t==NULL)
- {
- t=(nod *)malloc(sizeof(nod)+1);
- strcpy(t->tip_p,nume);
- t->pret=p;
- t->tp=tp;
- t->pi=pi;
- t->st=t->dr=NULL;
- }
- else if(strcmp(nume, t->tip_p)<0)
- t->st=adauga(t->st,nume,tp,p,pi);
- else if(strcmp(nume,t->tip_p)>0)
- t->dr=adauga(t->dr,nume,tp,p,pi);
- else
- printf("\nNodul nu exista! ");
- return t;
- }
- void afisare(nod *t)
- {
- if(t!=NULL)
- {
- afisare(t->st);
- printf("\n%s %d %f %d",t->tip_p,t->tp,t->pret,t->pi);
- afisare(t->dr);
- }
- }
- void citire(){
- FILE *f;
- char nume[50];
- tip_planta tp;
- float pret;
- perioada_de_inflorire pi;
- f=fopen("plante.txt","rt");
- if(f==NULL)
- printf("\nFisierul nu exista! ");
- else
- {
- while(!feof(f))
- {
- fscanf(f,"%s %d %f %d",nume,&tp,&pret,&pi);
- root=adauga(root,nume,tp,pret,pi);
- }
- }
- }
- nod *cautare(nod *r, char *numeplanta)
- {
- if(r==NULL)
- return NULL;
- else
- if(strcmp(numeplanta,r->tip_p)<0)
- return cautare(r->st,numeplanta);
- else
- if(strcmp(numeplanta,r->tip_p)>0)
- return cautare(r->dr,numeplanta);
- else
- return r;
- }
- void salvare_fisier(char nume[50]){
- FILE *f, *g;
- nod *p;
- float pret_total=0;
- f=fopen("plante.txt","rt");
- g=fopen("fact.txt","wt");
- while(!feof(f))
- {
- printf("\nDati numele plantei cautate: ");
- scanf("%s",nume);
- if(strcmp(nume,"gata")==0)
- break;
- else
- {
- p=cautare(root,nume);
- if(p!=NULL)
- {
- fprintf(g,"\n%s %d %f %d",p->tip_p,p->tp,p->pret,p->pi);
- pret_total=pret_total+p->pret;
- }
- else
- printf("\nPlanta nu exista! ");
- }
- }
- fprintf(g, "\nPretul total este: %f", pret_total);
- fclose(f);
- fclose(g);
- }
- nod *supred(nod *t, nod *p)
- {
- nod *q, *aux;
- q=t;
- if(q->dr!=NULL)
- q->dr=supred(q->dr,p);
- else
- {
- strcpy(p->tip_p,q->tip_p);
- p->tp=q->tp;
- p->pret=q->pret;
- p->pi=q->pi;
- aux=q;
- q=q->st;
- free(aux);
- }
- return q;
- }
- nod *stergere(nod *t, char *num)
- {
- nod *aux;
- if(t==NULL)
- printf("\nNodul nu exista! ");
- else
- {
- if(strcmp(num,t->tip_p)<0)
- t->st=stergere(t->st,num);
- else
- if(strcmp(num,t->tip_p)>0)
- t->dr=stergere(t->dr,num);
- else
- {
- if(t->st==NULL)
- {
- aux=t;
- t=t->dr;
- free(aux);
- }
- else if(t->dr==NULL)
- {
- aux=t;
- t=t->st;
- free(aux);
- }
- else
- t->st=supred(t->st,t);
- }
- }
- return t;
- }
- int main()
- {
- char n[50], planta[50];
- citire();
- afisare(root);
- getch();
- printf("\nZi planta: ");
- scanf("%s",n);
- stergere(root,n);
- printf("\n\n");
- afisare(root);
- printf("\nCe planta vrei sa salvezi in fisier: ");
- scanf("%s",planta);
- salvare_fisier(planta);
- system("pause");
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement