Advertisement
daniv1

Task1

Apr 22nd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.56 KB | None | 0 0
  1. program Project7;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8.   System.SysUtils ,
  9.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  10.   cthreads,
  11.   {$ENDIF}{$ENDIF}
  12.   Classes
  13.   { you can add units after this };
  14.  
  15. Type TWeight = class (TObject)
  16.  
  17.     private
  18.       kilograms, grams , Tonnes   : integer;
  19.       public
  20.      Constructor Create( t : integer ;  k : integer   ;  g : integer);
  21.       Procedure SetKilograms(k : integer);
  22.       procedure setGrams(g : integer);
  23.  
  24.       procedure setTonnes(t :integer);
  25.      function getKilograms : integer;
  26.      function getGrams : integer;
  27.      function getTonnes : integer;
  28.       procedure subtractGrams( g: integer);
  29.     procedure addGrams(g:integer);
  30.      procedure add(w1 : TWeight);
  31.      procedure subtract(w1: Tweight);
  32.    procedure displayStats ;
  33.  
  34.   end;
  35.    Constructor TWeight.Create(t : integer ; k : integer ;  g : integer);
  36.    begin
  37.      grams := g mod 1000 ;
  38.             kilograms := k mod 1000 + g div 1000 ;
  39.  
  40.             tonnes := t + k div 1000  + kilograms div 1000;
  41.    end;
  42.          Procedure TWeight.SetKilograms(k: integer);
  43.          begin
  44.               kilograms :=  k mod 1000;
  45.               tonnes := tonnes + k div 1000;
  46.          end;
  47.   procedure TWeight.setGrams(g: integer);
  48.   begin
  49.     grams:=g mod 1000;
  50.      kilograms :=  kilograms + g mod 1000;
  51.               tonnes := tonnes + kilograms  div 1000;
  52.   end;
  53.  
  54.     Procedure TWeight.setTonnes(t: integer);
  55.     begin
  56.      tonnes:= t;
  57.     end;
  58.  
  59.  
  60.    function TWeight.getKilograms;
  61.    begin
  62.       getKilograms := kilograms;
  63.    end;
  64.  
  65.     function TWeight.getGrams;
  66.    begin
  67.       getGrams := grams;
  68.    end;
  69.  
  70.     function TWeight.getTonnes;
  71.    begin
  72.       getTonnes := Tonnes;
  73.    end;
  74.  
  75.   procedure TWeight.add(w1 : tWeight);
  76.   begin
  77.    kilograms := (kilograms + w1.kilograms + (grams + w1.grams) div 1000) mod 1000 ;
  78.         grams := (grams + w1.grams) mod 1000 ;
  79.  
  80.  
  81.            tonnes := tonnes + w1.tonnes + (kilograms + w1.kilograms + (grams + w1.grams) div 1000) div 1000;
  82.   end;
  83.     procedure TWeight.subtract(w1 : tWeight);
  84.     var a , b , c : Longint;
  85.   begin
  86.   a := kilograms * 1000 + grams + tonnes* 1000000;
  87.   b :=               w1.kilograms * 1000 + w1.grams + w1.tonnes* 1000000;
  88.   c:= a - b;
  89.        kilograms :=  ( c div 1000) mod 1000;
  90.           grams := c mod 1000;
  91.            tonnes := c div 1000000;
  92.   end;
  93.  
  94.   procedure TWeight.subtractGrams(g: integer);
  95.    var a , c : Longint;
  96.   begin
  97.   a := kilograms * 1000 + grams + tonnes* 1000000;
  98.   c:= a - g;
  99.        kilograms :=  ( c div 1000) mod 1000;
  100.           grams := c mod 1000;
  101.            tonnes := c div 1000000;
  102.   end;
  103.  
  104.   procedure TWeight.addGrams(g: integer);
  105.     var a , c : Longint;
  106.   begin
  107.   a := kilograms * 1000 + grams + tonnes* 1000000;
  108.   c:= a + g;
  109.        kilograms :=  ( c div 1000) mod 1000;
  110.           grams := c mod 1000;
  111.            tonnes := c div 1000000;
  112.   end;
  113.  
  114.   procedure TWeight.displayStats;
  115.   begin
  116.    writeln(' grams = ',getgrams);
  117.    writeln(' kilograms = ',getkilograms);
  118.    writeln(' tonnes = ',gettonnes);
  119.   end;
  120.  
  121. Var
  122.  
  123.  ob1 : TWeight;
  124.  ob2 : TWeight;
  125.  ob3 : TWeight;
  126. begin
  127.  
  128.     ob1 := TWeight.Create(10 , 140  , 10);
  129.      ob2 := TWeight.Create(1 , 100  , 5);
  130.       ob3 := TWeight.Create(0 , 1  , 0);
  131.  
  132.       ob1.subtract(ob2);
  133.       ob1.displayStats;
  134.       ob1.SetKilograms(10000);
  135.       ob1.displayStats;
  136.       ob1.setTonnes(2);
  137.       ob1.displayStats;
  138.       ob1.addGrams(100);
  139.       ob1.displayStats;
  140.  
  141.    //   ob1.SetKilograms(10003);
  142.  
  143.     ///    writeln(ob1.getKilograms);
  144.     //  writeln(ob1.getTonnes);
  145. Readln;
  146. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement