Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 15.05.2013, 17:45   #1
airesjke
Пользователь
 
Регистрация: 29.10.2011
Сообщений: 24
Радость (переделать код на считывание из файла/запись в файл)

Код:
#include <stdio.h>
#define CITY_MAX   100
#define ROAD_MAX   1000
struct road
{
  int city1;
  int city2;
};
struct roadto
{
  struct roadto *next;
  int city;
};
struct city
{
  struct roadto *roads;
  int state;
};
void assignCity( struct city *cityList, struct city *city, int state)
{
  struct roadto *roadto;
  city->state = state;
  for( roadto = city->roads; roadto != NULL; roadto = roadto->next)
    if( cityList[roadto->city].state == 0 )
      assignCity( cityList, &cityList[roadto->city], state);
}
int main( void )
{
// in order to not alloc memory
  struct roadto roadHeap[2*ROAD_MAX];
  struct city cities[CITY_MAX+1];  // skipping city number 0
// some variables
  int stateCount = 0;
  int r, c;
// supposedly from data file, correct values
  int N = 6;
  int M = 3;
  struct road roadList[ROAD_MAX] = { { 1, 3}, { 1, 5}, { 2, 6} };
// Initing city list
  for( c = 1; c <= N; c++)
  {
    cities[c].roads = NULL;
    cities[c].state = 0; // not assigned
  }
// Linking cities with road list
  for( r = 0; r < M; r++)
  {
    struct road *road = &roadList[r];
    struct roadto *roadto;
    // city1 -> city2
    roadto = &roadHeap[2*r]; // instead of malloc
    roadto->city = road->city2;
    roadto->next = cities[road->city1].roads;
    cities[road->city1].roads = roadto;
    // city2 -> city1
    roadto = &roadHeap[2*r+1]; // instead of malloc
    roadto->city = road->city1;
    roadto->next = cities[road->city2].roads;
    cities[road->city2].roads = roadto;
  }
#if 0
// Checking links
  for( c = 1; c <= N; c++)
  {
    struct roadto *roadto;
    printf( "city #%d:", c);
    for( roadto = cities[c].roads; roadto != NULL; roadto = roadto->next)
      printf( " %d", roadto->city);
    printf( "\n" );
  }
#endif
// Assigning cities
  for( c = 1; c <= N; c++)
    if( cities[c].state == 0 )
    {
      ++stateCount;
      assignCity( cities, &cities[c], stateCount);
    }
// Wanted result
  printf( "State count: %d\n", stateCount);
}
airesjke вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
исправить код .Чтение из файла, фильтр данных и запись в новый файл. edikesh Помощь студентам 0 17.12.2011 18:59
Считывание и запись в файл Oki Помощь студентам 3 01.11.2010 21:08
Считывание и запись файла Paul_AG Общие вопросы C/C++ 3 27.02.2010 07:40
Считывание и запись структур в бинарный файл TheKnyazz Общие вопросы C/C++ 7 10.11.2009 13:47
Ещё запись в файл + считывание s-t-r-i-k-e-r Помощь студентам 3 03.06.2008 20:57