大疆0814
大疆8.14的C++开发编程题,大家可以一起分享思路呀
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const vector<string> s1 = { "0000","0001","0010","0011","0100","0101","0110","0111",
"1000","1001","1010","1011","1100","1101","1110","1111" };
const string s2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789+/";
int pow(int x, int n) {
int result = 1;
while (n--) {
result *= x;
}
return result;
}
int decode64(string& s) {
int result = 0;
int n = s.size() - 1, i = 0;
while (n > -1) {
result += (s[i++] - '0') * pow(2, n);
n--;
}
return result;
}
int main() {
string strs;
cin >> strs;
if (strs.size() == 0) cout << strs << endl;
int length = (int)strs.size() / 2;//字节数
string decodeStr;
string encodeStr;
string addStr;
for (char str : strs) {
int temp;
if (str >= '0' && str <= '9') {
temp = str - '0';
}
else {
temp = str - 'A' + 10;
}
decodeStr += s1[temp];
}
if (length % 3 == 1) {
decodeStr += "0000";
addStr = "==";
}
else if (length % 3 == 2) {
decodeStr += "00";
addStr = "=";
}
for (int i = 0; i < decodeStr.size(); i += 6) {
string temp = decodeStr.substr(i, 6);
int index = decode64(temp);
encodeStr += s2[index];
}
encodeStr += addStr;
cout << encodeStr << endl;
system("pause");
return 0;
} 