P66第三题
#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
_Bool Judge(char str[],int n)
{
int k=0,t=0;
for(int i=0;i<n;i++)
{
switch(str[i])
{
case 'I':
k++;
break;
case 'O':
t++;
if(t>k) return false;
break;
default:
break;
}
}
if(k!=t) return false;
return true;
}
int main()
{
char str[6]={'I','O','I','I','O','O'};
if(Judge(str,6))
{
printf("合法\n");
}else
{
printf("不合法\n");
}
return 0;
}
第4题
#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
#define MaxSize 50
typedef struct
{
char data[MaxSize];
int top;
}SqStack;
void IninStack(SqStack *S)
{
S->top=-1;
}
_Bool StackEmpty(SqStack *S)
{
if(S->top==-1)
return true;
else
return false;
}
_Bool Push(SqStack *S,char x)
{
if(S->top==MaxSize-1)
return false;
S->data[++S->top]= x;
return true;
}
_Bool Pop(SqStack *S,char * x)
{
if(S->top==-1)
return false;
*x=S->data[S->top];
S->top--;
return true;
}
_Bool Top(SqStack *S,char * x)
{
if(S->top==-1)
return false;
*x=S->data[S->top];
return true;
}
typedef struct
{
char data;
struct LNode *next;
}LNode,*LinkList;
LNode *GetElem (LinkList L,int i)
{
int j=1;
LNode *p=L->next;
if(i==0)
return L;
if(i<0)
return NULL;
while(p&&j<i)
{
p=p->next;
j++;
}
return p;
}
_Bool LinkInsert(LinkList L,int i,char x)
{
LNode *l=GetElem(L,i-1);
LNode *in = (LNode *)malloc(sizeof(LNode));
if(in==NULL)
return false;
in->data=x;
in->next=l->next;
l->next=in;
return true;
}
int GetLength(LinkList L)
{
int i=0;
LNode * p = L->next;
if(p==NULL)
i=0;
while(p)
{
i++;
p=p->next;
}
return i;
}
int main()
{
LinkList L=(LNode *)malloc(sizeof(LNode));
L->next=NULL;;
SqStack S;
IninStack(&S);
LinkInsert(L,1,'x');
LinkInsert(L,2,'z');
LinkInsert(L,3,'y');
LinkInsert(L,4,'z');
LinkInsert(L,5,'x');
int length = GetLength(L);
printf("%d\n",length);
int i=0,j=0;
for( i=0;i<length/2;i++)
{
char temp=GetElem(L,i+1)->data;
printf("%c\n",temp);
Push(&S,temp);
}
LNode *ll;
for(j=i;j<length;j++)
{
ll=GetElem(L,j+1);
char temp=ll->data;
char tt;
Pop(&S,&tt);
if(temp!=tt)
{
printf("不对称\n");
break;
}
printf("对称\n");
}
return 0;
}