@ Composer.
Man kann ja auswählen, ob man noch eine Rechnung durchführen will oder nicht.
@ superuser.
Ich finde es ist für den Beginn eine gute Übung, um zu sehen, wie ein Programm funktioniert, wie weit man mit if und while gehen kann. Klar kann man das auch mit Arrays schöner darstellen, aber für den Beginn ist das doch ganz gut, mMn.
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#include <cmath> #include <iostream> #include <string> #include <vector> #define BUFFER_SIZE 128 float multiply(const float a, const float b) { return (a*b); } float divide(const float a, const float b) { if ( b == 0 ) { return -1; } return (a / b); } bool identify(const std::string& str) { int find = 0; // Multiply find = str.find("*") ; if ( find != std::string::npos ) { std::string a = str.substr(0, find); std::string b = str.substr(find + 1, str.length()); float a1 = atof(a.c_str()); float b1 = atof(b.c_str()); std::cout << "Ergebnis der Multiplikation " << a1 << " * " << b1 << " : " << multiply(a1, b1) << std::endl; return true; } // Divide find = str.find("/") ; if ( find != std::string::npos ) { std::string a = str.substr(0, find); std::string b = str.substr(find + 1, str.length()); float a1 = atof(a.c_str()); float b1 = atof(b.c_str()); std::cout << "Ergebnis der Division " << a1 << " / " << b1 << " : " << divide(a1, b1) << std::endl; return true; } return false; } int main(int argc, char** argv) { char input[BUFFER_SIZE]; std::cout << "Bitte Rechnung eingeben: "; std::cin.getline(input, BUFFER_SIZE); if ( !identify(input) ) { std::cout << "Keine gueltige Rechnung erkannt!" << std::endl; } } |
This post has been edited 2 times, last edit by "pokertom" (May 2nd 2009, 9:15am)
This post has been edited 1 times, last edit by "pokertom" (May 2nd 2009, 9:51am)
Außerdem würde ich gerne wissen, wie man cin.get benutzt, verstehe es nicht wirklich???
|
|
Source code |
1 2 3 4 5 6 7 8 |
#include <iostream>
using namespace std;
int main(){
cout << "hi1";
cin.get();
cout << "hi";
return 0;
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream>
using namespace std;
void stopprog(); //Funktion bekannt machen
int main()
{
cout << "hi1\n";
stopprog();
// anderes Zeug...
return 0;
}
void stopprog(){
cout << "Drücken sie irgendeine Taste, um fortzufahren\n"; //gibt die anchricht aus
cin.get(); //wartet auf input vom User
}
|
This post has been edited 1 times, last edit by "malontop" (May 24th 2010, 7:21pm)
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
//Taschenrechner.cpp #include <cmath> #include "Formeln.hpp" int main() { int eingabe; std::cout << "Geben Sie bitte den Wert ein, den sie kennen" << std::endl; std::cout << "<1> Oberfläche" << std::endl; std::cout << "<2> Volumen" << std::endl; std::cout << "<3> Durchmesser" << std::endl; std::cout << "<4> Radius" << std::endl; std::cin >> eingabe; switch (eingabe) { case 1: Ko(); std::cin.get(); break; case 2: Kv(); std::cin.get(); break; case 3: Kd(); std::cin.get(); break; case 4: Kr(); std::cin.get(); default: std::cout << "Bitte Zahlen von 1 - 4 angeben." << std::endl; std::cout << "(Nicht erlaubt sind Buchstaben, Sonderzeichen und Kommazahlen)" << std::endl; } return 0; } |
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
//Formeln.hpp #ifndef FORMELN_H #define FORMELN_H #include <iostream> #define pi 3.1415926535897932384626433832795 double x, y, z, a, b, c, r; void Kv() { std::cout << "Geben Sie bitte das Volumen an." << std::endl; std::cin >> x; r = 2 * std::sqrt(x / (4 / 3 * pi)); y = (4 / 3) * pi * r * r * r; std::cout << "Oberfläche: " << y << std::endl; std::cout << "Durchmesser: " << r / 2 << std::endl; std::cout << "Radius: " << r << std::endl; } void Ko() { std::cout << "Geben Sie bitte die Oberfläche an:" << std::endl; std::cin >> x; r = sqrt(x/(4 * pi)); y = (4 / 3) * pi * r * r * r; std::cout << "Volumen: " << y << std::endl; std::cout << "Durchmesser: " << r / 2 << std::endl; std::cout << "Radius: " << r << std::endl; } void Kd() { std::cout << "Geben Sie bitte den Durchmesser an." << std::endl; std::cin >> x; r = (x / 2); y = 4 * pi * r * r; std::cout << "Oberfläche: " << y << std::endl; y = (4 / 3) * pi * r * r * r; std::cout << "Volumen: " << y << std::endl; std::cout << "Radius " << r << std::endl; } void Kr() { std::cout << "Geben Sie bitte den Radius an." << std::endl; std::cin >> x; r = x; y = 4 * pi * r * r; std::cout << "Oberfläche: " << y << std::endl; y = (4 / 3) * pi * r * r * r; std::cout << "Volumen: " << y << std::endl; std::cout << "Durchmesser " << r * 2 << std::endl; } #endif |
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 |
void Ko()
{
std::cout << "Geben Sie bitte die Oberfläche an:" << std::endl;
std::cin >> x;
r = sqrt(x/(4 * pi));
y = (4 / 3) * pi * r * r * r;
std::cout << "Volumen: " << y << std::endl;
std::cout << "Durchmesser: " << r / 2 << std::endl;
std::cout << "Radius: " << r << std::endl;
std::cin.get(); // Das hier ist das wichtige
}
|
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
//Taschenrechner.cpp #include <cmath> #include <iostream> #include "Formeln.hpp" int main() { int eingabe; std::cout << "Geben Sie bitte den Wert ein, den sie kennen" << std::endl; std::cout << "<1> Oberfl\204che" << std::endl; std::cout << "<2> Volumen" << std::endl; std::cout << "<3> Durchmesser" << std::endl; std::cout << "<4> Radius" << std::endl; std::cin >> eingabe; switch (eingabe) { case 1: Ko(); break; case 2: Kv(); break; case 3: Kd(); break; case 4: Kr(); break; default: std::cout << "Bitte Zahlen von 1 - 4 angeben." << std::endl; std::cout << "(Nicht erlaubt sind Buchstaben, Sonderzeichen und Kommazahlen)" << std::endl; } return 0; } |
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
//Formeln.hpp #ifndef FORMELN_HPP #define FORMELN_HPP #include <conio.h> #define pi 3.1415926535897932384626433832795 double x, y, z, a, b, c, r; void Kv() { std::cout << "Geben Sie bitte das Volumen an." << std::endl; std::cin >> x; r = 2 * std::sqrt(x / (4 / 3 * pi)); y = (4 / 3) * pi * r * r * r; std::cout << "Oberfl\204che: " << y << std::endl; std::cout << "Durchmesser: " << r / 2 << std::endl; std::cout << "Radius: " << r << std::endl; getch(); } void Ko() { std::cout << "Geben Sie bitte die Oberfl\204che an:" << std::endl; std::cin >> x; r = sqrt(x/(4 * pi)); y = (4 / 3) * pi * r * r * r; std::cout << "Volumen: " << y << std::endl; std::cout << "Durchmesser: " << r / 2 << std::endl; std::cout << "Radius: " << r << std::endl; getch(); } void Kd() { std::cout << "Geben Sie bitte den Durchmesser an." << std::endl; std::cin >> x; r = (x / 2); y = 4 * pi * r * r; std::cout << "Oberfl\204che: " << y << std::endl; y = (4 / 3) * pi * r * r * r; std::cout << "Volumen: " << y << std::endl; std::cout << "Radius " << r << std::endl; getch(); } void Kr() { std::cout << "Geben Sie bitte den Radius an." << std::endl; std::cin >> x; r = x; y = 4 * pi * r * r; std::cout << "Oberfl\204che: " << y << std::endl; y = (4 / 3) * pi * r * r * r; std::cout << "Volumen: " << y << std::endl; std::cout << "Durchmesser " << r * 2 << std::endl; getch(); } #endif |
This post has been edited 1 times, last edit by "pokertom" (Jun 4th 2010, 4:57pm)
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
//Taschenrechner.cpp #include <cmath> #include <iostream> #include "Formeln.hpp" int main() { int eingabe; std::cout << "Geben Sie bitte den Wert ein, den sie kennen" << std::endl; std::cout << "<1> Oberfl\204che" << std::endl; std::cout << "<2> Volumen" << std::endl; std::cout << "<3> Durchmesser" << std::endl; std::cout << "<4> Radius" << std::endl; std::cin >> eingabe; system("CLS"); switch (eingabe) { case 1: Ko(); break; system("CLS"); case 2: Kv(); break; system("CLS"); case 3: Kd(); break; system("CLS"); case 4: Kr(); break; system("CLS"); default: std::cout << "Bitte Zahlen von 1 - 4 angeben." << std::endl; std::cout << "(Nicht erlaubt sind Buchstaben, Sonderzeichen und Kommazahlen)" << std::endl; } return 0; } |
.
. This post has been edited 1 times, last edit by "pokertom" (Aug 1st 2010, 4:08pm)