Advertisement
desdemona

c concat

Oct 13th, 2015
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. // eh.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <random>
  6. #include <time.h>
  7.  
  8. int _tmain(int argc, _TCHAR* argv[])
  9. {
  10.     clock_t begin, end;
  11.     clock_t petla_begin, petla_end;
  12.  
  13.     double time_spent = 0;
  14.     double total_time = 0;
  15.     //total_time += time_spent;
  16.  
  17.     bool a = true;
  18.     int len_a = 0;
  19.     int len_b = 0;
  20.     int n = 0;
  21.  
  22.     char str_a[1000];
  23.     char str_b[1000];
  24.  
  25.     char* str_c;
  26.  
  27.     int ch, lastch = '\0';
  28.     begin = clock();
  29.     while ((ch = getchar()) != EOF)
  30.     {
  31.         if (ch == 10)
  32.         {
  33.             a = !a;
  34.            
  35.             str_c = (char *)malloc(sizeof(char) * (len_a + len_b + 3));
  36.             //strcat(str_c, str_a);
  37.             strncpy(str_c, str_a, len_a);
  38.             //strcat(str_c, str_b);
  39.             strncpy(str_c + len_a, str_b, len_b);
  40.  
  41.             //str_c[len_a + len_b] = '\0';
  42.                        
  43.             n++;
  44.  
  45.             free(str_c);
  46.             len_a = 0;
  47.             len_b = 0;
  48.         }
  49.         else if (ch == 32)
  50.         {
  51.             a = !a;
  52.         }
  53.         else
  54.         {
  55.             if (a)
  56.             {
  57.                 str_a[len_a] = ch;
  58.                 len_a++;
  59.             }
  60.             else
  61.             {
  62.                 str_b[len_b] = ch;
  63.                 len_b++;
  64.             }
  65.         }
  66.  
  67.     }
  68.  
  69.     end = clock();
  70.  
  71.     petla_begin = clock();
  72.     long ij = 0;
  73.     for (int i = 0; i < n; i++)
  74.     {
  75.         for (int j = 0; j < 1000; j++)
  76.         {
  77.             ij++;
  78.         }
  79.     }
  80.     petla_end = clock();
  81.     time_spent = (double)(end - begin) - (double)(petla_end-petla_begin);
  82.     time_spent = time_spent /  CLOCKS_PER_SEC;
  83.     printf("total time: %.50f\n", time_spent);
  84.     printf("avg: %.50f\n", time_spent / n);
  85.    
  86.     return (int)ij;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement