Thiết kế C_C++mã hoa1_32giai mã tham khảo
class MyClass {
public:
struct Point {
long long x;
int y;
string mrs;
vector<string> vmrs;
};
void
Mahoa(Point &Ec) {
string mbs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
long long num = Ec.x;
int bs = Ec.y;
string tms = "";
while (num > 0) {
int tm = num % bs;
tms = mbs[tm] + tms;
num /= bs;
}
Ec.vmrs.push_back(tms);
Ec.mrs += tms + "|";
}
//
Hàm tiện ích: nhận số hoặc vector, trả về Point đã mã hóa
Point
mahoavii(const long long &x = 0,
const int &base = 7,
const string &si =
"",
const vector<int>
&v = {}) {
Point pt;
pt.y = (base < 1 || base > 32) ? 7 : base;
pt.mrs = si; // chỉ để nối thêm chuỗi phụ
if (v.empty()) {
pt.x = x;
Mahoa(pt);
}
else {
for (auto val : v) {
pt.x = val;
Mahoa(pt);
}
}
return pt;
}
};
int main() {
MyClass mc;
// Mã
hóa một số đơn lẻ
auto
pt1 = mc.mahoavii(26, 16);
cout
<< "Ma hoa 26 he 16: " << pt1.mrs << endl;
// Mã
hóa cả vector
auto
pt2 = mc.mahoavii(0, 16, "prefix-", {10, 15, 26});
cout
<< "Ma hoa vector: " << pt2.mrs << endl;
// In
từng phần tử đã mã hóa
for
(auto &s : pt2.vmrs) {
cout << " -> " << s << endl;
}
}
Tóm lại
- si chỉ để nối thêm chuỗi phụ (ví dụ nhãn, tiền tố,
hậu tố).
- Mã hóa thực sự dựa vào x hoặc vector<int> v.
- Hàm mahoavii giúp bạn xử lý cả số đơn lẻ và danh
sách số trong một lần gọi, tiện cho dữ liệu file hoặc mảng dài.
LLONG_MAX
class RClass {
public:
struct Point {
long long x;
int y;
string mrs;
vector<string> vmrs;
};
void
Giaima(Point &Ec) {
vector<string> decoded;
string token;
stringstream ss(Ec.mrs);
while (getline(ss, token, '|')) {
if (token.empty()) continue;
long long num = 0;
for (char c : token) {
int value = -1;
if (isdigit(c)) value = c - '0';
else if (c >= 'A' && c <= 'Z') value = c - 'A' + 10;
else if (c >= 'a' && c <= 'z') value = c - 'a' + 10;
if (value < 0 || value >= Ec.y) {
cout << "Ky tu
" << c << " khong hop le trong he " << Ec.y
<< endl;
continue;
}
num = num * Ec.y + value;
}
decoded.push_back(to_string(num));
}
Ec.vmrs = decoded;
if (!decoded.empty()) Ec.x = stoll(decoded[0]);
}
Point
giaimavii(const long long &xi = 0,
const int &base = 7,
const string &si =
"",
const vector<int>
&v = {}) {
Point pt;
pt.y = (base < 1 || base > 32) ? 7 : base;
pt.x = (xi < 1 || xi > LLONG_MAX) ? 0 : xi;
pt.mrs = si;
// Nếu có vector thì chuyển thành chuỗi mrs để giải mã
if (!v.empty()) {
for (auto val : v) {
pt.mrs += to_string(val) + "|";
}
}
else {
pt.mrs += to_string(pt.x) + "|";
}
Giaima(pt);
return pt;
}
};
Giải thích
- Giaima tách chuỗi mrs theo dấu |, chuyển từng token
về số thập phân, lưu vào vmrs.
- giaimavii chuẩn bị dữ liệu: gán x, y, nối chuỗi mrs
từ xi hoặc từ vector v. Sau đó gọi Giaima để giải mã.
- Trả về Point đã giải mã.
////////////////////////////////////////////////////
/////////////////////////////////////////
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <map>
#include <bitset>
#include <limits.h>
#include <algorithm>
#include <string>
#include <cctype>
#include <bits/stdc++.h>
using namespace std;
class MyClass {
public:
struct Point
{
long
long x;
int y;
string
mrs;
vector<string> vmrs;
};
void
Mahoa(Point &Ec) {
string
mbs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
long
long num = Ec.x;
int bs =
Ec.y;
string
tms = "";
while
(num > 0) {
int
tm = num % bs;
tms
= mbs[tm] + tms;
num
/= bs;
}
Ec.vmrs.push_back(tms);
Ec.mrs
+= tms + "|";
}
Point
mahoavii(const long long &xi = 0,const int &base = 7,
const
string &si = "",const vector<int> &v = {}) {
vector
<int> vv=v;
Point
pt;
pt.x
= (xi < 1||xi > LLONG_MAX) ? 0 : xi;
pt.y =
(base < 1 || base > 32) ? 7 : base;
pt.mrs =
(si.empty()) ? si : "";
if
(vv.empty()) {vv.push_back(xi);
} else {
for
(auto val : vv) {
pt.x = val;
Mahoa(pt);
}
}
return
pt;
}
};
// ======================
// Class gia ma
// ======================
class RClass {
public:
struct Point
{
long
long x;
int y;
string
mrs;
vector<string> vmrs;
};
void
Giaima(Point &Ec) {
string
token,tm="";
stringstream ss(Ec.mrs);
while
(getline(ss, token, '|')) {
if
(token.empty()) continue;
long
long num = 0;
for
(char c : token) {
int value = -1;
if (isdigit(c)) value = c - '0';
else if (c >= 'A' && c <= 'Z') value = c - 'A' + 10;
else if (c >= 'a' && c <= 'z') value = c - 'a' + 10;
if (value < 0 || value >= Ec.y) {
cout << "Ky tu " << c << " khong hop le
trong he " << Ec.y << endl;
continue;
}
num = num * Ec.y + value;
}
tm+=to_string(num);
}Ec.mrs.clear(); Ec.mrs=tm;
vector
<string> ddd;
for(auto
d:Ec.vmrs){
for
(char c : d) {
int value = -1;
if (isdigit(c)) value = c - '0';
else if (c >= 'A' && c <= 'Z') value = c - 'A' + 10;
else if (c >= 'a' && c <= 'z') value = c - 'a' + 10;
if (value < 0 || value >= Ec.y) {
cout << "Ky tu " << c << " khong hop le
trong he " << Ec.y << endl;
continue;
}
ddd.push_back(to_string(value));
}
}
Ec.vmrs.clear();
Ec.vmrs=ddd;
string
kt;
for(auto
j:Ec.vmrs){
kt+=j+"|";
if
(kt==Ec.mrs) Ec.x = 0;
}
}
Point
giaimavii(const long long &xi = 0,const int &base = 7,
const
string &si = "",const vector<string> &v = {}) {
vector<string>
vd=v;
Point
pt;
MyClass::Point
pti;
pt.y
= (base < 1 || base > 32) ? pti.y : base;
pt.x
= (xi < 1||xi > LLONG_MAX) ? 0 : xi;
pt.mrs =
(si.empty()) ? pti.mrs : si;
if
(vd.empty()) vd.push_back(to_string(pt.x));
else
pt.vmrs=vd;
Giaima(pt);
return
pt;
}
};
class NhatTruong {
private:
// ===== DAY
CAC BIEN SO BM =====
string ss,
df, kqxl;
char ch;
double db;
int so;
vector<double> vec;
vector<int> vii;
vector<pair<int,int>> vp;
vector<vector<double>> vvc;
map<long
long,int> mp;
bitset<50> bits;
public:
// =====
NHAP VA XUAT BIEN & Getter/Setter =====
NhatTruong(): ss(""), ch('\0'), vec(), vp(), db(0), so(0),
mp(), df(""), vii(), vvc() ,kqxl(""){};
~NhatTruong() {
cout << "thoat memory\n";}
// Getter
string
getss(){ return ss; };
string
getdf(){ return df; };
char
getch(){ return ch; };
double
getdb(){ return db; };
int getso(){
return so; };
vector<double> getvec(){ return vec; };
vector<int> getvii(){ return vii; };
vector<pair<int,int>> getvp(){ return vp; };
map<long
long,int> getmp(){ return mp; };
// Setter
void
setws(const string &dat){ ss = dat; } ;
void
setwdf(const string &dat) { df = dat; } ;
void
setwso(int dat){ so = dat; } ;
void
setwdb(double dat){ db = dat; } ;
void
setwch(char dat){ ch = dat; } ;
// ===== XU
LI file =====
// ===== Doc file line den ss && vii =====
void docfl
(const string& tenf = ""){
string tenif = tenf.empty() ? "input.txt" :
tenf;
ifstream
fin(tenif);
if
(!fin.is_open()){
cerr
<< "Khong the mo file du lieu\n";
ofstream fout(tenif);
fout.close();
return;
}
string
lin;
bool
first = true;
ss.clear();
kqxl.clear();df.clear();
// /* while (fin >> lin) { */
while
(getline(fin, lin)) {
ss
+= lin + " ";
if
(first) {
df = lin;
istringstream iss(df);
iss >> so;
first = false;
}
}
fin.close();
chuyenstrstream_vii();
};
// ===== Doc file den ss&& vec =====
void
docf(const string& tenf = ""){
string tenif = tenf.empty() ? "input.txt" :
tenf;
ifstream
fin(tenif);
if
(!fin.is_open()){
cerr
<< "Khong the mo file du lieu\n";
ofstream fout(tenif);
fout.close();
return;
}
string
lin;
bool
first = true;
ss.clear();
kqxl.clear();df.clear();
// /* while (getline(fin, lin)) { */
while
(fin >> lin){
ss += lin + " ";
if
(first) {
df = lin;
istringstream iss(df);
iss >> so;
first = false;
}
}
fin.close();
chuyenstrstream_vec ();
};
// Xuat =====
xuatf(string kq,tenf) ===== ra
file
void
xuatf(const string& kq="", const string& teno = "")
{
string
tenof = teno.empty() ? "kqoutput.txt" : teno;
string
kqof = kq.empty() ? kqxl : kq;
ofstream
fout(tenof);
if
(!fout.is_open()) {
cerr
<< "Khong the xuat file ket qua\n";
return;
}
fout
<< kqof << endl;
fout.close();
};
// Xuat =====
xuatVec(vec,tenf) ===== ra file
void xuatVec(const vector<double>& arr = {},
const string& tenf = "") {
const
vector<double>& tmp = arr.empty() ? vec : arr;
string
tenfile = tenf.empty() ? "vecoutput.txt" : tenf; //
ofstream
fout(tenfile);
if
(!fout.is_open()) {
cerr
<< "Khong the mo file ket qua\n";
return;
}
for (auto x
: tmp) fout << x << " ";
fout
<< endl;
fout.close();
};
// Xuat =====
xuatVii(vii,tenf) ===== file
void xuatVii(const vector<int>& arr = {},
const string& tenf = "") {
const
vector<int>& tmp = arr.empty() ? vii : arr;
string
tenfile = tenf.empty() ? "viioutput.txt" : tenf;
ofstream
fout(tenfile);
if
(!fout.is_open()) {
cerr
<< "Khong the mo file ket qua\n";
return;
}
for (auto x
: tmp) fout << x << " ";
fout
<< endl;
fout.close();
};
// // Xuat =====
xuatVp(vp,tenf) ===== file
void xuatVp(const
vector<pair<int,int>>& arr = {}, const string& tenf =
"") {
const
vector<pair<int,int>> &tmp = arr.empty() ? vp : arr;
string
tenfile = tenf.empty() ? "vpoutput.txt" : tenf;
ofstream
fout(tenfile);
if
(!fout.is_open()) {
cerr
<< "Khong the mo file ket qua\n";
return;
}
for (auto
&p : tmp) {
fout
<< "(" << p.first << ", " <<
p.second << ") \n";
}
fout
<< endl;
};
// Xuat ===== xuatDouble(double val , tenf) ===== ra
file
void xuatDouble(double val, const string& tenf =
"") {
string
tenfile = tenf.empty() ? "doubleoutput.txt" : tenf;
ofstream
fout(tenfile);
if
(!fout.is_open()) {
cerr
<< "Khong the mo file ket qua\n";
return;
}
fout
<< val << endl;
fout.close();
};
// Xuat ===== xuatInt(int val, tenf) ===== ra file
void xuatInt(int val, const string& tenf =
"") {
string
tenfile = tenf.empty() ? "intoutput.txt" : tenf;
ofstream
fout(tenfile);
if
(!fout.is_open()) {
cerr
<< "Khong the mo file ket qua\n";
return;
}
fout
<< val << endl;
fout.close();
};
// =====
CHUYEN KIEU DATA=====
// =====
chuyenstrstream_vec(string) =====vec
bool
chuyenstrstream_vec(const string &si = "") {
string
csi = si.empty() ? ss : si;
vec.clear();
stringstream sss(csi);
double
tmp;
while
(sss >> tmp) {
vec.push_back(tmp);
}
return
(vec.size()>0);
};
// =====
chuyenstr_vec(string) =====vec
bool
chuyenstr_vec(const string &si = "") {
string csi = si.empty() ? ss : si;
vec.clear();
int dau = 1;
double tmp =
0;
double heSo
= 0.1;
bool
dangDocSo = false;
bool
dangDocThapPhan = false;
for (char c
: csi) {
if (c ==
'-') {
if
(dangDocSo) {
vec.push_back(tmp * dau);
tmp = 0; dau = 1; heSo = 0.1;
dangDocSo = false; dangDocThapPhan = false;
}
dau
= -1;
} else
if (isdigit(static_cast<unsigned char>(c))) {
if
(!dangDocThapPhan) {
tmp = tmp * 10 + (c - '0');
}
else {
tmp = tmp + (c - '0') * heSo;
heSo /= 10;
}
dangDocSo = true;
} else
if (c == '.') {
if
(!dangDocSo) {
// bo qua dau cham thua truoc so
continue;
}
if
(dangDocThapPhan) {
// gap dau cham lan 2 ngat so
vec.push_back(tmp * dau);
tmp = 0; dau = 1; heSo = 0.1;
dangDocSo = false; dangDocThapPhan = false;
}
else {
dangDocThapPhan = true;
}
} else {
if
(dangDocSo) {
vec.push_back(tmp * dau);
tmp = 0; dau = 1; heSo = 0.1;
dangDocSo = false; dangDocThapPhan = false;
}
}
}
if
(dangDocSo) {
vec.push_back(tmp * dau);
}
return true;
};
// ===== chuyenstrstream_vii(string) =====vii
bool
chuyenstrstream_vii(const string &si = "") {
string
csi = si.empty() ? ss : si;
vii.clear();
istringstream sss(csi);
int tmp;
while
(sss >> tmp) {
vii.push_back(tmp);
}
return
(vii.size()>0);
};
// =====
chuyenstr_vii(string) =====vii
bool
chuyenstr_vii(const string &si = "") {
string csi = si.empty() ? ss : si;
vec.clear();
int dau = 1;
double tmp =
0;
double heSo
= 0.1;
bool
dangDocSo = false;
bool
dangDocThapPhan = false;
for (char c
: csi) {
if (c ==
'-') {
if
(dangDocSo) {
vii.push_back((int)(tmp * dau));
tmp = 0; dau = 1; heSo = 0.1;
dangDocSo = false; dangDocThapPhan = false;
}
dau
= -1;
} else
if (isdigit(static_cast<unsigned char>(c))) {
if
(!dangDocThapPhan) {
tmp = tmp * 10 + (c - '0');
}
else {
tmp = tmp + (c - '0') * heSo;
heSo /= 10;
}
dangDocSo = true;
} else
if (c == '.') {
if
(!dangDocSo) {
continue;
}
if
(dangDocThapPhan) {
vii.push_back((int)(tmp * dau));
tmp = 0; dau = 1; heSo = 0.1;
dangDocSo = false; dangDocThapPhan = false;
}
else {
dangDocThapPhan = true;
}
} else {
if
(dangDocSo) {
vii.push_back((int)(tmp * dau));
tmp = 0; dau = 1; heSo = 0.1;
dangDocSo = false; dangDocThapPhan = false;
}
}
}
if
(dangDocSo) {
vii.push_back((int)(tmp * dau));
}
return true;
};
// ===== chuyenvec_str(arr vec) =====s1
string
chuyenvec_str(const vector<double> &arr={}){
const
vector<double> &tmp = arr.empty() ? vec : arr;
if
(tmp.empty()) return "";
string
s;
for(auto
x:tmp){
s +=
to_string(x) + " ";
}
return
s;
};
// ===== chuyenvec_strn(arr vec) =====s2
string
chuyenvec_strn(const vector<double> &arr={}) {
const
vector<double> &tmp = arr.empty() ? vec : arr;
if
(tmp.empty()) return "";
string
s;
for
(auto x : tmp) {
ostringstream oss;
oss
<< x;
s +=
oss.str();
s +=
" ";
}
return
s;
};
// ===== chuyenvii_str (arr vii) =====s1
string
chuyenvii_str(const vector<int> &v = {}) {
const
vector<int> &tmp = v.empty() ? vii : v;
if
(tmp.empty()) return "";
string
s;
for
(auto x : tmp) {
s +=
to_string(x) + " ";
}
return
s;
};
// ===== chuyenvii_strn (arr vii) =====s2
string
chuyenvii_strn(const vector<int> &v={}) {
const
vector<int> &tmp = v.empty() ? vii : v;
if
(tmp.empty()) return "";
string
s;
for
(auto x : tmp) {
ostringstream oss;
oss
<< x;
s +=
oss.str();
s +=
" ";
}
return
s;
};
// ===== chuyenint_str (int ii) =====to_string()
string
chuyenint_str(int ii = -1) {
int tmp
= (ii == -1) ? so : ii;
return
to_string(tmp);
};
// ===== chuyendb_str(double ii) =====to_string()
string
chuyendb_str(double ii = 0.0) {
double
tmp = (ii == 0.0) ? db : ii;
return
to_string(tmp);
};
// ===== chuyenstr_db (string si) =====string df
double
chuyenstr_db(const string &si = "") {
string
csi = si.empty() ? df : si;
istringstream sss(csi);
double
tmp = 0.0;
sss
>> tmp;
return
tmp;
};
// ===== chuyenstr_int (string si) =====string df
int
chuyenstr_int(const string &si = "") {
string
csi = si.empty() ? df : si;
istringstream sss(csi);
int tmp
= 0;
sss
>> tmp;
return
tmp;
};
// ===== MyClass::Point mahoavii(a,b,c,d) =====
MyClass::Point mavii(const long long &x = 0,const int &base = 7,
const
string &si = "|",const vector<int> &v = {}) {
long long mx
= (x < 1 ? 0 : x);
int mbase =
(base < 1 || base > 32) ? 7 : base;
string sii =
(si.empty()) ? "|" : si;
MyClass mc;
auto pt =
mc.mahoavii(mx, mbase, si,v);
return pt;
};
RClass::Point giaimav(const long long &x = 0,const int &base =
7,
const
string &si = "",const vector<int> &v = {}) {
MyClass::Point
pti;
long
long mx = (x < 1||x>LLONG_MAX) ? 0 : x;
int mbase =
(base < 1 || base > 32) ? pti.y : base;
string sii =
(si.empty()) ? pti.mrs : si;
vector<string> vi;
for(auto
c:v) vi.push_back(to_string(c));
if(vi.empty())
vi.push_back(to_string(mx));
vi =
(vi[0]=="0") ? pti.vmrs : vi;
RClass mcr;
auto pt =
mcr.giaimavii(mx,mbase,sii,vi);
return pt;
};
// ===== HAM THONG KE & Logic =====
// ===== tongVec (double arr) =====vec
double tongVec(const vector<double> &arr =
{}) {
const
vector<double> &tmp = arr.empty() ? vec : arr;
double sum =
0;
for (auto x
: tmp) sum += x;
return sum;
};
// ===== tongVii (int arr) =====vii
int tongVii(const vector<int> &arr = {}) {
const
vector<int> &tmp = arr.empty() ? vii : arr;
int sum = 0;
for (auto x
: tmp) sum += x;
return sum;
};
// ===== trungBinhVec (double arr) =====vec
double trungBinhVec(const vector<double>
&arr = {}) {
const
vector<double> &tmp = arr.empty() ? vec : arr;
if
(tmp.empty()) return 0;
return
tongVec(tmp) / tmp.size();
};
// ===== minVec (double arr) =====vec
double minVec(const vector<double> &arr =
{}) {
const
vector<double> &tmp = arr.empty() ? vec : arr;
if
(tmp.empty()) return 0;
return
*min_element(tmp.begin(), tmp.end());
};
// ===== minVii (int arr) =====vii
int minVii(const vector<int> &v = {}) {
const
vector<int> &tmp = v.empty() ? vii : v;
if
(tmp.empty()) return 0;
return
*min_element(tmp.begin(), tmp.end());
};
// ===== maxVec (double arr) =====vec
double maxVec(const vector<double> &arr =
{}) {
const
vector<double> &tmp = arr.empty() ? vec : arr;
if
(tmp.empty()) return 0;
return
*max_element(tmp.begin(), tmp.end());
};
// ===== maxVii (int arr) =====vii
int maxVii(const vector<int> &v = {}) {
const
vector<int> &tmp = v.empty() ? vii : v;
if
(tmp.empty()) return 0;
return
*max_element(tmp.begin(), tmp.end());
};
// ===== demTu1 (string si) =====ss
int
demTu1(const string &si="") {
string
csi = si.empty() ? ss : si;
stringstream ssin(csi);
string
word;
int
count = 0;
while
(ssin >> word) count++;
return
count;
};
// ===== demTu2 (string si) =====ss
int
demTu2(const string &si="") {
string
csi = si.empty() ? kqxl : si;
stringstream ssin(csi);
string
word;
int
count = 0;
while
(ssin >> word) count++;
return
count;
};
// /*=====
duyet_vec([](double &x)) =====ss===vd duyet_vec([](double &x)) {x *=
2;}; */
void
duyet_vec(const std::function<void(double&)> &func) {
for (auto
&x : vec) {
func(x);
}
};
// /*===== duyet_vii([](int &x)) =====ss===vd
duyet_vii([](int &x)) {x *= 2;}; */
void
duyet_vii(const std::function<void(int&)> &func) {
for (auto
&x : vii) {
func(x);
}
};
// ===== Giao dien nguoi dung =====khoidong()
void
khoidong1(){
cout << "Ban muon nhap du
lieu tu file hay truc tiep (f/p): ";
char tl;
cin >>
tl;
if(tl == 'f'
|| tl == 'F'){
cout
<< "Nhap ten file input (vd: input.txt): ";
string
ten;
cin
>> ten;
docf(ten);
}
else {
cout
<< "Nhap chuoi du lieu dau vao: ";
cin.ignore();
string
dv="";
getline(cin, dv);
ss.clear();kqxl.clear();df.clear();
setws(dv);
chuyenstrstream_vec();
so=(int)vec[0];
df=to_string(so);
}
};
// ===== Giao dien khoi dong =====khoidong()
void
khoidong2(){
cout << "Ban muon nhap du
lieu tu file hay truc tiep (f/p): \n";
char tl;
cin >>
tl;
if(tl == 'f'
|| tl == 'F'){
cout
<< "Nhap ten file input (vd: input.txt): \n";
string
ten;
cin
>> ten;
docfl(ten);
}
else {
cout
<< "Nhap chuoi du lieu dau vao: \n";
cin.ignore();
string
dv="";
getline(cin, dv);
ss.clear();kqxl.clear();df.clear();
setws(dv);
chuyenstrstream_vii();
so=vii[0];
df=to_string(so);
}
};
// ===== Giao dien menu nguoi dung =====Tmenu(tenfile
xuat.txt)
void Tmenu()
{
int
chon;
do {
cout
<< "\n===== MENU =====\n";
cout
<< "1. Khoi dong 1(nhap chuoi || file || so thuc)\n";
cout
<< "2. Khoi dong 2(nhap chuoi || file || so nguyen)\n";
cout
<< "3. Xem du lieu nhap 1\n";
cout
<< "4. Xem du lieu nhap 2\n";
cout
<< "5. Dem so tu trong chuoi1\n";
cout
<< "6. Dem so tu trong chuoi2\n";
cout
<< "7. Xuat chuoi ket qua ra file\n";
cout
<< "8. Xuat mang double ra file\n";
cout
<< "9. Xuat mang int ra file\n";
cout
<< "10. Xuat mang pair ra file\n";
cout
<< "11. Xuat so double ra file\n";
cout
<< "12. Xuat so int ra file\n";
cout
<< "13. Chuan chuyen chuoi sang vector so thuc mo rong \n";
cout
<< "14. Chuan chuyen chuoi sang vector so nguyen mo rong \n";
cout
<< "15. Ma hoa \n";
cout
<< "16. Giai Ma \n";
cout
<< "0. Thoat\n";
cout
<< "================\n";
cout
<< "Moi ban chon chuc nang: ";
cin
>> chon;
cin.ignore();
switch (chon) {
case 1: khoidong1(); break;
case 2: khoidong2(); break;
case 3: {
vector<double> v = getvec();
cout << "Data input " << ss <<
"\n";
cout << "Vector: " << chuyenvec_strn(v) <<
"\n";
cout
<< "Du lieu dau file " << df << "\n";
break;
}
case
4: {
vector<int> v = getvii();
cout << "Data input " << ss <<
"\n";
cout << "Vector: " << chuyenvii_strn(v) <<
"\n";
cout
<< "Du lieu dau file " << df << "\n";
break;
}
case 5: cout << "So tu trong chuoi nhap " <<
demTu1() << "\n"; break;
case 6: cout << "So tu trong chuoi kq= " <<
demTu2() << "\n"; break;
case 7: {
cout << "Ban hay nhap ten tep muon xem ket qua hoac enter mac
dinh: ";
string tenoff;
getline(cin, tenoff);
if (tenoff.empty()) tenoff = "NTmacdinh7.txt";
xuatf(kqxl, tenoff);
break;
}
case 8: {
cout << "Ban hay nhap ten tep muon xem ket qua hoac enter mac
dinh: ";
string tenoff;
getline(cin, tenoff);
if (tenoff.empty()) tenoff = "NTmacdinh8.txt";
xuatVec(vec, tenoff);
break;
}
case 9: {
cout << "Ban hay nhap ten tep muon xem ket qua hoac enter mac
dinh: ";
string tenoff;
getline(cin, tenoff);
if (tenoff.empty()) tenoff = "NTmacdinh9.txt";
xuatVii(vii, tenoff);
break;
}
case 10: {
cout << "Ban hay nhap ten tep muon xem ket qua hoac enter mac
dinh: ";
string tenoff;
getline(cin, tenoff);
if (tenoff.empty()) tenoff = "NTmacdinh10.txt";
xuatVp(vp, tenoff);
break;
}
case 11: {
cout << "Ban hay nhap ten tep muon xem ket qua hoac enter mac
dinh: ";
string tenoff;
getline(cin, tenoff);
if (tenoff.empty()) tenoff = "NTmacdinh11.txt";
xuatDouble(db, tenoff);
break;
}
case 12: {
cout << "Ban hay nhap ten tep muon xem ket qua hoac enter mac
dinh: ";
string tenoff;
getline(cin, tenoff);
if (tenoff.empty()) tenoff = "NTmacdinh12.txt";
xuatInt(so, tenoff);
break;
}
case 13: {
chuyenstr_vec();
cout
<< "Cac phan tu vecdb sau chuyen la:\n";
duyet_vec([](double &x) {
cout
<< setw(5) << x;
});
break;
}
case 14: {
chuyenstr_vii();
cout
<< "Cac phan tu viint sau chuyen la:\n";
duyet_vii([](int &x) {
cout
<< setw(5) << x;
});
break;
}
case 15: {
cout<<"Ban
hay nhap so muon ma hoa va he ma hoa(enter 0 0 de nhan mac dinh)";
long
long k; int h;
cin>>k>>h;
vector<int>
vmh=getvii();
string
s=getss();
MyClass::Point pti=mavii(k,h,s,vmh);
cout
<< "Ma hoa vector: " << pti.mrs <<"\n\n";
for (auto
&sj : pti.vmrs) {
cout
<< sj <<" ";
}cout
<<"\n";
break;
}
case 16: {
cout<<"Ban
muon nhap so nguyen de ma hoa va he ma hoa(enter 0 0 nhan mac dinh)";
long
long k; int h;
cin>>k>>h;
string
s=getss();
RClass::Point pti=giaimav(k,h,s,getvii());
cout
<< "Ma hoa vector: " << pti.mrs <<"\n\n";
for (auto
&sj : pti.vmrs) {
cout
<< sj <<" ";
}cout
<<"\,";
break;
}
case 0: cout << "Thoat chuong trinh.\n"; break;
default: cout << "Lua chon khong hop le!\n"; break;
}
} while
(chon != 0);
};
// /*
===== XU LY MO RONG =====*/
int
minusc(int a, int b){
if (a*b<=0) {a=abs(a); b=abs(b);}
if (b == 0)
return a;
return
minusc(b, a % b);
};
int bscln(int a, int b){
if (a*b<=0) {a=abs(a); b=abs(b);}
return (a/minusc(a,b)*b);
};
bool
checkcapnt(int a, int b){
if (a <=
0 || b <= 0) return false;
return
(minusc(a, b) == 1);
};
bool caccapnt(int a, int b){
vp.clear();
if (a >
b) swap(a, b);
for (int i =
a; i < b; i++) {
for (int
j = i + 1; j <= b; j++) {
if
(checkcapnt(i, j)) {
vp.push_back({i, j});
}
}
}
return
(vp.size()>0);
};
};
int main (int argc, char** argv){
NhatTruong
T;
cout
<< "Day la chuong trinh ....
\n";
cout
<< "Ban chuan bi nhap Data input \n";
T.Tmenu();
cout
<< "Chuc mung, ban co the xem ket qua o file da xuat" <<
endl;
return 0;
}
//////////////////////////////////
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
// ======================
// Class mã hóa
// ======================
class MyClass {
public:
struct Point
{
long
long x;
int y;
string
mrs;
vector<string> vmrs;
};
void
Mahoa(Point &Ec) {
string
mbs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
long
long num = Ec.x;
int bs =
Ec.y;
string
tms = "";
while
(num > 0) {
int
tm = num % bs;
tms
= mbs[tm] + tms;
num
/= bs;
}
Ec.vmrs.push_back(tms);
Ec.mrs
+= tms + "|";
}
Point
mahoavii(const vector<int> &v, int base) {
Point
pt;
pt.y =
(base < 1 || base > 32) ? 7 : base;
pt.mrs =
"";
pt.vmrs.clear();
for
(auto val : v) {
pt.x
= val;
Mahoa(pt);
}
return
pt;
}
};
// ======================
// Class giải mã
// ======================
class RClass {
public:
struct Point
{
long
long x;
int y;
string
mrs;
vector<string> vmrs;
};
void
Giaima(Point &Ec) {
vector<string> decoded;
string
token;
stringstream ss(Ec.mrs);
while
(getline(ss, token, '|')) {
if
(token.empty()) continue;
long
long num = 0;
for
(char c : token) {
int value = -1;
if (isdigit(c)) value = c - '0';
else if (c >= 'A' && c <= 'Z') value = c - 'A' + 10;
else if (c >= 'a' && c <= 'z') value = c - 'a' + 10;
if (value < 0 || value >= Ec.y) {
cout << "Ky tu " << c << " khong hop le
trong he " << Ec.y << endl;
continue;
}
num = num * Ec.y + value;
}
decoded.push_back(to_string(num));
}
Ec.vmrs
= decoded;
if
(!decoded.empty()) Ec.x = stoll(decoded[0]);
}
};
int main() {
MyClass mc;
RClass rc;
// Mã hóa
vector số
auto ptEnc =
mc.mahoavii({10, 15, 26}, 16);
cout
<< "Ma hoa (mrs): " << ptEnc.mrs << endl;
// Giải mã
chuỗi vừa tạo
RClass::Point ptDec;
ptDec.y =
16;
ptDec.mrs =
ptEnc.mrs;
rc.Giaima(ptDec);
cout
<< "Giai ma (vmrs): ";
for (auto
&s : ptDec.vmrs) cout << s << " ";
cout
<< endl;
}
Nhận xét
Đăng nhận xét