fork download
  1. {$APPTYPE CONSOLE} //Ja korzystam z Delphi, gdzie aby mieć aplikację konsolową trzeba dodać taką dyrektywę
  2. Program Kalkulator; //Nazwa programu
  3. Uses SysUtils; //Funkcja TryStrToInt użyta niżej
  4.  
  5. Function GetInput(Title: String): Integer; //Funkcja pobiera od użytkownika wpisaną liczbę
  6. Var Temp: String;
  7. Out : Integer;
  8. Begin
  9. Write(Title+' ');
  10. Readln(Temp);
  11. if (not TryStrToInt(Temp, Out)) Then //Jeżeli tego, co wpisał nie da się zamienić na liczbę (wpisał np. 123foo123), to wyświetlamy błąd
  12. Begin
  13. Writeln('', Title, ''' nie jest liczba!');
  14. Readln;
  15. Halt; //i wyłączamy program
  16. End;
  17. GetInput := Out;
  18. End;
  19.  
  20. (* Tych funkcji niżej chyba nie trzeba komentować *)
  21. Procedure Dodawanie(A, B: Integer); pascal;
  22. Begin
  23. Writeln;
  24. Writeln(A,'+',B,'=',A+B);
  25. End;
  26.  
  27. Procedure Odejmowanie(A, B: Integer); pascal;
  28. Begin
  29. Writeln;
  30. Writeln(A,'-',B,'=',A-B);
  31. End;
  32.  
  33. Procedure Dzielenie(A, B: Integer); pascal;
  34. Begin
  35. Writeln;
  36. Writeln(A,'/',B,'=',A/B:6:4,' (reszta ',A mod B,')');
  37. End;
  38.  
  39. Procedure Mnozenie(A, B: Integer); pascal;
  40. Begin
  41. Writeln;
  42. Writeln(A,'*',B,'=',A*B);
  43. End;
  44.  
  45. Procedure Pierwiastek(A: Integer); pascal;
  46. Begin
  47. Writeln;
  48. Writeln('sqrt(',A,')=',sqrt(A):6:4); //sqrt = square root = pierwiastek
  49. End;
  50.  
  51. Procedure Potega(A, B: Integer); pascal;
  52. Function Power(iBase, iExponent: Integer): Extended;
  53. Begin
  54. Power := Exp(Ln(iBase) * iExponent);
  55. End;
  56. Begin
  57. Writeln;
  58. Writeln(A,'^',B,'=',round(Power(A, B)));
  59. End;
  60.  
  61. Procedure Silnia(A: Integer); pascal;
  62. Var Res, I: Integer;
  63. Begin
  64. Res := 1;
  65. For I := 1 To A Do //Prosty algorytm iteracyjny na silnię
  66. Res := Res*I;
  67. Writeln;
  68. Writeln(A,'!=',Res);
  69. End;
  70.  
  71. Procedure MainMenu;
  72. Const Opcje: Array[0..7] of String = ('dodawanie', 'odejmowanie', 'dzielenie', 'mnozenie', 'pierwiastek', 'potega', 'silnia', 'wyjscie'); //Nasza lista opcji (to jest wygodniejsze, zamiast korzystania z drabinki if'ow typu: if opcja='foo' then foo else if opcja='cos_innego' then cos_innego else (itd)
  73. Var Opcja: String; //wprowadzona przez użytkownika opcja
  74. I, Op: Integer; //"I" jest używane do iteracji po w/w tablicy, Op to numer naszej opcji (czyli dla 'dodawanie' jest to 0, dla 'odejmowanie' jest to 1, itd.)
  75. Begin
  76. Op := -1;
  77. Repeat
  78. Writeln;
  79. Write('Podaj nazwe dzialania do wykonania: ');
  80. Readln(Opcja);
  81. Opcja := LowerCase(Opcja);
  82. For I := Low(Opcje) To High(Opcje) Do //Iterujemy po całej tablicy
  83. if (Opcja = Opcje[I]) Then //Jeżeli aktualnie sprawdzany indeks z tablicy jest równy temu, co wprowadził użytkownik
  84. Op := I; //to zapisujemy numer indeksu do zmiennej
  85. Until (Op >= 0); //Jeżeli opcja została odnaleziona
  86. Case Op Of //To jest coś a'la if
  87. 0: Dodawanie(GetInput('Podaj skladnik:'), GetInput('Podaj skladnik:'));
  88. 1: Odejmowanie(GetInput('Podaj odjemna:'), GetInput('Podaj odjemnik:'));
  89. 2: Dzielenie(GetInput('Podaj dzielna:'), GetInput('Podaj dzielnik:'));
  90. 3: Mnozenie(GetInput('Podaj czynnik:'), GetInput('Podaj czynnik:'));
  91. 4: Pierwiastek(GetInput('Podaj liczbe:'));
  92. 5: Potega(GetInput('Podaj podstawe:'), GetInput('Podaj wykladnik:'));
  93. 6: Silnia(GetInput('Podaj liczbe:'));
  94. 7: Halt;
  95. End;
  96. MainMenu; //I od nowa
  97. End;
  98. Begin
  99. Writeln('--------------');
  100. Writeln('- Kalkulator -');
  101. Writeln('--------------');
  102. Writeln;
  103. Writeln('Dostepne funkcje:');
  104. Writeln;
  105. Writeln('dodawanie, odejmowanie, dzielenie, mnozenie, pierwiastek, potega, silnia,wyjscie');
  106. Writeln;
  107. MainMenu;
  108. End.
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Free Pascal Compiler version 2.2.0 [2009/11/16] for i386
Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling prog.pas
prog.pas(1,2) Warning: APPTYPE is not supported by the target OS
prog.pas(11,31) Error: Call by var for arg no. 2 has to match exactly: Got "SmallInt" expected "LongInt"
prog.pas(17,17) Warning: Local variable "Out" does not seem to be initialized
prog.pas(108,4) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)
stdout
Standard output is empty