Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void spoji_saberi(int argc, char *argv[], char* (*dodaj)(char*, char*), int (*saberi)(int, int));
- char *dodaj(char*, char*);
- int saberi(int a, int b);
- int main(int argc, char *argv[])
- {
- spoji_saberi(argc, argv, dodaj, saberi);
- }
- void spoji_saberi(int argc, char *argv[], char* (*dodaj)(char*, char*), int (*saberi)(int, int))
- {
- int wt = 0;
- int sum = 0;
- char *ans = NULL;
- int un = 0, dun = 0;
- int cnt = 0;
- for (int i = 1; i < argc; ++i)
- {
- if (!strcmp(argv[i], "-s"))
- {
- if (wt == 2)
- {
- printf("Suma je %d.", sum);
- }
- wt = 1;
- }
- else if (!strcmp(argv[i], "-b"))
- {
- if (wt == 1)
- {
- printf("String je '%s'.", ans);
- }
- wt = 2;
- }
- if (wt == 1 && strcmp(argv[i], "-s"))
- {
- ans = (*dodaj)(ans, argv[i]);
- }
- else if (wt == 2 && strcmp(argv[i], "-b"));
- {
- sum = (*saberi)(sum, atoi(argv[i]));
- }
- }
- if (wt == 1)
- printf("String je '%s'.", ans);
- else if (wt == 2)
- printf("Suma je %d.", sum);
- free(ans);
- }
- int saberi(int a, int b)
- {
- return a + b;
- }
- char *dodaj(char *a, char *b)
- {
- char *f;
- if (a != NULL)
- f = calloc((strlen(a) + strlen(b) + 1), sizeof(char));
- else
- f = calloc((strlen(b) + 1), sizeof(char));
- if (a != NULL)
- {
- strcpy(f, a);
- free(a);
- }
- strcat(f, b);
- return f;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement