Commit 13dbdb7b authored by Karine's avatar Karine

exercise_06_42, version1

parent bcc2b70a
progname = exercise_06_42
CC = g++
CFLAGS = -Wall -Wextra -Werror -g3
$(progname): $(progname).o | .gitignore
$(CC) $(CFLAGS) $^ -o $@
%.ii: %.cpp
$(CC) -E $(CFLAGS) $< -o $@
%.s: %.ii
$(CC) -S $(CFLAGS) $< -o $@
%.o: %.s
$(CC) -c $(CFLAGS) $< -o $@
.gitignore:
echo $(progname) > $@
clean:
rm -f *.ii *.s *.o *.out $(progname) .gitignore
test: $(progname)
./$(progname) < $@.in > $@.out
diff $@.out $@.expected && echo PASSED || echo FAILED
.PRECIOUS: $(progname).ii $(progname).s
.SECONDARY: $(progname).ii $(progname).s
#include <iostream>
#include <cassert>
void
hanoi(const int count, const int start, const int temp, const int end)
{
assert(count > 0);
if (1 == count) {
std::cout << start << "-->" << end << std::endl;
} else {
hanoi(count -1, start, end, temp);
hanoi(1, start, temp, end);
hanoi(count - 1, temp, start, end);
}
}
int
main()
{
int count;
std::cout << "Enter the count of disks: ";
std::cin >> count;
if (count <= 0) {
std::cerr << "Error 1: Invalid count." << std::endl;
return 1;
}
int start;
int end;
int temp;
std::cout << "Enter the numbers for start, temporary, end pegs (from 1 to 3): ";
std::cin >> start >> temp >> end;
if ((start < 1 && start > 3) || (temp < 1 && temp > 3) || (end < 1 && end > 3)) {
std::cerr << "Error 2: Invalid value for pegs." << std::endl;
return 2;
}
hanoi(count, start, temp, end);
return 0;
}
Enter the count of disks: Enter the numbers for start, temporary, end pegs (from 1 to 3): 1-->2
1-->3
2-->3
1-->2
3-->1
3-->2
1-->2
1-->3
2-->3
2-->1
3-->1
2-->3
1-->2
1-->3
2-->3
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment