Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 10: Practical Applications

This chapter puts the concepts from the earlier chapters to work in three practical programs: a calculator, a simple game, and a basic database application.

Building a calculator

This simple calculator performs the basic arithmetic operations: addition, subtraction, multiplication, and division.

Calculator program

program Calculator;

uses
  crt, SysUtils;

var
  num1, num2, result: real;
  operation: char;

begin
  ClrScr;

  writeln('Simple Calculator');
  writeln('-----------------');

  writeln('Enter first number: ');
  readln(num1);

  writeln('Enter an operation (+, -, *, /): ');
  readln(operation);

  writeln('Enter second number: ');
  readln(num2);

  case operation of
    '+': result := num1 + num2;
    '-': result := num1 - num2;
    '*': result := num1 * num2;
    '/': if num2 <> 0 then
           result := num1 / num2
         else
         begin
           writeln('Error: Division by zero');
           exit;
         end;
  else
    writeln('Error: Invalid operation');
    exit;
  end;

  writeln('Result: ', result:0:2);
end.

Explanation

  1. The program asks the user to enter two numbers and an operation.
  2. A case statement performs whichever arithmetic operation the user chose.
  3. The program displays the result.

Simple game development

In this simple guessing game, the user has to guess a randomly generated number.

Guessing game program

program GuessingGame;

uses
  crt, SysUtils;

var
  secretNumber, guess: integer;
  attempts: integer;

begin
  ClrScr;

  Randomize;
  secretNumber := Random(100) + 1;  { Generate a random number between 1 and 100 }
  attempts := 0;

  writeln('Guess the Number Game');
  writeln('---------------------');

  repeat
    writeln('Enter your guess (1-100): ');
    readln(guess);
    attempts := attempts + 1;

    if guess < secretNumber then
      writeln('Too low!')
    else if guess > secretNumber then
      writeln('Too high!')
    else
      writeln('Congratulations! You guessed the number in ', attempts, ' attempts.');
  until guess = secretNumber;
end.

Explanation

  1. The program generates a random number between 1 and 100.
  2. It asks the user for a guess and says whether the guess is too low, too high, or correct.
  3. The game continues until the user guesses the number.

Database applications

This simple database application stores and manages student records. Each record holds a name and an age.

Student record program

program StudentDatabase;

uses
  crt, SysUtils;

type
  Student = record
    name: string[50];
    age: integer;
  end;

var
  students: array[1..100] of Student;
  studentCount: integer;

procedure AddStudent(name: string; age: integer);
begin
  if studentCount < 100 then
  begin
    studentCount := studentCount + 1;
    students[studentCount].name := name;
    students[studentCount].age := age;
  end
  else
    writeln('Error: Student database is full.');
end;

procedure ListStudents;
var
  i: integer;
begin
  writeln('Student List:');
  writeln('-------------');
  for i := 1 to studentCount do
    writeln(i, ': ', students[i].name, ', Age: ', students[i].age);
end;

var
  choice: char;
  name: string;
  age: integer;

begin
  ClrScr;
  studentCount := 0;

  repeat
    writeln('Student Database');
    writeln('----------------');
    writeln('1. Add Student');
    writeln('2. List Students');
    writeln('3. Exit');
    writeln('Enter your choice: ');
    readln(choice);

    case choice of
      '1':
        begin
          writeln('Enter student name: ');
          readln(name);
          writeln('Enter student age: ');
          readln(age);
          AddStudent(name, age);
        end;
      '2': ListStudents;
      '3': writeln('Exiting...');
    else
      writeln('Invalid choice.');
    end;

    writeln;
  until choice = '3';
end.

Explanation

  1. A record type defines a student, with a name and an age.
  2. The program keeps an array of student records and a count of how many students there are.
  3. The AddStudent procedure adds a new student to the array.
  4. ListStudents lists all the students in the database.
  5. The main program shows a menu where the user can add students, list them, or exit.