Advertisement
userxbw

Crazy argc argv voting program C

Sep 11th, 2022
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.57 KB | None | 0 0
  1.  
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7.     struct Elections
  8.     { /* don't skimp on size for name */
  9.         char name[1024];
  10.         char * n1;
  11.         //int votes;
  12.         /* testing purposes */
  13.         char value[1024];
  14.         char *v1;
  15.     };
  16.  
  17.  
  18.       char * get_can_name_votes(char *s);
  19.       int chop_string(char * s, struct Elections *e,
  20.       int c);
  21.  
  22. /*    if pattern is, name, value
  23.  
  24.       sequential in sets of 2
  25.       [ num x]     name == argc 1
  26.       [ num x]     value == argc 2
  27.  
  28.       anything more is discaeded/not gotten
  29.       quit command is a single char len 1
  30.  
  31. */
  32.  
  33.  
  34.       #define MAX_A 6
  35.  
  36. int main(int argc, char *argv[]) {
  37.     struct Elections egor[MAX_A];
  38.     int  count=0,c=0,r;
  39.     char run='y';
  40.     char *string;
  41.       for(int i=0;i<argc;i++)
  42.       printf("main top\n"
  43.       "argc %d : %d argv %s\ncount %d",argc,i,argv[i],count);
  44.       c=argc;
  45.       while(run == 'y'){
  46.       /* just the program name */
  47.             switch(c){
  48.                   case 1:
  49.                        r = chop_string(
  50.                          get_can_name_votes(string),
  51.                          egor,count);
  52.                          switch(r){
  53.                               case 0: /*bad input */
  54.                                     printf("ERROR\n"
  55.                                           "bad input\n");
  56.                                    c=1;
  57.                                    continue;
  58.                               break;
  59.                               case 1: /* good */
  60.                                     c=1;
  61.                                     count++;
  62.                                     continue;
  63.                               break;
  64.                               case 2:
  65.                                   run='n';
  66.                              break;
  67.                              } // end inner switch
  68.                   break;
  69.                   case 2: /* program name & 1 of
  70.                               something else
  71.                               if it's not q || Q
  72.                           */
  73.                               if((strcmp(argv[1],"q")==160)||
  74.                                  (strcmp(argv[1],"Q")==-128)){
  75.                                           run='n';
  76.                             }else{
  77.                               printf("\nerror %s\n",argv[1]);
  78.                               c=1;
  79.                               continue;
  80.                               }
  81.                   break;
  82.                   case 3:/* program & 2 other things
  83.                             hopfully name & votes */
  84.                         printf("OFF THE LINE\n"
  85.                         "count= %d\n"
  86.                         "%s argv[1], %s argc[2]\n",
  87.                         count,argv[1],argv[2]);
  88.                         strcpy(egor[count].name,argv[1]);
  89.                         egor[count].n1=strdup(argv[1]);
  90.                         /*
  91.                         needs stoi and error checking
  92.                         if integer
  93.                         */
  94.                         strcpy(egor[count].value,argv[2]);
  95.                         egor[count].v1=strdup(argv[2]);
  96.                         count++;
  97.                         c=1;
  98.                   printf("\nOFF THE LINE POST\n"
  99.                     "%s \n%s\n%d\n",
  100.                     egor[count-1].name,egor[count-1].value,
  101.                     count);
  102.                   break;
  103.                   default:
  104.  
  105.                   break;
  106.                   } // end switch
  107.            if(count == MAX_A) run='n';
  108.       } // end while
  109.  
  110.    printf("PRINT RESULTS\n"
  111.    " end while\ncount = %d\n",count);
  112.    for(int i=0;i<count;i++)
  113.       printf("end of  main\n"
  114.       " egor[%d].name %s\n "
  115.       " egor[%d].n1 %s\n "
  116.  
  117.       "egor[%d].value %s\n"
  118.       "egor[%d].v1 %s\n",
  119.       i,egor[i].name,
  120.       i,egor[i].n1,
  121.       i,egor[i].value,
  122.       i,egor[i].v1);
  123.  
  124.  
  125. /*
  126.  int k;
  127.   for(k=count;k>=0;k--){
  128.    printf("\n\nFREE ME\n"
  129.       "%d %d\n",count,k);
  130.       EVEN I QUESTION
  131.       THIS.
  132.       DOES THAT RELEASE
  133.       EVERYTHING STRDUPPED?
  134.       */
  135.  
  136.       free(egor[0].n1);
  137.       free(egor[0].v1);
  138.   //}
  139.  
  140.  
  141.         return 0;
  142. }
  143. char * get_can_name_votes(char *s)
  144.     {
  145.         s = malloc(255);
  146.         puts("Type name and votes \n"
  147.              "or q to quit\n");
  148.         fgets(s, 255, stdin);
  149.         return s;
  150.     }
  151.  
  152.  
  153. int chop_string(char * s, struct Elections *e,int c)
  154. {
  155.       int i=0;
  156.       char *temp;
  157.      char *tok = strtok_r(s," ",&temp);
  158.  
  159.       printf("functon s=%s\n"
  160.       "strlen s %lu\n"
  161.       " tok = %s\n"
  162.       "strcmp s q %d\n",s,
  163.       strlen(s),tok,strcmp(s,"q"));
  164.  
  165.       printf("c count= %d\n",c);
  166.       if((strlen(s)==2) && (strcmp(s,"q") == 160) ||
  167.       (strcmp(s,"Q") == -128)){
  168.             free(s);
  169.             return 2;
  170.  
  171.       }
  172.       /* we're only getting 2 at a time
  173.          2nd needs stoi and checking
  174.          if number or not */
  175.          printf("chop\nbefore for loop\n"
  176.          "c=%d\n",c);
  177.  
  178.       /* neeed 2 if only 1 report error */
  179.      for(i=0;i<1;i++){
  180.       printf("INSIDE for i=%d tok\ns=%s\n",i,s);
  181.            e[c].n1=strdup(tok);
  182.            strcpy(e[c].name,tok);
  183.  
  184.             printf("t %d tok = %s\n",i,tok);
  185.             tok=strtok_r(NULL," ",&temp);
  186.             printf("b %d tok = %s\n\n",i,tok);
  187.             if(tok == NULL)
  188.             {
  189.                   free(s);
  190.                   return 0;
  191.              }
  192.             e[c].v1=strdup(tok);
  193.             strcpy(e[c].value,tok);
  194.             i++;
  195.       }  // end loop
  196.       free(s);
  197.       printf("\nchop\nreturn true\n");
  198.   return 1;
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement