Advertisement
Guest User

so far

a guest
Dec 1st, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.92 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 Assignment_week_1_1
  8. {
  9. class Program
  10. {
  11. private static void RunExerciseOne()
  12. {
  13. Console.WriteLine("You successfully ran exercise one!");
  14. string firstName = "Jonte";
  15. string lastName = "Adolfsson";
  16. Console.WriteLine("Hello " + firstName + " " + lastName + "! I'm glad to inform you that you are the test subject of my very first assignment!");
  17. }
  18. // Dumförklarad " " = Mellanslag, satt och kliade på den ett tag.
  19. private static void RunExerciseTwo()
  20. {
  21. Console.WriteLine("You successfully ran exercise two!");
  22. DateTime today = DateTime.Now;
  23. DateTime tomorrow = today.AddDays(1);
  24. DateTime yesterday = today.AddDays(-1);
  25. Console.WriteLine("Todays date is " + today);
  26. Console.WriteLine("Tomorrows date is " + tomorrow);
  27. Console.WriteLine("Yesterdays date was " + yesterday);
  28. }
  29. // No comment.
  30. private static void RunExerciseThree()
  31. {
  32. Console.WriteLine("You successfully ran exercise three!");
  33. Console.ForegroundColor = ConsoleColor.White;
  34. Console.Write("Enter you first name: ");
  35. string firstname = Console.ReadLine();
  36. Console.Write("Enter you last name: ");
  37. string lastname = Console.ReadLine();
  38. Console.Clear();
  39. Console.ForegroundColor = ConsoleColor.Green;
  40. Console.WriteLine("Alright, I will call you " + firstname + " " + lastname + " from now on!");
  41. }
  42. // So far so good.
  43. private static void RunExerciseFour()
  44. {
  45. Console.WriteLine("You successfully ran exercise four!");
  46. String str = "The quick fox jumped Over the DOG";
  47. Console.ForegroundColor = ConsoleColor.Red;
  48. Console.WriteLine(str);
  49. Console.ResetColor();
  50. Console.WriteLine("Hit any key to fix the issues!");
  51. Console.ReadKey();
  52. Console.Clear();
  53. {
  54. if (str.Contains("DOG")) // Replace
  55. str = str.Replace("DOG", "LAZY DOG");
  56. if (str.Contains("quick")) // Remove
  57. str = str.Remove(4, 6);
  58. if (str.Contains("Over")) // Substring Tolower
  59. str = str.Substring(0, 1) + str.Substring(1).ToLower();
  60. if (str.Contains("fox")) // Insert
  61. str = str.Insert(4, "brown ");
  62. }
  63. Console.ForegroundColor = ConsoleColor.Green;
  64. Console.WriteLine(str);
  65. }
  66. // Försökte använda lite olika metoder utan att behöva göra en ny string
  67. private static void RunExerciseFive()
  68.  
  69. {
  70. Console.WriteLine("You successfully ran exercise five!");
  71. Console.ResetColor();
  72. String str = "Arrays are very common in programming, they look something like: [1,2,3,4,5]";
  73. Console.ReadLine();
  74. string substr = str.Substring(65, 11);
  75. if (substr.Contains("1"))
  76. substr = substr.Replace("[1,2,3,4,5]", "[1,4,5,6,7,8,9,10]");
  77. Console.WriteLine(substr);
  78. }
  79. // va lite lat, visdomstand gör mig irriterad x(
  80.  
  81. private static void RunExerciseSix()
  82. {
  83. Console.WriteLine("You successfully ran exercise six!");
  84. Console.ResetColor();
  85. var num1 = 1;
  86. var num2 = 0;
  87. Console.Write("Enter your first number: ");
  88. num1 = Convert.ToInt32(Console.ReadLine());
  89. Console.Write("Enter you second number: ");
  90. num2 = Convert.ToInt32(Console.ReadLine());
  91. Console.Clear();
  92. string biggest = ("The biggest number is: ");
  93. Console.WriteLine(biggest + Math.Max(num1, num2));
  94. string smallest = ("The smallest number is: ");
  95. Console.WriteLine(smallest + Math.Min(num1, num2));
  96. string difference = ("The difference between the two numbers are: ");
  97. Console.WriteLine(difference + Math.Abs(num1 - num2));
  98. string sum = ("The sum of the two numbers are: ");
  99. Console.WriteLine(sum + Math.Abs(num1 + num2));
  100. string product = ("The product between the two numbers are: ");
  101. Console.WriteLine(product + Math.Abs(num1 * num2));
  102. string ratio = ("The ratio between the two numbers are: ");
  103. Console.WriteLine(ratio + Math.Abs(num1 / num2));
  104. }
  105. // tandvärk all day erry day wooh
  106.  
  107. private static void RunExerciseSeven()
  108. {
  109. Console.WriteLine("You successfully ran exercise seven!");
  110. Console.ResetColor();
  111. {
  112. double c, cir;
  113. double PI = 3.14;
  114. double s, spha, sphv;
  115.  
  116. Console.WriteLine("Enter the radius of the circle : ");
  117. c = Convert.ToDouble(Console.ReadLine());
  118. cir = PI * c * c;
  119. Console.WriteLine("Enter the Radius of the Sphere: ");
  120. s = Convert.ToDouble(Console.ReadLine());
  121. spha = 4 * PI * s * s;
  122. sphv = (4.0 / 3) * PI * s * s * s;
  123. Console.WriteLine("The area of the circle is : {0}", cir);
  124. Console.WriteLine("The area of the sphere is : {0} ", spha);
  125. Console.WriteLine("The volume of the sphere is : {0}", sphv);
  126.  
  127. }
  128. }
  129. // matte ;c
  130.  
  131. private static void RunExerciseEight()
  132. {
  133. Console.WriteLine("You successfully ran exercise eight!");
  134. Console.ResetColor();
  135.  
  136. {
  137. double num, sqrt = 0;
  138. int currentDecimal = 0, dA = 5, iA = 5;
  139.  
  140. do
  141. {
  142. Console.Write("Enter a decimal value: ");
  143. } while (!double.TryParse(Console.ReadLine(), out num));
  144.  
  145. while (0 < iA)
  146. {
  147. double currentIncrement = Math.Pow(10, iA);
  148.  
  149. while (Math.Pow(sqrt + currentIncrement, 2) <= num)
  150. {
  151. sqrt += currentIncrement;
  152. }
  153.  
  154. iA--;
  155. }
  156.  
  157. while (currentDecimal <= dA)
  158. {
  159. double currentIncrement = (double)1 / Math.Pow(10, currentDecimal);
  160.  
  161. while (Math.Pow(sqrt + currentIncrement, 2) <= num)
  162. {
  163. sqrt += currentIncrement;
  164. }
  165.  
  166. currentDecimal++;
  167. }
  168.  
  169. Console.WriteLine(("Square root: ") + sqrt);
  170. Console.WriteLine(("Raised to the power of 2: ") + Math.Pow(num, 2));
  171. Console.WriteLine(("Raised to the power of 10: ") + Math.Pow(num, 10));
  172. }
  173. }
  174. // Det funkade till slut tror jag
  175.  
  176. private static void RunExerciseNine()
  177. {
  178.  
  179.  
  180. DateTime today = DateTime.Today;
  181. Console.WriteLine("You successfully ran exercise nine!");
  182. Console.ResetColor();
  183. Console.WriteLine("Please enter your name: ");
  184. string name = (Console.ReadLine());
  185. while (true)
  186.  
  187. {
  188. Console.WriteLine("Hello " + name + ", please enter your date of birth (MM/DD/YYYY): ");
  189. DateTime birthDate;
  190.  
  191. if (DateTime.TryParse(Console.ReadLine(), out birthDate))
  192. {
  193. TimeSpan age = DateTime.Now - birthDate;
  194. var a = (int)(age.Days / 365.25);
  195. if (a < 18)
  196. {
  197. Console.Clear();
  198. Console.WriteLine("Would you like to order a coke? (y/n):");
  199. string coke = Console.ReadLine();
  200. if (coke.Equals("y", StringComparison.OrdinalIgnoreCase) || coke.Equals("yes", StringComparison.OrdinalIgnoreCase))
  201. {
  202. Console.ForegroundColor = ConsoleColor.Green;
  203. Console.WriteLine("A coke has been served!");
  204. Console.ResetColor();
  205. break;
  206. }
  207. if (coke.Equals("n", StringComparison.OrdinalIgnoreCase) || coke.Equals("no", StringComparison.OrdinalIgnoreCase))
  208. {
  209. Console.ForegroundColor = ConsoleColor.Red;
  210. Console.WriteLine("No order options are available!");
  211. Console.ResetColor();
  212. break;
  213. }
  214. }
  215. else
  216. Console.Clear();
  217. Console.WriteLine("Would you like to order a beer? (y/n):");
  218. string beer = Console.ReadLine();
  219.  
  220. if (beer.Equals("y", StringComparison.OrdinalIgnoreCase) || beer.Equals("yes", StringComparison.OrdinalIgnoreCase))
  221. {
  222. Console.ForegroundColor = ConsoleColor.Green;
  223. Console.WriteLine("A beer has been served!");
  224. Console.ResetColor();
  225. break;
  226. }
  227. if (beer.Equals("n", StringComparison.OrdinalIgnoreCase) || beer.Equals("no", StringComparison.OrdinalIgnoreCase))
  228. {
  229. Console.WriteLine("Would you like to order a coke instead?(y/n):");
  230. string coke = Console.ReadLine();
  231. if (coke.Equals("y", StringComparison.OrdinalIgnoreCase) || coke.Equals("yes", StringComparison.OrdinalIgnoreCase))
  232. {
  233. Console.ForegroundColor = ConsoleColor.Green;
  234. Console.WriteLine("A coke has been served!");
  235. Console.ResetColor();
  236. break;
  237. }
  238.  
  239. if (coke.Equals("n", StringComparison.OrdinalIgnoreCase) || coke.Equals("no", StringComparison.OrdinalIgnoreCase))
  240. {
  241. Console.ForegroundColor = ConsoleColor.Red;
  242. Console.WriteLine("No order options are available!");
  243. Console.ResetColor();
  244. break;
  245. }
  246. }
  247.  
  248. }
  249.  
  250. else
  251. Console.ForegroundColor = ConsoleColor.Red;
  252. Console.WriteLine("You have entered an invalid birthdate, try again." + Environment.NewLine);
  253. Console.ResetColor();
  254. }
  255. }
  256. // null
  257.  
  258. private static void RunExerciseTen()
  259. {
  260.  
  261. var keepAlive = true;
  262. while (keepAlive)
  263.  
  264. {
  265. try
  266. {
  267.  
  268. Console.WriteLine("You successfully ran exercise ten!");
  269. Console.WriteLine("Input 1 for division.");
  270. Console.WriteLine("Input 2 for Exercise 4.");
  271. Console.WriteLine("Input 3 for color switch.");
  272. Console.WriteLine("Input -1 to exit");
  273. var ExerciseTen = int.Parse(Console.ReadLine() ?? "");
  274. switch (ExerciseTen)
  275. {
  276. case 1:
  277. Console.ResetColor();
  278. var num1 = 1;
  279. var num2 = 0;
  280. Console.Write("Enter your first number: ");
  281. num1 = Convert.ToInt32(Console.ReadLine());
  282. Console.Write("Enter you second number: ");
  283. num2 = Convert.ToInt32(Console.ReadLine());
  284. if (num2 == 0)
  285.  
  286. {
  287. Console.ForegroundColor = ConsoleColor.Red;
  288. Console.WriteLine("You have entered a invalid number, try again." + Environment.NewLine);
  289. Console.ResetColor();
  290. }
  291. else
  292. {
  293. Console.ForegroundColor = ConsoleColor.Green;
  294. Console.WriteLine(num1 + " divided by " + num2 + " is: " + (num1 / num2));
  295. Console.ReadKey();
  296. }
  297. break;
  298. case 2:
  299. RunExerciseFour();
  300. break;
  301. case 3:
  302.  
  303. if (Console.ForegroundColor == ConsoleColor.Green)
  304. {
  305. Console.ForegroundColor = ConsoleColor.Red;
  306. }
  307. else
  308. {
  309. Console.ForegroundColor = ConsoleColor.Green;
  310. }
  311. break;
  312. case -1:
  313. keepAlive = false;
  314. break;
  315. default:
  316. Console.ForegroundColor = ConsoleColor.Red;
  317. Console.WriteLine("That is not a valid test number!");
  318. break;
  319. }
  320. Console.WriteLine("Hit any key to continue!");
  321. Console.ReadKey();
  322. Console.Clear();
  323. }
  324. catch
  325. {
  326. Console.ForegroundColor = ConsoleColor.Red;
  327. Console.WriteLine("That is not a valid test number!");
  328. Console.ResetColor();
  329. }
  330. }
  331. }
  332.  
  333.  
  334. private static void RunExerciseEleven()
  335. {
  336. Console.WriteLine("You successfully ran exercise eleven!");
  337. Console.ResetColor();
  338. var num1 = 1;
  339.  
  340. Console.Write("Enter a number over 0:");
  341. num1 = Convert.ToInt32(Console.ReadLine());
  342. if (num1 == 0)
  343.  
  344. {
  345. Console.ForegroundColor = ConsoleColor.Red;
  346. Console.WriteLine("You have entered a invalid number, try again." + Environment.NewLine);
  347. Console.ResetColor();
  348.  
  349. }
  350.  
  351.  
  352. Console.WriteLine(num1 + 5);
  353. Console.ReadKey();
  354.  
  355. }
  356.  
  357. private static void RunExerciseTwelve()
  358. {
  359. Console.WriteLine("You successfully ran exercise twelve!");
  360. Console.ResetColor();
  361.  
  362. int start = 1;
  363. int end = 10;
  364. for (int i = start; i <= end; i++)
  365. {
  366. for (int j = start; j <= end; j++)
  367. {
  368. Console.Write("{0,6:d}", (i * j));
  369. }
  370. Console.WriteLine();
  371. }
  372. }
  373. // Hoppas det duger för uppgiften.
  374. private static void RunExerciseThirteen()
  375. {
  376. Console.WriteLine("You successfully ran exercise eleven!");
  377. Console.ResetColor();
  378. Random r = new Random();
  379.  
  380. int val = r.Next(1, 500);
  381. int guess = 0;
  382. int guessnum = 0;
  383. bool correct = false;
  384.  
  385. Console.WriteLine("I'm thinking of a number between 1-500, can you guess what number it is?");
  386.  
  387. while (!correct)
  388. {
  389. Console.Write("Guess: ");
  390. string input = Console.ReadLine();
  391.  
  392. if (!int.TryParse(input, out guess))
  393. {
  394. Console.WriteLine("That's not a number, try again.");
  395. continue;
  396. }
  397. guessnum++;
  398. if (guess < val)
  399. {
  400. Console.WriteLine("No, the number I'm thinking of is higher than that number.");
  401. }
  402. else if (guess > val)
  403. {
  404. Console.WriteLine("No, the number I'm thinking of is lower than that number.");
  405. }
  406. else
  407. {
  408. correct = true;
  409. Console.ForegroundColor = ConsoleColor.Green;
  410. Console.WriteLine("You guessed right and it only took you " + guessnum + " tries!");
  411. }
  412. }
  413. }
  414.  
  415. private static void RunExerciseFourteen()
  416. {
  417. Console.WriteLine("You successfully ran exercise fourteen!");
  418. Console.ResetColor();
  419.  
  420. }
  421.  
  422. private static void RunExerciseTwentyOne()
  423. {
  424. Console.WriteLine("You successfully ran exercise twentyone!");
  425. Console.ResetColor();
  426.  
  427. }
  428.  
  429. private static void RunTest() // använder för att lättare testa saker
  430. {
  431.  
  432.  
  433. //Console.WriteLine("Enter a number: ");
  434. //int x = int.Parse(Console.ReadLine());
  435. //double sum = 0;
  436. //for (int i = 0; i < x; i++)
  437. //{
  438. // Console.WriteLine("Ange tal {0}: ", i);
  439. // double number = double.Parse(Console.ReadLine());
  440. // sum += number;
  441. //}
  442.  
  443. //Console.WriteLine("Sum of the entered numbers is: {0}", sum);
  444. //Console.ReadLine();
  445.  
  446. //{
  447. // Console.WriteLine("Enter the radius of the circle : ");
  448. // var c = Convert.ToInt32(Console.ReadLine());
  449.  
  450.  
  451. // while (c <= 4)
  452. // {
  453. // Console.WriteLine("Number {0}", c);
  454. // c++;
  455. // }
  456.  
  457. //}
  458.  
  459. //string number;
  460. //double total = 0;
  461. //int numbers = 0;
  462. //while ((number = Console.ReadLine()) != "")
  463. //{
  464. // total += double.Parse(number);
  465. // numbers++;
  466. //}
  467.  
  468. //Console.WriteLine("The average of the numbers entered is {0}", total / numbers);
  469.  
  470.  
  471. }
  472.  
  473.  
  474.  
  475.  
  476. static void Main(string[] args)
  477. {
  478. var keepAlive = true;
  479. while (keepAlive)
  480. {
  481. try
  482. {
  483. Console.Write("Enter assignment number (or -1 to exit): ");
  484. var assignmentChoice = int.Parse(Console.ReadLine() ?? "");
  485. Console.ForegroundColor = ConsoleColor.Green;
  486. switch (assignmentChoice)
  487. {
  488. case 1:
  489. RunExerciseOne();
  490. break;
  491. case 2:
  492. RunExerciseTwo();
  493. break;
  494. case 3:
  495. RunExerciseThree();
  496. break;
  497. case 4:
  498. RunExerciseFour();
  499. break;
  500. case 5:
  501. RunExerciseFive();
  502. break;
  503. case 6:
  504. RunExerciseSix();
  505. break;
  506. case 7:
  507. RunExerciseSeven();
  508. break;
  509. case 8:
  510. RunExerciseEight();
  511. break;
  512. case 9:
  513. RunExerciseNine();
  514. break;
  515. case 10:
  516. RunExerciseTen();
  517. break;
  518. case 11:
  519. RunExerciseEleven();
  520. break;
  521. case 12:
  522. RunExerciseTwelve();
  523. break;
  524. case 13:
  525. RunExerciseThirteen();
  526. break;
  527. case 14:
  528. RunExerciseFourteen();
  529. break;
  530. case 21:
  531. RunExerciseTwentyOne();
  532. break;
  533. case -1:
  534. keepAlive = false;
  535. break;
  536. case 77: // Använder för att testa saker
  537. RunTest();
  538. break;
  539. default:
  540. Console.ForegroundColor = ConsoleColor.Red;
  541. Console.WriteLine("That is not a valid assignment number!");
  542. break;
  543. }
  544. Console.ResetColor();
  545. Console.WriteLine("Hit any key to continue!");
  546. Console.ReadKey();
  547. Console.Clear();
  548. }
  549. catch
  550. {
  551. Console.ForegroundColor = ConsoleColor.Red;
  552. Console.WriteLine("That is not a valid assignment number!");
  553. Console.ResetColor();
  554. }
  555. }
  556. }
  557. }
  558. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement