Skip to content

Instantly share code, notes, and snippets.

@a60814billy
Created November 3, 2013 09:27
Show Gist options
  • Save a60814billy/7288019 to your computer and use it in GitHub Desktop.
Save a60814billy/7288019 to your computer and use it in GitHub Desktop.
#include <stack>
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
stack<string> addBit(stack<string> ori);
void generating(stack<string> ori, stack<string> *n);
int main(int argc, char *argv[])
{
stack<string> grayCode;
int codeBit;
// initial base grayCode
grayCode.push("1");
grayCode.push("0");
cout << "Enter an integer n: ";
cin >> codeBit;
for(int i=1;i<codeBit;i++) grayCode = addBit(grayCode);
cout << "The list of " << codeBit << "-bit Gray code is: ";
while(!grayCode.empty()){
cout << grayCode.top() << " ";
grayCode.pop();
}
cout << endl;
return 0;
}
stack<string> addBit(stack<string> ori){
stack<string> n;
generating(ori , &n);
return n;
}
void generating(stack<string> ori, stack<string> *n){
if(ori.empty()) return;
string top = ori.top();
ori.pop();
n->push("1" + top);
generating(ori, n);
n->push("0" + top);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment