|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请教,如果只有一对匹配的符号的时候,它在截图部分是直接跳过的,求助!!!
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
char charz;
struct node * next;
}NODE;
void Input(NODE * head)
{
NODE * p;
p = head;
char ch;
while((ch = getchar())!= '\n')
{
p->next = (NODE * )malloc(sizeof(NODE));
p->next->charz = ch;
p = p->next;
p->next = NULL;
}
}
void Target(NODE * head1, NODE * head2)
{
NODE * p1, * p2;
p1 = head1->next;
p2 = head2;
int t = 0; //t记录字符对数
while(p1 != NULL) //录入所给字符
{
if(p1->charz == '(' || p1->charz == ')' || p1->charz == '[' || p1->charz ==']')
{
t++;
p2->next = (NODE * )malloc(sizeof(NODE));
p2->next->charz = p1->charz;
p2 = p2->next;
p2->next = NULL;
}
p1 = p1->next;
}
if(t%2 != 0)
{
printf("Match false!\n");
return ;
}
else
{
NODE * q1, * q2;
q1 = head2;
while(q1->next != NULL)
{
q2 = q1->next;
while(q2 != NULL)
{
if((q1->next->charz == '(' && q2->next->charz == ')') || (q1->next->charz == '[' && q2->next->charz == ']'))
{
t = t-2;
NODE * n1, * n2;
n1 = q1->next;
n2 = q2->next;
if(q1->next->next->next == NULL)
{
q1->next = NULL;
q2 = NULL;
}
else if(q1->next->next == q2->next)
{
q1->next = q2->next->next;
while(q2->next != NULL)
{
q2 = q2->next;
}
q2 = q2->next;
}
else
{
q1->next = q1->next->next;
if(q2->next->next != NULL)
{
while(q2->next != NULL)
{
q2 = q2->next;
}
q2 = q2->next;
}
else
{
q2 = NULL;
}
}
free(n1);
free(n2);
}
else
{
q2 = q2->next;
}
}
}
}
if(t == 0)
{
printf("Match succeed!\n");
}
else
{
printf("Match false!\n");
}
}
int main()
{
NODE * head1, * head2;
head1 = (NODE * )malloc(sizeof(NODE));
head2 = (NODE * )malloc(sizeof(NODE));
Input(head1);
//Print(head1);
Target(head1,head2);
return 0;
}
#include<stdio.h>
int Target(void)
{
char s[100], c;
int i;
i = 0;
while ((c = getchar()) != '\n')
{
switch (c)
{
case '(':
case '[':
s[i] = c;
i++;
break;
case ')':
if (i > 0)
{
i--;
if (s[i] != '(')
{
return 0;
}
}
else
{
return 0;
}
break;
case ']':
if (i > 0)
{
i--;
if (s[i] != '[')
{
return 0;
}
}
else
{
return 0;
}
break;
}
}
if (i == 0)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
if (Target())
{
printf("匹配成功!\n");
}
else
{
printf("匹配失败!\n");
}
return 0;
}
这个重新写的,你琢磨着改一下吧
|
-
|