Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X

Pascal Program to Calculate Student CGPA, Reading From CSV File

Someone asked me to help her put up a pascal program to calculate student CGPA, reading from a CSV file. After few hours of putting this together I knew someone might love to do some similar stuff with this, so I decided to share my code with everyone.

Pascal language is cool...I love the syntax.

Below is the solution. I guess this makes someone happy.

 


Program CGPA_Calculator;

	uses Classes, SysUtils;
	
	{function IScan
	Parameters:
	ch: Character to scan for
	S : string to scan
	fromPos: first character to scan
	Returns: position of next occurence of character ch, or 0, if none found
	Description: Search for next occurence of a character in a string.
	Error Conditions: none
	Created: 11/27/96 by P. Below}
	
	function IScan(ch: Char; const S: string; fromPos: Integer): Integer;
	var
	  i : Integer;
	begin
	  Result := 0;
	  for i := fromPos to Length(S) do
	  begin
		if S[i] = ch then
		begin
		  Result := i;
		  Break;
		end;
	  end;
	end;
	
	{procedure SplitString
	Parameters:
	S: string to split
	separator: character to use as separator between substrings
	substrings: list to take the substrings
	Description:
	Isolates the individual substrings and copies them into the passed stringlist. Note 
	that we only add to the list, we do not clear it first! if two separators follow 
	each other directly an empty string will be added to the list.
	Error Conditions:
	will do nothing if the stringlist is not assigned
	Created: 08.07.97 by P. Below}
	
	procedure SplitString(const S: string; separator: Char; substrings: TStringList);
	var
	  i, n: Integer;
	begin
	  if Assigned(substrings) and (Length(S) > 0) then
	  begin
		i := 1;
		repeat
		  n := IScan(separator, S, i);
		  if n = 0 then
			n := Length(S) + 1;
		  substrings.Add(Copy(S, i, n - i));
		  i := n + 1;
		until
		  i > Length(S);
	  end;
	end;
    
    var
    
    i, numerator : integer;
    denominator, cgpa : real;
	input_file: textfile;
	input_rec : string;
	fileStr, resultDetails : TStringList;
    
Begin

    denominator := 0;
    numerator := 0;
	
	writeln('Welcome to Pascal CGPA Calculator!');
	
	AssignFile(input_file, 'student_gpa.csv');
	
	if FileExists('student_gpa.csv') then
		reset(input_file);
		
		Repeat
			 
			readln(input_file, input_rec);
			
			fileStr := TStringList.Create;
			
			SplitString(input_rec, ',', fileStr);

				for i := 1 to fileStr.Count - 1 do
					
					Begin
						
						resultDetails := TStringList.Create;
						SplitString(fileStr[i], '|', resultDetails);
							
						denominator :=  denominator + (StrToFloat(resultDetails[0]) * StrToFloat(resultDetails[1]));
						numerator := numerator + StrToInt(resultDetails[1]);
							
					End;
					
			cgpa := denominator / numerator;
			 
			writeln(fileStr[0] , ' CGPA is: ',  cgpa:5:2);
		
		Until Eof(input_file);
	
	close(input_file);
    
End.

Below is sample content of the CSV:

John Doe,3.2|8,3.0|10
Smart Smith, 2.5|18,3.2|12,3.0|15
Jane Joan,3.2|8,3.0|10

 


comments powered by Disqus