Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's easiest if you decompose the problem into the small parts (as said above). That is an important part of engineering in general (whether software or otherwise).</p> <p>The problems are roughly:</p> <ul> <li>Read the file</li> <li>output the result</li> <li>count the number of items in a line</li> <li>extract the number at the end of the line</li> <li>Store the file</li> <li>sort the file by number of items in a line</li> <li>sort the file by number at the end of the file</li> </ul> <p>After that it is simple!</p> <pre><code>#include &lt;string.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #define BUFFER_SIZE 100 #define MAX_NODES 1000 int count_spaces( char * s ) { int spaces = 0; int i; for( i = 0; i &lt; strlen( s ) ; i ++ ) { if( s[ i ] == ' ' ) { spaces++; } i++; } return spaces; } /* sort by number of spaces */ int sort1( char *a, char * b ) { int spaces_a = count_spaces( a ); int spaces_b = count_spaces( b ); if( spaces_a &lt; spaces_b ) return -1; if( spaces_a &gt; spaces_b ) return 1; return 0; } int get_digits( char *s ) { char buffer[ 10 ]; memset( buffer, 0, 10 ); int state = 0; int i = 0, j = 0; int number = 0; for( i = 0; i &lt; strlen( s ) ; i ++ ) { if( state == 0 &amp;&amp; s[ i ] == '[' ) { state = 1; } if( state == 1 &amp;&amp; s[ i ] != ']' ) { buffer[ j ] = s[ i ]; j++; } if( state == 1 &amp;&amp; s[ i ] == ']' ) { break; } } sscanf( buffer, "%d", &amp;number ); } /* sort by digits in brackets at the end of the line */ int sort2( char *a, char * b ) { int a_num = get_digits( a ); int b_num = get_digits( b ); if( a_num &lt; b_num ) return -1; if( a_num &gt; b_num ) return 1; return 0; } int main() { FILE *infile; char line[ BUFFER_SIZE ]; int i = 0; char *nodes[ MAX_NODES ]; if( (infile = fopen( "data.txt", "r" )) == NULL ) { printf( "Error opening file\n" ); return -1; } while( fgets( line, BUFFER_SIZE, infile ) != NULL ) { if( (nodes[ i ] = malloc( strlen( line ) + 1 )) == NULL ) { printf( "Malloc failed\n" ); return -1; } strcpy( nodes[ i ], line ); i++; } // sort by # of items in a line qsort( nodes, i, sizeof( char *), (void *)(&amp;sort1) ); // sort by number qsort( nodes, i, sizeof( char *), (void *)(&amp;sort2) ); /* output results */ i--; do { printf( "%s", nodes[i] ); i--; } while( i &gt;= 0 ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload