All pastes #468047 Raw Edit

Someone

public text v1 · immutable
·published 2007-05-02 21:17 UTC
rendered paste body
//H ASKHSH APOTELEITE APO 3 ARXEIA!

//askisi1.c
//////////////////////////////////////////////////////
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "list.h"
/*line and column size */
#define LIN 3
#define COL 4

void input_table(int*,int,int);
void print_table(int*,int,int);
void print_list(list*);

int main(int argc, char *argv[])
{
	int cnt1,cnt2,a[LIN][COL],*b;
	input_table((int*)a,LIN,COL); /*gemizoume ton pinaka */

	/******************************************************
	IMPORTANT NOTE: o arithmos twn stoixiwn einai tyxeos kai
	dinete apo mia rand(). Yparxei loipon periptosi na einai
	0 kai etsi o pinakas na einai kenos! Se afthn tin 
	periptwsh, ksanatrexte to programma
	******************************************************/

	list *mylist = ttol(a,LIN,COL); /*Metatrepoume ton pinaka se lista */
	b = ltot(mylist); /*Metatrepoume tin Lista se Pinaka */

	printf("Note: The table is filled randomly, so there is a chanse to be blank!\n\n");
	/*Emfanisi */
	print_table(a,LIN,COL); /*Arxikos pinakas */
	print_list(mylist); /*Eikona tis listas */
	print_table(b,LIN,COL); /*Pinakas-apotelesma apo lista */
	return EXIT_SUCCESS; /*Finished */
}

/*Gemizoume ton pinaka me Tyxea Stoixeia, Se tyxeo
  arithmo (mikrotero apo ta misa stoixeia gia na 
  einai areos. */
void input_table(int *tbl, int x, int y)
{
	srand (time(NULL));
	int tmp,elm = rand()%((x*y)/2);

	/*Arxikopoioume me 0 */
	for (tmp = 0; tmp < x*y; tmp++)
		tbl[tmp] = 0;

	/*Gemizoume tuxees theseis me tixea noumera */
	for (tmp = rand()%(x*y);elm > 0;tmp = rand()%(x*y)){
		if (!*(tbl+tmp)){ /*Diaforo tou 0 */
			tbl[tmp] = rand()%100;
			elm--;
		}
	}
	return; /*Finished & return */
}

void print_table(int *tbl, int x, int y)
{
	int cnt1,cnt2;
	/*xrisimopoiontas diktes einai diskolo na diaxeiristoume
	  disdiastatous (h kai perisoterwn diastasewn) pinakes
	  Gi afto tha doulepsoume etsi: pter[X*linesize + offset] */
	for (cnt1 = 0; cnt1 < 3; cnt1++){
		for (cnt2 = 0; cnt2 < 4; cnt2++)
			printf ("%d\t",tbl[cnt1*4 + cnt2]);
		printf("\n");
	}
	printf("\n\n"); /*Emfanisi enos kenou */
	return; /*Finished & return */
}

void print_list(list *mylist)
{
	node *tmpnode;
	/*Diatrexoume kai tiponoume tin lista */
	for (tmpnode = mylist->head; tmpnode; tmpnode = tmpnode->next)
		printf("%d\t%d\t%d\n",tmpnode->coors[0],tmpnode->coors[1],tmpnode->val);
	printf("\n\n"); /*Emfanisi enos kenou */
	return; /*Finished & return */
}


/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

//list.c
/////////////////////////////////////////////////////////////////////////
#include <malloc.h>
#include <stdlib.h>

#include "list.h" /*definition of structs */

/*Sinartiseis gia diaxeirisi listas */
node * list_createitem(int,int,int);
void list_additem(list*,node*);

/*"Kyries" synarthseis */
list* ttol(int*,int,int); /*table to list */
int* ltot(list*); /*list to table */

node * list_createitem(int x, int y, int val)
{
	node *newnode = (node *)malloc(sizeof(node));

	newnode->coors[0] = x;
	newnode->coors[1] = y;
	newnode->val = val;
	newnode->next = NULL;

	return newnode; /*Finished & return */
}

void list_additem(list *mylist, node *newnode)
{
	if (mylist->head == NULL) /*adeia lista */
	{
		mylist->head = newnode; /*Arxikopoioume head */
		mylist->tail = mylist->head;
	}
	else
	{
		mylist->tail->next = newnode; /*Prosthetoume sthn oura */
		mylist->tail = mylist->tail->next;
	}
	return; /*Finished & return */
}

list* ttol(int *tbl, int x, int y)
{
	int cnt1,cnt2;
	node *tmpnode;
	list *mylist = (list*)malloc(sizeof(list));
	/*thetoume NULL gia apofigi provlimatwn */
	mylist->head = NULL;
	mylist->tail = NULL;
	
	tmpnode = list_createitem(x,y,0); /*1st node with dimentions */
	list_additem(mylist, tmpnode);

	/*psaxnoume ton pinaka gia mh mhdenika stoixeia */
	for (cnt1 = 0; cnt1 < x; cnt1++){
		for (cnt2 = 0; cnt2 < y; cnt2++){
			if (tbl[cnt1*y + cnt2] != 0){ /*mh mhdeniko */
				/*Dimiourgoume komvo kai prosthetoume stin lista */
				tmpnode = list_createitem(cnt1,cnt2,tbl[cnt1*y + cnt2]);
				list_additem(mylist, tmpnode);
			}
		}
	}
	return mylist; /*Finished & return */
}

int* ltot(list *mylist)
{
	int x,y,tmp,*tbl;
	node *tmpnode;
	/*Diastaseis gia desmefsi mnimis */
	x = mylist->head->coors[0];
	y = mylist->head->coors[1];

	tbl=(int*)malloc(x*y*sizeof(int));

	for (tmp = 0; tmp < x*y; tmp++)
		tbl[tmp] = 0; /*Arxikopoioume se "0" */
	/*Diatrexoume tin lista kai antikathistoume ston pinaka */
	for (tmpnode = mylist->head->next; tmpnode != NULL; tmpnode = tmpnode->next)
		tbl[tmpnode->coors[0]*y + tmpnode->coors[1]] = tmpnode->val;

	return tbl; /*Finished & return */
}


/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

//list.h
/////////////////////////////////////////////////////////////////////////
#ifndef LIST_H
#define LIST_H
/*Node struct */
typedef struct list_entry
{
       int coors[2],val; /*coordinates and value */
       struct list_entry *next;
}node;

/*List struct */
typedef struct
{
        node *head;
        node *tail;
}list;
#endif