Eukleides project

from http://d.hatena.ne.jp/u5_h/

LIFOリスト(動作わざと不可、関数の参考程度に)

/*list stack(LIFO) buffer*/
#include

struct cell{
double data;
struct cell *next;
};

struct cell *stack='\0';
/*リストend関数*/
void end(void)
{
stack ='\0';
exit(1);
}
/*リストpush関数*/
void push(double x)
{
struct cell *p;
p=(struct cell *)malloc(sizeof(struct cell));
p->data=x;
p->next=stack;
stack=p;
}
/*リストpop関数*/
double pop(void)
{
struct cell *p;
double a;
if(stack=='\0')
{
printf("stack is empity.\n");
exit(1);
}
a=stack->data;
p=stack;
stack=stack->next;
free((void*)p);
return a;
}
/*操作評価関数*/
int my_str_comp(char str1,char str2)
{
int j;

for(j=0;str1[j] == str2[j];j++)
{
if(str1[j] == '\0')
{
return 0;
}
}
return str1[j] - str2[j];
}
int main(void)
{
double a;
char b[3];
struct cell *p;
printf("操作を選択してください (push/pop/end)==>");
scanf("%s",b);
while(my_str_comp(b,"push")==0)
{
printf("pushする値を入力してください==>\n");
scanf("%lf",&a);
push(a);
for(p=stack;p!='\0';p=p->next)
printf("->%f",p->data);
printf("\n");
printf("操作を選択して下さい (push/pop/end)==>");
scanf("%s",b);

while(my_str_comp(b,"pop")==0)
{
a=pop();
printf("popされたデータは%fです.\n",a);
for(p=stack;p!='\0';p=p->next)
printf("->%f",p->data);
printf("\n");

printf("操作を選択して下さい(push/pop/end)==>");
scanf("%s",b);

while(my_str_comp(b,"end")==0)
{
end();
}
}

}
return 0;
}