<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Was haltet ihr von diesem Program, das zwei strings als Zahlen interpretiert und addiert?]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe dieses Program geschrieben das zwei Zahlen die in strings gespeichert sind zusammenzaehlt:</p>
<pre><code>std::string zahl1  = &quot;15566155151&quot;;
	std::string zahl2 = &quot;1661&quot;;
	auto max_str = std::max(zahl1.length(), zahl2.length());
	std::string result(max_str.length()+1,'0');

	zahl1 = std::string(std::abs((int)(result.length() - zahl1.length())), '0') + zahl1;
	zahl2 = std::string(std::abs((int)(result.length() - zahl2.length())), '0') + zahl2;

	bool overflow = false;
	for (auto ri = zahl1.rbegin(),
		      rb = zahl2.rbegin(),
			  rd = result.rbegin(); ri != zahl1.rend(); ++ri, ++rb, ++rd)
	{
		int ir = ((*ri) - 48) + ((*rb) - 48) + (overflow? 1 : 0);
		overflow = false;
	    auto s = std::to_string(ir);
		if (ir &gt;= 10)
		{
			*rd = s[1];
			overflow = true;
		}else
			*rd = s[0];

	}
</code></pre>
<p>Was haltet ihr davon? Ist das gut und fehlerfrei? Oder ist es nicht effizient und schlechtes C++?</p>
<p>Vielen Dank</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/327908/was-haltet-ihr-von-diesem-program-das-zwei-strings-als-zahlen-interpretiert-und-addiert</link><generator>RSS for Node</generator><lastBuildDate>Wed, 08 Jul 2026 18:12:30 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/327908.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 11 Sep 2014 07:59:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Was haltet ihr von diesem Program, das zwei strings als Zahlen interpretiert und addiert? on Thu, 11 Sep 2014 07:59:23 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe dieses Program geschrieben das zwei Zahlen die in strings gespeichert sind zusammenzaehlt:</p>
<pre><code>std::string zahl1  = &quot;15566155151&quot;;
	std::string zahl2 = &quot;1661&quot;;
	auto max_str = std::max(zahl1.length(), zahl2.length());
	std::string result(max_str.length()+1,'0');

	zahl1 = std::string(std::abs((int)(result.length() - zahl1.length())), '0') + zahl1;
	zahl2 = std::string(std::abs((int)(result.length() - zahl2.length())), '0') + zahl2;

	bool overflow = false;
	for (auto ri = zahl1.rbegin(),
		      rb = zahl2.rbegin(),
			  rd = result.rbegin(); ri != zahl1.rend(); ++ri, ++rb, ++rd)
	{
		int ir = ((*ri) - 48) + ((*rb) - 48) + (overflow? 1 : 0);
		overflow = false;
	    auto s = std::to_string(ir);
		if (ir &gt;= 10)
		{
			*rd = s[1];
			overflow = true;
		}else
			*rd = s[0];

	}
</code></pre>
<p>Was haltet ihr davon? Ist das gut und fehlerfrei? Oder ist es nicht effizient und schlechtes C++?</p>
<p>Vielen Dank</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2416982</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2416982</guid><dc:creator><![CDATA[demk]]></dc:creator><pubDate>Thu, 11 Sep 2014 07:59:23 GMT</pubDate></item><item><title><![CDATA[Reply to Was haltet ihr von diesem Program, das zwei strings als Zahlen interpretiert und addiert? on Thu, 11 Sep 2014 08:09:33 GMT]]></title><description><![CDATA[<p>Abgesehen vom fehlenden main(), kompiliert, linkt und läuft es?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2416985</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2416985</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Thu, 11 Sep 2014 08:09:33 GMT</pubDate></item><item><title><![CDATA[Reply to Was haltet ihr von diesem Program, das zwei strings als Zahlen interpretiert und addiert? on Thu, 11 Sep 2014 08:14:12 GMT]]></title><description><![CDATA[<p>Ja es laeuft:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;cmath&gt;

int main()
{
	std::string zahl1  = &quot;15566155151&quot;;
	std::string zahl2 = &quot;1661&quot;;
	auto max_str = std::max(zahl1.length(), zahl2.length());
	std::string result(max_str+1,'0');

	zahl1 = std::string(std::abs((int)(result.length() - zahl1.length())), '0') + zahl1;
	zahl2 = std::string(std::abs((int)(result.length() - zahl2.length())), '0') + zahl2;

	bool overflow = false;
	for (auto ri = zahl1.rbegin(),
		      rb = zahl2.rbegin(),
			  rd = result.rbegin(); ri != zahl1.rend(); ++ri, ++rb, ++rd)
	{
		int ir = ((*ri) - 48) + ((*rb) - 48) + (overflow? 1 : 0);
		overflow = false;
	    auto s = std::to_string(ir);
		if (ir &gt;= 10)
		{
			*rd = s[1];
			overflow = true;
		}else
			*rd = s[0];

	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2416986</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2416986</guid><dc:creator><![CDATA[demk]]></dc:creator><pubDate>Thu, 11 Sep 2014 08:14:12 GMT</pubDate></item><item><title><![CDATA[Reply to Was haltet ihr von diesem Program, das zwei strings als Zahlen interpretiert und addiert? on Thu, 11 Sep 2014 08:38:30 GMT]]></title><description><![CDATA[<p>Hallo.</p>
<p>Was mir auffällt:<br />
- 48 im Programmcode ist unlesbar und unportabel (Implementierungen müssen dem ASCII-Standard nicht Folge leisten). Besser <code>'0'</code> .<br />
- Zeile 18 - 23 dürfte fehlerhaft sein. Wie die schriftliche Addition funktioniert:</p>
<pre><code>9  Lhs
+9  Rhs
10  Carry
------------
18  Ergebnis
</code></pre>
<p>Die niederwertigste Ziffer des Ergebnisses ist also auch die niederwertigste Ziffer der überflossenen (klingt künstlich) Addition.<br />
Hier meine Alternative (die auch den teuren temporären String und <code>std::tostring</code> vermeidet) für die Zeilen 16 - 23:</p>
<pre><code>overflow = ir &gt;= 10; 
		*rd += ir - (overflow ? 10 : 0);
</code></pre>
<p>Statt dem <code>?:</code> -Operator kann man auch eine <code>if</code> -Bedingung machen wenn man das lesbarer findet.</p>
<p>Edit: Gänzlich ohne Verzweigungen aber dafür unlesbarer wäre:</p>
<pre><code>overflow = ir &gt;= 10; 
		*rd += ir - overflow * 10;
</code></pre>
<p>Habe aber gerade keine Lust auszumessen, ob das überhaupt irgendetwas bringt oder ob der Compiler sowieso denselben Code generiert.</p>
<p>Edit 2:</p>
<ul>
<li><code>&lt;vector&gt;</code> , <code>&lt;algorithm&gt;</code> und <code>&lt;cmath&gt;</code> brauchst du nicht, <code>&lt;cstdlib&gt;</code> hingegen schon. Resp. auch nicht, da du die <code>std::abs</code> -Aufrufe in den Zeilen 14 und 15 auch nicht brauchst.</li>
</ul>
]]></description><link>https://www.c-plusplus.net/forum/post/2416990</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2416990</guid><dc:creator><![CDATA[Fytch]]></dc:creator><pubDate>Thu, 11 Sep 2014 08:38:30 GMT</pubDate></item></channel></rss>