fork(1) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. enum class ReplaceState {
  6. Begin, End
  7. };
  8.  
  9. int main() {
  10.  
  11. using namespace std;
  12.  
  13. string const begin_replace = "0 / END OF GENERATOR DATA, BEGIN BRANCH DATA";
  14. string const end_replace = "0 / END OF BRANCH DATA, BEGIN TRANSFORMER DATA";
  15.  
  16. ReplaceState state = ReplaceState::End;
  17. string line; int replacing_number = 1;
  18. while( getline(cin, line) ) {
  19. // state change
  20. if( line.find(begin_replace) != string::npos ) {
  21. cout << line << endl;
  22. state = ReplaceState::Begin;
  23. continue;
  24. }
  25. else if( line.find(end_replace) != string::npos ) {
  26. cout << line << endl;
  27. state = ReplaceState::End;
  28. continue;
  29. }
  30. // determine to replace or not
  31. if( state == ReplaceState::Begin ) {
  32. // find 3rd comma
  33. string::size_type comma = 0;
  34. for( int cnt = 0; cnt != 3; ++cnt )
  35. comma = line.find_first_of( ',', comma+1 );
  36.  
  37. // output first two fields
  38. cout << line.substr( 0, comma+1 ) << " ";
  39.  
  40. // output replacing number
  41. cout << fixed << setprecision(5) << (float)replacing_number++;
  42.  
  43. // output remained fields
  44. comma = line.find_first_of( ',', comma+1 );
  45. cout << line.substr( comma ) << endl;
  46. } else {
  47. cout << line << endl;
  48. }
  49. }
  50. } // main()
Success #stdin #stdout 0s 3020KB
stdin
Standard input is empty
stdout
Standard output is empty