Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.  
  7.     int i=0, k=0; // Common variables for loops
  8.  
  9.  
  10. // -----------------------------
  11.     char pairs[100][20];    // here would be extracted pairs in text format
  12.  
  13.     for(i=0;i<100;i++)      // to avoid garbage set all to zero
  14.         for(k=0; k<20; k++)
  15.             pairs[i][k] = 0;
  16. // -----------------------------
  17.  
  18.     int bound[100][2];      // here would be pairs in int format (the result)
  19.  
  20.  
  21.  
  22. // READING IN "text" UNTIL EOF
  23.     int c;
  24.     char text[100]="";
  25.     int n=0;
  26.     c = getchar();
  27.  
  28.     while(c!=EOF){
  29.  
  30.         text[n] = (char) c;
  31.         c = getchar();
  32.         n++;
  33.     }
  34.         n--; // Amount of characters entered
  35.  
  36. //-----------------------------
  37.  
  38.  
  39.  
  40. // EXTRACTING PAIRS FROM "text" to "pairs[i]"
  41.  
  42.     int start, stop; // indexes of < and >
  43.     int p=0; // Amount of pairs
  44.  
  45.     while(p<=n){ // extra condition (if zero input)
  46.  
  47.  
  48.         printf("\n\t[debug] text = %s", text);
  49.  
  50.  
  51.         // initialize
  52.         start = -1;
  53.         stop = -1;
  54.  
  55.  
  56.         // finding indexes
  57.         for(i=0; i<=n; i++)
  58.             if(text[i] == '<'){
  59.                 start = i;
  60.                 printf("\n\t\t[debug] start=%i", start);
  61.                 break;
  62.             }
  63.  
  64.         for(i=0; i<=n; i++)
  65.             if(text[i] == '>'){
  66.                 stop = i;
  67.                 printf("\n\t\t[debug] stop=%i", stop);
  68.                 break;
  69.             }
  70.  
  71.  
  72.         k=0;
  73.         if(start!=-1 && stop!=-1){ // if found
  74.             for(i=start; i<=stop; i++){ // fill pairs[p] with text between < and > in main variable
  75.                 pairs[p][k]=text[i];
  76.                 text[i] = ' '; // and erase it from main variable
  77.                 k++;
  78.  
  79.             }
  80.  
  81.             printf("\n\t[debug] extracted pair = %s", pairs[p]);
  82.             p++; // increase amount of already founded pairs
  83.         }
  84.         else{
  85.             printf("\n\t[debug] Searching done");
  86.             break; // exit loop when nothing found (because we erased every found pair)
  87.  
  88.         }
  89.  
  90.         printf("\n");
  91.  
  92.     }
  93.  
  94. // ---------------------------------------------
  95.  
  96.  
  97.  
  98.  
  99. // CONVERTING WITH FILTER FROM "pairs[i]" TO "bound[i]"
  100.  
  101.     printf("\n");
  102.     printf("\n\t[debug]Converting started:\n");
  103.  
  104.     int dc, num;
  105.  
  106.     for(i=0; i<p; i++){
  107.  
  108.         printf("\n\t[debug] scanning: %s", pairs[i]);
  109.  
  110.  
  111.         dc=0;   // Amount of dot-coma
  112.         for(k=0; k<20; k++)
  113.             if(pairs[i][k] == ';')
  114.                 dc++;
  115.  
  116.         if(dc==1){
  117.             num = sscanf(pairs[i], "<%d; %d>", &bound[i][0], &bound[i][1]);
  118.             if(num!=2){
  119.                 printf("\n\t[debug] error reading value");
  120.                 bound[i][0] = 0;
  121.                 bound[i][1] = 0;
  122.             }
  123.  
  124.         }else{
  125.             printf("\n\t[debug] too much or no ';'");
  126.             bound[i][0] = 0;
  127.             bound[i][1] = 0;
  128.         }
  129.  
  130.         printf("\n\t[debug] scanned in this pair: %d %d", bound[i][0], bound[i][1]);
  131.         printf("\n");
  132.     }
  133.  
  134. // ---------------------------------
  135.  
  136.  
  137.     printf("\n\n");
  138.  
  139. // RESULTS
  140.     for(i=0;i<p;i++){
  141.  
  142.         printf("\n\t[debug] [RESULT] bounds = %d %d", bound[i][0], bound[i][1]);
  143.     }
  144.  
  145.  
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement