📄 s1.c
字号:
#include <stdio.h>
typedef enum[atom,list]elemtag;
typedef struct glnode{
elemtag tag;
union {
char data;
struct{struct glnode *hp,*tp;}ptr;
}
}*glist;
char gl_elem(glist ls);
void createlist(glist *ls)
{
getachar(ch);
if(ch!='c')return 0;
else return crtltbu(*ls);
}
void getachar(char *ch)
void crtltbu(glist *ls)
{
getachar(ch);
if(ch=='')
{ls=NULL;
getachar(ch);
if(ch!='(') return 0;
}
else{
ls=(glist)malloc(sizeof(glnode));
ls->tag=list;
if(isalpha(ch))
{
ls->hp=(glist)malloc(sizeof(glnode));
p=s->hp;p->tag=atom;
p->data=ch;
}
else if(ch=='c')crtltbu(ls->hp);
else return 0;
getachar(ch);
if(ch==')')ls->tp=NULL;
else if(ch==',')crtltbu(ls->tp);
else return 0;
}
}
void traverse_gl(glist ls)
{
if(!ls)printf("()");
else
if(ls->tag==atom)
printf(gl_elem(ls));
else{
printf('(')
while(ls!=NULL)
{
traverse_gl(gethead(ls));
ls=gettail(ls);
if(ls)printf(',')
}
printf(',');
}
}
void main()
{
initialization();
do{
readcommand(cmd);
interpret(cmd);
}while(cm!='q'&&cmd!='Q');
}
void initialization()
{
clrscr();
initglist(hlink);
}
void readcommand(char cmd)
{
do{
cmd=getchar();
}
while(cmd!='c'&&cmd!='C'&&cmd!='d'&&cm!='D'&&cm!='q'&&cm!='Q');
}
void interpret(char cmd)
{
switch(cmd)
{
case 'c','C':
if(!(glistempty(hlink)))
destroyglist(hlink);
createglist(hlink);
break;
case 'd','D':
if(glistempty(hlink))
else decompose(hlink,str);
break;
case'q','Q':
}
}
void decompose(glist hlink,string str)
{
n=strlen(str);p=hlink;
for(i=0;i<n;i++)
{
if(!p)
printf("The list is a emptylist!");
else {
if(str[i]=='h')
{
p=gethead(p);
printf("Thelisthead is:");
}
else
if(str[i]=='t')
{
p=gettail(p);
printf("The listtail is:");
}
traverse_gl(lp);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -