#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std; // For std::cout and std::endl
// Each rod in the tower has a variable number of rings.
typedef vector<size_t> Rod;
// Displays the tower.
void display(Rod tower[], size_t move=0)
{
if(!move)
cout<<"Initial Position";
else
cout<<"Move "<<move;
cout<<":\n"<<endl;
static const char label[]="ABC";
for(size_t rod=0; rod<3; ++rod)
{
cout<<label[rod]<<":";
for(size_t ring=0; ring<tower[rod].size(); ++ring)
cout<<" "<<tower[rod][ring];
cout<<endl;
}
cout<<endl;
}
int main()
{
cout<<"Tower of Hannoi\n===============\n"<<endl;
size_t rings=1;
do
{
if( rings<1 rings>9 )
cout<<"You must enter a value in the range 1..9.\n"<<endl;
cout<<"Enter the no. of rings (1..9): ";
string input;
getline(cin, input);
stringstream(input)>>rings;
cout<<endl;
}while(rings<1 rings>9);
// Instantiate the three towers.
Rod tower[3];
// Push all the rings onto the 1st tower (largest to smallest)
// and display the initial position.
for(size_t ring=0; ring<rings; ++ring)
tower[0].push_back(rings-ring);
display(tower);
// Determine the minimum no. of moves required
// (2 raised to the power of rings, minus 1).
size_t moves=(1<<rings)-1;
// Determine if the number of rings is odd or even.
size_t odd=rings&1;
// Begin moving rings...
size_t move=0;
while(move<moves)
{
// Determine which 2 of the 3 rods to compare.
size_t rod_index1=0, rod_index2=odd?1:2;
switch(move%3)
{
case(0): rod_index2=odd?2:1; break;
case(2): rod_index1=odd?2:1; break;
}
// Reference the two rods.
Rod& rod1=tower[rod_index1];
Rod& rod2=tower[rod_index2];
// Obtain the top-most ring sizes (0 if rod is empty).
size_t ring1=rod1.size()?*rod1.rbegin():0;
size_t ring2=rod2.size()?*rod2.rbegin():0;
// Invariant: ring1 + ring2 > 0.
// Move the smallest ring to the other rod.
if(!ring2 (ring1 && ring1<ring2))
{
rod1.pop_back();
rod2.push_back(ring1);
}
else
{
rod2.pop_back();
rod1.push_back(ring2);
}
// Display the result of the move.
display(tower, ++move);
}
// Finished!
cout<<"Complete in "<<moves<<" moves!\n"<<endl;
}
The complete history of Visual C++ can be found in the "Visual C++" article in Wikipedia.
Visual C++ 6.0 is more than 16 years old. It is no longer available to buy and it was never available for free. The current version is Visual C++ 2013.
/* hanoi.c */ #include <stdio.h> #include <stdlib.h> static long step; static void Hanoi (int n, int from, int to,int spare) { if (n>1) Hanoi (n-1,from,spare,to); printf ("Step %ld: move #%d %d-->%d\n", ++step, n, from, to); if (n>1) Hanoi (n-1,spare,to,from); } int main (int argc, char **argv) { int n; if (argc==1 (n= atoi(argv[1]))<=0) n= 5; step= 0; Hanoi (n, 1, 2, 3); return 0; }
Your question request a comparison between apples and pears. C++ is a programming language. Microsoft Visual C++ Express is one of many development tools that you can use to create programs using the C++ programming language. Microsoft Visual C++ Express supports native C++ development, and supports development in 'managed C++,' a Microsoft-specific derivative of the C++ language, aimed specifically at the .NET platform.
You can download latest version of Visual Studio from microsoft.com
Microsoft developed Visual C++.
The complete history of Visual C++ can be found in the "Visual C++" article in Wikipedia.
No, M$ Windoze only.
Yes. Microsoft Visual C++ is the correct name, but it is often abbreviated to MSVC++ or just VC++. They are all the same.
Yes.
No. Visual C++ Express is free, but Visual Studio C++ must be bought. The Express edition is a subset of the Studio release.
stack abstract datatype
It is the "Tower of Hanoi" and it is a puzzle. When you can solve the puzzle you can write the program, when you have learned enough to write simple programs. You do want to be able to write computer programs, don't you?
Nevermind, I did it.
Visual C++ 6.0 is more than 16 years old. It is no longer available to buy and it was never available for free. The current version is Visual C++ 2013.
Microsoft Visual C++ or Embarcadero C++ Builder (formally Borland C++ Builder).
Chuck Sphar has written: 'Learn Microsoft Visual C++ 6.0 now' -- subject(s): C++ (Computer program language), Microsoft Visual C++ 'Learn Microsoft Visual C [plus plus] 6.0 now' -- subject(s): C