Advertisement
Guest User

Simple Expression

a guest
Oct 3rd, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. class UserLogs
  8. {
  9. static void Main()
  10. {
  11. string input = Console.ReadLine();
  12. List<string> nums = input.Split(new string[] { " ", "-", "+", "*", "/" }, StringSplitOptions.RemoveEmptyEntries).Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
  13. input = Regex.Replace(input, " ", "");
  14.  
  15. string pattern = @"[0-9.]+";
  16. string[] math = Regex.Split(input, pattern);
  17.  
  18. decimal count = decimal.Parse(nums[0]);
  19.  
  20. for (int i = 0; i < math.Length; i++)
  21. {
  22. if (math[i] == "+")
  23. {
  24. count += decimal.Parse(nums[i]);
  25. }
  26. else if (math[i] == "-")
  27. {
  28. count -= decimal.Parse(nums[i]);
  29. }
  30. else if (math[i] == "*")
  31. {
  32. count *= decimal.Parse(nums[i]);
  33. }
  34. else if (math[i] == "/")
  35. {
  36. count /= decimal.Parse(nums[i]);
  37. }
  38. }
  39. Console.WriteLine("{0:F9}", count);
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement