Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Pascal Online

//  Brandon Treno
//  DONT FORGET TO FILL OUT A HEADER AND COMMENTS
//  ..................................................
//

Program ElectionResults(input, output);
{$mode objfpc} // directive to be used for defining classes
{$m+}		   // directive to be used for using constructor

// Begin Candidate class 
type
    Candidate = class
    private
        name: string;
        votes: integer;
        
    public
        constructor create(_name: string; _votes: integer);
        function getName(): string;
        function getVotes(): integer;
end;

type
    // array of Candidates
    CandidateArray = array of Candidate;

// Variables
var
    totalVotes, numCandidates, i: integer;
    candidates: CandidateArray;

// Candidate class functions + constructor
constructor Candidate.create(_name: string; _votes: integer);
begin
    name := _name;
    votes := _votes;
end;

function Candidate.getName(): string;
begin
    getName := name;
end;

function Candidate.getVotes(): integer;
begin
    getVotes := votes;
end;

// Functions and Procedures
(*reads 2 lines of stdin, makes a candidate obj, and returns the votes received*)
function readCand(): Candidate;

var
    name: string;
    votes: integer;
    can: Candidate;

begin
    readln(name);
    readln(votes);
    
    can.create(name, votes);
    readCand := can;
end;

// procedure to make a candidate object add it to array and increment
// totalVotes
// function to find the percentage of total votes
begin
    //get num at start of file and set length of Candidate array
    readln(numCandidates);
    setLength(candidates, numCandidates);
    
    //initialize
    totalVotes := 0;
    
    //read all candidate names and votesReceived; store in candidates[i]
    for i := 1 to numCandidates do
    begin
        candidates[i] := readCand();
        
        totalVotes := totalVotes + candidates[i].getVotes();
        writeln(totalVotes);
    end;
    
    
end.

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.