Advertisement
bartigames

Wykład - 03.03.2018

Mar 3rd, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Siszarp
  8. {
  9.     class kalkulator
  10.     {
  11.         private int a1, a2;
  12.         private char dzialanie;
  13.      
  14.         public kalkulator() { }
  15.  
  16.  
  17.         public void getValues(int ar1, int ar2, char dzi){
  18.  
  19.  
  20.             a1 = ar1;
  21.             a2 = ar2;
  22.             dzialanie = dzi;
  23.  
  24.  
  25.         }
  26.        
  27.        
  28.        
  29.  
  30.  
  31.  
  32.  
  33.         public int oblicz(){
  34.  
  35.             int wynik = 0;
  36.             switch (dzialanie)
  37.             {
  38.  
  39.                 case '+': wynik = a1 + a2;
  40.                 break;                    
  41.                 case '-': wynik = a1 - a2;
  42.                 break;
  43.                 case '*': wynik = a1 * a2;
  44.                 break;
  45.                 case '/': wynik = a1 / a2;
  46.                 break;
  47.  
  48.             }
  49.             return wynik;
  50.  
  51.     }
  52.  
  53.  
  54.     }
  55.  
  56.  
  57.     class Program
  58.     {
  59.         static void Main(string[] args)
  60.  
  61.         {
  62.             int a, b;
  63.             char dz;
  64.             string s = " ";
  65.             string[] elementy = new string [3];
  66.             kalkulator k = new kalkulator();
  67.  
  68.             while (s != "x")
  69.             {
  70.  
  71.  
  72.                 Console.WriteLine("Podaj co chcesz policzyć:");
  73.                 s = Console.ReadLine();
  74.                 elementy = s.Split(' ');
  75.                 try
  76.                 {
  77.                     a = Int32.Parse(elementy[0]);
  78.                     dz = char.Parse(elementy[1]);
  79.                     b = Int32.Parse(elementy[2]);
  80.                 }
  81.                 catch
  82.                 {
  83.                     Console.WriteLine("Podane dane niewłaściwe!");
  84.                     Console.ReadLine();
  85.                     continue;
  86.                 }
  87.          
  88.                 Console.WriteLine("{0},{1},{2}", a, b, dz);
  89.                 k.getValues(a, b, dz);
  90.                 Console.WriteLine("Wynik: {0}", k.oblicz());
  91.  
  92.  
  93.  
  94.  
  95.                 // koniec
  96.                 Console.WriteLine("Wpisz x aby wyjść lub enter by kontynuować...");
  97.                 s = Console.ReadLine();
  98.             }
  99.  
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement