Created
January 3, 2017 01:59
-
-
Save WildGenie/097f062fc9f2a5325be0c1f450a2a713 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Prefix | |
{ | |
static class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
Console.WriteLine("İşlemi giriniz:"); | |
string ifade = Console.ReadLine(); | |
Console.WriteLine("Sonuc: " + HesaplaOnEkliIfade(ifade)); | |
Console.ReadLine(); | |
} | |
private static double HesaplaOnEkliIfade(string onEkliIfade) | |
{ | |
string[] degerler = onEkliIfade.Split(' '); | |
Stack<int> islemYigini = new Stack<int>(); | |
for (int i = degerler.Length - 1; i >= 0; i--) | |
{ | |
if (OperatorKontrol(degerler[i])) | |
{ | |
int islem1 = islemYigini.Pop(); | |
int islem2 = islemYigini.Pop(); | |
switch (degerler[i]) | |
{ | |
case "+": | |
islemYigini.Push(islem1 + islem2); | |
break; | |
case "-": | |
islemYigini.Push(islem1 - islem2); | |
break; | |
case "*": | |
islemYigini.Push(islem1 * islem2); | |
break; | |
case "/": | |
islemYigini.Push(islem1 / islem2); | |
break; | |
} | |
} | |
else | |
{ | |
islemYigini.Push(int.Parse(degerler[i])); | |
} | |
} | |
return islemYigini.Pop(); | |
} | |
private static bool OperatorKontrol(string op) | |
{ | |
return "+" == op || "-" == op || "*" == op || "/" == op; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment