Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int readarr(FILE* fin, int* data, int cnt); //считываем данные из файла в массив
- int readarr(FILE* fin, int* data, int cnt)
- {
- for (int k = 0; k < cnt; k++)
- {
- if (fscanf(fin, "%d", data + k) != 1){
- return -1;
- }
- }
- return 0;
- }
- void PrintArray(int* data, int count);
- void PrintArray(int* data, int count) {
- for (int k = 0; k < count; k++)
- printf("%d ", data[k]);
- }
- void proc(int* arr, int* el, int N);//проверяем что в массивах на одних тех же местах стоят равные элементы
- void proc(int* arr, int* el, int N)
- {
- for (int i = 0; i < N; i++)
- {
- el[arr[i]] = el[arr[i]] + 1;
- }
- }
- int main(void)
- {
- FILE* file;
- int* arr1;
- int* arr2;
- int* el1;
- int* el2;
- int N;
- int f = 0;
- if ((file = fopen("in.txt", "rt")) == NULL) {
- printf("Can't open input file!\n");
- return -1;
- }
- if (fscanf(file, "%d", &N) != 1) {
- printf("Can't read N!\n");
- fclose(file);
- return -1;
- }
- if (N < 1) {
- printf("Invalid N!\n");
- fclose(file);
- return -1;
- }
- if ((arr1 = (int*) malloc(N * sizeof(int))) == NULL)
- {
- printf("Can't allocate memory!\n");
- fclose(file);
- return -1;
- }
- if ((arr2 = (int*) malloc(N * sizeof(int))) == NULL)
- {
- printf("Can't allocate memory 2!\n");
- fclose(file);
- free(arr1);
- return -1;
- }
- if ((el1 = (int*) malloc(102 * sizeof(int))) == NULL)
- {
- printf("Can't allocate memory 3!\n");
- fclose(file);
- free(arr1);
- free(arr2);
- return -1;
- }
- if ((el2 = (int*) malloc(102 * sizeof(int))) == NULL)
- {
- printf("Can't allocate memory 4!\n");
- fclose(file);
- free(arr1);
- free(arr2);
- free(el1);
- return -1;
- }
- if (readarr(file, arr1, N) < 0) {
- printf("Can't read array 1!\n");
- fclose(file);
- free(arr1);
- free(arr2);
- }
- if (readarr(file, arr2, N) < 0) {
- printf("Can't read array 1!\n");
- fclose(file);
- free(arr1);
- free(arr2);
- }
- for (int i = 0; i < 102; i++){
- el1[i] = 0;
- }
- for (int i = 0; i < 102; i++){
- el2[i] = 0;
- }
- proc(arr1, el1, N);
- proc(arr2, el2, N);
- for (int i = 0; i < 102; i++)
- {
- if (el1[i] != el2[i])
- {
- f = 1;
- }
- }
- if (f == 0)
- {
- printf("arrays are anagrams\n");
- }
- else
- {
- printf("arrays are NOT anagrams\n");
- }
- free(arr1);
- free(arr2);
- free(el1);
- free(el2);
- fclose(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement