<?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[[C++ Rechner&#x2F;Anfänger]]]></title><description><![CDATA[<p>Hallo ich Lerne seit kurzem C++ und habe nun einen kleinen Rechner gecodet,<br />
der Rechner funktioniert auch soweit.<br />
Meine Frage ist nun die Ob sich das mal jemand anschauen könnte und mit<br />
vielleicht sagen kann was man verbessern könnte.</p>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstdio&gt;
#include &quot;grundrechnung.h&quot;
using namespace std;

int main()
{
	int auswahl;
	int weiterRechnen = 0;

	cout &lt;&lt; &quot;Rechner&quot;
		 &lt;&lt; &quot;\n-------------------------&quot;
		 &lt;&lt; &quot;\nFunktionsauswahl:&quot;
		 &lt;&lt; &quot;\nAddieren	=&gt; 1&quot;
		 &lt;&lt; &quot;\nSubtrahieren	=&gt; 2&quot;
		 &lt;&lt; &quot;\nMultiplizieren	=&gt; 3&quot;
		 &lt;&lt; &quot;\nDividieren	=&gt; 4&quot;
		 &lt;&lt; &quot;\nBeenden		=&gt; 0&quot;
		 &lt;&lt; &quot;\n-------------------------&quot;
		 &lt;&lt; &quot;\nAuswahl: &quot;;
	cin &gt;&gt; auswahl;

	double zahl1, zahl2;
	double ergebnis;

	switch( auswahl )
	{		//switchA 
		case 0:
			return 0;
			break;
		case 1:
			cout &lt;&lt; &quot;\nAddieren von zwei Zahlen\n&quot;
				 &lt;&lt; &quot;\nZahl1: &quot;;
			cin &gt;&gt; zahl1;
			cout &lt;&lt; &quot;\nZahl2: &quot;;
			cin &gt;&gt; zahl2;
			cout &lt;&lt; endl;

			ergebnis = addieren( zahl1, zahl2);

			weiterRechnen = 1;
			break;
		case 2:
			cout &lt;&lt; &quot;\nSubtrahieren von zwei Zahlen\n&quot;
				 &lt;&lt; &quot;\nZahl1: &quot;;
			cin &gt;&gt; zahl1;
			cout &lt;&lt; &quot;\nZahl2: &quot;;
			cin &gt;&gt; zahl2;
			cout &lt;&lt; endl;

			ergebnis = subtrahieren( zahl1, zahl2 );

			weiterRechnen = 1;
			break;
		case 3:
			cout &lt;&lt; &quot;\nMultiplizieren von zwei Zahlen\n&quot;
				 &lt;&lt; &quot;\nZahl1: &quot;;
			cin &gt;&gt; zahl1;
			cout &lt;&lt; &quot;\nZahl2: &quot;;
			cin &gt;&gt; zahl2;
			cout &lt;&lt; endl;

			ergebnis = multiplizieren( zahl1, zahl2 );

			weiterRechnen = 1;
			break;
		case 4:
			cout &lt;&lt; &quot;\nDividieren von zwei Zahlen\n&quot;
				 &lt;&lt; &quot;\nZahl1: &quot;;
			cin &gt;&gt; zahl1;
			cout &lt;&lt; &quot;\nZahl2: &quot;;
			cin &gt;&gt; zahl2;
			cout &lt;&lt; endl;

			ergebnis = dividieren( zahl1, zahl2 );

			weiterRechnen = 1;
			break;
		default:
			cout &lt;&lt; &quot;\nUngueltige Eingabe!!!&quot; &lt;&lt; endl;
			break;
	}		//switchA

	switch( weiterRechnen )
	{		//switchB
		case 1:
			while( 1 )
			{		//while Schleife

				char awahl;
				int wahl;

				cout &lt;&lt; &quot;\nMit Ergebnis weiterrechnen? (j/n)&quot;
					 &lt;&lt; &quot;\nAuswahl: &quot;;
				cin &gt;&gt; awahl;

				if( awahl == 'j' ) wahl = 1;
				if( awahl == 'n' ) wahl = 0;

				switch( wahl )
				{		//switchC
					case 0:
						return 0;
						break;
					case 1:
						cout &lt;&lt; &quot;\n-------------------------&quot;
							 &lt;&lt; &quot;\nErgebnis:&quot;
							 &lt;&lt; &quot;\nAddieren	=&gt; 1&quot;
							 &lt;&lt; &quot;\nSubtrahieren	=&gt; 2&quot;
							 &lt;&lt; &quot;\nMultiplizieren	=&gt; 3&quot;
							 &lt;&lt; &quot;\nDividieren	=&gt; 4&quot;
							 &lt;&lt; &quot;\nBeenden		=&gt; 0&quot;
							 &lt;&lt; &quot;\n-------------------------&quot;
							 &lt;&lt; &quot;\nAuswahl: &quot;;
						cin &gt;&gt; wahl;
						cout &lt;&lt; endl;
						switch( wahl )
						{		//switchD
							case 1:
								zahl1 = ergebnis;
								cout &lt;&lt; &quot;\nErgebnis Addieren\n&quot;
									 &lt;&lt; &quot;\n&quot; &lt;&lt; ergebnis &lt;&lt; endl
									 &lt;&lt; &quot;\nZahl: &quot;;
								cin &gt;&gt; zahl2;
								cout &lt;&lt; endl;

								ergebnis = addieren( zahl1, zahl2);
								break;
							case 2:
								zahl1 = ergebnis;
								cout &lt;&lt; &quot;\nErgebnis Subtrahieren\n&quot;
									 &lt;&lt; &quot;\n&quot; &lt;&lt; ergebnis &lt;&lt; endl
									 &lt;&lt; &quot;\nZahl: &quot;;
								cin &gt;&gt; zahl2;
								cout &lt;&lt; endl;

								ergebnis = subtrahieren( zahl1, zahl2);
								break;
							case 3:
								zahl1 = ergebnis;
								cout &lt;&lt; &quot;\nErgebnis Multiplizieren\n&quot;
									 &lt;&lt; &quot;\n&quot; &lt;&lt; ergebnis &lt;&lt; endl
									 &lt;&lt; &quot;\nZahl: &quot;;
								cin &gt;&gt; zahl2;
								cout &lt;&lt; endl;

								ergebnis = multiplizieren( zahl1, zahl2);
								break;
							case 4:
								zahl1 = ergebnis;
								cout &lt;&lt; &quot;\nErgebnis Dividieren\n&quot;
									 &lt;&lt; &quot;\n&quot; &lt;&lt; ergebnis &lt;&lt; endl
									 &lt;&lt; &quot;\nZahl: &quot;;
								cin &gt;&gt; zahl2;
								cout &lt;&lt; endl;

								ergebnis = dividieren( zahl1, zahl2);
								break;
							default:
								cout &lt;&lt; &quot;\nUngueltige Eingabe!!!&quot; &lt;&lt; endl;
								break;
						}		//switchD
				}		//swichC
			}		//while schleife

		case 0:
			system(&quot;pause&quot;);
			return 0;
			break;
	}		//switchB
}
</code></pre>
<p>grundrechnung.h</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

double addieren( double summand1, double summand2 );

double subtrahieren( double summand1, double summand2 );

double multiplizieren( double summand1, double summand2 );

double dividieren( double summand1, double summand2 );
</code></pre>
<p>grundrechnung.cpp</p>
<pre><code class="language-cpp">#include &quot;grundrechnung.h&quot;

double addieren( double z1, double z2 )
{
	double ergebnis = z1 + z2;

	cout &lt;&lt; z1 &lt;&lt; &quot;+&quot; &lt;&lt; z2 &lt;&lt; &quot;= &quot; &lt;&lt; ergebnis &lt;&lt; endl;
	cout &lt;&lt; endl;

	return ergebnis;
}

double subtrahieren( double z1, double z2 )
{
	double ergebnis = z1 - z2;

	cout &lt;&lt; z1 &lt;&lt; &quot;-&quot; &lt;&lt; z2 &lt;&lt; &quot;= &quot; &lt;&lt; ergebnis &lt;&lt; endl;
	cout &lt;&lt; endl;

	return ergebnis;
}

double multiplizieren( double z1, double z2 )
{
	double ergebnis = z1 * z2;

	cout &lt;&lt; z1 &lt;&lt; &quot;*&quot; &lt;&lt; z2 &lt;&lt; &quot;= &quot; &lt;&lt; ergebnis &lt;&lt; endl;
	cout &lt;&lt; endl;

	return ergebnis;
}
double dividieren( double z1, double z2 )
{
	double ergebnis = z1 / z2;

	cout &lt;&lt; z1 &lt;&lt; &quot;/&quot; &lt;&lt; z2 &lt;&lt; &quot;= &quot; &lt;&lt; ergebnis &lt;&lt; endl;
	cout &lt;&lt; endl;

	return ergebnis;
}
</code></pre>
<p>edit: main.cpp verbessert</p>
<p>mfg rufrider</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/246097/c-rechner-anfänger</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 09:32:14 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/246097.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 23 Jul 2009 00:13:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [C++ Rechner&#x2F;Anfänger] on Thu, 23 Jul 2009 11:09:02 GMT]]></title><description><![CDATA[<p>Hallo ich Lerne seit kurzem C++ und habe nun einen kleinen Rechner gecodet,<br />
der Rechner funktioniert auch soweit.<br />
Meine Frage ist nun die Ob sich das mal jemand anschauen könnte und mit<br />
vielleicht sagen kann was man verbessern könnte.</p>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstdio&gt;
#include &quot;grundrechnung.h&quot;
using namespace std;

int main()
{
	int auswahl;
	int weiterRechnen = 0;

	cout &lt;&lt; &quot;Rechner&quot;
		 &lt;&lt; &quot;\n-------------------------&quot;
		 &lt;&lt; &quot;\nFunktionsauswahl:&quot;
		 &lt;&lt; &quot;\nAddieren	=&gt; 1&quot;
		 &lt;&lt; &quot;\nSubtrahieren	=&gt; 2&quot;
		 &lt;&lt; &quot;\nMultiplizieren	=&gt; 3&quot;
		 &lt;&lt; &quot;\nDividieren	=&gt; 4&quot;
		 &lt;&lt; &quot;\nBeenden		=&gt; 0&quot;
		 &lt;&lt; &quot;\n-------------------------&quot;
		 &lt;&lt; &quot;\nAuswahl: &quot;;
	cin &gt;&gt; auswahl;

	double zahl1, zahl2;
	double ergebnis;

	switch( auswahl )
	{		//switchA 
		case 0:
			return 0;
			break;
		case 1:
			cout &lt;&lt; &quot;\nAddieren von zwei Zahlen\n&quot;
				 &lt;&lt; &quot;\nZahl1: &quot;;
			cin &gt;&gt; zahl1;
			cout &lt;&lt; &quot;\nZahl2: &quot;;
			cin &gt;&gt; zahl2;
			cout &lt;&lt; endl;

			ergebnis = addieren( zahl1, zahl2);

			weiterRechnen = 1;
			break;
		case 2:
			cout &lt;&lt; &quot;\nSubtrahieren von zwei Zahlen\n&quot;
				 &lt;&lt; &quot;\nZahl1: &quot;;
			cin &gt;&gt; zahl1;
			cout &lt;&lt; &quot;\nZahl2: &quot;;
			cin &gt;&gt; zahl2;
			cout &lt;&lt; endl;

			ergebnis = subtrahieren( zahl1, zahl2 );

			weiterRechnen = 1;
			break;
		case 3:
			cout &lt;&lt; &quot;\nMultiplizieren von zwei Zahlen\n&quot;
				 &lt;&lt; &quot;\nZahl1: &quot;;
			cin &gt;&gt; zahl1;
			cout &lt;&lt; &quot;\nZahl2: &quot;;
			cin &gt;&gt; zahl2;
			cout &lt;&lt; endl;

			ergebnis = multiplizieren( zahl1, zahl2 );

			weiterRechnen = 1;
			break;
		case 4:
			cout &lt;&lt; &quot;\nDividieren von zwei Zahlen\n&quot;
				 &lt;&lt; &quot;\nZahl1: &quot;;
			cin &gt;&gt; zahl1;
			cout &lt;&lt; &quot;\nZahl2: &quot;;
			cin &gt;&gt; zahl2;
			cout &lt;&lt; endl;

			ergebnis = dividieren( zahl1, zahl2 );

			weiterRechnen = 1;
			break;
		default:
			cout &lt;&lt; &quot;\nUngueltige Eingabe!!!&quot; &lt;&lt; endl;
			break;
	}		//switchA

	switch( weiterRechnen )
	{		//switchB
		case 1:
			while( 1 )
			{		//while Schleife

				char awahl;
				int wahl;

				cout &lt;&lt; &quot;\nMit Ergebnis weiterrechnen? (j/n)&quot;
					 &lt;&lt; &quot;\nAuswahl: &quot;;
				cin &gt;&gt; awahl;

				if( awahl == 'j' ) wahl = 1;
				if( awahl == 'n' ) wahl = 0;

				switch( wahl )
				{		//switchC
					case 0:
						return 0;
						break;
					case 1:
						cout &lt;&lt; &quot;\n-------------------------&quot;
							 &lt;&lt; &quot;\nErgebnis:&quot;
							 &lt;&lt; &quot;\nAddieren	=&gt; 1&quot;
							 &lt;&lt; &quot;\nSubtrahieren	=&gt; 2&quot;
							 &lt;&lt; &quot;\nMultiplizieren	=&gt; 3&quot;
							 &lt;&lt; &quot;\nDividieren	=&gt; 4&quot;
							 &lt;&lt; &quot;\nBeenden		=&gt; 0&quot;
							 &lt;&lt; &quot;\n-------------------------&quot;
							 &lt;&lt; &quot;\nAuswahl: &quot;;
						cin &gt;&gt; wahl;
						cout &lt;&lt; endl;
						switch( wahl )
						{		//switchD
							case 1:
								zahl1 = ergebnis;
								cout &lt;&lt; &quot;\nErgebnis Addieren\n&quot;
									 &lt;&lt; &quot;\n&quot; &lt;&lt; ergebnis &lt;&lt; endl
									 &lt;&lt; &quot;\nZahl: &quot;;
								cin &gt;&gt; zahl2;
								cout &lt;&lt; endl;

								ergebnis = addieren( zahl1, zahl2);
								break;
							case 2:
								zahl1 = ergebnis;
								cout &lt;&lt; &quot;\nErgebnis Subtrahieren\n&quot;
									 &lt;&lt; &quot;\n&quot; &lt;&lt; ergebnis &lt;&lt; endl
									 &lt;&lt; &quot;\nZahl: &quot;;
								cin &gt;&gt; zahl2;
								cout &lt;&lt; endl;

								ergebnis = subtrahieren( zahl1, zahl2);
								break;
							case 3:
								zahl1 = ergebnis;
								cout &lt;&lt; &quot;\nErgebnis Multiplizieren\n&quot;
									 &lt;&lt; &quot;\n&quot; &lt;&lt; ergebnis &lt;&lt; endl
									 &lt;&lt; &quot;\nZahl: &quot;;
								cin &gt;&gt; zahl2;
								cout &lt;&lt; endl;

								ergebnis = multiplizieren( zahl1, zahl2);
								break;
							case 4:
								zahl1 = ergebnis;
								cout &lt;&lt; &quot;\nErgebnis Dividieren\n&quot;
									 &lt;&lt; &quot;\n&quot; &lt;&lt; ergebnis &lt;&lt; endl
									 &lt;&lt; &quot;\nZahl: &quot;;
								cin &gt;&gt; zahl2;
								cout &lt;&lt; endl;

								ergebnis = dividieren( zahl1, zahl2);
								break;
							default:
								cout &lt;&lt; &quot;\nUngueltige Eingabe!!!&quot; &lt;&lt; endl;
								break;
						}		//switchD
				}		//swichC
			}		//while schleife

		case 0:
			system(&quot;pause&quot;);
			return 0;
			break;
	}		//switchB
}
</code></pre>
<p>grundrechnung.h</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

double addieren( double summand1, double summand2 );

double subtrahieren( double summand1, double summand2 );

double multiplizieren( double summand1, double summand2 );

double dividieren( double summand1, double summand2 );
</code></pre>
<p>grundrechnung.cpp</p>
<pre><code class="language-cpp">#include &quot;grundrechnung.h&quot;

double addieren( double z1, double z2 )
{
	double ergebnis = z1 + z2;

	cout &lt;&lt; z1 &lt;&lt; &quot;+&quot; &lt;&lt; z2 &lt;&lt; &quot;= &quot; &lt;&lt; ergebnis &lt;&lt; endl;
	cout &lt;&lt; endl;

	return ergebnis;
}

double subtrahieren( double z1, double z2 )
{
	double ergebnis = z1 - z2;

	cout &lt;&lt; z1 &lt;&lt; &quot;-&quot; &lt;&lt; z2 &lt;&lt; &quot;= &quot; &lt;&lt; ergebnis &lt;&lt; endl;
	cout &lt;&lt; endl;

	return ergebnis;
}

double multiplizieren( double z1, double z2 )
{
	double ergebnis = z1 * z2;

	cout &lt;&lt; z1 &lt;&lt; &quot;*&quot; &lt;&lt; z2 &lt;&lt; &quot;= &quot; &lt;&lt; ergebnis &lt;&lt; endl;
	cout &lt;&lt; endl;

	return ergebnis;
}
double dividieren( double z1, double z2 )
{
	double ergebnis = z1 / z2;

	cout &lt;&lt; z1 &lt;&lt; &quot;/&quot; &lt;&lt; z2 &lt;&lt; &quot;= &quot; &lt;&lt; ergebnis &lt;&lt; endl;
	cout &lt;&lt; endl;

	return ergebnis;
}
</code></pre>
<p>edit: main.cpp verbessert</p>
<p>mfg rufrider</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1747952</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1747952</guid><dc:creator><![CDATA[rufrider]]></dc:creator><pubDate>Thu, 23 Jul 2009 11:09:02 GMT</pubDate></item><item><title><![CDATA[Reply to [C++ Rechner&#x2F;Anfänger] on Thu, 23 Jul 2009 00:28:48 GMT]]></title><description><![CDATA[<p>Keine GoTos verwenden. Sowas ist hässlich und unübersichtlich.</p>
<p>Du hast bei den Switch-Anweisungen kein default-wert angegeben.</p>
<p>Das heißt, wenn ich &quot;123&quot; eingebe, fällt dein Programm am Boden.</p>
<pre><code class="language-cpp">if( auswahl == 0 ) goto ende; //Eckelhaft...

    switch( auswahl )
    {
   //.....
    case 4:
        cout &lt;&lt; &quot;\nDividieren von zwei Zahlen\n&quot;
             &lt;&lt; &quot;\nZahl1: &quot;;
        cin &gt;&gt; zahl1;
        cout &lt;&lt; &quot;\nZahl2: &quot;;
        cin &gt;&gt; zahl2;
        cout &lt;&lt; endl;

        ergebnis = dividieren( zahl1, zahl2 );
        break;
    //default:
    // std::cout &lt;&lt; &quot;Ungültige Eingabe!!!!!&quot;;
    }
</code></pre>
<p>Wieso du '0' auch nicht als Case-Konstante angibst verstehe ich nicht. Da bräuchtest du nicht extra eine If-Abfrage.</p>
<p>Und das mit GoTo ende; Könntest du gleich so machen:</p>
<pre><code class="language-cpp">switch( auswahl )
    {
        //..............

case 0:
return 0;

    }
</code></pre>
<p>Mehr finde ich nicht bzw. bin zu faul um dein Code zu optimieren.</p>
<p>Ergo: Keine GoTos verwenden,... ned schön!</p>
<p>Lg Mentras</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1747956</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1747956</guid><dc:creator><![CDATA[Mentras]]></dc:creator><pubDate>Thu, 23 Jul 2009 00:28:48 GMT</pubDate></item><item><title><![CDATA[Reply to [C++ Rechner&#x2F;Anfänger] on Thu, 23 Jul 2009 11:11:38 GMT]]></title><description><![CDATA[<p>Ich hab die Sachen mit dem goto und mit dem if verbessert.</p>
<p>Und das mit dem goto werd ich mir merken.</p>
<p>mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1748147</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1748147</guid><dc:creator><![CDATA[rufrider]]></dc:creator><pubDate>Thu, 23 Jul 2009 11:11:38 GMT</pubDate></item><item><title><![CDATA[Reply to [C++ Rechner&#x2F;Anfänger] on Thu, 23 Jul 2009 12:11:01 GMT]]></title><description><![CDATA[<p>Hier ist mal eine &quot;50 Zeilen&quot;-Lösung als Beispiel, wie man es auch hätte implementieren können.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;map&gt;
#include &lt;string&gt;
#include &lt;utility&gt;

using std::cout;
using std::cin;
using std::map;
using std::string;
using std::pair;
using std::make_pair;

double my_add(double a, double b) {return a+b;}
double my_sub(double a, double b) {return a-b;}
double my_mul(double a, double b) {return a*b;}
double my_div(double a, double b) {return a/b;}

typedef double binfun_t(double,double);

int main()
{
  // Operator-Zeichen --&gt; Funktion X Beschreibung
  typedef map&lt;string,pair&lt;binfun_t*,string&gt; &gt; bomap_t;
  bomap_t binops;
  binops[&quot;+&quot;] = make_pair(&amp;my_add,&quot;Addition&quot;);
  binops[&quot;-&quot;] = make_pair(&amp;my_sub,&quot;Subtraktion&quot;);
  binops[&quot;*&quot;] = make_pair(&amp;my_mul,&quot;Multiplikation&quot;);
  binops[&quot;/&quot;] = make_pair(&amp;my_div,&quot;Division&quot;);

  for (;;) {
    cout &lt;&lt; &quot;Was willst Du machen?\n&quot;;
    for (bomap_t::iterator beg=binops.begin(),
                           end=binops.end();
         beg!=end; ++beg)
    {
      cout &lt;&lt; beg-&gt;first &lt;&lt; &quot; (&quot; &lt;&lt; beg-&gt;second.second &lt;&lt; &quot;)\n&quot;;
    }
    cout &lt;&lt; &quot;(Abbruch ueber andere Eingabe)\n&quot;;
    string eingabe;
    if (!(cin &gt;&gt; eingabe)) break;
    bomap_t::iterator it = binops.find(eingabe);
    if (it==binops.end()) break;
    binfun_t* bf = it-&gt;second.first;
    double a, b;
    cout &lt;&lt; it-&gt;second.second &lt;&lt; &quot; von Zwei zahlen:\n&quot;;
    if (!(cin &gt;&gt; a)) break;
    if (!(cin &gt;&gt; b)) break;
    cout &lt;&lt; a &lt;&lt; it-&gt;first &lt;&lt; b &lt;&lt; '=' &lt;&lt; bf(a,b) &lt;&lt; &quot;\n\n&quot;;
  }
}
</code></pre>
<p>HTH,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1748176</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1748176</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Thu, 23 Jul 2009 12:11:01 GMT</pubDate></item></channel></rss>