Advertisement
Guest User

Untitled

a guest
Jun 11th, 2017
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 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 Arrays_MoreEx
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string[] name = Console.ReadLine().Split(' '); // same
  14. long[] quantity = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
  15. decimal[] price = Console.ReadLine().Split(' ').Select(decimal.Parse).ToArray(); // same
  16.  
  17. while (true)
  18. {
  19. string[] inputName = Console.ReadLine().Split();
  20. //От инпута ти идва "Bread 10" няма как да го парснеш към лонг
  21.  
  22. if (inputName[0] == "done") // край на цикъла
  23. {
  24. break;
  25. }
  26. //вземаш 2рия елемент от масива и го парсваш
  27. long inputQuantity = long.Parse(inputName[1]);
  28.  
  29.  
  30.  
  31. int index = Array.IndexOf(name, inputName[0]); //индекс на търсения елемент
  32. int quantityL = quantity.Length;
  33.  
  34. if (index >= quantityL) // ако няма индекса на дадения елемент в quantity,го приравняваме на 0
  35. {
  36. quantity = new long[quantityL + index];
  37. for (int i = 0; i < quantityL; i++)
  38. {
  39. quantity[quantityL - 1 + i] = 0;
  40. }
  41. }
  42.  
  43. // изчисляваме количеството
  44. if (quantity[index] >= inputQuantity)
  45. {
  46. decimal totalPrice = (decimal)price[index] * inputQuantity;
  47. quantity[index] -= inputQuantity;
  48. Console.WriteLine($"{inputQuantity} x {inputName} costs {totalPrice:F2}");
  49. }
  50. else
  51. {
  52. Console.WriteLine($"We do not have enough {inputName[0]}");
  53. }
  54.  
  55.  
  56. }
  57.  
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement