Advertisement
Guest User

Untitled

a guest
May 20th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. static void GraphInput()
  2. {
  3. //kx+m
  4. Console.SetCursorPosition(0, 0);
  5. Console.ForegroundColor = ConsoleColor.Gray;
  6. string stringK = null;
  7. string stringM = null;
  8. Console.WriteLine("Förstagradsgrafritaren v1\n");
  9. Console.WriteLine("Mata in din graf (kx+m)");
  10. Console.Write("\ny = ");
  11. string graph = Console.ReadLine().ToLower();
  12.  
  13.  
  14. foreach (char c in graph)
  15. {
  16. if (c == 'x')
  17. {
  18.  
  19. break;
  20. }
  21.  
  22. stringK += c;
  23. }
  24. string check = null;
  25. foreach (char c in graph)
  26. {
  27.  
  28. check += c;
  29. if (check.Contains('+') || check.Contains('-') && check.Contains(stringK) && check.Contains('x'))
  30. {
  31. stringM += c;
  32. }
  33. }
  34.  
  35. //-1x+3
  36. string check2 = null;
  37. string stringM2 = null;
  38. if (stringM.Contains('x'))
  39. {
  40. foreach (char c in stringM)
  41. {
  42. check2 += c;
  43. if (check2.Contains('+') || check2.Contains('-'))
  44. {
  45. stringM2 += c;
  46. }
  47. }
  48. }
  49.  
  50. int k = Int32.Parse(stringK);
  51. int m;
  52. if (stringM2 == null)
  53. {
  54. m = Int32.Parse(stringM);
  55. }
  56.  
  57. else
  58. {
  59. m = Int32.Parse(stringM2);
  60. }
  61. CreateGraph(k, m);
  62.  
  63. GraphMenu();
  64. }
  65.  
  66. static void CreateGraph(int k, int m)
  67. {
  68. m = Dot.y0 - m;
  69.  
  70. Dot skärY = new Dot(Dot.x0, m);
  71. Dot.graph[0] = skärY;
  72.  
  73. int indexCounter = 1;
  74. //Alla punkter som har x > 0
  75. for (int i = 1; i < 50; i++)
  76. {
  77. Dot.graph[indexCounter] = new Dot(Dot.x0 + i, m - (k * i));
  78. indexCounter++;
  79.  
  80. }
  81. //Alla punkter som har x < 0
  82. for (int i = 1; i < 51;i++)
  83. {
  84. Dot.graph[indexCounter] = new Dot(Dot.x0 - i, m + (k * i));
  85. indexCounter++;
  86. }
  87.  
  88. GraphOutput();
  89. }
  90.  
  91. static void GraphOutput()
  92. {
  93.  
  94. for (int i = 0; i < Dot.graph.Length; i++)
  95. {
  96. if (Dot.graph[i] != null && Dot.graph[i].posY > 0 && Dot.graph[i].posY < Console.WindowHeight && Dot.graph[i].posX > 0 && Dot.graph[i].posX < Console.WindowWidth)
  97. {
  98. Dot.graph[i].PrintDot();
  99. }
  100.  
  101. }
  102.  
  103.  
  104. }
  105.  
  106. static void GraphMenu()
  107. {
  108. Console.ForegroundColor = ConsoleColor.Gray;
  109. PrintStringAt("1: Nytt koordinatsystem", 0, 6);
  110. PrintStringAt("2: Lägg till en graf i samma koordinatsystem", 0, 7);
  111. PrintStringAt("Val: ", 0, 9);
  112. string val = Console.ReadLine();
  113. switch (val)
  114. {
  115. case "1":
  116. Restart();
  117. break;
  118. case "2":
  119. ConsoleClearArea(0, 0, 25, 5);
  120. ConsoleClearArea(0, 6, 43, 9);
  121. Console.SetCursorPosition(0, 0);
  122. GraphInput();
  123. break;
  124. }
  125. }
  126.  
  127. //Vilket x den ska börja ifrån, vilket x den ska sluta på och vilken y den ska börja på och vilken y den ska sluta på.
  128. static void ConsoleClearArea(int xStart, int yStart, int xEnd, int yEnd)
  129. {
  130. for (int i = yStart; i <= yEnd; i++)
  131. {
  132.  
  133. for (int j = xStart; j <= xEnd; j++)
  134. {
  135. Console.SetCursorPosition(j, i);
  136. Console.Write(" ");
  137. }
  138. }
  139.  
  140. }
  141.  
  142. static void Restart()
  143. {
  144. Console.Clear();
  145. CoordinateSystem();
  146. GraphInput();
  147. }
  148.  
  149. static void PrintStringAt(string text, int x, int y)
  150. {
  151. Console.SetCursorPosition(x, y);
  152. Console.Write(text);
  153. }
  154.  
  155. static void CoordinateSystem()
  156. {
  157. //X-axeln
  158. for (int i = 0; i < Console.WindowWidth; i++)
  159. {
  160. Console.SetCursorPosition(i, Dot.y0);
  161. Console.Write("■");
  162. }
  163. //Y-axeln
  164. for (int i = 0; i < Console.WindowHeight; i++)
  165. {
  166. Console.SetCursorPosition(Dot.x0, i);
  167. Console.Write("■");
  168.  
  169. }
  170. }
  171.  
  172. }
  173.  
  174. class Dot
  175. {
  176. public static int x0 = Console.WindowWidth / 2;
  177. public static int y0 = Console.WindowHeight / 2;
  178. public static Dot[] graph = new Dot[101];
  179. public int posX;
  180. public int posY;
  181.  
  182. public Dot(int posX, int posY)
  183. {
  184. this.posX = posX;
  185. this.posY = posY;
  186. }
  187.  
  188. public void PrintInfo()
  189. {
  190. Console.WriteLine("posX: {0}, posY: {1}", posX, posY);
  191. }
  192.  
  193. public void PrintDot()
  194. {
  195. int randomInt = 0;
  196. Random rnd = new Random();
  197. ConsoleColor[] Colors = { ConsoleColor.Red, ConsoleColor.Yellow,ConsoleColor.Magenta,ConsoleColor.Green,ConsoleColor.DarkRed,ConsoleColor.DarkBlue,ConsoleColor.DarkGreen };
  198. randomInt = rnd.Next(0, 6);
  199. Console.ForegroundColor = Colors[randomInt];
  200. Console.SetCursorPosition(posX, posY);
  201. Console.Write("■");
  202. }
  203. }
  204.  
  205. class Globals
  206. {
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement