|
|
Регистрация Восстановить пароль |
Повторная активизация e-mail |
Регистрация | Задать вопрос |
Заплачу за решение |
Новые сообщения |
Сообщения за день |
Расширенный поиск |
Правила |
Всё прочитано |
|
Опции темы | Поиск в этой теме |
25.12.2016, 19:16 | #1 |
Новичок
Джуниор
Регистрация: 25.12.2016
Сообщений: 2
|
Перевод с С++ на Си
Как записать эти строки в СИ? Собествено new не работает, прошу помощи
typedef struct CFind { int x, y, value; struct CFind *nextRight, *nextBelow, *nextTop, *nextLeft, *last; CFind(int _x, int _y, int _v): x(_x), y(_y), value(_v){} }CFind; temp = new CFind(f->x - 1, f->y, f->value + 1); temp = new CFind(f->x + 1, f->y, f->value + 1); CFind *f = new CFind(0,0,1); #include <stdio.h> #include <stdlib.h> #include <windows.h> #define N 7 int arr[N][N] = { {'S','#',0,0,0,0,0}, //задання квадрату {0,0,0,0,0,0,0}, {0,0,0,0,'#',0,0}, {'#',0,0,0,0,0,0}, {0,0,'#','#',0,0,'#'}, {0,0,0,0,'#',0,0}, {0,0,'#',0,0,0,'E'}}; typedef struct CFind { int x, y, value; struct CFind *nextRight, *nextBelow, *nextTop, *nextLeft, *last; CFind(int _x, int _y, int _v): x(_x), y(_y), value(_v){} }CFind; CFind* find(CFind *, int); int verification(CFind *); void outputWay(CFind *); int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); CFind *nextRight=NULL, *nextBelow=NULL, *nextTop=NULL, *nextLeft=NULL, *last=NULL; int sum = 0, i, j; for (i = 0; i < N; i++) for (j = 0; j < N; j++) if (arr[i][j] == '#') sum++; // Обраховування скільки є чорних квадратів CFind *temp; CFind *f = new CFind(0,0,1); //Задання координат початкової точки f = find(f, sum); if (f) outputWay(f); else printf("Не існує такого шляху!"); while (f) //очищення динамічної пам'яті { temp = f; f = f->last; free(temp); } } CFind* find(CFind *f, int s) { CFind *temp; if (f->x - 1 >= 0) // вверх { temp = new CFind(f->x - 1, f->y, f->value + 1); temp->last = f; if (arr[temp->x][temp->y] == 'E' && f->value == N * N - s-1) { free(temp); return f; } if (verification(temp)) free(temp); else { f->nextTop = temp; temp = find(f->nextTop, s); if (temp) return temp; else { free(f->nextTop); f->nextTop = NULL; free(temp); } } } if (f->y - 1 >= 0) // вліво { temp = new CFind(f->x, f->y - 1, f->value + 1); temp->last = f; if (arr[temp->x][temp->y] == 'E' && f->value == N * N - s-1) { free(temp); return f; } if (verification(temp)) { free(temp); } else { f->nextLeft = temp; temp = find(f->nextLeft, s); if (temp) return temp; else { free(f->nextLeft); f->nextLeft = NULL; free(temp); } } } if (f->x + 1 < N) { // вниз temp = new CFind(f->x + 1, f->y, f->value + 1); temp->last = f; if (arr[temp->x][temp->y] == 'E' && f->value == N * N - s-1) { free(temp); return f; } if (verification(temp)) { free(temp); } else { f->nextBelow = temp; temp = find(f->nextBelow, s); if (temp) return temp; else { free(f->nextBelow); f->nextBelow = NULL; free(temp); } } } if (f->y + 1 < N) { // вправо temp = new CFind(f->x, f->y + 1, f->value + 1); temp->last = f; if (arr[temp->x][temp->y] == 'E' && f->value == N * N - s-1) { free(temp); return f; } if (verification(temp)) { free(temp); } else { f->nextRight = temp; temp = find(f->nextRight, s); if (temp) return temp; else { free(f->nextRight); f->nextRight = NULL; free(temp); } } } return NULL; } int verification(CFind *f) { int x = f->x, y = f->y; while (f->last) { if (x == f->last->x && y == f->last->y || arr[f->last->x][f->last->y] == '#') return 1; f = f->last; } return 0; } void outputWay(CFind *f) //функція для кінцевого виведення { int a[N][N] = {{0}}, i, j; while (f) { a[f->x][f->y] = f->value; f = f->last; } puts("Шлях:"); //виведення пройденого шляху for (i = 0; i < N; i++) { for (j = 0; j < N; j++) printf("%3d", a[i][j]); printf("\n"); } } вот весь код, пытаюсь перевести его в СИ, но си ругается на new и CFind(int _x, int _y, int _v): x(_x), y(_y), value(_v){} Прошу помощи, времени очень мало осталось, нужно еще успеть описать алгоритм, но не получается перевести в СИ |
25.12.2016, 19:21 | #2 |
Старожил
Регистрация: 15.02.2010
Сообщений: 15,709
|
Вместо new malloc и аналоги
Вместо CFind(int _x, int _y, int _v): x(_x), y(_y), value(_v){} ручное заполнение или инициализация |
25.12.2016, 19:29 | #3 |
Новичок
Джуниор
Регистрация: 25.12.2016
Сообщений: 2
|
Знаю что нужно малок, но в моем случае new передает даные, инициализует, а малок, как мне кажеться, не может такого сделать. И еще наведите пожайлуста пример как это ручное заполнение в этом случае CFind(int _x, int _y, int _v): x(_x), y(_y), value(_v){}, просто осталось очень мало времени чтобы сдать эту програму, а я не могу понять как это сделать
Последний раз редактировалось chasnik; 25.12.2016 в 19:33. |
25.12.2016, 20:52 | #4 |
Старожил
Регистрация: 15.02.2010
Сообщений: 15,709
|
Я ж написал - ручное заполнение.
Код:
|