Facebook
From Que, 5 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 226
  1. -----------------------------------------------------------------------------------------------------
  2. ---------------------------------------MY_Structs.h---------------------------------------------------
  3. #pragma once
  4.  
  5. struct MY_CAR
  6. {
  7.         int year;        //rok produkcji
  8.         char nazwa[64];  //nazwa samochodu
  9. };
  10.  
  11. MY_CAR * fun_create_array(int dim);
  12. void fun_fill_array(MY_CAR *tab);
  13. MY_CAR * fun_add(int num, MY_CAR *t1, MY_CAR *t2);
  14. void fun_print_tab(MY_CAR *t, const char *title);
  15. void fun_free(MY_CAR **t);
  16.  
  17.  
  18.  
  19. ---------------------------------------------------------------------------------------------------
  20. ---------------------------------------------------.cpp---------------------------------------------
  21. // NaukaNaFialko111Kol.cpp : Defines the entry point for the console application.
  22. //
  23.  
  24. #include "stdafx.h"
  25. #include<stdio.h>
  26. #include<stdlib.h>
  27. #include<malloc.h>
  28. #include"MY_Structs.h"
  29.  
  30. #pragma warning (disable:4996)
  31.  
  32. int main()
  33. {
  34.         //printf("char ma %d\n", sizeof(char));
  35.         //printf("int ma %d\n", sizeof(int));
  36.         //printf("double ma %d\n", sizeof(double));
  37.         int dim_1, dim_2;
  38.         printf("podaj rozmiar pierwszej tablicy aut\n");
  39.         scanf("%d", &dim_1);
  40.         printf("podaj rozmiar drugiej tablicy aut\n");
  41.         scanf("%d", &dim_2);
  42.         MY_CAR* tab_1 = fun_create_array(dim_1);
  43.         MY_CAR* tab_2 = fun_create_array(dim_2);
  44.         fun_fill_array(tab_1);
  45.         fun_fill_array(tab_2);
  46.         fun_print_tab(tab_1, "numer 1");
  47.         fun_print_tab(tab_2, "numer 2");
  48.         fun_add(2, tab_1, tab_2);
  49.         fun_print_tab(tab_1, "numer 1");
  50.  
  51.     return 0;
  52. }
  53.  
  54. MY_CAR * fun_create_array(int dim)
  55. {
  56.         MY_CAR* Tmp;
  57.         Tmp = (MY_CAR*)malloc(dim * sizeof(MY_CAR));
  58.         if (!Tmp)
  59.         {
  60.                 exit(1);
  61.         }
  62.         for (size_t i = 0; i < dim; i++)
  63.         {
  64.                 memset(Tmp[i].nazwa, 0, 64);
  65.         }
  66.         return Tmp;
  67. }
  68. void fun_fill_array(MY_CAR *tab)
  69. {
  70.         char symb = 'a';
  71.         int roz = _msize(tab) / sizeof(MY_CAR);
  72.         for (int i = 0; i < roz; i++)
  73.         {
  74.                 tab[i].nazwa[0] = symb;
  75.                 tab[i].year = (1+i) * 100;
  76.                 symb = symb + 1;
  77.         }
  78.  
  79. }
  80. MY_CAR * fun_add(int num, MY_CAR *t1, MY_CAR *t2)
  81. {
  82.         int roz_1 = _msize(t1) / sizeof(MY_CAR);
  83.         int roz_2 = _msize(t2) / sizeof(MY_CAR);
  84.         if (1)
  85.         {
  86.                 t1 = (MY_CAR*)realloc(t1, ((roz_1 + roz_2) * sizeof(MY_CAR)));
  87.         }
  88.         memmove(t1 + roz_2 + num, t1 + num, (roz_1 - num) * sizeof(MY_CAR));
  89.         memcpy(t1 + num, t2, roz_2 * sizeof(MY_CAR));
  90.         /*memmove(t1+roz_2, t1 , roz_1 * sizeof(MY_CAR));
  91.         memcpy(t1, t2, roz_2 * sizeof(MY_CAR));*/
  92.         return t1;
  93. }
  94. void fun_print_tab(MY_CAR *t, const char *title)
  95. {
  96.         int roz = _msize(t) / sizeof(MY_CAR);
  97.         for (int i = 0; i < roz; i++)
  98.         {
  99.                 printf("auto numer %d\n", i);
  100.                 printf("rok auta %d\n", t[i].year);
  101.                 printf("nazwa auta  %s\n", t[i].nazwa);
  102.         }
  103.  
  104. }
  105. void fun_free(MY_CAR **t)
  106. {
  107.  
  108. }
  109.  
  110.