By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,188 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,188 IT Pros & Developers. It's quick & easy.

i have a problem with sorting in c

P: 7
sorting People by there names
i create a program that manipulate HR but i have a small problem with sorting People by there names
https://code.hackerearth.com/cdabb5c...4f46d5704a8225
if you can help me to solve it i will be grateful
by the way the problem is in the case 9
Apr 6 '17 #1
Share this Question
Share on Google+
10 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Do you have to write the sort or can you use a utility like qsort?
Apr 6 '17 #2

P: 7
i did but didn't work
Apr 7 '17 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Maybe you could post your code.
Apr 7 '17 #4

P: 7
i had posted the link , (thank you by the way)
Apr 7 '17 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
I can't go to links due to security concerns. Please post your code.
Apr 8 '17 #6

P: 7
this is it , the problem is in the case 9
Apr 8 '17 #7

P: 7
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4. #define l_nom 20
  5. #include<time.h>
  6. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  7. struct dat{
  8.     int jour;
  9.     int mois;
  10.     int anne;
  11. };
  12.  
  13. struct Personne {
  14.   char identifiant[11];
  15.   char nom[20];
  16.   char prenom[20];
  17.   char sexe; 
  18.   struct dat D;
  19.   int nombre_d_enfent ;
  20.   char id_enfent[l_nom][15];
  21.    char enfent[l_nom] ;
  22. };
  23.  
  24. int main(int argc, char *argv[]) {
  25. //time_t mytime;
  26. // SYSTEMTIME NO;
  27. // GetLocalTime (&NO);
  28. int useconds ; 
  29.  
  30.  
  31.  
  32.  
  33.  
  34.     struct Personne novp[200] ;
  35.     int i=0,j,choix,cpt=0;
  36.     char nomfich[]="base de donne p ";
  37.     char nomrech[20];
  38.   struct  Personne temp[200];
  39.        char choix_fich[5];
  40.        char conv[5]="";
  41.        int depa=0;
  42.         FILE *BD;
  43.  
  44. BD=fopen(nomfich,"r");
  45.  
  46.  
  47.             if(BD==NULL)
  48.             {
  49.                 printf("!!!ERREUR!!!!");
  50.                 puts("");
  51.                 puts(" le fichier ne exciste pas , voulez vous ecrire un fichier maintenant ??");
  52.                 gets(choix_fich);
  53.                 if(strcmp(choix_fich,"non")==0){
  54.                                 exit(-1);
  55.  
  56.                 }
  57.                 else{
  58.  
  59.                         BD=fopen(nomfich,"w");
  60.  
  61.                 }
  62.             }
  63.  
  64.         while(fread(&novp[i],sizeof(novp[i]),1,BD),!feof(BD))
  65.         {
  66.         i++;
  67.         cpt++;    
  68.  
  69.         }
  70.         fclose(BD);
  71.     do{
  72.  
  73.         puts("1| ajjouter");
  74.         puts("2| modefier");
  75.         puts("3| supprimer");
  76.         puts("4| afficher");
  77.         puts("5| rechercher");
  78.         puts("6| enregester");
  79.         puts("7| INFO  d enfent : ");
  80.         puts("8|Retourner l'âge d'une personne.    ");
  81.         puts("9| Trier la liste des personnes (selon le nom et l'identifiant))" );
  82.         puts("10|     Ajouter les enfants d une personne  : ");
  83.         puts("11|     afficher la liste des personne  : ");
  84.  
  85.         puts("0| quiter");
  86.         puts("votre choix est : ");
  87.         scanf("%d",&choix);
  88.                               switch(choix)
  89.                               {
  90.  
  91.         case 1: 
  92.               getchar();
  93.  
  94. //system("cls");    
  95.         puts("entre le nom");
  96.         gets(novp[cpt].nom);
  97.         puts("entre le prenom");
  98.         gets(novp[cpt].prenom);
  99.         puts("entre la  date de naissance (jour, mois, année)");
  100.         scanf("%d",&novp[cpt].D.jour);       
  101.                 scanf("%d",&novp[cpt].D.mois);    
  102.                         scanf("%d",&novp[cpt].D.anne);  
  103.                         getchar(); 
  104.                         puts("entre le Sex : M/F") ;
  105.                         scanf("%c",&novp[cpt].sexe);
  106.                                                 getchar(); 
  107.  
  108.                         puts("entre l identifient ");
  109.                         gets(novp[cpt].identifiant);
  110.                         getchar();
  111.  
  112.                         puts("entre le nombre d enfent ");
  113.                         scanf("%d",&novp[cpt].nombre_d_enfent);
  114.                                                                             getchar();
  115.  
  116.                         if(novp[cpt].nombre_d_enfent !=0){
  117.                             for(i=0;i<novp[cpt].nombre_d_enfent;i++){
  118.  
  119.                         puts("entre l id d enfent ");
  120.                         gets(novp[cpt].id_enfent[i]);
  121.  
  122.  
  123. }
  124.  
  125.  
  126.  
  127.  
  128.                         }
  129.  
  130.  
  131.  
  132.         cpt++;
  133.         break;
  134.  
  135.                     //    system("cls");       
  136.  
  137.  
  138.  
  139.         case 2 :
  140.             getchar();
  141.         system("cls");
  142.             if(fread(&novp[i],sizeof(novp[i]),1,BD),!feof(BD))
  143.             {
  144.                                             puts("entre l ID RECHERCHER ");
  145.  
  146.                                             gets(nomrech);
  147.  
  148.                 puts(" choisire que vou devez modifier ");
  149.                             puts(" 1 | ID ");
  150.                              puts(" 2 | Nom ");
  151.                              puts(" 3 | PRENOM");
  152.                             puts(" 4 | DATE DE NICCENSE  ");
  153.                                         scanf("%d",&choix);
  154.                                         getchar();
  155. switch(choix) {
  156.     case 1:
  157.         puts("entre le nouveau ID :");
  158.         gets(novp[i].identifiant);
  159.         break;
  160.     case 2:
  161.         puts("entre le nouveau NOM:");
  162.         gets(novp[i].nom);
  163.         break;
  164.         case 3:
  165.                 puts("entre le nouveau NOM:");
  166.         gets(novp[i].prenom);
  167.             break;
  168. case 4:
  169.  
  170.         puts("1| Modifier le jour ");
  171. //        gets(novp[i].D.jour);
  172.         puts("2| Modifier le mois ");
  173. //        gets(novp[i].D.mois);
  174.         puts("3|Modifier l anne ");
  175. //        gets(novp[i].D.anne);
  176.         scanf("%d",&choix);
  177.  
  178.         if(choix==1){
  179.             puts("entre  le jour ");
  180.             gets(novp[i].D.jour);
  181.  
  182.         }
  183.         else if(choix ==2){
  184.             puts("entre  le mois ");
  185.             gets(novp[i].D.mois);
  186.  
  187.         }
  188.  
  189.         else{
  190.             puts("entre  l anne ");
  191.             gets(novp[i].D.anne);
  192.         }
  193.     break;
  194.  
  195.     default:puts(" les INFO MODIFIER AVEC SUCCES");
  196. }
  197.  
  198.  
  199.  
  200. //            puts("entre le nom rechercher");
  201. //                gets(nomrech);
  202. }
  203. else{
  204.     puts(" l id rechercher est introuvable");
  205. }
  206.             for(i=0;i<cpt;i++){
  207.  
  208.                 if(strcmp(nomrech,novp[i].nom)==0){
  209.                     puts("entre la date de naissance (jour, mois, année)");
  210.                     scanf("%d",&novp[i].D.jour);
  211.                                         scanf("%d",&novp[i].D.mois);
  212.                                                             scanf("%d",&novp[i].D.anne);
  213.  
  214.  
  215.  
  216.                 }
  217.  
  218.                 }
  219.                             fclose(BD);
  220.                            break;  
  221.                 case 3 :
  222.                                 getchar();
  223. system("cls");
  224. puts("entre le nom que vous volez supprimer ");
  225.                 gets(nomrech);
  226.                     for(i=0;i<cpt;i++){
  227.  
  228.                 if(strcmp(nomrech,novp[i].nom)==0){
  229.                     novp[i]=novp[i+1];
  230.                     cpt--;
  231.  
  232.                 }
  233.  
  234.                 }
  235.                                break;
  236.  
  237.                 case 4 :
  238.                                 getchar();
  239.                             //    system("cls");    
  240.                  int size = ftell(BD); 
  241.        if (size == 0){
  242.     printf("\n");
  243.     printf(" !!!!!!!!!!!!!!!!!!!!!!! File is Empty  !!!!!!!!!!!!!!!!!!!!!!!");
  244. puts("");
  245. }
  246.  
  247. for(i=0;i<cpt;i++) 
  248.             {
  249.  
  250.  
  251.  
  252.             for(i=0;i<cpt;i++)
  253.                 {
  254.                     puts("_________________________________________________");
  255.                     printf(" nom : ");
  256.                     puts(novp[i].nom);
  257.  
  258.                     printf("prenom : ");
  259.                     puts(novp[i].prenom);
  260.  
  261.                     printf(" date de naissance (jour, mois, année): ");
  262.                                         puts("");
  263.  
  264.                     printf("%d / %d /%d",novp[i].D.jour,novp[i].D.mois,novp[i].D.anne);
  265.                     puts("");
  266.                     printf(" le Sexe :");
  267.                     printf("%c",novp[i].sexe);
  268.                     puts("");
  269.                                         printf(" ID :  %s  " ,novp[i].identifiant);
  270.                     puts("");
  271.  
  272.                     printf(" le nombre d enfent : ");
  273.                     printf("%d",novp[i].nombre_d_enfent);
  274.                     puts("");
  275.                         if(novp[i].nombre_d_enfent !=0){
  276.                         //    depa=i;
  277.                         int j;
  278.                      for(j=0;j<novp[i].nombre_d_enfent;j++){
  279.                             printf("   l enfent %d  : %s ",j+1,novp[i].id_enfent[j]);
  280.                             puts("");
  281.                         }
  282. }
  283.  
  284.  
  285.  
  286.                 }
  287.                 puts("_____________________________________");
  288.                 }
  289.                 break;
  290.  
  291.  
  292.                         case 5 :
  293.                                         getchar();
  294.                                     //    system("cls");
  295.                                         if(fread(&novp[i],sizeof(novp),1,BD),!feof(BD))
  296.                 {
  297.                                         puts(" Rechercher Par : ");
  298.                             puts(" 1 | ID ");
  299.                              puts(" 2 | Nom ");
  300.                              puts(" 3 | PRENOM");
  301.                                         scanf("%d",&choix);
  302.                                         getchar();
  303. if(choix ==1 ){
  304.  
  305.         puts("entre le  ID :");
  306.              gets(nomrech);
  307.          }
  308. else if(choix == 2){
  309.  
  310.         puts("entre le  NOM:");
  311.              gets(nomrech);
  312.     }
  313.         else{
  314.  
  315.             puts("entre le  PRENOM:");
  316.              gets(nomrech);
  317.  
  318.         }
  319.  
  320.  
  321.  
  322.  
  323.                             for(i=0;i<cpt;i++){
  324.                 if(strcmp(nomrech,novp[i].identifiant)==0 || strcmp(nomrech,novp[i].nom)==0 || strcmp(nomrech,novp[i].prenom)==0 ){
  325.                     puts("_________________________________________");
  326.                                         printf("");
  327.  
  328.                     puts(novp[i].prenom);
  329.                     puts(novp[i].nom);
  330.                     puts(novp[i].identifiant);
  331.                     printf("%c",novp[i].sexe);
  332.                                                                              puts("");
  333.  
  334.                     printf("la date de la date de naissance %d / %d / %d",novp[i].D.jour, novp[i].D.mois,novp[i].D.anne);
  335.                                                                                                  puts("");
  336.  
  337.                     puts(novp[i].nombre_d_enfent);
  338.                     puts(novp[i].id_enfent[i]);
  339.  
  340.  
  341.  
  342.                                                          puts("");
  343.  
  344.  
  345.  
  346.                     puts("_________________________________________");
  347.  
  348.  
  349.  
  350.  
  351.                 }
  352.  
  353.                }
  354.            }
  355.  
  356.                fclose(BD);
  357.  
  358.             break;
  359.  
  360.                case 6 :
  361.                                getchar();
  362. //system("cls");
  363.              BD=fopen(nomfich,"w");
  364.  
  365.             if(BD==NULL)
  366.             {
  367.                 printf("!!!!!!!!!!!ERREUR!!!!!!!!!!!!!");
  368.                 exit(-1);
  369.             }
  370.             for(i=0;i<cpt;i++) 
  371.             {
  372.                 fwrite(&novp[i] ,sizeof(novp[i]),1,BD);
  373.             }
  374.  
  375.  
  376.             fclose(BD);
  377.  
  378.             puts("_____________________________________________________________________________");
  379.             break;
  380.             case 7:
  381.                 getchar();
  382.  
  383.                 if(fread(&novp[i],sizeof(novp),1,BD),!feof(BD))
  384.                 {
  385.                                     puts("entre l ID de l enfent");
  386.                                      gets(nomrech);
  387. }
  388.                             for(i=0;i<cpt;i++){
  389.                 if(strcmp(nomrech,novp[i].id_enfent) ==0){
  390.                     puts("_________________________________________");
  391.             //        puts(novp[cpt].t.prenome);
  392.                     printf("le parent est : %s  %s",novp[i].nom ,novp[i].prenom);
  393.                     puts("");
  394.  
  395.  
  396.                     puts("_________________________________________");
  397.  
  398.  
  399.                 break;
  400.                             case 8:
  401.                                 getchar();
  402.                                 puts("entre le nom de person que vous voulez Retourner l'âge ");
  403.                                 gets(nomrech);
  404.                                                     int years  ;
  405.  
  406.                                 puts("entre l anne acctuel");
  407.                                 scanf("%d",&years);    
  408.  
  409.                                 for(i=0;i<cpt;i++){  
  410.  
  411.                 if(strcmp(nomrech,novp[i].nom)==0){
  412.                     puts("_________________________________________");
  413.                                                                                int a=novp[i].D.anne;
  414.  
  415. //                    years=novp[cpt].D.anne;
  416.                     int diff=years -a;
  417.                                     //    years=diff-a;
  418.  
  419.                       printf("%d  ANS", diff );
  420.  
  421.  
  422. puts("");
  423.                     puts("_________________________________________");
  424.  
  425.  
  426.  
  427.  
  428.                 }
  429.  
  430.                }
  431.                         break ;        
  432.                     case 9 :
  433.  
  434.                     for(i=0; i<cpt-1; i++)
  435.     {
  436.  
  437.       temp=novp[i];
  438.         for(j=i+1; j<cpt; j++)
  439.         {
  440.           if(strcmp(temp, novp[j].nom )> 0)
  441. //if(qsort(novp[cpt].nom, cpt, sizeof(char), strcmp(novp[i].nom,novp[j].nom));
  442.  
  443.             {
  444.                   temp[i]=novp[j];
  445.                   novp[j]=novp[i];
  446.                   novp[i]=temp;
  447. //                  strcpy (novp[i].nom,temp[i]);
  448.  
  449.             }
  450.  
  451.         }
  452.     }
  453.  
  454.     for(i=0; i<cpt; i++)
  455.     {
  456.             puts("_________________________________________");
  457.         printf("%s",novp[i].nom);
  458.         puts("");
  459.             puts("_________________________________________");
  460.     }
  461.  
  462.  
  463.     printf("\n");
  464.  
  465.  
  466.                         break;
  467.                                                        int enf;
  468.  
  469.                                         case 10:   
  470.                                         getchar();
  471.                                         puts("les enfents de quell personne ?");
  472.                                         gets(nomrech);
  473.                                         for(i=0;i<cpt;i++){
  474.  
  475.         if(strcmp(nomrech,novp[i].identifiant)==0){
  476.  
  477.  
  478.  
  479.                                puts("combien d enfent ?");
  480.  
  481.                                    scanf("%d",&enf);
  482.                                                                            getchar();
  483.  
  484.                                    for(j=novp[j].nombre_d_enfent;i<novp[j].identifiant+enf;j++){
  485.  
  486.  
  487.                                        printf("entre l ID d enfent %d :" , i+1);
  488.                                                                               puts("");
  489.  
  490.                                        gets(novp[j].id_enfent[j]);
  491.                                    //    gets(E.prenome);
  492.  
  493.  
  494.                                        //    gets(E.sexen);
  495.  
  496.  
  497.  
  498.                                    puts(" ajutter avec sucsses ");
  499.                            novp[j].identifiant=novp[j].identifiant+enf;
  500.  
  501.                                }
  502.  
  503.  
  504.  
  505.  
  506.                             }
  507.  
  508.                             }        
  509.                                         break;      
  510.  
  511.  
  512.                             case 11:
  513. system("cls");
  514.                                  break;
  515.  
  516.  
  517.                 case 0:         
  518.                         getchar();
  519.  
  520.         //    system("cls");
  521.             exit(-1);
  522.  
  523.             break;            
  524.  
  525.  
  526.                 }
  527.  
  528.         }
  529.     }
  530.  
  531.             }while(choix >=0 && choix <=9);
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.     return 0;
  541. }
  542.  
Apr 8 '17 #8

P: 7
i had tried all my best , but without any result
Apr 8 '17 #9

weaknessforcats
Expert Mod 5K+
P: 9,197
The code you posted won't compile. I can't help until the code compiles.
Apr 8 '17 #10

P: 7
oh , thanks anyway ;)
Apr 9 '17 #11

Post your reply

Sign in to post your reply or Sign up for a free account.