Here are some useful code fragements for Java. I haven't included anything for C++ since most students are using Java and if you don't know how to parse a string of words in C++ then perhaps you want to use Java since more support is provided.
//ArrayList is in this package
import java.util.*;
//Definition for infinity
public static final double INF = java.lang.Double.POSITIVE_INFINITY;
//--------------------------------- reading airport-data.txt --------------------------------
BufferedReader r;
try {
InputStream is = new FileInputStream("airport-data.txt");
r = new BufferedReader(new InputStreamReader(is));
}
catch (IOException e) {
System.out.println("IOException while opening airport-data.txt\n" + e);
return;
}
try {
String nextline = r.readLine();
StringTokenizer st = new StringTokenizer(nextline);
int numAirports = Integer.parseInt(st.nextToken());
for (int i = 0; i < numAirports; i++){
nextline = r.readLine();
st = new StringTokenizer(nextline);
String airportCode = st.nextToken();
int gmtOffset = Integer.parseInt(st.nextToken());
// You now have the airportCode and gmtOffset to use as you want
}
}
catch (IOException e) {
System.out.println("IOException while reading sequence from " +
"airport-data.txt\n" + e);
//--------------------------------- reading flight-data.txt --------------------------------
BufferedReader r;
try {
InputStream is = new FileInputStream("flight-data.txt");
r = new BufferedReader(new InputStreamReader(is));
}
catch (IOException e) {
System.out.println("IOException while opening flight-data.txt\n" + e);
return;
}
try {
boolean stop = false;
while (!stop){
String nextline = r.readLine();
if (nextline == null) // end of file
stop = true;
else {
StringTokenizer st = new StringTokenizer(nextline);
String airline = st.nextToken();
int flightNum = Integer.parseInt(st.nextToken());
String departureAirportCode = st.nextToken();
int localDepartTime = Integer.parseInt(st.nextToken());
boolean departAM = st.nextToken().equals("A");
String arrivalAirportCode = st.nextToken();
int localArriveTime = Integer.parseInt(st.nextToken());
boolean arriveAM = st.nextToken().equals("A");
// The above reads in the airline, flight number, and the 3 fields (airport, time, A or P)
// for both the departure and arrival airports. You take it from here.
}
}
}
catch (IOException e) {
System.out.println("IOException while reading sequence from " +
"flight-data.txt\n" + e);