• Re: Isn't that beauty ? (no it's not)

    From Bonita Montero@3:633/10 to All on Thu Mar 12 15:03:22 2026
    Am 12.03.2026 um 15:00 schrieb DFS:
    On 3/12/2026 2:24 AM, Bonita Montero wrote:
    There was a programming-"contest" on comp.lang.c and I wanted to show
    the simpler code in C, here it is:

    #include <iostream>
    #include <sstream>
    #include <iomanip>
    #include <optional>
    #include <algorithm>

    using namespace std;

    static optional<size_t> parse( const char *str );

    int main( int argc, char **argv )
    {
    ˙˙˙˙˙if( argc < 3 )
    ˙˙˙˙˙˙˙˙ return EXIT_FAILURE;
    ˙˙˙˙˙optional<size_t>
    ˙˙˙˙˙˙˙˙ pRows = parse( argv[1] ),
    ˙˙˙˙˙˙˙˙ pCols = parse( argv[2] );
    ˙˙˙˙˙if( !pRows || !pCols )
    ˙˙˙˙˙˙˙˙ return EXIT_FAILURE;
    ˙˙˙˙˙size_t rows = *pRows, cols = *pCols;
    ˙˙˙˙˙optional<size_t> pClip( rows * cols );
    ˙˙˙˙˙if( argc >= 4 && !(pClip = parse( argv[3] )) )
    ˙˙˙˙˙˙˙˙ return EXIT_FAILURE;
    ˙˙˙˙˙size_t clip = min( *pClip, rows * cols );
    ˙˙˙˙˙streamsize width = (ostringstream() << clip).str().length();
    ˙˙˙˙˙for( size_t row = 1; row <= min( rows, clip ); ++row )
    ˙˙˙˙˙{
    ˙˙˙˙˙˙˙˙ bool head = true;
    ˙˙˙˙˙˙˙˙ for( size_t value = row; value <= clip; value += rows, head =
    false )
    ˙˙˙˙˙˙˙˙˙˙˙˙ cout << " "sv.substr( head, !head ) << right <<
    setw( width ) << value;
    ˙˙˙˙˙˙˙˙ cout << endl;
    ˙˙˙˙˙}
    }

    static optional<size_t> parse( const char *str )
    {
    ˙˙˙˙˙istringstream iss( str );
    ˙˙˙˙˙size_t ret;
    ˙˙˙˙˙iss >> ret;
    ˙˙˙˙˙if( !iss || !iss.eof() )
    ˙˙˙˙˙˙˙˙ return nullopt;
    ˙˙˙˙˙return ret;
    }

    C++ really rocks since you've to deal with much less details than in C.



    Is your strategy to just ignore reality, and keep making bogus claims
    that - for this challenge at least - you can't support?

    Where's your check for parsing errors ? Your code would be longer with
    that.
    But real C++ code is usually multiple times shorter, mostly because of
    generic programming. C really sucks since you have to flip every bit
    on your own.




    #include <stdlib.h>
    #include <stdio.h>

    int main(int argc, char *argv[]) {
    ˙˙˙˙if (argc < 3 || argc > 4) {
    ˙˙˙˙˙˙˙ printf("Enter 2 or 3 arguments:\n$./prog rows columns [stop]\n");
    ˙˙˙˙˙˙˙ return 0;
    ˙˙˙˙}

    ˙˙˙˙int rows = atoi(argv[1]);
    ˙˙˙˙int cols = atoi(argv[2]);
    ˙˙˙˙int max˙ = (argc == 4) ? (atoi(argv[3])) : (rows * cols);
    ˙˙˙˙char cw[12];
    ˙˙˙˙int colwidth = sprintf(cw,"%d ",rows * cols);

    ˙˙˙˙for (int r = 1; r <= rows; r++) {
    ˙˙˙˙˙˙˙ if (r <= max) {
    ˙˙˙˙˙˙˙˙˙˙˙ int nbr = r;
    ˙˙˙˙˙˙˙˙˙˙˙ printf("%*d", colwidth, nbr);
    ˙˙˙˙˙˙˙˙˙˙˙ for (int i = 0; i < (cols - 1); i++) {
    ˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙ ((nbr += rows) <= max) ? (printf("%*d", colwidth,
    nbr)) : (0) ;
    ˙˙˙˙˙˙˙˙˙˙˙ }
    ˙˙˙˙˙˙˙˙˙˙˙ printf("\n");
    ˙˙˙˙˙˙˙ }
    ˙˙˙˙}

    ˙˙˙˙return 0;

    }


    --- PyGate Linux v1.5.12
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From tTh@3:633/10 to All on Thu Mar 12 15:27:50 2026
    On 3/12/26 15:03, Bonita Montero wrote:

    But real C++ code is usually multiple times shorter, mostly because of generic programming. C really sucks since you have to flip every bit
    on your own.

    C++ really sucks because you can't know who flip the bits.

    --
    ** **
    * tTh des Bourtoulots *
    * http://maison.tth.netlib.re/ *
    ** **

    --- PyGate Linux v1.5.12
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Thu Mar 12 15:34:23 2026
    Am 12.03.2026 um 15:27 schrieb tTh:
    On 3/12/26 15:03, Bonita Montero wrote:

    But real C++ code is usually multiple times shorter, mostly because of
    generic programming. C really sucks since you have to flip every bit
    on your own.

    ˙˙ C++ really sucks because you can't know who flip the bits.

    Absolutely not, C++ can anyhing you want if you need that. But in C++
    you often doesn't need that. Bad programmers which don't understand
    that are rare since C++ has very high skill demands.


    --- PyGate Linux v1.5.12
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Thu Mar 12 16:10:41 2026
    Am 12.03.2026 um 15:43 schrieb DFS:
    On 3/12/2026 10:13 AM, Bonita Montero wrote:
    Here, do that in C:

    static optional<size_t> parse( const char *str )
    {
    ˙˙˙˙˙size_t ret;
    ˙˙˙˙˙if( from_chars_result fcr = from_chars( str, str + strlen( str ),
    ret ); (bool)fcr.ec || *fcr.ptr )
    ˙˙˙˙˙˙˙˙ return nullopt;
    ˙˙˙˙˙return ret;
    }


    Explain in detail what it does.

    If that isn't self-explanatory stick with C.

    I've got a task for you: Do the same in C:

    #include <iostream>
    #include <regex>
    #include <fstream>
    #include <vector>
    #include <algorithm>
    #include <vector>

    using namespace std;

    int main( int argc, char **argv )
    {
    if( argc < 2 )
    return EXIT_FAILURE;
    ifstream ifs( argv[1] );
    static regex rxNameTel( "^\\s*\"([^\"]*)\"\\s*\"([^\"]*)\"\\s*$" );
    struct name_tel { string name, tel; };
    vector<name_tel> phoneList;
    while( !ifs.eof() )
    {
    string line;
    getline( ifs, line );
    match_results<string::const_iterator> sm;
    if( regex_match( line, sm, rxNameTel ) )
    phoneList.emplace_back( string( sm[1].first, sm[1].second ), string(
    sm[2].first, sm[2].second ) );
    }
    sort( phoneList.begin(), phoneList.end(),
    []( const name_tel &left, const name_tel &right ) { return left.name <
    right.name; } );
    for( name_tel &phone : phoneList )
    cout << "\"" << phone.name << "\"\t\"" << phone.tel << "\"" << endl;
    }

    1. Read a file and parse it with ne mentioned regex-pattern.
    2. Split both parts of every line in two strings.
    3. Sort the "vector" according to the first string.
    4. Print it.

    I guess you don't manage to do that with less than five times the work.
    Every external lib allowed.


    --- PyGate Linux v1.5.12
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Thu Mar 12 16:44:01 2026
    Am 12.03.2026 um 16:10 schrieb Bonita Montero:

    ˙˙˙˙static regex rxNameTel( "^\\s*\"([^\"]*)\"\\s*\"([^\"]*)\"\\s*$" );

    Easier to read as a C++ raw string: R"~(^\s*"([^"]*)"\s*"([^"]*)"\s*$)~"
    The regex is between ~( and )~.


    --- PyGate Linux v1.5.12
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Scott Lurndal@3:633/10 to All on Thu Mar 12 17:32:08 2026
    Bonita Montero <Bonita.Montero@gmail.com> writes:
    Am 12.03.2026 um 15:43 schrieb DFS:
    On 3/12/2026 10:13 AM, Bonita Montero wrote:
    Here, do that in C:

    static optional<size_t> parse( const char *str )
    {
    ˙˙˙˙˙size_t ret;
    ˙˙˙˙˙if( from_chars_result fcr = from_chars( str, str + strlen( str ),
    ret ); (bool)fcr.ec || *fcr.ptr )
    ˙˙˙˙˙˙˙˙ return nullopt;
    ˙˙˙˙˙return ret;
    }


    Explain in detail what it does.

    If that isn't self-explanatory stick with C.

    I've got a task for you: Do the same in C:

    #include <iostream>
    #include <regex>
    #include <fstream>
    #include <vector>
    #include <algorithm>
    #include <vector>

    using namespace std;

    int main( int argc, char **argv )
    {
    if( argc < 2 )
    return EXIT_FAILURE;

    A silent failure implies to the interactive user
    that the command succeeded. You should print
    a responsive message before returning a failure code.

    cerr << "Usage: " << argv[0] << " " <phonelist>" << eol;

    (although I would prefer fprintf(stderr, ...)).


    ifstream ifs( argv[1] );

    You didn't bother to check if the file was opened.
    You should print a responsive message and exit if the streams
    failbit is set.

    static regex rxNameTel( "^\\s*\"([^\"]*)\"\\s*\"([^\"]*)\"\\s*$" );
    struct name_tel { string name, tel; };
    vector<name_tel> phoneList;
    while( !ifs.eof() )
    {
    string line;
    getline( ifs, line );
    match_results<string::const_iterator> sm;
    if( regex_match( line, sm, rxNameTel ) )
    phoneList.emplace_back( string( sm[1].first, sm[1].second ), string(
    sm[2].first, sm[2].second ) );
    }
    sort( phoneList.begin(), phoneList.end(),
    []( const name_tel &left, const name_tel &right ) { return left.name <
    right.name; } );
    for( name_tel &phone : phoneList )
    cout << "\"" << phone.name << "\"\t\"" << phone.tel << "\"" << endl;
    }

    1. Read a file and parse it with ne mentioned regex-pattern.
    2. Split both parts of every line in two strings.
    3. Sort the "vector" according to the first string.
    4. Print it.

    This is certainly a task that can be done simply using a shell
    script and the standard POSIX utility set. awk(1) would be
    a good starting point; it's possible that it can be done with
    a single invocation of the posix 'sort' utility.

    Of course, the lack of any in-line documentation (e.g. comments) is
    a typical defect in your C++ code.


    --- PyGate Linux v1.5.12
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Fri Mar 13 00:56:21 2026
    Am 12.03.2026 um 18:32 schrieb Scott Lurndal:

    You didn't bother to check if the file was opened.

    That's not necessary to compare it against a equal solution in C.


    This is certainly a task that can be done simply using a shell
    script and the standard POSIX utility set. awk(1) would be
    a good starting point; it's possible that it can be done with
    a single invocation of the posix 'sort' utility.

    It's comparison of C against C++.

    Of course, the lack of any in-line documentation (e.g. comments) is
    a typical defect in your C++ code.

    Complete idiot.


    --- PyGate Linux v1.5.12
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Fri Mar 13 01:14:00 2026
    Am 12.03.2026 um 18:32 schrieb Scott Lurndal:

    You didn't bother to check if the file was opened.

    If the file couldn't been opened nothing happens.
    No difference to a check.

    --- PyGate Linux v1.5.12
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Fri Mar 13 01:27:52 2026
    I shortened my code a bit.
    Do that:

    #include <iostream>
    #include <regex>
    #include <fstream>
    #include <vector>
    #include <algorithm>

    using namespace std;

    int main( int argc, char **argv )
    {
    if( argc < 2 )
    return EXIT_FAILURE;
    ifstream ifs( argv[1] );
    static regex rxNameTel( R"~(^\s*"([^"]*)"\s*"([^"]*)"\s*$)~" );
    struct name_tel { string name, tel; };
    vector<name_tel> phoneList;
    smatch sm;
    for( string line; getline( ifs, line ); )
    if( regex_match( line, sm, rxNameTel ) )
    phoneList.emplace_back( sm[1].str(), sm[2].str() );
    sort( phoneList.begin(), phoneList.end(),
    []( name_tel &left, name_tel &right ) { return left.name < right.name;
    } );
    for( name_tel &phone : phoneList )
    cout << "\"" << phone.name << "\"\t\"" << phone.tel << "\"" << endl;
    }

    Have a closer look at the regex.

    --- PyGate Linux v1.5.12
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Chris M. Thomasson@3:633/10 to All on Fri Mar 13 11:54:23 2026
    On 3/12/2026 4:56 PM, Bonita Montero wrote:
    Am 12.03.2026 um 18:32 schrieb Scott Lurndal:

    You didn't bother to check if the file was opened.

    That's not necessary to compare it against a equal solution in C.


    This is certainly a task that can be done simply using a shell
    script and the standard POSIX utility set.˙ awk(1) would be
    a good starting point; it's possible that it can be done with
    a single invocation of the posix 'sort' utility.

    It's comparison of C against C++.

    Of course, the lack of any in-line documentation (e.g. comments) is
    a typical defect in your C++ code.

    Complete idiot.


    Scott is the opposite of idiot.

    --- PyGate Linux v1.5.13
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Sat Mar 14 06:01:53 2026
    Am 13.03.2026 um 21:11 schrieb DFS:
    On 3/12/2026 8:27 PM, Bonita Montero wrote:
    I shortened my code a bit.
    Do that:

    #include <iostream>
    #include <regex>
    #include <fstream>
    #include <vector>
    #include <algorithm>

    using namespace std;

    int main( int argc, char **argv )
    {
    ˙˙˙˙˙if( argc < 2 )
    ˙˙˙˙˙˙˙˙ return EXIT_FAILURE;
    ˙˙˙˙˙ifstream ifs( argv[1] );
    ˙˙˙˙˙static regex rxNameTel( R"~(^\s*"([^"]*)"\s*"([^"]*)"\s*$)~" );
    ˙˙˙˙˙struct name_tel { string name, tel; };
    ˙˙˙˙˙vector<name_tel> phoneList;
    ˙˙˙˙˙smatch sm;
    ˙˙˙˙˙for( string line; getline( ifs, line ); )
    ˙˙˙˙˙˙˙˙ if( regex_match( line, sm, rxNameTel ) )
    ˙˙˙˙˙˙˙˙˙˙˙˙ phoneList.emplace_back( sm[1].str(), sm[2].str() );
    ˙˙˙˙˙sort( phoneList.begin(), phoneList.end(),
    ˙˙˙˙˙˙˙˙ []( name_tel &left, name_tel &right ) { return left.name <
    right.name; } );
    ˙˙˙˙˙for( name_tel &phone : phoneList )
    ˙˙˙˙˙˙˙˙ cout << "\"" << phone.name << "\"\t\"" << phone.tel << "\""
    << endl;
    }

    Have a closer look at the regex.


    Got a massive set of compile errors:

    = -std=c++20

    --- PyGate Linux v1.5.13
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Sat Mar 14 07:23:45 2026
    Am 14.03.2026 um 06:49 schrieb DFS:

    Your output is messy.˙ What you want to do is iterate the data and find
    the longest name, then pad spaces after the name so the phone numbers
    line up.

    No, alphabetically ordered. My code exactly does that.

    88 LOC
    16.84MB executable
    perfect output


    $ ./dfs names-numbers-unsorted-montero.txt
    ˙1. Anna Becker˙˙˙˙˙˙˙˙˙ 0170-2233445
    ...


    My output is the same without line numbers:

    "Anna Becker" "0170-2233445"
    "Anna Fischer" "0341-9988776"
    "Anna Mller" "0987-6543210"
    "Ben Meier" "0341-5566778"
    "Ben Richter" "069-3344556"
    "Clara Hofmann" "0157-2233445"
    "Clara Zimmermann" "040-5566778"
    "David Schulz" "030-9988776"
    "Emma Bauer" "0157-9988776"
    "Emma Wolf" "040-5566778"
    "Felix Hoffmann" "0711-5566778"
    "Felix Neumann" "0170-2233445"
    "Hannah Wagner" "0221-5566778"
    "Jan Hoffmann" "0711-3344556"
    "Jana Zimmer" "030-6677889"
    "Jonas Klein" "0171-4455667"
    "Jonas Klein" "030-4455667"
    "Julia Neumann" "0221-3344556"
    "Julia Schulz" "0228-4455667"
    "Laura Fischer" "040-9876543"
    "Laura Schulze" "0228-1122334"
    "Lea Richter" "0341-1122334"
    "Lea Wagner" "0151-3344556"
    "Lena Fischer" "0151-6677889"
    "Leon Krause" "089-1122334"
    "Leon Zimmermann" "0711-1122445"
    "Leonie Klein" "0341-5566778"
    "Lukas Hofmann" "089-6677889"
    "Lukas K”nig" "069-7788990"
    "Marie Becker" "040-4455667"
    "Marie Richter" "0221-7788990"
    "Max Mustermann" "0123-4567890"
    "Maximilian Keller" "030-1122334"
    "Mia Keller" "089-6677889"
    "Michael Braun" "0170-9988776"
    "Moritz Wolf" "0157-4455667"
    "Nina Krause" "0341-4455667"
    "Paul Sch„fer" "0228-3344556"
    "Paul Wolf" "030-2233445"
    "Peter Schmidt" "030-1234567"
    "Philipp K”nig" "089-9988776"
    "Sarah Braun" "040-7788990"
    "Sarah Lehmann" "040-2233445"
    "Simon Meier" "030-7788990"
    "Sophie Neumann" "0711-3344556"
    "Sophie Wagner" "089-2233445"
    "Tim Becker" "0151-1112223"
    "Tim Richter" "030-6677889"
    "Tim Sch„fer" "0711-5566778"
    "Tom Bauer" "0171-1122334"

    But your code is 101 lines of code, my 25. C really sucks.

    --- PyGate Linux v1.5.13
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)