Advertisement
Guest User

Emily Brent Lab 04

a guest
Feb 12th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. /*
  2. Emily Brent brentemi@xserver
  3. 2016-02-09
  4. lab04.cpp
  5.  
  6. Your Comments
  7. */
  8.  
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <cmath>
  12.  
  13. using std::cout; using std::endl; using std::cin;
  14. using std::sort; using std::string;
  15.  
  16. long loc_to_dec(string loc){
  17. //convert location arithmetic string to an int
  18.  
  19. long sum=0;
  20.  
  21. for (auto c:loc){
  22. sum+=pow(2,c-'a');
  23. }
  24. return sum;
  25.  
  26. }
  27.  
  28. string abbreviate(string loc){
  29. //take a location string and reduce it to its abbreviated form
  30.  
  31. bool bk=true;
  32. string ns = "";
  33. while(bk){
  34. for(unsigned int i = 0; i < loc.size(); i++){
  35. if(loc[i]==loc[i+1]){
  36. ns = loc.substr(0,i)+char(loc[i]+1)+loc.substr(i+2);
  37. //cout << ns << endl;
  38. loc = ns;
  39. break;
  40. }
  41. if(i+1 == loc.size()){
  42. bk = false;
  43. }
  44. }
  45. }
  46. return ns;
  47. }
  48.  
  49.  
  50. string dec_to_loc(long dec){
  51.  
  52. //convert int to abbreviated location string
  53. string ns = "";
  54. for (int i=0;i<dec;i++){
  55. ns+='a';
  56.  
  57. }
  58. ns=abbreviate(ns);
  59.  
  60. return ns;
  61. }
  62.  
  63. long add_loc(string loc1, string loc2){
  64.  
  65. string sum="";
  66. sum= loc1+loc2;
  67. sort(sum.begin(), sum.end());
  68. sum=abbreviate(sum);
  69. return loc_to_dec(sum);
  70. }
  71.  
  72.  
  73.  
  74. int main(){
  75. /*long ck;
  76. string x;
  77. cin >> ck;
  78. cout << "before: " << ck << endl;
  79. x = dec_to_loc(ck);
  80. cout << "after: " << x << endl;
  81. cout << "loc to dec: " << loc_to_dec(x) << endl;
  82. cout<<add_loc("abc","cda")<<endl;*/
  83.  
  84. string a;
  85. int b;
  86.  
  87. cout<<"Give me a string and an integer: "<<endl;
  88. cin>> a >> b;
  89. cout<<"Location string: " << a << ", Integer Equivalent: " << loc_to_dec(a) << endl;
  90. cout<<"Location string: "<< a <<", Reduced form: "<< abbreviate(a)<< endl;
  91. cout<<"Integer: "<<b<<", Location String: "<< dec_to_loc(b)<<endl;
  92. cout<<"String: "<<a<<", Added string: "<<add_loc(a,a)<<endl;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement