Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //head
- void *xmalloc(unsigned int nrOcteti);
- int citireText(char *text[],int LMax);
- void afisareText(char *text[], int n);
- void ordonareText(char *text[],int n);
- void swapL(char **l1, char **l2);
- void eliberareMemorie(char *text[],int n);
- void ordonareText2(char *text[],int n);
- //c
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "l12p1.h"
- void *xmalloc(unsigned int nrOcteti)
- {
- void *p;
- p = malloc(nrOcteti);
- if(!p)
- {
- fprintf(stderr,"Memorie insuficienta");
- exit(EXIT_FAILURE);
- }
- return p;
- }
- int citireText(char *text[],int LMax)
- {
- int nl=0;
- char temp[82];
- int lung;
- printf("\nIntroduceti liniile end cu CTRL+Z!!!\n");
- while(nl<LMax && fgets(temp,81,stdin))
- {
- lung=strlen(temp);
- *(temp+lung)='\0';
- text[nl]=(char*)xmalloc((strlen(temp)+1)*sizeof(char));
- strcpy(text[nl],temp);
- nl++;
- }
- return nl;
- }
- void afisareText(char *text[], int n)
- {
- int i;
- printf("\n");
- for(i=0;i<n;i++)
- puts(text[i]);
- }
- void ordonareText(char *text[],int n)
- {
- int i,ok,k=n-1;
- do
- {
- ok=1;
- for(i=0;i<k;i++)
- {
- if(strcmp(text[i],text[i+1])>0)
- {
- swapL(&text[i],&text[i+1]);
- ok=0;
- }
- }k--;
- }while((!ok) && (k>0));
- }
- void swapL(char **l1, char **l2)
- {
- char *aux;
- aux=*l1;
- *l1=*l2;
- *l2=aux;
- }
- void eliberareMemorie(char *text[],int n)
- {
- int i;
- for(i=0;i<n;i++)
- {
- free(text[i]);
- text[i]=0;
- }
- }
- void ordonareText2(char *text[],int n)
- {
- int i,ok,k=n-1;
- do
- {
- ok=1;
- for(i=0;i<k;i++)
- {
- if(strlen(text[i])>strlen(text[i+1]))
- {
- swapL(&text[i],&text[i+1]);
- ok=0;
- }
- }k--;
- }while((!ok) && (k>0));
- }
- //main
- /*
- * main.c
- *
- * Created on: May 15, 2012
- * Author: lab
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "l12p1.h"
- int main(void)
- {
- char *text[50];
- int nl=0,opt;
- nl=citireText(text,50);
- if(nl>0)
- { puts("Textul citit este:\n ");
- afisareText(text, nl);
- do{
- printf("1. Ordonati textul alfabetic.\n");
- printf("2. Ordonati dupa lungime.\n");
- printf("3. Exit\n");
- scanf("%d",&opt);
- switch(opt)
- {
- case 1:
- ordonareText(text, nl);
- puts("Textul ordonat este:");
- afisareText(text,nl);
- eliberareMemorie(text,nl);
- break;
- case 2:
- ordonareText2(text, nl);
- puts("Textul ordonat este:");
- afisareText(text,nl);
- eliberareMemorie(text,nl);
- break;
- case 3:
- break;
- }
- }while(opt<3);
- }
- else
- fprintf(stderr,"Nu exista text!");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement