Task: Text processing_1


C++: #include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>

using std::cout;
using std::endl;
const int NumFlags = 24;

int main()
{
    std::fstream file("readings.txt");

    int badCount = 0;
    std::string badDate;
    int badCountMax = 0;
    while(true)
    {
        std::string line;
        getline(file, line);
        if(!file.good())
            break;

        std::vector<std::string> tokens;
        boost::algorithm::split(tokens, line, boost::is_space());

        if(tokens.size() != NumFlags * 2 + 1)
        {
            cout << "Bad input file." << endl;
            return 0;
        }

        double total = 0.0;
        int accepted = 0;
        for(size_t i = 1; i < tokens.size(); i += 2)
        {
            double val = boost::lexical_cast<double>(tokens[i]);
            int flag = boost::lexical_cast<int>(tokens[i+1]);
            if(flag > 0)
            {
                total += val;
                ++accepted;
                badCount = 0;
            }
            else
            {
                ++badCount;
                if(badCount > badCountMax)
                {
                    badCountMax = badCount;
                    badDate = tokens[0];
                }
            }
        }

        cout << tokens[0];
        cout << "  Reject: " << std::setw(2) << (NumFlags - accepted);
        cout << "  Accept: " << std::setw(2) << accepted;
        cout << "  Average: " << std::setprecision(5) << total / accepted << endl;
    }
    cout << endl;
    cout << "Maximum number of consecutive bad readings is " << badCountMax << endl;
    cout << "Ends on date " << badDate << endl;
}









Fortran: Crunches a set of hourly data. Starts with a date, then 24 pairs of value,indicator for that day, on one line.
      INTEGER Y,M,D		
      INTEGER GOOD(24)		
      REAL*8 V(24),VTOT,T	
      INTEGER NV,N,NB		
      INTEGER I,NREC,HIC	
      INTEGER BI,BN,BBI,BBN	
      CHARACTER*10 BDATE,BBDATE	
      LOGICAL INGOOD		
      INTEGER IN,MSG		
      CHARACTER*666 ACARD	
      IN = 10		
      MSG = 6		
      OPEN (IN,FILE="Readings1.txt", FORM="FORMATTED",	
     1 STATUS ="OLD",ACTION="READ")			
      NB = 0		
      NV = 0		
      VTOT = 0		
      NREC = 0		
      HIC = 0		
      INGOOD = .TRUE.	
      BBN = 0		
Chew into the file.
   10 READ (IN,11,END=100,ERR=666) L,ACARD(1:MIN(L,LEN(ACARD)))	
      NREC = NREC + 1		
   11 FORMAT (Q,A)		
      READ (ACARD,12,END=600,ERR=601) Y,M,D	
   12 FORMAT (I4,2(1X,I2))				
      READ (ACARD(11:L),*,END=600,ERR=601) (V(I),GOOD(I),I = 1,24)	
Calculations. Could use COUNT(array) and SUM(array), but each requires its own pass through the array.
   20 T = 0		
      N = 0		
      DO I = 1,24	
        IF (GOOD(I).GT.0) THEN	
          N = N + 1		
          T = T + V(I)		
          IF (.NOT.INGOOD) THEN	
            INGOOD = .TRUE.	
            IF (BN.GT.BBN) THEN	
              BBN = BN		
              BBI = BI		
              BBDATE = BDATE	
            END IF		
          END IF		
         ELSE		
          IF (INGOOD) THEN	
            INGOOD = .FALSE.	
            BDATE = ACARD(1:10)	
            BI = I		
            BN = 0		
          END IF		
          BN = BN + 1	
        END IF		
      END DO		
Commentary for the day's data..
      IF (N.LE.0) THEN	
        WRITE (MSG,21) NREC,ACARD(1:10)	
   21   FORMAT ("Record",I8," (",A,") has no good data
       ELSE			
        WRITE(MSG,22) NREC,ACARD(1:10),N,T/N	
   22   FORMAT("Record",I8," (",A,")",I3," good, average",F9.3)	
        NB = NB + 24 - N	
        NV = NV + N		
        VTOT = VTOT + T		
      END IF			
      GO TO 10		

Complaints. Should really distinguish between trouble in the date part and in the data part.
  600 WRITE (MSG,*) '"END" declared - insufficient data?'	
      GO TO 602				
  601 WRITE (MSG,*) '"ERR" declared - improper number format?'	
  602 WRITE (MSG,603) NREC,L,ACARD(1:L)	
  603 FORMAT(" Record ",I0,", length ",I0," reads ",A)	
      HIC = HIC + 1			
      IF (HIC.LE.12) GO TO 10		
      STOP "Enough distaste."		
  666 WRITE (MSG,101) NREC,"format error
      GO TO 900				

Closedown.
  100 WRITE (MSG,101) NREC,"then end-of-file"	
  101 FORMAT (" Record ",I0,": ",A)		
      WRITE (MSG,102) NV,NB,VTOT/NV		
  102 FORMAT (I8," values, ",I0," bad. Average",F9.4)	
      IF (BBN.LE.0) THEN		
        WRITE (MSG,*) "No bad value presented, so no longest run."	
       ELSE				
        WRITE (MSG,103) BBN,BBI,BBDATE	
  103   FORMAT ("Longest bad run: ",I0,", starting hour ",I0," on ",A)	
      END IF			
  900 CLOSE(IN)		
      END