[code]
// hanoi.h
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
extern int cnt;
extern int x;
int get_data();
void move(int n, char a, char b, char c);
// hanoi.c
#include "hanoi.h"
int cnt = 0;
int x = 0;
int main()
{
int n;
n = get_data();
assert(n>0);
move(n,'A','B','C');
return 0;
}
// get_data.c
#include "hanoi.h"
int get_data()
{
int n;
char c;
printf("insert number");
if(scanf("%d",&n) != 1 || n<1)
{
printf("nonono");
exit(1);
}
printf("\n");
scanf("%c",&c);
return n;
}
// move.c
#include "hanoi.h"
void move(int n, char a, char b, char c)
{
++x;
printf("%d",x);
if(n==1)
{
++cnt;
printf("%5d : %s%d%s%c%s%c.\n", cnt, "move disk", 1, " from tower ", a, " to tower " , c);
}
else
{
move(n-1,a,c,b);
++cnt;
printf("%5d : %s%d%s%c%s%c.\n", cnt, "move disk", n, " from tower ", a, " to tower " , c);
move(n-1,b,a,c);
}
}
[/code]
// hanoi.h
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
extern int cnt;
extern int x;
int get_data();
void move(int n, char a, char b, char c);
// hanoi.c
#include "hanoi.h"
int cnt = 0;
int x = 0;
int main()
{
int n;
n = get_data();
assert(n>0);
move(n,'A','B','C');
return 0;
}
// get_data.c
#include "hanoi.h"
int get_data()
{
int n;
char c;
printf("insert number");
if(scanf("%d",&n) != 1 || n<1)
{
printf("nonono");
exit(1);
}
printf("\n");
scanf("%c",&c);
return n;
}
// move.c
#include "hanoi.h"
void move(int n, char a, char b, char c)
{
++x;
printf("%d",x);
if(n==1)
{
++cnt;
printf("%5d : %s%d%s%c%s%c.\n", cnt, "move disk", 1, " from tower ", a, " to tower " , c);
}
else
{
move(n-1,a,c,b);
++cnt;
printf("%5d : %s%d%s%c%s%c.\n", cnt, "move disk", n, " from tower ", a, " to tower " , c);
move(n-1,b,a,c);
}
}
[/code]
'Native > C' 카테고리의 다른 글
stdio.h (0) | 2013.10.02 |
---|---|
power (0) | 2013.10.02 |
기억영역 클래스(auto,extern,register,static) (0) | 2013.10.02 |
간단한 c 프로그램과 cron으로 mysql 백업을 편하게... (0) | 2013.10.02 |
구조체 기본형 (0) | 2013.10.02 |