Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Emily Brent brentemi@xserver
- 2016-02-09
- lab04.cpp
- Your Comments
- */
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- using std::cout; using std::endl; using std::cin;
- using std::sort; using std::string;
- long loc_to_dec(string loc){
- //convert location arithmetic string to an int
- long sum=0;
- for (auto c:loc){
- sum+=pow(2,c-'a');
- }
- return sum;
- }
- string abbreviate(string loc){
- //take a location string and reduce it to its abbreviated form
- bool bk=true;
- string ns = "";
- while(bk){
- for(unsigned int i = 0; i < loc.size(); i++){
- if(loc[i]==loc[i+1]){
- ns = loc.substr(0,i)+char(loc[i]+1)+loc.substr(i+2);
- //cout << ns << endl;
- loc = ns;
- break;
- }
- if(i+1 == loc.size()){
- bk = false;
- }
- }
- }
- return ns;
- }
- string dec_to_loc(long dec){
- //convert int to abbreviated location string
- string ns = "";
- for (int i=0;i<dec;i++){
- ns+='a';
- }
- ns=abbreviate(ns);
- return ns;
- }
- long add_loc(string loc1, string loc2){
- string sum="";
- sum= loc1+loc2;
- sort(sum.begin(), sum.end());
- sum=abbreviate(sum);
- return loc_to_dec(sum);
- }
- int main(){
- /*long ck;
- string x;
- cin >> ck;
- cout << "before: " << ck << endl;
- x = dec_to_loc(ck);
- cout << "after: " << x << endl;
- cout << "loc to dec: " << loc_to_dec(x) << endl;
- cout<<add_loc("abc","cda")<<endl;*/
- string a;
- int b;
- cout<<"Give me a string and an integer: "<<endl;
- cin>> a >> b;
- cout<<"Location string: " << a << ", Integer Equivalent: " << loc_to_dec(a) << endl;
- cout<<"Location string: "<< a <<", Reduced form: "<< abbreviate(a)<< endl;
- cout<<"Integer: "<<b<<", Location String: "<< dec_to_loc(b)<<endl;
- cout<<"String: "<<a<<", Added string: "<<add_loc(a,a)<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement