我是 C++ 的初学者。我在从程序中自动屏蔽数据时遇到问题。程序应自动选择要屏蔽的数据。现在,我的程序只能通过插入输入来手动选择需要屏蔽哪些数据。我试图弄清楚,但我无法解决它。下面是我的代码。
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
using namespace std;
const int ROW = 7;
const int COL = 7;
string raw[ROW][COL] = {{"Customer ID", "Name", "Premise", "Street No.", "Town", "Meter No.", "Usage (kWh)"},
{"112", "mike", "87", "Jln 12", "port", "24564532", "466"},
{"545", "danny", "25", "Jln 24", "casa", "82251928", "578"},
{"323", "tan", "34", "Jln 36", "cattleya", "12193729", "253"},
{"843", "jess", "86", "Jln 48", "mount", "98829101", "316"},
{"218", "jack", "44", "Jln 11", "river", "1927192", "212"},
{"298", "bob", "99", "Jln 60", "stall", "53328789", "266"}};
void loadData(vector<vector<string> > &data)
{
for (int i = 0; i < ROW; i++) {
vector<string> thisrow;
for (int j = 0; j < COL; j++) {
thisrow.push_back(raw[i][j]);
}
data.push_back(thisrow);
}
}
void printData(vector<vector<string> > data)
{
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < data[i].size(); j++) {
cout << setw(8) << left << data[i][j] << "\t";
}
cout << endl;
}
cout << endl;
}
void swapCol(vector<vector<string> > &data, const int colno, const int x)
{
for (int i = 0; i < x; i++) {
int s = (rand() % (data.size()-1)) + 1;
int r = (rand() % (data.size()-1)) + 1;
// Swap premise number
string temp = data[s][colno];
data[s][colno] = data[r][colno];
data[r][colno] = temp;
}
}
void maskCol(vector<vector<string> > &data, const int colno)
{
for (int i = 1; i < data.size(); i++) {
for (int j = 0; j < data[i][colno].length(); j++) {
if (j != 0) {
if (data[i][colno][j] >= 'a' && data[i][colno][j] <= 'z') data[i][colno][j] = 'z';
else if (data[i][colno][j] >= 'A' && data[i][colno][j] <= 'Z') data[i][colno][j] = 'Z';
else if (data[i][colno][j] >= '0' && data[i][colno][j] <= '9') data[i][colno][j] = '9';
}
}
}
}
return 0;
}
int main(void)
{
srand(time(0));
vector<vector<string> > data;
int operation, input;
string cust;
// Insert data into vector
loadData(data);
while (true) {
cout << "Current data:\n";
printData(data);
cout << "Available Events:\n";
cout << "1. Energy Consumption\n";
cout << "2. Theft Detection\n";
cout << "\nChoose an event: ";
cin >> operation;
if(operation < 2)
{
cout << "\nTelco Provider want to know electric consumption.\n";
switch (operation) {
case 1:
cout << "\nWhich attribute to be swapped?\n";
cout << "\n1. Customer ID\n";
cout << "2. Customer name\n";
cout << "3. Premise number\n";
cout << "4. Street number\n";
cout << "5. Town\n";
cout << "6. Meter number\n";
cout << "7. Power usage\n";
cout << "\nYour input: ";
cin >> input;
swapCol(data, input-1, data.size()/2);
break;}
}
else
{
cout << "\nCST want to identify energy consumption for investigation purpose on electric theft.\n";
switch (operation){
case 2:
cout << "Which attribute to be masked?\n";
cout << "1. Customer ID\n";
cout << "2. Customer name\n";
cout << "3. Premise number\n";
cout << "4. Street number\n";
cout << "5. Town\n";
cout << "6. Meter number\n";
cout << "\nYour input: ";
cin >> input;
maskCol(data, input-1);
break;
case 3:
data.clear();
loadData(data);
break;
default:
cout << "Invalid operation. Please try again.\n\n";
}
}
}
}