Program.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright (c) 2015 Muhammad Moaz Imtiaz.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. using System;
  15. namespace EssenNote
  16. {
  17. class MainClass
  18. {
  19. public static int Main (string[] args)
  20. {
  21. try
  22. {
  23. Console.Write("Total Weight: ");
  24. double weight = double.Parse(Console.ReadLine());
  25. Console.Write("Total Carbohydrates: ");
  26. double carbs = double.Parse(Console.ReadLine());
  27. Console.Write("Sugars: ");
  28. double sugars = double.Parse(Console.ReadLine());
  29. Console.Write("Total Fat: ");
  30. double fats = double.Parse(Console.ReadLine());
  31. Console.Write("Total Protein: ");
  32. double protein = double.Parse(Console.ReadLine());
  33. double sugarPercent = (100*sugars) / weight;
  34. double fatPercent = (900*fats)/(9*fats+4*(protein+carbs));
  35. double overallGrade = PercentGrade(sugarPercent, fatPercent);
  36. Console.WriteLine(LetterGrade(overallGrade) + "\t" + Math.Round(overallGrade*100)/100 + "%");
  37. return 0;
  38. }
  39. catch(Exception e) {
  40. Console.Error.WriteLine ("Error: " + e.GetType ().ToString () + " occurred.");
  41. Console.Error.WriteLine ("Description: " + e.Message);
  42. return -1;
  43. }
  44. }
  45. protected static double PercentGrade (double sugarPercent, double fatPercent)
  46. {
  47. double _sugarPercent = 100 - sugarPercent;
  48. double _fatPercent = 100 - fatPercent;
  49. double resultantGrade = 2*((_sugarPercent * _fatPercent) / (_sugarPercent+_fatPercent));
  50. return resultantGrade;
  51. }
  52. protected static string LetterGrade (double overallGrade)
  53. {
  54. overallGrade += .5;
  55. if (overallGrade < 60)
  56. return "E";
  57. else {
  58. string mainGrade = new string[] { "D", "C", "B", "A" , "A" }[(int)Math.Floor(overallGrade/10)-6];
  59. mainGrade += (overallGrade % 10 < 3 && overallGrade != 100) ? "-" : (overallGrade % 10 >= 8 || overallGrade == 100) ? "+" : "";
  60. return mainGrade;
  61. }
  62. }
  63. }
  64. }