cs1713-day1-prog5.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2020, 2019, 2018, 2017 Girish M
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. *
  18. */
  19. /*-----------------------------------
  20. Name: Girish M
  21. Roll number: cs1713
  22. Date: 25 July 2017
  23. Program description:Take positive integers x,n from the user, and compute x to the power of n. How long does it take with naive multiplications? Can you do any better?
  24. Acknowledgements:http://pubs.opengroup.org/onlinepubs/7908799/xsh/regcomp.html
  25. ------------------------------------*/
  26. #include <stdio.h>
  27. #include <regex.h>
  28. #include <stdlib.h>
  29. int match(const char *string, char *pattern)
  30. {
  31. int status;
  32. regex_t re;
  33. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
  34. return(0); /* report error */
  35. }
  36. status = regexec(&re, string, (size_t) 0, NULL, 0);
  37. regfree(&re);
  38. if (status != 0) {
  39. return(0); /* report error */
  40. }
  41. return(1);
  42. }
  43. /* Extended version of power function that can work
  44. for float x and negative y*/
  45. /*float power(float x, int y)
  46. {
  47. float temp;
  48. if( y == 0)
  49. return 1;
  50. temp = power(x, y/2);
  51. if (y%2 == 0)
  52. return temp*temp;
  53. else
  54. {
  55. if(y > 0)
  56. return x*temp*temp;
  57. else
  58. return (temp*temp)/x;
  59. }
  60. }*/
  61. /* Function to calculate x raised to the power y in O(logn)*/
  62. long int power(long int x, long int y)
  63. {
  64. long int temp;
  65. if( y == 0)
  66. return 1;
  67. temp = power(x, y/2);
  68. if (y%2 == 0)
  69. return temp*temp;
  70. else
  71. {
  72. if(y > 0)
  73. return x*temp*temp;
  74. else
  75. return (temp*temp)/x;
  76. }
  77. }
  78. int main(int argc, char* argv[])
  79. {
  80. long int x, n, prod=1;
  81. char* pattern = "^[+-]?[0-9]+$";
  82. if(argc == 3)
  83. {
  84. if(match(argv[1],pattern) && match(argv[2],pattern))
  85. {
  86. x = atoi(argv[1]);
  87. n = atoi(argv[2]);
  88. if(n >= 0)
  89. {
  90. /* Iterative logic
  91. while(n != 0)
  92. {
  93. prod = x*prod;
  94. n--;
  95. }*/
  96. printf("\n %s to the power of %s is %ld\n", argv[1], argv[2], power(x,n));
  97. }
  98. else
  99. printf("\nUsage: ./cs1713-day1-prog5.o int positiveInt\n");
  100. }
  101. else
  102. printf("\nUsage: ./cs1713-day1-prog5.o int positiveInt\n");
  103. }
  104. else
  105. printf("\nUsage: ./cs1713-day1-prog5.o int positiveInt\n");
  106. return 0;
  107. }