-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
161 lines (154 loc) · 3.23 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stack
{
char bracket;
struct stack *next;
};
void push(struct stack **head, char ch)
{
struct stack *current=*head;
current=(struct stack*)calloc(1,sizeof(struct stack));
current->bracket=ch;
current->next=(*head);
*head=current;
}
void pop(struct stack **head)
{
struct stack *current;
current=*head;
*head=current->next;
free(current);
}
int control(char ch,char ch_next)
{
static struct stack *head=NULL;
int i;
if(ch=='(' || ch=='{' || ch=='[')
{
push(&head,ch);
return 1;
}
if(ch==')' || ch=='}' || ch==']')
{
if(head==0)
{
puts("INCORRECT ARRENGEMENT");
return 2;
}
i=0;
switch(ch)
{
case ')': if(head->bracket=='(') i=1; break;
case ']': if(head->bracket=='[') i=1; break;
case '}': if(head->bracket=='{') i=1; break;
}
if(i)
pop(&head);
else
{
while(head!=NULL)
pop(&head);
puts("INCORRECT ARRENGEMENT");
return 2;
}
}
if(ch_next=='\0' && !head)
{
puts("CORRECT ARRANGEMENT");
return 1;
}
else if(ch_next=='\0' && head!=NULL)
{
while(head!=NULL)
pop(&head);
puts("INCORRECT ARRANGEMENT");
return 2;
}
return 0;
}
char* check_input(int length)
{
char *string=(char*)calloc(length,sizeof(char));
do {
fgets(string,length, stdin);
if(strlen(string)==length-1)
while(getchar()!='\n');
} while (string[0] == '\n');
return string;
}
void show_help(void)
{
puts("enter strings");
}
int main(int argc, char *argv[])
{
if(argc>1)
{
if(strcmp(argv[1],"-h"))
show_help();
}
int i,j,length,number;
char **strings,string[5],ch;
do{
while(1)
{
puts("Enter the number of strings");
fgets(string,5,stdin);
if(!(number=atoi(string)))
{
while(getchar()!='\n');
puts("error");
continue;
}
else break;
}
while(1)
{
puts("Enter the length of one string");
fgets(string,5,stdin);
if((length=atoi(string))>200 || !length)
{
while(getchar()!='\n');
puts("error");
continue;
}
else break;
}
if(!(strings=(char**)calloc(number,sizeof(char*))))
puts("error");
for(i=0;i<number;)
{
if(!(strings[i]=(char*)calloc(length+1,sizeof(char))))
{
puts("error");
for(j=0;j<number;j++)
{
free(strings[j]);
free(strings);
strings=NULL;
}
}
else i++;
}
}while(!strings);
for(i=0;i<number;i++)
{
printf("\nenter %d string ",i+1);
strings[i]=check_input(length);
}
for(i=0;i<number;i++)
puts(strings[i]);
for(i=0;i<number;i++)
{
printf("\nstring %d:\n",i+1);
for(j=0;strings[i][j]!='\0';)
{
if(control(ch=strings[i][j],strings[i][j+1])==2)
break;
j++;
}
}
return 0;
}