InvestmentPlanner.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:3k
源码类别:

.net编程

开发平台:

Visual C++

  1. // This is the main project file for VC++ application project 
  2. // generated using an Application Wizard.
  3. #include "stdafx.h"
  4. #using <mscorlib.dll>
  5. using namespace System;
  6. // Function prototypes
  7. void DisplayWelcome();
  8. void DisplayProjectedValue(double amount, int years, double rate);
  9. void DisplayProjectedValue(double amount, int years);
  10. double GetInvestmentAmount();
  11. int GetInvestmentPeriod(int min=10, int max=25);
  12. // Define and initialize a global integer variable
  13. int numberOfYourFunctionsCalled = 0;
  14. // This is the entry point for this application
  15. #ifdef _UNICODE
  16. int wmain(void)
  17. #else
  18. int main(void)
  19. #endif
  20. {
  21. DisplayWelcome();
  22. Console::WriteLine(S"nIllustration...");
  23. DisplayProjectedValue(10000, 25, 6.0);
  24. Console::WriteLine(S"nEnter details for your investment:");
  25. double sum = GetInvestmentAmount();        
  26. int period = GetInvestmentPeriod(5, 25);                          
  27. Console::WriteLine(S"nYour plan...");
  28. DisplayProjectedValue(sum, period, 6.0);
  29. Console::Write(S"nNumber of your functions called: ");
  30. Console::WriteLine(numberOfYourFunctionsCalled);
  31. return 0;
  32. }
  33. // Display a welcome message to the user
  34. void DisplayWelcome()
  35. {
  36. numberOfYourFunctionsCalled++;
  37. Console::WriteLine(S"-------------------------------------------");
  38. Console::WriteLine(S"Welcome to your friendly Investment Planner");
  39. Console::WriteLine(S"-------------------------------------------");
  40. return;
  41. }
  42. // Calculate and display the projected value of the investment
  43. void DisplayProjectedValue(double amount, int years, double rate)
  44. {
  45. numberOfYourFunctionsCalled++;
  46. double rateFraction = 1 + (rate/100);
  47. double finalAmount = amount * Math::Pow(rateFraction, years);
  48. finalAmount = Math::Round(finalAmount, 2);
  49. Console::Write(S"Investment amount: ");
  50. Console::WriteLine(amount);
  51. Console::Write(S"Growth rate [%]: ");
  52. Console::WriteLine(rate);
  53. Console::Write(S"Period [years]: ");
  54. Console::WriteLine(years);
  55. Console::Write(S"Projected final value of investment: ");
  56. Console::WriteLine(finalAmount);
  57. return;
  58. }
  59. // Ask the user how much money they want to invest
  60. double GetInvestmentAmount()
  61. {
  62.     numberOfYourFunctionsCalled++;
  63. Console::Write(S"How much money do you want to invest? ");
  64.     String __gc * input = Console::ReadLine();
  65.     double amount = input->ToDouble(0);
  66.     return amount;
  67. }
  68. // Ask the user how long they want to invest their money (between min and max)
  69. int GetInvestmentPeriod(int min, int max)
  70. {
  71. numberOfYourFunctionsCalled++;
  72. Console::Write(S"Over how many years [");
  73. Console::Write(S"min=");
  74. Console::Write(min);
  75. Console::Write(S", max=");
  76. Console::Write(max);
  77. Console::Write(S"] ? ");
  78. String __gc * input = Console::ReadLine();
  79. int years = input->ToInt32(0);
  80. return years;
  81. }