iLastRescue
Komplett-PC-Aufrüster(in)
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void ham(int start, int k, string s, int length);
/*
The ham method uses three base cases to print all possible outcomes
Case 1:No more changes left, print the string
Case 2:Cannot make any more changes, end of string
Case 3:Cannot reference a bit past the end of the string
*/
int main()
{
string input;
string bit;
int start = 0;
cout << "Enter a hamming distance" << endl;
cin >> input;
int k = atoi(input.c_str()); //parse the input into an int
cout << "Enter a binary string" << endl;
cin >> bit;
ham(start, k, bit, bit.size());
}
void ham(int start, int k, string bit, int length)
{
if (k == 0) {
cout << bit << endl;
return;
}
if (k>1 && start == length - 1)
return;
if (start>length - 1)
return;
ham(start + 1, k, bit, length); //Called because no change occurs
if (bit.at(start) == '0') { //Called when a change occurs from 0 to 1
bit.replace(start, 1, "1");
ham(start + 1, k - 1, bit, length);
}
else if (bit.at(start) == '1') { //Called when a change occurs from 1 to 0
bit.replace(start, 1, "0");
ham(start + 1, k - 1, bit, length);
}
}
Wie kann ich das Programm anhalten am ende ?
Danke im Voraus
#include <cstdlib>
#include <string>
using namespace std;
void ham(int start, int k, string s, int length);
/*
The ham method uses three base cases to print all possible outcomes
Case 1:No more changes left, print the string
Case 2:Cannot make any more changes, end of string
Case 3:Cannot reference a bit past the end of the string
*/
int main()
{
string input;
string bit;
int start = 0;
cout << "Enter a hamming distance" << endl;
cin >> input;
int k = atoi(input.c_str()); //parse the input into an int
cout << "Enter a binary string" << endl;
cin >> bit;
ham(start, k, bit, bit.size());
}
void ham(int start, int k, string bit, int length)
{
if (k == 0) {
cout << bit << endl;
return;
}
if (k>1 && start == length - 1)
return;
if (start>length - 1)
return;
ham(start + 1, k, bit, length); //Called because no change occurs
if (bit.at(start) == '0') { //Called when a change occurs from 0 to 1
bit.replace(start, 1, "1");
ham(start + 1, k - 1, bit, length);
}
else if (bit.at(start) == '1') { //Called when a change occurs from 1 to 0
bit.replace(start, 1, "0");
ham(start + 1, k - 1, bit, length);
}
}
Wie kann ich das Programm anhalten am ende ?
Danke im Voraus