I am trying to convert the following program to C and actually understand it. This is an introductory type program for the beginner that is me. In my intro to C class we have just barely got to if statements and it would be fantastic if I could get this program working in time for a little fantasy football. If someone could take the time and help me through this and give me guidance from square one (especially with inputting external files into my program) I would be forever in their debt (ok not forever but it would be greatly appreciated).
Anyways, here is the code in java:
Code:
import java.io.*;
import java.util.*;
public class FootballPlayer implements Comparable<FootballPlayer> {
// Components of a football player.
private String name;
private String position;
private int powerRanking;
// Creates a Football player object with all the necessary statistics.
public FootballPlayer(String n, String p, int pass, int rush, int rec, int TD) {
name = n;
position = p;
powerRanking = pass/30 + (rush+rec)/10 + 6*TD;
}
// Here is how we want a player to print out.
public String toString() {
return name + " " + position + " " + powerRanking;
}
public int compareTo(FootballPlayer other) {
// First sort by power ranking.
if (this.powerRanking != other.powerRanking)
return other.powerRanking - this.powerRanking;
// Break ties by name.
return this.name.compareTo(other.name);
}
public static void main(String[] args) throws Exception {
// Open file.
Scanner stdin = new Scanner(System.in):
System.out.println("What is the name of your input file?");
String fileName = stdin.next();
Scanner fin = new Scanner(new File(fileName));
ArrayList<FootballPlayer> nfl = new ArrayList<FootballPlayer>();
int numPlayers = fin.nextInt();
// Go through the file.
for (int i=0; i<numPlayers; i++) {
// Get this player's stats.
String name = fin.next();
String position = fin.next();
int pass = fin.nextInt();
int rush = fin.nextInt();
int rec = fin.nextInt();
int TDs = fin.nextInt();
// Add this guy to our list!
FootballPlayer tmp = new FootballPlayer(name,position,pass,rush,rec,TDs);
nfl.add(tmp);
}
// Sort and print.
Collections.sort(nfl);
for (int i=0; i<nfl.size(); i++)
System.out.println(nfl.get(i));
fin.close();
}
}