コード例 #1
0
void getFlights(Graph g, int * dfsOrdered){
    int nDiffCountries = 1; 
    int nFlights = 0;
    int currCountry = 0;
       
    while (nDiffCountries < numV(g)){
        Edge nextFlight = getNextFlight(g, dfsOrdered, &nDiffCountries, currCountry);  
        nFlights++;
        printf("Flight %d %s %s\n", nFlights, getVertexLabel(g,nextFlight.v), getVertexLabel(g,nextFlight.w));
	currCountry = nextFlight.w;
    }
    
}
コード例 #2
0
ファイル: RouteTime.c プロジェクト: HongjianZhang/CS61C
/*
   Given a file with routes and a flight system, parses the file and determine (and print) the earliest time each route can be completed.
   Lines of bad form are skipped while new lines are ignored.
 */
void calcRouteTimes(flightSys_t* s, FILE* routes) {
    char* newRoutePrefix = "ROUTE: ";
    size_t prefixLen = strlen(newRoutePrefix);
    char line[MAX_LINE_LEN];
    timeHM_t now;
    char route[MAX_LINE_LEN];
    char timeBuf[MAX_LINE_LEN];
    char airportBuf[MAX_LINE_LEN];
    airport_t* curAirport = NULL;
    int curCost = 0;
    int totalCost = 0;

    /*
       This while loop parses the file line by line and calculate the route times as it goes.
       If a new route line is found, the current route (if it exists) would be done and the result is printed.
       A current route exists if curAirport is not null. 

       curAirport tells us where we are on the current route.

       now tells us the current time (the earliest time we can reach the curAirport on the current route).
     */
    while(fgets(line, MAX_LINE_LEN,routes)) {
	stripNewLine(line);

	if(!strlen(line)) continue; //ignore line if it's empty
	else if(strncmp(line,newRoutePrefix,prefixLen) == 0) { //if beginning of line starts with "ROUTE: ", we are calculating for a new route
	    if(curAirport) printCompleteRoute(route,&now, totalCost); //if curAirport is not NULL, we are done with the previous route so print the result

	    curAirport = NULL;
	    totalCost = 0;
	    if(3!=sscanf(line+prefixLen,"%s %s %s",route,airportBuf,timeBuf) //parse rest of new route line into route name, airport name, and start time 
		    || !stringToTime(timeBuf,&now) //ensure time is valid
		    || !(curAirport = getAirport(s,airportBuf))) { //ensure airport exists and sets curAirport to the starting airport
		printf("Skipping line: %s\n",line);
		continue;
	    }
	} else if (curAirport) {
	    /*
	       This is the case when we are in the middle of calculating for a route,
	       and the line should just be next airport we have to get to.
	       Here, we use getNextFlight to determine the flight to take to the next airport.
	       If there are no possible flights, the route cannot be completed.
	     */
	    airport_t* next = getAirport(s,line);
	    if(!next) {
		printf("Skipping line: %s\n",line);
		continue;
	    }
	    timeHM_t depart;
	    timeHM_t arrival;
	    if(!getNextFlight(curAirport,next,&now,&depart,&arrival,&curCost)) {
		curAirport = NULL;
		totalCost = 0;
		printf("Route %s cannot be completed\n",route);
		continue;
	    }
	    now = arrival;
	    curAirport = next;
	    totalCost += curCost;
	}
    }
    if(curAirport) printCompleteRoute(route,&now,totalCost);
}