Solution of Decl

An exercise in string splitting.

C solution (Marius Gavrilescu)

    1 #include<stdio.h>
    2 #include<stdlib.h>
    3 #include<string.h>
    4 
    5 char decl[105], *var[105], delim[105] = "][\n";
    6 
    7 int main(void){
    8     for(char c = 'a' ; c <= 'z' ; c++) // Construct the delimiter containing
    9         delim[c - 'a' + 3] = c;        // all letters and ], [, newline
   10 
   11     while(!feof(stdin)){                            // While we aren't done
   12         fgets(decl, 105, stdin);                    // Read a declaration into decl
   13         scanf(" ");                                 // Trigger EOF if necessary
   14         char *last_space = strrchr(decl, ' ');      // Find the last space character
   15         *last_space = 0;                            // Split the string at this char
   16         char *type = decl, *vars = last_space + 1;  // into the type and a string of vars
   17 
   18         int varn = 0, sum = 0;
   19         var[0] = strtok(vars, ",");                 // Split the string of vars into an array
   20         while((var[++varn] = strtok(NULL, ",")));
   21 
   22         for(int i = 0 ; i < varn ; i++){            // For each variable
   23             int nr = 1;                             // nr is the dimension of the variable (number of elements)
   24             char *part = strtok(var[i], delim);     // Split it into parts using delim
   25             while(part) {                           // Each part is a dimension of the vector
   26                 nr *= atoi(part);                   // Multiply nr by the dimension
   27                 part = strtok(NULL, delim);
   28             }
   29             sum += nr * 4;                          // int is the default type and has 4 bytes
   30         }
   31 
   32         if(strstr(type, "short"))
   33             sum /= 2;
   34         if(strstr(type, "char"))
   35             sum /= 4;
   36         printf("%d\n", sum);
   37     }
   38 
   39     return 0;
   40 }

Perl solution (Marius Gavrilescu)

    1 #!/usr/bin/perl
    2 use v5.14;
    3 use warnings;
    4 
    5 use List::Util qw/reduce sum/;
    6 
    7 while (<>) {
    8     my ($type, $vars) = /(.*) ([^ ]+)$/; # Split the declaration into type and string of vars
    9     my @vars = split ',', $vars;         # Split the string of vars into an array
   10     y/0-9]//cd for @vars;                # Remove all characters from variables except for digits and ]
   11     my $result = sum map { reduce { $a * $b } 4, split ']' } @vars; # Split each var into dimensions, multiply the dimensions together and sum the results
   12     $result /= 2 if $type =~ /short/;
   13     $result /= 4 if $type =~ /char/;
   14     say $result;
   15 }
Questions?

Sponsors Gold