暫無描述

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <iostream>
  2. #include <cassert>
  3. using namespace std;
  4. //
  5. // Function fact(n): Given a positive number n will return its factorial.
  6. //
  7. unsigned int fact(unsigned int n) {
  8. if (n <= 0) return 0;
  9. int result = 1;
  10. for (unsigned int i = 1; i < n; i++)
  11. result = result * i;
  12. return result;
  13. }
  14. //
  15. // Function isALetter(c):
  16. // Given a character c will return true if it is a letter,
  17. // otherwise returns false.
  18. //
  19. bool isALetter(char c) {
  20. return ( c >= 'A' && c <= 'z');
  21. }
  22. //
  23. // Function isValidTime(n:
  24. // Given a time in military format, e.g. 1147, determines if it is valid.
  25. // Returns true if valid, false otherwise.
  26. //
  27. bool isValidTime(unsigned int n) {
  28. return ( n >= 0 && n <= 2359 );
  29. }
  30. //
  31. // Function gcd(a,b):
  32. // Given two positive integeres, determines their greatest common divisor
  33. // and returns it.
  34. //
  35. int gcd ( int a, int b ) {
  36. int c;
  37. while ( a > 1 ) {
  38. c = a;
  39. a = b % a;
  40. b = c;
  41. }
  42. return b;
  43. }
  44. //
  45. // Function test_fact():
  46. // This function is provided as an example unit test for the fact() function.
  47. //
  48. void test_fact() {
  49. assert( fact(1) == 1 );
  50. assert( fact(2) == 2);
  51. assert( fact(4) == 24);
  52. cout << "Function fact() passed all the tests!!" << endl;
  53. }
  54. //
  55. // EXERCISE 3: Write your test functions here!!!
  56. //
  57. int main()
  58. {
  59. cout << "Go ahead and test!\n";
  60. test_fact();
  61. return 0;
  62. }