Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  Calculator
Language: C/C++
Code:
program calculator;
uses crt;

var
     sel      : integer;   {selecting the calculation type}
     choice   : char;      {continuing calculation selection}
     i        : boolean;   {continuing calculation selection}
     continue : boolean;   {continuing calculation selection}
     num_1    : real;      {first number}
     num_2    : real;      {second number}
     total    : real;      {sum / difference / product / quotient}

begin
clrscr;

writeln('Made by: Giorgos Atheridis');

writeln; {white space}
writeln; {white space}

i := true; {setting i to true for the while loop}

while i = true do {allowing the user as many calculations as the user likes}
     begin
          writeln('What type of calculation would you like?');

          writeln; {white space}

          writeln(' [1].ADDITION');
          writeln(' [2].SUBTRACTION');
          writeln(' [3].MULTIPLICATION');
          writeln(' [4].DIVISION');

          readln(sel); {user selects the calculation type}

          writeln; {white space}

          if sel = 1 then {Addition}
               begin
                    writeln('Addition');
                    write('First Number = ');
                    readln(num_1); {user enters first number}

                    write('Second Number = ');
                    readln(num_2); {user enters second number}

                    writeln; {white space}

                    total := num_1 + num_2;
                    writeln('The sum of the two numbers is = ' , total:0:2); {the sum is printed to the screen}
               end;

          if sel = 2 then {Subtraction}
               begin
                    writeln('Subtraction');
                    write('First Number = ');
                    readln(num_1); {user enters first number}

                    write('Second Number = ');
                    readln(num_2); {user enters second number}

                    writeln; {white space}

                    total := num_1 - num_2;
                    writeln('The difference of the two numbers is = ' , total:0:2); {the difference is printed to the screen}
               end;

          if sel = 3 then {Multiplication}
               begin
                    writeln('Multiplication');
                    write('First Number = ');
                    readln(num_1); {user enters first number}

                    write('Second Number = ');
                    readln(num_2); {user enters second number}

                    writeln; {white space}

                    total := num_1 * num_2;
                    writeln('The product of the two numbers is = ' , total:0:2); {the product is printed to the screen}
               end;

          if sel = 4 then {division}
               begin
                    writeln('division');
                    write('First Number = ');
                    readln(num_1); {user enters first number}

                    write('Second Number = ');
                    readln(num_2); {user enters second number}

                    writeln; {white space}

                    total := num_1 / num_2;
                    writeln('The quotient of the two numbers is = ' , total:0:2); {the quotient is printed to the screen}
               end;

          writeln; {white space}


          {start of: calculation loop} {allows the user to do more than 1 calculation at a time}

          writeln('Do you want to continue your calculations?');
          writeln('Enter Y/y for yes, and N/n for no');
          readln(choice);

          writeln; {white space}

          if (choice = 'y') then
               continue := true;
          if (choice = 'n') then
               continue := false;
          
          if (continue) then
               writeln('Please continue with your calculations.')
          else
               i := false;


          writeln; {white space}

          {end of: calculation loop}

     end;

writeln('Thanks for using my calculator.');

readln;
end.
          
Comments: