"2*4+3"
"1+4+5*9"
usw. Es darf auch ein Leerzeichen dazwischen sein.
"2 * 4 + 3"
Der Rest ist nicht erlaubt. Also kein
"3++3"
"3+2*4
( muss in " " sein)
Naja, soweit bin ich erst gekommen...
Quellcode
- #include<iostream>
- #include<string>
- #include<stdlib.h>
- using namespace std;
- std::ifstream inFile("file1.txt", std::ios::in);
- if ( ! inFile.is_open() ){
- std::cerr << "Error: unable to open file for input" << std::endl;
- return 1;
- }
- std::ofstream outFile( "file2.txt", std::ios::app );
- if ( ! outFile.is_open() ){
- std::cerr << "Error, unable to open file for output" << std::endl;
- return 2;
- }
- while ( inFile.good() ){
- std::string line;
- getline( inFile, line)
- outFile << line << std::endl;
- }
- static int eval(string line)
- {
- if(line.length() < 1) </b>ression is 0
- return 0;
- if(line.find_first_of('+') == string::npos)
- {
- if(line.find_first_of('*') == string::npos)
- return atoi(line.c_str());
- int pos = line.find_first_of('*');
- return eval(line.substr(0, pos)) * eval(line.substr(pos+1, line.length()));
- }
- int pos = line.find_first_of('+');
- return eval(line.substr(0, pos)) + eval(line.substr(pos+1, line.length()));
- }
- return 0;
- }
Also erst einlesen, dann eine rekursive Funktion die mir das berechnet. Bin mir nicht sicher, ob das mit dem "linie" einfach so geht, aber da das ja hier "outFile << line << std::endl;" gespeichert wird, hab ich das halt bei der rekursiven Funktion natuerlich weiterbenutzt.
Wie ueberpruefe ich jetzt, ob das auch alles im richtigen Format ist und es keine Abweichungen gibt? Hab da keine Ahnung ehrlich gesagt:(