Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void spoji_saberi(int argc, char *argv[], char* (*dodaj)(char*, char*), int (*saberi)(int, int));
  6.  
  7. char *dodaj(char*, char*);
  8.  
  9. int saberi(int a, int b);
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     spoji_saberi(argc, argv, dodaj, saberi);
  14. }
  15.  
  16. void spoji_saberi(int argc, char *argv[], char* (*dodaj)(char*, char*), int (*saberi)(int, int))
  17. {
  18.     int wt = 0;
  19.     int sum = 0;
  20.     char *ans = NULL;
  21.     int un = 0, dun = 0;
  22.     int cnt = 0;
  23.     for (int i = 1; i < argc; ++i)
  24.     {
  25.         if (!strcmp(argv[i], "-s"))
  26.         {
  27.             if (wt == 2)
  28.             {
  29.                 printf("Suma je %d.", sum);
  30.             }
  31.             wt = 1;
  32.         }
  33.         else if (!strcmp(argv[i], "-b"))
  34.         {
  35.             if (wt == 1)
  36.             {
  37.                 printf("String je '%s'.", ans);
  38.             }
  39.             wt = 2;
  40.         }
  41.  
  42.         if (wt == 1 && strcmp(argv[i], "-s"))
  43.         {
  44.             ans = (*dodaj)(ans, argv[i]);
  45.         }
  46.         else if (wt == 2 && strcmp(argv[i], "-b"));
  47.         {
  48.             sum = (*saberi)(sum, atoi(argv[i]));
  49.         }
  50.     }
  51.  
  52.     if (wt == 1)
  53.         printf("String je '%s'.", ans);
  54.     else if (wt == 2)
  55.         printf("Suma je %d.", sum);
  56.  
  57.     free(ans);
  58. }
  59.  
  60. int saberi(int a, int b)
  61. {
  62.     return a + b;
  63. }
  64.  
  65. char *dodaj(char *a, char *b)
  66. {
  67.     char *f;
  68.     if (a != NULL)
  69.         f = calloc((strlen(a) + strlen(b) + 1), sizeof(char));
  70.     else
  71.         f = calloc((strlen(b) + 1), sizeof(char));
  72.     if (a != NULL)
  73.     {
  74.         strcpy(f, a);
  75.         free(a);
  76.     }
  77.     strcat(f, b);
  78.  
  79.     return f;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement