A feladat leírását, melynek a megoldását közzéteszem, innen töltheted le.
#include <iostream> #include <fstream> #include <sstream> #include <cmath> #include <iomanip> // setprecision miatt #include <cstdlib> using namespace std; struct prog { string utasitas; int hossz; string toString() { stringstream ss; ss << utasitas << " " << hossz << endl; return ss.str(); } }; int main() { // 1. feladat int meret; ifstream input( "program.txt" ); input >> meret; prog programok[meret]; for ( int i = 0; i < meret; i++ ) { input >> programok[i].utasitas; programok[i].hossz = programok[i].utasitas.length(); // cout << i << " " << programok[i].toString(); } // 2. feladat cout << "2. feladat: Kerem az utasitassor szamat! " ; int sorszam; cin >> sorszam; sorszam--; // a. prog pr = programok[sorszam]; if ( pr.utasitas.find( "ED" ) < pr.utasitas.length() || pr.utasitas.find( "DE" ) < pr.utasitas.length() || pr.utasitas.find( "KN" ) < pr.utasitas.length() || pr.utasitas.find( "NK" ) < pr.utasitas.length() ) { cout << "egyszerusitheto." << endl; } else { cout << "nem egyszerusitheto." << endl; } // b. int x = 0; int y = 0; for ( int i = 0; i < pr.hossz; i++ ) { if ( pr.utasitas[i] == 'E' ) { y++; } else if ( pr.utasitas[i] == 'D' ) { y--; } else if ( pr.utasitas[i] == 'K' ) { x++; } else { x--; } } cout << abs (y) << " lepest kell tenni az ED, " << abs (x) << " lepest a KN tengely menten." << endl; // c. x = 0; y = 0; double maxtav = 0; int lepes = 0; for ( int i = 0; i < pr.hossz; i++ ) { if ( pr.utasitas[i] == 'E' ) { y++; } else if ( pr.utasitas[i] == 'D' ) { y--; } else if ( pr.utasitas[i] == 'K' ) { x++; } else { x--; } double tav = sqrt ((x*x) + (y*y)); if ( tav > maxtav ) { maxtav = tav; lepes = i + 1; } } cout << lepes << " " << fixed << setprecision(2) << maxtav << endl; // 3. feladat cout << "3. feladat" << endl; for ( int i = 0; i < meret; i++ ) { int energia = 2 + programok[i].hossz; for ( int j = 1; j < programok[i].hossz; j++ ) { if ( programok[i].utasitas[j - 1] != programok[i].utasitas[j] ) { energia += 2; } } if ( energia <= 100 ) { cout << (i+1) << " " << energia << endl; } } // 4. feladat cout << "4. feladat" << endl; ofstream output( "ujprog.txt" ); for ( int i = 0; i < meret; i++ ) { stringstream ss; int db = 1; for ( int j = 1; j < programok[i].hossz; j++ ) { if ( programok[i].utasitas[j-1] != programok[i].utasitas[j] ) { if ( db == 1 ) { ss << programok[i].utasitas[j-1]; } else { ss << db << programok[i].utasitas[j-1]; } db = 1; } else { db++; } } int utolso = programok[i].hossz; if ( db == 1 ) { ss << programok[i].utasitas[utolso-1]; } else { ss << db << programok[i].utasitas[utolso-1]; } output << ss.str() << endl; } output.close(); // 5. feladat cout << "5. feladat" << endl; string rovid; cout << "Adj meg egy roviditett utasitassort:" << endl; cin >> rovid; stringstream ss; stringstream utasitas; int hossz = rovid.length(); for ( int i = 0; i < hossz; i++ ) { if ( rovid[i] >= '0' && rovid[i] <= '9' ) { ss << rovid[i]; } else { if ( ss.str() != "" ) { int db; ss >> db; for ( int j = 0; j < db; j++ ) { utasitas << rovid[i]; } // stringstream kiuritese (2 utasitas!) ss.clear(); ss.str( "" ); } else { utasitas << rovid[i]; } } } cout << utasitas.str() << endl; return 0; } |
Ha hibát találsz benne, kérlek jelezd!