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?
#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;
}
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.
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.
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.
˙˙˙˙static regex rxNameTel( "^\\s*\"([^\"]*)\"\\s*\"([^\"]*)\"\\s*$" );
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.
You didn't bother to check if the file was opened.
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.
You didn't bother to check if the file was opened.
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.
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
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.
88 LOC
16.84MB executable
perfect output
$ ./dfs names-numbers-unsorted-montero.txt
˙1. Anna Becker˙˙˙˙˙˙˙˙˙ 0170-2233445
...
| Sysop: | Tetrazocine |
|---|---|
| Location: | Melbourne, VIC, Australia |
| Users: | 16 |
| Nodes: | 8 (0 / 8) |
| Uptime: | 40:03:40 |
| Calls: | 208 |
| Files: | 21,502 |
| Messages: | 82,823 |