//411440281 Lu-Wei-Yi
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct Node *NodePtr;
typedef struct Node{
int pos;
char symb;
NodePtr next;
}Node;
int empty(NodePtr top){
if(top==NULL){
return 1;
}else{
return 0;
}
}
void push(char symb,int pos,NodePtr *top){
NodePtr newNode = (Node*)malloc(sizeof(Node));
newNode->pos=pos;
newNode->symb=symb;
newNode->next=*top;
*top = newNode;
}
NodePtr pop(NodePtr *top){
if(empty(*top)==1){
return NULL;
}else{
NodePtr temp=*top;
*top = (*top)->next;//411440281 Lu-Wei-Yi
return temp;
}
}
void prints(NodePtr *top){
NodePtr current=*top;
while(current!=NULL){
printf("%d ",current->symb);
current = current->next;
}
printf("\n");
}
void random_generate_number(){
srand(time(NULL));
int i;
for(i=1;i<=50;i++){
int number=rand()%10;
printf("%d ",number);
if(i%10==0&&i!=1){
printf("\n");
}
}
}
int main(){
//part1-1
NodePtr top;
int pos=0;
char symb;//411440281 Lu-Wei-Yi
while((symb=getchar())!='\n'){
if(symb=='('||symb=='['){
if(symb=='('){
push(symb,pos,&top);
}else{
push(symb,pos,&top);
}
}else if(symb==')'||symb==']'){
if(!empty(top)){
NodePtr left_parenthesis_stack=pop(&top);
char left_parenthesis=left_parenthesis_stack->symb;
int left_parenthesis_pos=left_parenthesis_stack->pos;
char right_parenthesis=symb;
int right_parenthesis_pos=pos;
if(left_parenthesis=='('&&right_parenthesis==')'){
printf("(%d,%d)",left_parenthesis_pos,right_parenthesis_pos);
printf("\n");
}else if(left_parenthesis=='['&&right_parenthesis==']'){
printf("[%d,%d]",left_parenthesis_pos,right_parenthesis_pos);
printf("\n");
}else if(left_parenthesis=='('&&right_parenthesis==']'){
printf("the right parenthesis ] in %d cannot find the left parenthesis [",right_parenthesis_pos);
printf("\n");
}else if(left_parenthesis=='['&&right_parenthesis==')'){
printf("the right parenthesis ) in %d cannot find the left parenthesis (",right_parenthesis_pos);
printf("\n");
}
}
}//411440281 Lu-Wei-Yi
pos++;
}
while(top!=NULL){
NodePtr left_parenthesis_stack=pop(&top);
char left_parenthesis_symb=left_parenthesis_stack->symb;
int left_parenthesis_pos=left_parenthesis_stack->pos;
if(left_parenthesis_symb=='('){
printf("the left parenthesis ( in %d cannot find the right parenthesis )",left_parenthesis_pos);
printf("\n");
}else if(left_parenthesis_symb=='['){
printf("the left parenthesis [ in %d cannot find the right parenthesis ]",left_parenthesis_pos);
printf("\n");
}
}
} //411440281 Lu-Wei-Yi
To embed this project on your website, copy the following code and paste it into your website's HTML: