Advertisement
Guest User

Untitled

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