Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // h.w.8
- //
- // Created by Темирхан Мамаев on 05/12/2019.
- // Copyright © 2019 Темирхан Мамаев. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <limits.h>
- char *min_alpha(char **s, int size_arr){
- char *p_min_alpha = NULL;
- int min_count = INT_MAX;
- for (char **ps = s; ps < s + size_arr; ps++){
- int count = 0;
- for (char *sub = *ps; sub < *ps + strlen(*ps); sub++){
- if (isalpha(*sub)) {
- count++;
- }
- }
- if (count < min_count) {
- p_min_alpha = *ps;
- min_count = count;
- }
- }
- return p_min_alpha;
- }
- char **podstr_split(char **s, int size_arr, char fs, char ls){
- int n = 0;
- char **podstr_arr = malloc(sizeof(char*) * 5);
- for(char **str = s; str < s + size_arr; str++){
- char *q = *str, *p = *str;
- while((q = strchr(q, fs)) && (p = strchr(q, ls)))
- if (strchr(q, '*')) {
- *(podstr_arr + n) = malloc(sizeof(char) * (p - q));
- strncpy(*(podstr_arr + n), (q + 1), (p - q - 1));
- n++;
- q++;
- // *(*(podstr_arr + n) + (p - q + 1)) = '\0';
- }
- if (n % 5 == 0) {
- podstr_arr = realloc(podstr_arr, sizeof(char*) * (n + 5));
- }
- }
- *(podstr_arr + n) = NULL;
- return podstr_arr;
- }
- int podstr_arr(char **s, char **podstr, int size_arr){
- int n = 1;
- char **pod = (char**)malloc(sizeof(char*) * 5);
- for (char **str = s; str < s + size_arr; str++) {
- for (pod = podstr; pod < podstr + n; pod++) {
- char *q = strchr(*str, '('), *p = strchr(*str, ')'), *star = *str;
- while (q && p) {
- q = strchr(q, '(');
- p = strchr(q, ')');
- star = strchr(q, '*');
- if (p && q && p > q && star < p){
- *pod = malloc(sizeof(char) * (p - q));
- strncpy(*pod, p + 1, (p - q - 1));
- **(pod + (p - q)) = '\0';
- n++;
- }
- q++; p++; star++;
- if (n % 5 == 0) {
- pod = realloc(pod, sizeof(char*) * (n + 5));
- }
- }
- }
- }
- return n;
- }
- void vstav_char(char *s, char ps, char h){
- char *p = s, *q;
- while((p = strchr(p, h))){
- q = strchr(p, '\0');
- while(q > p){
- *(q + 1) = *q;
- q--;
- }
- *(p + 1) = ps;
- p++;
- }
- }
- int main(int argc, const char * argv[]) {
- FILE *f;
- char buf[80], **p = NULL, **str = NULL, **s, *p_max, **podstr;
- int n = 0;
- if (!(f = fopen("data.txt", "rb"))) {
- printf("file don't exist");
- return 1;
- }
- else{
- while (!feof(f)) {
- str = (char**)malloc(sizeof(char*) * 5);
- p = str;
- while (!feof(f)) {
- fgets(buf, 79, f);
- *p = (char*)malloc((strlen(buf) + 1) * sizeof(char));
- strcpy(*p, buf);
- p++;
- n++;
- if (n % 5 == 0) {
- str = (char**)realloc(str, sizeof(char*) * (n + 5));
- p = str + n;
- }
- }
- }
- }
- podstr = podstr_split(str, n, '(', ')');
- p_max = min_alpha(str, n);
- puts(p_max);
- for(s = str; s < str + n; s++){
- // vstav_char(*s, '0', '1');
- puts(*s);
- free(*s);
- }
- printf("\n__________________________________\n");
- for(s = podstr; *s; s++){
- puts(*s);
- free(*s);
- }
- free(podstr);
- free(str);
- fclose(f);
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement