<?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[Anfänger-Taschenrechner mal richtig gemacht]]></title><description><![CDATA[<p>Was haltet ihr von dieser Implementation des &quot;Taschenrechners&quot; wie ihn jeder Anfänger mal schreibt:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;functional&gt;
#include &lt;limits&gt;
#include &lt;map&gt;
#include &lt;boost/function.hpp&gt;

int main()
{
	using std::cout;
	using std::cin;
	using std::endl;

	typedef boost::function&lt; double( double, double ) &gt; arithmetic_func;
	typedef std::map&lt; char, arithmetic_func &gt; operation_table;

	operation_table operations;

	operations.insert( make_pair( '+', std::plus&lt; double &gt;() ) );
	operations.insert( make_pair( '-', std::minus&lt; double &gt;() ) );
	operations.insert( make_pair( '*', std::multiplies&lt; double &gt;() ) );
	operations.insert( make_pair( '/', std::divides&lt; double &gt;() ) );

	char op;
	double arg1, arg2;

	while( !( ( cout &lt;&lt; &quot;Enter the arithmetic operations in the following syntax: &quot;
						&quot;[op] [arg1] [arg2]\nExpression: &quot; ) 
			&amp;&amp; ( cin &gt;&gt; op &amp;&amp; cin &gt;&gt; arg1 &amp;&amp; cin &gt;&gt; arg2 ) ) )
	{
		cout &lt;&lt; &quot;\nInvalid expression. Try again.&quot; &lt;&lt; endl;

		cin.clear();
		cin.ignore( std::numeric_limits&lt; std::streamsize &gt;::max(), '\n' );
	}

	operation_table::const_iterator it = operations.find( op );

	if( it == operations.end() )
	{
		cout &lt;&lt; &quot;\nUnsupported operation: &quot; &lt;&lt; op &lt;&lt; endl;
	}

	cout &lt;&lt; ( it-&gt;second )( arg1, arg2 );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/169988/anfänger-taschenrechner-mal-richtig-gemacht</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 22:13:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/169988.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 10 Jan 2007 18:11:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Anfänger-Taschenrechner mal richtig gemacht on Wed, 10 Jan 2007 18:11:35 GMT]]></title><description><![CDATA[<p>Was haltet ihr von dieser Implementation des &quot;Taschenrechners&quot; wie ihn jeder Anfänger mal schreibt:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;functional&gt;
#include &lt;limits&gt;
#include &lt;map&gt;
#include &lt;boost/function.hpp&gt;

int main()
{
	using std::cout;
	using std::cin;
	using std::endl;

	typedef boost::function&lt; double( double, double ) &gt; arithmetic_func;
	typedef std::map&lt; char, arithmetic_func &gt; operation_table;

	operation_table operations;

	operations.insert( make_pair( '+', std::plus&lt; double &gt;() ) );
	operations.insert( make_pair( '-', std::minus&lt; double &gt;() ) );
	operations.insert( make_pair( '*', std::multiplies&lt; double &gt;() ) );
	operations.insert( make_pair( '/', std::divides&lt; double &gt;() ) );

	char op;
	double arg1, arg2;

	while( !( ( cout &lt;&lt; &quot;Enter the arithmetic operations in the following syntax: &quot;
						&quot;[op] [arg1] [arg2]\nExpression: &quot; ) 
			&amp;&amp; ( cin &gt;&gt; op &amp;&amp; cin &gt;&gt; arg1 &amp;&amp; cin &gt;&gt; arg2 ) ) )
	{
		cout &lt;&lt; &quot;\nInvalid expression. Try again.&quot; &lt;&lt; endl;

		cin.clear();
		cin.ignore( std::numeric_limits&lt; std::streamsize &gt;::max(), '\n' );
	}

	operation_table::const_iterator it = operations.find( op );

	if( it == operations.end() )
	{
		cout &lt;&lt; &quot;\nUnsupported operation: &quot; &lt;&lt; op &lt;&lt; endl;
	}

	cout &lt;&lt; ( it-&gt;second )( arg1, arg2 );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1207337</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207337</guid><dc:creator><![CDATA[C++-Lehrmeister]]></dc:creator><pubDate>Wed, 10 Jan 2007 18:11:35 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger-Taschenrechner mal richtig gemacht on Wed, 10 Jan 2007 20:06:22 GMT]]></title><description><![CDATA[<p>nichts, da ein anfänger mit so einem beispiel nichts anfangen kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207447</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207447</guid><dc:creator><![CDATA[thordk]]></dc:creator><pubDate>Wed, 10 Jan 2007 20:06:22 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger-Taschenrechner mal richtig gemacht on Wed, 10 Jan 2007 21:24:43 GMT]]></title><description><![CDATA[<p>Hmm vllt wäre es auch sinnvoll die Konsole offen zu halten -.-</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207526</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207526</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Wed, 10 Jan 2007 21:24:43 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger-Taschenrechner mal richtig gemacht on Wed, 10 Jan 2007 21:34:51 GMT]]></title><description><![CDATA[<p>C++-Lehrmeister schrieb:</p>
<blockquote>
<p>Was haltet ihr von dieser Implementation des &quot;Taschenrechners&quot;...</p>
</blockquote>
<p>da sieht man mal, wie pervers sowas aussehen kann <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /><br />
was ist denn diese 'operation_table' ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207534</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207534</guid><dc:creator><![CDATA[ten]]></dc:creator><pubDate>Wed, 10 Jan 2007 21:34:51 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger-Taschenrechner mal richtig gemacht on Wed, 10 Jan 2007 21:37:27 GMT]]></title><description><![CDATA[<p>Hallo</p>
<blockquote>
<p>was ist denn diese 'operation_table' ?</p>
</blockquote>
<p>Steht eine Zeile über der ersten Benutzung :<br />
Ein typedef auf eine Map von chars (den Operationssymbolen) auf Funktoren (die Operation)</p>
<p>bis bald<br />
akari</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207536</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207536</guid><dc:creator><![CDATA[akari]]></dc:creator><pubDate>Wed, 10 Jan 2007 21:37:27 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger-Taschenrechner mal richtig gemacht on Wed, 10 Jan 2007 21:39:31 GMT]]></title><description><![CDATA[<p>akari schrieb:</p>
<blockquote>
<p>Hallo</p>
<blockquote>
<p>was ist denn diese 'operation_table' ?</p>
</blockquote>
<p>Steht eine Zeile über der ersten Benutzung :<br />
Ein typedef auf eine Map von chars (den Operationssymbolen) auf Funktoren (die Operation)</p>
</blockquote>
<p>danke. von dem code kriegt man augenkrebs. ich hab's voll übersehen...<br />
:xmas2:</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207539</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207539</guid><dc:creator><![CDATA[ten]]></dc:creator><pubDate>Wed, 10 Jan 2007 21:39:31 GMT</pubDate></item><item><title><![CDATA[Reply to Anfänger-Taschenrechner mal richtig gemacht on Wed, 10 Jan 2007 22:00:15 GMT]]></title><description><![CDATA[<p>Das einzig &quot;unschöne&quot; ist die Bedingung in der While-Schleife die ist nicht ganz so übersichtlich aufgrund der Größe, aber von der Idee her ist es das einfachste eine Ausgabe die zur Eingabe auffordert zusammengekoppelt in eine while-Schleife zu packen.<br />
Hätte man in eine Funktion auslagern können, dann wäre es leserlicher geworden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1207550</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1207550</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Wed, 10 Jan 2007 22:00:15 GMT</pubDate></item></channel></rss>