Advertisement
Adytzu04

l12p1

May 15th, 2012
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. //head
  2.  
  3. void *xmalloc(unsigned int nrOcteti);
  4. int citireText(char *text[],int LMax);
  5. void afisareText(char *text[], int n);
  6. void ordonareText(char *text[],int n);
  7. void swapL(char **l1, char **l2);
  8. void eliberareMemorie(char *text[],int n);
  9. void ordonareText2(char *text[],int n);
  10.  
  11.  
  12. //c
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include "l12p1.h"
  17.  
  18. void *xmalloc(unsigned int nrOcteti)
  19. {
  20.     void *p;
  21.     p = malloc(nrOcteti);
  22.     if(!p)
  23.     {
  24.             fprintf(stderr,"Memorie insuficienta");
  25.             exit(EXIT_FAILURE);
  26.     }
  27.     return p;
  28.  
  29. }
  30.  
  31.  
  32. int citireText(char *text[],int LMax)
  33. {
  34.         int nl=0;
  35.         char temp[82];
  36.         int lung;
  37.         printf("\nIntroduceti liniile end cu CTRL+Z!!!\n");
  38.         while(nl<LMax && fgets(temp,81,stdin))
  39.         {
  40.             lung=strlen(temp);
  41.             *(temp+lung)='\0';
  42.             text[nl]=(char*)xmalloc((strlen(temp)+1)*sizeof(char));
  43.             strcpy(text[nl],temp);
  44.             nl++;
  45.         }
  46.         return nl;
  47. }
  48.  
  49. void afisareText(char *text[], int n)
  50. {
  51.     int i;
  52.     printf("\n");
  53.     for(i=0;i<n;i++)
  54.         puts(text[i]);
  55. }
  56.  
  57. void ordonareText(char *text[],int n)
  58. {
  59.     int i,ok,k=n-1;
  60.     do
  61.         {
  62.             ok=1;
  63.             for(i=0;i<k;i++)
  64.             {
  65.                 if(strcmp(text[i],text[i+1])>0)
  66.                 {
  67.                     swapL(&text[i],&text[i+1]);
  68.                     ok=0;
  69.                 }
  70.             }k--;
  71.         }while((!ok) && (k>0));
  72. }
  73.  
  74. void swapL(char **l1, char **l2)
  75. {
  76.     char *aux;
  77.     aux=*l1;
  78.     *l1=*l2;
  79.     *l2=aux;
  80. }
  81.  
  82. void eliberareMemorie(char *text[],int n)
  83. {
  84.     int i;
  85.     for(i=0;i<n;i++)
  86.     {
  87.         free(text[i]);
  88.         text[i]=0;
  89.     }
  90. }
  91.  
  92. void ordonareText2(char *text[],int n)
  93. {
  94.     int i,ok,k=n-1;
  95.         do
  96.             {
  97.                 ok=1;
  98.                 for(i=0;i<k;i++)
  99.                 {
  100.                     if(strlen(text[i])>strlen(text[i+1]))
  101.                     {
  102.                         swapL(&text[i],&text[i+1]);
  103.                         ok=0;
  104.                     }
  105.                 }k--;
  106.             }while((!ok) && (k>0));
  107. }
  108.  
  109. //main
  110.  
  111. /*
  112.  * main.c
  113.  *
  114.  *  Created on: May 15, 2012
  115.  *      Author: lab
  116.  */
  117.  
  118. #include <stdio.h>
  119. #include <string.h>
  120. #include <stdlib.h>
  121. #include "l12p1.h"
  122.  
  123. int main(void)
  124. {
  125.     char *text[50];
  126.     int nl=0,opt;
  127.     nl=citireText(text,50);
  128.  
  129.     if(nl>0)
  130.         { puts("Textul citit este:\n ");
  131.           afisareText(text, nl);
  132.  
  133.  
  134.         do{
  135.             printf("1. Ordonati textul alfabetic.\n");
  136.             printf("2. Ordonati dupa lungime.\n");
  137.             printf("3. Exit\n");
  138.  
  139.             scanf("%d",&opt);
  140.  
  141.             switch(opt)
  142.             {
  143.         case 1:
  144.  
  145.             ordonareText(text, nl);
  146.             puts("Textul ordonat este:");
  147.             afisareText(text,nl);
  148.             eliberareMemorie(text,nl);
  149.  
  150.         break;
  151.  
  152.         case 2:
  153.             ordonareText2(text, nl);
  154.             puts("Textul ordonat este:");
  155.             afisareText(text,nl);
  156.             eliberareMemorie(text,nl);
  157.  
  158.             break;
  159.  
  160.         case 3:
  161.             break;
  162.  
  163.         }
  164.     }while(opt<3);
  165.         }
  166.     else
  167.         fprintf(stderr,"Nu exista text!");
  168.  
  169.  
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement