yukisaw

High pow function

Jun 11th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <string.h>
  2. #include <iostream>
  3. using namespace std;
  4. string pow(unsigned int x, unsigned int y,unsigned int memsize)
  5. {
  6. unsigned char *memory = (char*)malloc(memsize);
  7. memset(&memory[0], 0, memsize);
  8. memory[0] = 1;
  9. string pow;char buf[2];
  10. bool zeros=true;
  11. int i, in;
  12. for (i =0; i<y;  i++)
  13.     {
  14.     for (in=0; in<y; in++)
  15.             memory[in]*=x;
  16.     for (in=0; in<y-1; in++)
  17.         if(memory[in]>9)
  18.         {
  19.         memory[in+1] += memory[in]/10;
  20.         memory[in] %=10;
  21.         }
  22.     }
  23.     for (i=y-1; i>=0; i--)
  24.     {
  25.     if (zeros) if (memory[i] == 0) continue;
  26.     else    {
  27.     in = i;
  28.     zeros = false;
  29.             }
  30.     itoa(memory[i], &buf[0], 10);
  31.     pow+=buf;
  32.     }
  33. free(memory);
  34. return pow;
  35. }
  36.  
  37. int main()
  38. {
  39. int x = 1, y;
  40. cout<<"x^y"<<endl;
  41. while (x)
  42.     {
  43.     cout<<"x: "; cin>>x;
  44.     cout<<"y: "; cin>>y;
  45.     string result = pow(x,y,8000);
  46.     cout<<result<<endl;
  47.     }
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment