課題の答え(ほぼ完成版)
C言語 ソースコード
--------------------------------------回答例1(発展課題付き)----------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int x;
int y;
int dx;
int dy;
int n=0;
char ch_h[100];
int h=0;
int elase=0;/* 足跡を残すか判定用 Eを打つことによってon offの切替え*/
void put_mark(int x, int y){
move(x-2,y-2); cont(x+2,y+2);
move(x+2,y-2); cont(x-2,y+2);
move(x,y);
}
void forward(){
x=x+dx;
y=y+dy;
if(elase==0){
cont(x,y);
put_mark(x,y);
}else if(elase==1){
move(x,y);
}
}
void left_turn(){
int tmp;
tmp=dx;
dx=-dy;
dy=tmp;
}
void right_turn(){
int tmp;
tmp=dx;
dx=dy;
dy=-tmp;
}
void analysis(ch){
int i;
if(n==0)n=1;
switch(ch){
case 'F':
for(i=0;i<n;i++)
forward();
break;
case 'R':
for(i=0;i<n;i++)
right_turn();
break;
case 'L':
for(i=0;i<n;i++)
left_turn();
break;
case 'E':
for(i=0;i<n;i++){
if(elase==0)elase=1;
else elase=0;
}
break;
}
n=0;
}
void turtle_h(){
char ch,ch2[100];
int i=0,j,k;
for(k=0;k<h;k++){
if(isalpha(ch_h[k])){
n=atoi(ch2);
analysis(ch_h[k]);
for(j=0;j<=i;j++)
ch2[j]='\0';/* =0でも良いのか? それともi=0だけでも?*/
i=0;
}
else if(isdigit(ch_h[k])){ch2[i]=ch_h[k]; i++;}
}
}
void hozon(){
char ch,ch2[100];
int i=0,j;
for(i=0;i<h;i++)ch_h[i]='\0';
h=0;
while((ch=getchar())!='#'){
ch_h[h]=ch;
if(isalpha(ch)){
n=atoi(ch2);
analysis(ch);
for(j=0;j<=i;j++)
ch2[j]='\0';/* =0でも良いのか? それともi=0だけでも?*/
i=0;
}
if(isdigit(ch)){ch2[i]=ch; i++;}
h++;
}
}
void turtle(){
char ch,ch2[100];
int i=0,j;
while((ch=getchar())!=EOF){
if(isalpha(ch)){
n=atoi(ch2);
analysis(ch);
for(j=0;j<=i;j++)
ch2[j]='\0';/* =0でも良いのか? それともi=0だけでも?*/
i=0;
}
else if(isdigit(ch)){ch2[i]=ch; i++;}
else if(ch=='@')hozon();
else if(ch=='!')turtle_h();
}
}
/* */
int main(){
openpl();
space(-300,-300,300,300);
x=0;
y=0;
dx=0;
dy=10;
put_mark(x,y);
turtle();
closepl();
return 0;
}
---------------------------------------解答例2(発展課題なしatoi未使用)------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int x;
int y;
int dx;
int dy;
int n=0;
void put_mark(int x, int y){
move(x-2,y-2); cont(x+2,y+2);
move(x+2,y-2); cont(x-2,y+2);
move(x,y);
}
/* */
void forward(){
x=x+dx;
y=y+dy;
cont(x,y);
put_mark(x,y);
}
void left_turn(){
int tmp;
tmp=dx;
dx=-dy;
dy=tmp;
}
void right_turn(){
int tmp;
tmp=dx;
dx=dy;
dy=-tmp;
}
void analysis(ch){
int i;
if(n==0)n=1;
switch(ch){
case 'F':
for(i=0;i<n;i++)
forward();
break;
case 'R':
for(i=0;i<n;i++)
right_turn();
break;
case 'L':
for(i=0;i<n;i++)
left_turn();
}
n=0;
}
void henkan(ch){
switch(ch){
case '1':
n=n*10+1;
break;
case '2':
n=n*10+2;
break;
case '3':
n=n*10+3;
break;
case '4':
n=n*10+4;
break;
case '5':
n=n*10+5;
break;
case '6':
n=n*10+6;
break;
case '7':
n=n*10+7;
break;
case '8':
n=n*10+8;
break;
case '9':
n=n*10+9;
break;
case '0':
n=n*10;
break;
}
}
void turtle(){
char ch;
while((ch=getchar())!=EOF){
if(isalpha(ch))analysis(ch);
if(isdigit(ch))henkan(ch);
}
}
/* */
int main(){
openpl();
space(-300,-300,300,300);
x=0;
y=0;
dx=0;
dy=10;
put_mark(x,y);
turtle();
closepl();
return 0;
}
最終更新:2007年12月12日 12:28