<?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[Taschenrechner Frage]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich wollte gerne einen einfachen Taschenrechner schreiben der die 4 Grundrechenarten beherrscht.</p>
<p>Ich würde in die Konsole dann gerne z.B. 3+5 oder 3/5 eingeben und das Ergebnis mit Enter anfordern. Ich kann nur einen Rechner erstellen den man z.B. so bedient: 4 5 Enter -&gt; 9. Also die beiden Zahlen schreiben ohne den Operator. Jenachdem ob man summe=zahl1+zahl2 oder summe=zahl1/zahl2 eingegeben hat, wird es dann berechnet. Ich würde die Rechenart also direkt mit dem Operator den ich eingeben bestimmen.</p>
<p>Mein Ansatz bisher sieht so aus:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

int main ()

{

	float zahl1, zahl2, summe; 
	char operator; 

	cin &gt;&gt; zahl1 &gt;&gt; &quot;operator&quot; &gt;&gt; zahl2; 

	summe= 

	cout &lt;&lt; summe; 

}
</code></pre>
<p>ich weiß, dass es so absolut falsch ist. Aber ich komme grade einfach nicht weiter. Ich hoffe mir kann jemand einen Tipp geben, wie ich am beste vorgehe.</p>
<p>Danke schonmal im voraus.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/310129/taschenrechner-frage</link><generator>RSS for Node</generator><lastBuildDate>Tue, 04 Aug 2026 23:26:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/310129.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 04 Nov 2012 15:10:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Taschenrechner Frage on Sun, 04 Nov 2012 15:11:12 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich wollte gerne einen einfachen Taschenrechner schreiben der die 4 Grundrechenarten beherrscht.</p>
<p>Ich würde in die Konsole dann gerne z.B. 3+5 oder 3/5 eingeben und das Ergebnis mit Enter anfordern. Ich kann nur einen Rechner erstellen den man z.B. so bedient: 4 5 Enter -&gt; 9. Also die beiden Zahlen schreiben ohne den Operator. Jenachdem ob man summe=zahl1+zahl2 oder summe=zahl1/zahl2 eingegeben hat, wird es dann berechnet. Ich würde die Rechenart also direkt mit dem Operator den ich eingeben bestimmen.</p>
<p>Mein Ansatz bisher sieht so aus:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

int main ()

{

	float zahl1, zahl2, summe; 
	char operator; 

	cin &gt;&gt; zahl1 &gt;&gt; &quot;operator&quot; &gt;&gt; zahl2; 

	summe= 

	cout &lt;&lt; summe; 

}
</code></pre>
<p>ich weiß, dass es so absolut falsch ist. Aber ich komme grade einfach nicht weiter. Ich hoffe mir kann jemand einen Tipp geben, wie ich am beste vorgehe.</p>
<p>Danke schonmal im voraus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2267228</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2267228</guid><dc:creator><![CDATA[Addicted]]></dc:creator><pubDate>Sun, 04 Nov 2012 15:11:12 GMT</pubDate></item><item><title><![CDATA[Reply to Taschenrechner Frage on Sun, 04 Nov 2012 15:14:10 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">char op; //operator isn blöder name, is nämlich ein schlüsselwort
int ergebnis, zahl1, zahl2; //oder lieber float, wegen /
cin &gt;&gt; zahl1 &gt;&gt; op &gt;&gt; zahl2;
switch(op) //kannst auf if nehmen, googel mal c++ switch
{
    case '*': ergebnis = zahl1*zahl2; break;
    //usw.
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2267229</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2267229</guid><dc:creator><![CDATA[Incocnito]]></dc:creator><pubDate>Sun, 04 Nov 2012 15:14:10 GMT</pubDate></item><item><title><![CDATA[Reply to Taschenrechner Frage on Sun, 04 Nov 2012 15:30:00 GMT]]></title><description><![CDATA[<p>yes, danke dir. funktioniert jetzt so wie ichs mir vorgestellt hatte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2267237</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2267237</guid><dc:creator><![CDATA[Addicted]]></dc:creator><pubDate>Sun, 04 Nov 2012 15:30:00 GMT</pubDate></item><item><title><![CDATA[Reply to Taschenrechner Frage on Sun, 04 Nov 2012 15:33:30 GMT]]></title><description><![CDATA[<p>Komfortables Geschmacksmuster:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;

int main()
{
	std::cout &lt;&lt; &quot;Simple calculator.\n\n&quot;;

	std::string input;
	float lhs = 0.;
	float rhs = 0.;
	char op = 0;
	double result = 0.;

	for( ;; ) {

		std::cout &lt;&lt; &quot;&gt; &quot;;
		std::getline( std::cin, input );

		if( input.find( &quot;exit&quot; ) != input.npos )
			break;

		std::stringstream ss( input );
		if( !( ss &gt;&gt; lhs &gt;&gt; op &gt;&gt; rhs ) ) op = 0;

		switch( op ) {

			case '+': result = lhs + rhs; break;
			case '-': result = lhs - rhs; break;
			case '*': result = lhs * rhs; break;
			case '/': result = lhs / rhs; break;
			default:
				std::cerr &lt;&lt; &quot;Input error.\n\n&quot;;
				continue;
		}

		std::cout &lt;&lt; lhs &lt;&lt; ' ' &lt;&lt; op &lt;&lt; ' ' &lt;&lt; rhs &lt;&lt; &quot; = &quot; &lt;&lt; result &lt;&lt; &quot;\n\n&quot;;
	}

	std::cout &lt;&lt; &quot;bye.\n\n&quot;;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2267238</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2267238</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sun, 04 Nov 2012 15:33:30 GMT</pubDate></item><item><title><![CDATA[Reply to Taschenrechner Frage on Sun, 04 Nov 2012 15:46:45 GMT]]></title><description><![CDATA[<p>Mit meiner Klasse <a href="http://www.c-plusplus.net/forum/p2266317#2266317" rel="nofollow">line_guard</a> geht das auch ohne den Umweg über den stringstream:</p>
<pre><code class="language-cpp">int main()
{
  std::cout &lt;&lt; &quot;Simple calculator.\n\n&quot;;

  float lhs, rhs, result;
  char op;

  for( ;; ) {
    std::cout &lt;&lt; &quot;&gt; &quot;;
    line_guard g;

    if (std::cin &gt;&gt; std::skipws &amp;&amp; std::cin.peek() == 'q')
      break;

    if (!(std::cin &gt;&gt; lhs &gt;&gt; op &gt;&gt; rhs) || !g.eol())
      op = '\0';

    switch (op) {       
    case '+': result = lhs + rhs; break;
    case '-': result = lhs - rhs; break;
    case '*': result = lhs * rhs; break;
    case '/': result = lhs / rhs; break;
    default:
      std::cerr &lt;&lt; &quot;Input error\n\n&quot;;
      continue;
    }
    std::cout &lt;&lt; lhs &lt;&lt; ' ' &lt;&lt; op &lt;&lt; ' ' &lt;&lt; rhs &lt;&lt; &quot; = &quot; &lt;&lt; result &lt;&lt; &quot;\n\n&quot;;
  }
  std::cout &lt;&lt; &quot;bye.\n\n&quot;;
}
</code></pre>
<p>Ausserdem werden falsche Eingaben wie &quot;1+2*3&quot; abgefangen. Und wenn man ein eof sendet, verfängt sich das Programm nicht in einer Endlosschleife.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2267244</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2267244</guid><dc:creator><![CDATA[mont]]></dc:creator><pubDate>Sun, 04 Nov 2012 15:46:45 GMT</pubDate></item><item><title><![CDATA[Reply to Taschenrechner Frage on Sun, 04 Nov 2012 15:57:25 GMT]]></title><description><![CDATA[<p>danke sind sehr hilfreiche antworten dabei.</p>
<p>aber wie bekomme ich es jetzt hin, dass wie hier z.B.</p>
<pre><code class="language-cpp">case '+': summe=zahl1+zahl2;
		cout &lt;&lt; &quot;=&quot; &lt;&lt; summe &lt;&lt; endl &lt;&lt; endl; 
		break;
</code></pre>
<p>mein cout auf der gleichen zeile wie mein cin steht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2267250</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2267250</guid><dc:creator><![CDATA[Addicted]]></dc:creator><pubDate>Sun, 04 Nov 2012 15:57:25 GMT</pubDate></item><item><title><![CDATA[Reply to Taschenrechner Frage on Sun, 04 Nov 2012 16:00:05 GMT]]></title><description><![CDATA[<p>Mit Standard-C++ garnicht. Da musst Du entsprechende APIs deines Betriebssytems bitten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2267252</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2267252</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sun, 04 Nov 2012 16:00:05 GMT</pubDate></item><item><title><![CDATA[Reply to Taschenrechner Frage on Sun, 04 Nov 2012 16:09:58 GMT]]></title><description><![CDATA[<p>gut, dann lass ich das vorerst ^^</p>
<p>danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2267257</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2267257</guid><dc:creator><![CDATA[Addicted]]></dc:creator><pubDate>Sun, 04 Nov 2012 16:09:58 GMT</pubDate></item><item><title><![CDATA[Reply to Taschenrechner Frage on Sun, 04 Nov 2012 16:26:27 GMT]]></title><description><![CDATA[<p>Für Windows:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;

#include &lt;windows.h&gt; // platform-specific: GetStdHandle(), GetConsoleScreenBufferInfo(), SetConsoleCursorPosition()

void gotoxy( short x, short y )
{
	HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
    COORD coord = { x, y };
	SetConsoleCursorPosition( hstdout, coord );
}

short wherey( )
{
    HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
	CONSOLE_SCREEN_BUFFER_INFO csbi;	
	GetConsoleScreenBufferInfo( hstdout, &amp;csbi );
	return csbi.dwCursorPosition.Y;
}

int main()
{
    std::cout &lt;&lt; &quot;Simple calculator.\n\n&quot;;

    std::string input;
    float lhs = 0.;
    float rhs = 0.;
    char op = 0;
    double result = 0.;

    for( ;; ) {

        std::cout &lt;&lt; &quot;&gt; &quot;;
        std::getline( std::cin, input );

        if( input.find( &quot;exit&quot; ) != input.npos )
            break;

        std::stringstream ss( input );
        if( !( ss &gt;&gt; lhs &gt;&gt; op &gt;&gt; rhs ) ) op = 0;

		short x = input.length() + 2;
		short y = wherey() - 1;

        switch( op ) {

            case '+': result = lhs + rhs; break;
            case '-': result = lhs - rhs; break;
            case '*': result = lhs * rhs; break;
            case '/': result = lhs / rhs; break;
            default:
                std::cerr &lt;&lt; &quot;Input error.\n\n&quot;;
                continue;
        }

		gotoxy( x, y );
        std::cout &lt;&lt; &quot; = &quot; &lt;&lt; result &lt;&lt; &quot;\n\n&quot;;
    }

    std::cout &lt;&lt; &quot;bye.\n\n&quot;;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2267274</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2267274</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sun, 04 Nov 2012 16:26:27 GMT</pubDate></item></channel></rss>