Silverbullets

Data Structures Program 1

Jan 20th, 2015
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include "sqroot.h"
  5. #include "LL.h"
  6. using namespace std;
  7.  
  8. int main (int argc, char **argv) {
  9.    
  10.     cout << "Enter a radicand:";
  11.     string in;
  12.     getline (cin,in);
  13.     root(in);
  14.     system ("pause");
  15.     return 0;
  16. }
  17. //implements string multiplications
  18. string mult (string a, string b) {
  19.     //makes a place for us to store our output
  20.     string out = "";
  21.     //interates through the both the strings
  22.     for (int i=0; i < (int)a.length(); i++)
  23.         for (int j=0; j < (int)b.length(); j++) {
  24.             //multiplies each value
  25.             int p = (a[i]-'0')*(b[j]-'0');
  26.             //breaks that value into the first digit and the second digit
  27.             string pstr = ((string)"")+(char)(p/10+'0')+(char)(p%10+'0');
  28.             //adds zeroes to get to the place that i is at
  29.             for (int k=0; k < (int)a.length()-i-1 + (int)b.length()-j-1; k++)
  30.                 pstr += "0";
  31.             //adds the output of that case of multiplication to the output
  32.             out = sum (out,pstr);
  33.         }
  34.     return out;
  35. }
  36. //implements string addition
  37. string sum (string a, string b) {
  38.     string out = "";
  39.     while (a.length() < b.length()) a = "0"+a;
  40.     while (b.length() < a.length()) b = "0"+b;
  41.     int carry = 0;
  42.     for (int i=a.length()-1; i >= 0; i--) {
  43.         int l = a[i]-'0' + b[i]-'0' + carry;
  44.         carry = l/10;
  45.         l %= 10;
  46.         out = ((char)(l+'0')) + out;
  47.     }
  48.     if (carry == 1){
  49.         out ="1" + out;
  50.     }
  51.     return out;
  52. }
  53. //implements string subtraction
  54. string sub (string a, string b){
  55.     string out = "";
  56.     while (b.length() < a.length()){
  57.         b = "0"+b;
  58.     }
  59.     for (int i=b.length()-1; i>= 0; i--) {
  60.         int k= b[i]-'0';
  61.         k= 9-k;
  62.         out=(char)(k+'0') + out;
  63.     }
  64.     out = sum(out,"1");
  65.     out = sum(a,out);
  66.     out[0]= '0';
  67.     return out;
  68. }
  69. //does the meat and potatoes of the program, square roots
  70. void root (string input){
  71.     string rem = input;
  72.     string ans = "0";
  73.     //get the linked list of powers of 4
  74.     LL fours = LL ();
  75.     fours.addtofront("1");
  76.     while (comp(rem,mult(fours.head->word,"4"))){
  77.         fours.addtofront(mult(fours.head->word,"4"));
  78.     }
  79.     /*while(fours.head!=NULL){
  80.         cout<<"squares: "<<endl;
  81.         cout<<fours.head->word<<endl;
  82.         fours.pop();
  83.     }*/
  84.     string check;
  85.     while(fours.head!=NULL){
  86.         //check = table value*(4*ans+1)
  87.         check = mult(fours.head->word, sum(mult("4",ans),"1"));
  88.         //Implement the decision of the formula as to whether to just multiply by two or add one afterwards
  89.         //multiplies the answer by two and adds one if rem is greater than the sum of the previous formula
  90.         if(comp(rem,check)){
  91.             //subtracts the value of the formula from the remainder
  92.             rem = sub(rem,check);
  93.             ans = sum(mult(ans,"2"),"1");
  94.             fours.pop();
  95.         }
  96.         //if the remainder is less than the formula value just multiply remainder by 2
  97.         else{
  98.             ans = mult(ans,"2");
  99.             fours.pop();
  100.         }
  101.     }
  102.     //cuts leading zeroes off everything
  103.     input=realzero(input);
  104.     ans=realzero(ans);
  105.     rem=realzero(rem);
  106.     //prints off the final line
  107.     cout<<"the square root of " + input + " is " + ans + " and the remainder is " + rem<<endl;
  108. }
  109. //returns if a is larger than b as a number
  110. bool comp(string a,string b){
  111.     //removes leading zeroes as they'd screw up the length check
  112.     a=realzero(a);
  113.     b=realzero(b);
  114.     //finds if a or b has more character than the other and returns if a is either longer or shorter than b if true
  115.     if (a.length()>b.length())return true;
  116.     else if (a.length()<b.length())return false;
  117.     //if that check didn't work we move into checking every individual character to see if there is any discrepancy
  118.     for(int i = 0; i<a.length(); i++){
  119.         //only check a value if they don't equal each other
  120.         if ((int)a[i]!=(int)b[i]){
  121.             //returns if at the first non equal value is less
  122.             if((int)a[i]<(int)b[i]){
  123.                 return false;
  124.             }
  125.             //If it isn't equal or lesser only greater than is left to return, also if both values are equal it will return true, because that's what the implementations need.
  126.             else return true;
  127.  
  128.         }
  129.     }
  130.     //extra return just in case, should not get down here under normal circumstances
  131.     return true;
  132. }
  133. //removes leading zeroes
  134. string realzero(string a){
  135.     while (a!="" && a[0]=='0') a = a.substr(1);
  136.     if (a=="") a= "0";
  137.     return a;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment