Guest User

Untitled

a guest
Jun 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 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 Typing_Speed
  8. {
  9. class Stopwatch
  10. {
  11. private DateTime startTime;
  12. private DateTime stopTime;
  13. public double time;
  14.  
  15. public void Start()
  16. {
  17. startTime = DateTime.Now;
  18. }
  19.  
  20. public void Stop()
  21. {
  22. stopTime = DateTime.Now;
  23. TimeSpan TotalTime = stopTime - startTime;
  24. time = TotalTime.TotalSeconds;
  25. }
  26.  
  27. public double Display()
  28. {
  29. return Math.Round(time, 2);
  30. }
  31. }
  32.  
  33. class Program
  34. {
  35. static void Main(string[] args)
  36. {
  37. Stopwatch stopwatch = new Stopwatch();
  38. Random random = new Random();
  39.  
  40. string[] sentences = new string[] {"The quick brown fox jumps over the lazy dog."};
  41.  
  42. bool exit = false;
  43. while (exit == false)
  44. {
  45. Console.WriteLine("Write the following sentence:");
  46. Console.WriteLine("----------------------------------\n");
  47. string sentence = sentences[random.Next(0, sentences.Count())];
  48. Console.WriteLine(sentence);
  49.  
  50. Console.WriteLine("Enter to start!");
  51. Console.ReadLine();
  52. stopwatch.Start();
  53. string input = Console.ReadLine();
  54. stopwatch.Stop();
  55.  
  56. if (string.IsNullOrWhiteSpace(input))
  57. {
  58. exit = true;
  59. break;
  60. }
  61. else
  62. {
  63. string[] words = input.Trim().Split(' ');
  64. string[] sentenceRandom = sentence.Split(' ');
  65. int mistakes = 0;
  66.  
  67. if (sentenceRandom.Count() > words.Count())
  68. {
  69. for (int i = 0; i < sentenceRandom.Count(); i++)
  70. {
  71. try
  72. {
  73. if (!(sentenceRandom[i].Equals(words[i])))
  74. {
  75. mistakes++;
  76. }
  77. }
  78. catch (IndexOutOfRangeException)
  79. {
  80. mistakes++;
  81. }
  82. }
  83. }
  84. else
  85. {
  86. for (int i = 0; i < words.Count(); i++)
  87. {
  88. try
  89. {
  90. if (!(sentenceRandom[i].Equals(words[i])))
  91. {
  92. mistakes++;
  93. }
  94. }
  95. catch (IndexOutOfRangeException)
  96. {
  97. mistakes++;
  98. }
  99. }
  100. }
  101.  
  102. double speed = sentenceRandom.Count() / stopwatch.time;
  103.  
  104. int characters = 0;
  105. foreach (char character in input)
  106. {
  107. characters++;
  108. }
  109.  
  110. Console.WriteLine("\n---------------------------");
  111. Console.WriteLine("Mistakes made: {0}", mistakes);
  112. Console.WriteLine("Words typed: {0}", words.Count());
  113. Console.WriteLine("Characters typed: {0}", characters);
  114. Console.WriteLine("Words per second: {0}", Math.Round(speed, 1));
  115. Console.WriteLine("Characters per second: {0}", Math.Round(characters / stopwatch.time, 1));
  116. Console.WriteLine("---------------------------\n\n");
  117. }
  118. }
  119. }
  120. }
  121. }
Add Comment
Please, Sign In to add comment