Advertisement
antisa

LV7

Apr 11th, 2023
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.37 KB | None | 0 0
  1. //PRVI
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define MAX_STUDENTS 100
  7.  
  8. void write_students_to_file(char* filename, char** names, char** surnames, int count) {
  9.     FILE* file = fopen(filename, "w");
  10.     if (file == NULL) {
  11.         printf("Failed to open file %s for writing.\n", filename);
  12.         return;
  13.     }
  14.  
  15.     for (int i = 0; i < count; i++) {
  16.         fprintf(file, "Student broj: %d.\tIme: %s\t Prezime: %s\n", i + 1, names[i], surnames[i]);
  17.     }
  18.  
  19.     fclose(file);
  20. }
  21.  
  22. int main() {
  23.     char* filename = "dat.txt";
  24.     FILE* file = fopen(filename, "r");
  25.     if (file == NULL) {
  26.         printf("Failed to open file %s for reading.\n", filename);
  27.         return 1;
  28.     }
  29.  
  30.     int num_students;
  31.     if (fscanf(file, "%d", &num_students) != 1) {
  32.         printf("Failed to read number of students from file %s.\n", filename);
  33.         fclose(file);
  34.         return 1;
  35.     }
  36.  
  37.     fclose(file);
  38.  
  39.     if (num_students < 1 || num_students > MAX_STUDENTS) {
  40.         printf("Invalid number of students: %d (must be between 1 and %d).\n", num_students, MAX_STUDENTS);
  41.         return 1;
  42.     }
  43.  
  44.     char* names[MAX_STUDENTS];
  45.     char* surnames[MAX_STUDENTS];
  46.  
  47.     for (int i = 0; i < num_students; i++) {
  48.         char name[100], surname[100];
  49.         printf("Unesite ime i prezime za studenta broj %d:\n", i + 1);
  50.         scanf("%s %s", name, surname);
  51.  
  52.         names[i] = malloc(strlen(name) + 1);
  53.         surnames[i] = malloc(strlen(surname) + 1);
  54.  
  55.         if (names[i] == NULL || surnames[i] == NULL) {
  56.             printf("Failed to allocate memory for student names.\n");
  57.             return 1;
  58.         }
  59.  
  60.         strcpy(names[i], name);
  61.         strcpy(surnames[i], surname);
  62.     }
  63.  
  64.     write_students_to_file("studenti.txt", names, surnames, num_students);
  65.  
  66.     for (int i = 0; i < num_students; i++) {
  67.         free(names[i]);
  68.         free(surnames[i]);
  69.     }
  70.  
  71.     return 0;
  72. }
  73.  
  74. //DRUGI
  75.  
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <time.h>
  79.  
  80. // Funkcija za ispis matrice
  81. void print_matrix(double **mat, int rows, int cols) {
  82.     printf("Matrica:\n");
  83.     for (int i = 0; i < rows; i++) {
  84.         for (int j = 0; j < cols; j++) {
  85.             printf("%10.2f", mat[i][j]);
  86.         }
  87.         printf("\n");
  88.     }
  89. }
  90.  
  91. int main() {
  92.     // Ucitavanje brojeva iz datoteke
  93.     FILE *in_file = fopen("C:\\path\\to\\in.txt", "r");
  94.     int n, m;
  95.     fscanf(in_file, "%d %d", &n, &m);
  96.     fclose(in_file);
  97.  
  98.     // Alociranje memorije za matricu
  99.     double **mat = (double **) malloc(n * sizeof(double *));
  100.     for (int i = 0; i < n; i++) {
  101.         mat[i] = (double *) malloc(m * sizeof(double));
  102.     }
  103.  
  104.     // Popunjavanje matrice pseudo-slucajnim brojevima
  105.     srand(time(NULL)); // Postavljanje sjemena za generator pseudo-slucajnih brojeva
  106.     for (int i = 0; i < n; i++) {
  107.         for (int j = 0; j < m; j++) {
  108.             mat[i][j] = -125.5 + (double) rand() / RAND_MAX * (65.8 - (-125.5));
  109.         }
  110.     }
  111.  
  112.     // Ispisivanje matrice
  113.     print_matrix(mat, n, m);
  114.  
  115.     // Alociranje memorije za izlaznu matricu
  116.     double **out_mat = (double **) malloc((n-1) * sizeof(double *));
  117.     for (int i = 0; i < n-1; i++) {
  118.         out_mat[i] = (double *) malloc((m-1) * sizeof(double));
  119.     }
  120.  
  121.     // Popunjavanje izlazne matrice
  122.     for (int i = 1; i < n; i++) {
  123.         for (int j = 0; j < m-1; j++) {
  124.             out_mat[i-1][j] = mat[i][j+1];
  125.         }
  126.     }
  127.  
  128.     // Ispisivanje izlazne matrice
  129.     print_matrix(out_mat, n-1, m-1);
  130.  
  131.     // Zatvaranje izlazne datoteke
  132.     fclose(out_file);
  133.  
  134.     // Oslobadanje memorije
  135.     for (int i = 0; i < n; i++) {
  136.         free(mat[i]);
  137.     }
  138.     free(mat);
  139.  
  140.     for (int i = 0; i < n-1; i++) {
  141.         free(out_mat[i]);
  142.     }
  143.     free(out_mat);
  144.  
  145.     return 0;
  146. }
  147.  
  148.  
  149. //TRECI
  150.  
  151. #include <stdio.h>
  152. #include <ctype.h>
  153.  
  154. void copyFile(FILE* source, FILE* destination) {
  155.     char ch;
  156.     while ((ch = fgetc(source)) != EOF) {
  157.         fputc(toupper(ch), destination);
  158.     }
  159. }
  160.  
  161. int main() {
  162.     FILE* sourceFile = fopen("C:/putanja/do/prva.txt", "r");
  163.     FILE* destinationFile = fopen("druga.txt", "w");
  164.     if (sourceFile == NULL || destinationFile == NULL) {
  165.         printf("Nemoguće otvoriti datoteke!\n");
  166.         return 1;
  167.     }
  168.     copyFile(sourceFile, destinationFile);
  169.     printf("Kopiranje završeno!\n");
  170.     fclose(sourceFile);
  171.     fclose(destinationFile);
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement