First Upload
This commit is contained in:
2587
Projects/AtomBomb/General.c
Normal file
2587
Projects/AtomBomb/General.c
Normal file
File diff suppressed because it is too large
Load Diff
274
Projects/AtomBomb/General.h
Normal file
274
Projects/AtomBomb/General.h
Normal file
@@ -0,0 +1,274 @@
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#define DAYS_IN_MONTH 30
|
||||
#define MONTHS_IN_YEAR 12
|
||||
#define ONE_HUNDRED 100
|
||||
#define ONE_THOSEND 10000
|
||||
#define BOOLEAN unsigned short
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define SEVEN 7
|
||||
#define EIGHT 8
|
||||
#define TEN 10
|
||||
#define TWELVE 12
|
||||
#define THIRTY 30
|
||||
#define ZERO 0
|
||||
#define ONE 1
|
||||
#define TWO 2
|
||||
#define THREE 3
|
||||
#define FOUR 4
|
||||
#define FIVE 5
|
||||
#define TAKE_SIGNED_MATH(x) (((2 * (x)) + 1) % 2)
|
||||
#define TAKE_SIGNED(x) ((x) < 0) ? -1 : 1
|
||||
#define ABS(x) (x) * (((2 * (x)) + 1) % 2)
|
||||
#define MAX(x, y) (x > y) ? x : y
|
||||
#define MIN(x, y) (x < y) ? x : y
|
||||
#define BACKSLASH_ZERO '\0'
|
||||
#define MAX_SIZE_STRING 256
|
||||
#define MASK_FIRST_BIT 0x1
|
||||
|
||||
typedef char string[MAX_SIZE_STRING];
|
||||
typedef unsigned char byte;
|
||||
typedef unsigned short word;
|
||||
typedef unsigned int dblword;
|
||||
|
||||
typedef union
|
||||
{
|
||||
int int_;
|
||||
char char_;
|
||||
double double_;
|
||||
void * pointer;
|
||||
} Data_Type;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Data_Type * values;
|
||||
unsigned int items;
|
||||
} stack;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Data_Type * values;
|
||||
unsigned int items;
|
||||
} queue;
|
||||
|
||||
struct LLL
|
||||
{
|
||||
Data_Type value;
|
||||
struct LLL * next;
|
||||
};
|
||||
|
||||
struct DLLL
|
||||
{
|
||||
Data_Type info;
|
||||
struct DLLL * next;
|
||||
struct DLLL * prev;
|
||||
};
|
||||
|
||||
struct CLLL
|
||||
{
|
||||
Data_Type info;
|
||||
struct CLLL * next;
|
||||
};
|
||||
|
||||
struct CDLLL
|
||||
{
|
||||
Data_Type info;
|
||||
struct CDLLL * next;
|
||||
struct CDLLL * prev;
|
||||
};
|
||||
|
||||
struct SM
|
||||
{
|
||||
Data_Type info;
|
||||
int row;
|
||||
int col;
|
||||
struct SM * col_next;
|
||||
struct SM * row_next;
|
||||
};
|
||||
|
||||
struct BinaryTree
|
||||
{
|
||||
Data_Type info;
|
||||
struct BinaryTree * right;
|
||||
struct BinaryTree * left;
|
||||
};
|
||||
|
||||
struct ThirdTree
|
||||
{
|
||||
Data_Type info;
|
||||
struct ThirdTree * right;
|
||||
struct ThirdTree * left;
|
||||
struct ThirdTree * middle;
|
||||
};
|
||||
|
||||
struct GeneralTree
|
||||
{
|
||||
Data_Type info;
|
||||
struct LLL * manager_general_trees;
|
||||
};
|
||||
|
||||
typedef struct SM SM;
|
||||
typedef struct LLL LLL;
|
||||
typedef struct DLLL DLLL;
|
||||
typedef struct CLLL CLLL;
|
||||
typedef struct CDLLL CDLLL;
|
||||
typedef struct BinaryTree BinaryTree;
|
||||
typedef struct ThirdTree ThirdTree;
|
||||
typedef struct GeneralTree GeneralTree;
|
||||
|
||||
void ChangeYearsToMonths(int *years, short *month);
|
||||
void SwapNumbers(int *ptr_number1, int *ptr_number2);
|
||||
void SwapChars(char *ptr_char1, char *ptr_char2);
|
||||
void ChangeMonthsToDays(short *months, int *days);
|
||||
int Random(int ** seed, int min, int max);
|
||||
void ChangeDateToDays(int *date, int *days);
|
||||
void ChangeDaysToMonths(int *days, int *months);
|
||||
void ChangeMonthsToYears(int *months, int *years);
|
||||
void ChangeDaysToDate(int *days, int *date);
|
||||
int DifferentDates(int *date1, int *date2, int *different);
|
||||
BOOLEAN EvenNumber(int num);
|
||||
int OppositeNumber(int num);
|
||||
int OddDigitsInNumber(int num);
|
||||
int EvenDigitsInNumber(int num);
|
||||
int SumOfNumberWithPositiveDigitsAndNumberWithNegativeDigits(int num);
|
||||
double Power(float num, short pow);
|
||||
int GetRest(double num, unsigned short count_digits);
|
||||
unsigned short CountDigits(int num);
|
||||
float ConvertBaseToDeciaml(float num, float base, unsigned short rest_size);
|
||||
double Sum(double number1, double number2);
|
||||
int SumOfDigitsNumber(double number);
|
||||
int SumEvenDigits(double number);
|
||||
int SumOddDigits(double number);
|
||||
BOOLEAN CommonDigitsInTwoNumbers(double number1, double number2);
|
||||
double Sqrt(double number, short root);
|
||||
BOOLEAN PrimeNumber(int number);
|
||||
double Module(double number, double div); // TODO : FIX IT
|
||||
BOOLEAN DigitInNumber(double number, unsigned short digit);
|
||||
int ConcatenationNumbers(short number1, short number2);
|
||||
int Multiplication(short number1, short multiply); // TODO : FIX IT
|
||||
int GetDenominator(double number);
|
||||
short GetCounter(double number);
|
||||
double Divide(double number, double div); // TODO : FIX IT
|
||||
BOOLEAN SumInArray(int array[], unsigned int size, int sum);
|
||||
BOOLEAN ChangeBool(BOOLEAN bool1);
|
||||
BOOLEAN Equality(int number1, int number2);
|
||||
BOOLEAN NumBBiggerThenNumA(int a, int b);
|
||||
unsigned int SumOfTimesCharsArray1InCharsArray2(char array1[],
|
||||
unsigned int array1_size,
|
||||
char array2[],
|
||||
unsigned int array2_size);
|
||||
unsigned int CreateMask(unsigned short size, unsigned short digit_of_mask);
|
||||
unsigned short CountADigitInNumber(int number, unsigned short digit);
|
||||
short GetDigit(int number, unsigned short location);
|
||||
unsigned short CountOfIdenticalDigitsInSomeIndex(int number1, int number2);
|
||||
unsigned short CountOfIdenticalDigits(int number1, int number2);
|
||||
BOOLEAN ForeignDigits(int number);
|
||||
void PrintMatrix(void * ptr_mat, unsigned short col, unsigned short row, void print(void *));
|
||||
void CopyChar(char * ch, char * copy);
|
||||
void ResetMatrix(void * ptr_mat, unsigned short col, unsigned short row, void * type, void insert(void *, void *));
|
||||
BOOLEAN CharInString(char text[], char c);
|
||||
char * LastCharOfString(char * start_string);
|
||||
unsigned int StringLength(char * str);
|
||||
BOOLEAN StringCompare(char *start_stringA_index, char *start_stringB_index);
|
||||
char * IndexCharInPointers(char *start_pointer, char *end_pointer , char c);
|
||||
unsigned short CountCharInString(char *start_string_index, char *c);
|
||||
void CutString(char *start_textA_index, char *end_textA_index, char *start_textB_index);
|
||||
void CopyString(char *start_string_index, char *start_copy_index);
|
||||
void LinkingString(char *start_stringA_index, char *start_stringB_index);
|
||||
void ReverseString(char textA[]);
|
||||
BOOLEAN StringBInStringA(char *start_string_a, char *start_string_b);
|
||||
char * IndexStringBInStringA(char *start_string_a, char *start_string_b);
|
||||
unsigned int CountStringBInStringA(char *start_string_a, char *start_string_b);
|
||||
void RemoveStringBFromStringA(char *start_string_a, char *start_string_b);
|
||||
void RemoveAllStringBFromStringA(char *start_stringA_index, char *start_stringB_index);
|
||||
BOOLEAN ValidParenthesesTemplate(char text[]);
|
||||
unsigned short CharToNumber(char *c);
|
||||
unsigned int MaxCountCharInString(char *start_string_index);
|
||||
void CopyPointers(char *start_string_index, char *start_copy_index, char *end_copy_index);
|
||||
BOOLEAN EvenBits(byte b);
|
||||
unsigned short CountBits(byte b);
|
||||
void Multiply(float *ptr_number1, float *ptr_number2, double *ptr_result);
|
||||
// -------------------------------------- Stack -----------------------------------------
|
||||
void InitStack(stack * sk);
|
||||
BOOLEAN IsEmptyStack(stack * sk);
|
||||
void PushStack(stack * sk , Data_Type item);
|
||||
Data_Type PopStack(stack * sk);
|
||||
void EmptyStack(stack * sk);
|
||||
void CopyStack(stack * sk, stack * copy);
|
||||
void OppositeStack(stack * sk);
|
||||
BOOLEAN IsEqualsStack(stack * sk1, stack * sk2);
|
||||
unsigned int ItemsStack(stack * sk);
|
||||
int SumStack(stack * sk);
|
||||
BOOLEAN FindNumberInStack(stack * sk, Data_Type item);
|
||||
void UnionStack(stack * sk1, stack * sk2);
|
||||
// -------------------------------------- Queue -----------------------------------------
|
||||
void InitQueue(queue * q);
|
||||
void InsertQueue(queue * q, Data_Type item);
|
||||
Data_Type RemoveQueue(queue * q);
|
||||
BOOLEAN IsEmptyQueue(queue * q);
|
||||
// -------------------------------------- LLL -------------------------------------------
|
||||
void InsertAfterLLL(LLL * manager);
|
||||
void PushLLL(LLL ** manager);
|
||||
void PopLLL(LLL ** manager);
|
||||
void DeleteLLL(LLL * manager);
|
||||
LLL * GetLoactionLLL(LLL ** manager, unsigned short location);
|
||||
// -------------------------------------- DLLL ------------------------------------------
|
||||
void InitDLLL(DLLL ** manager);
|
||||
void PushDLLL(DLLL ** manager);
|
||||
void InsertAfterDLLL(DLLL * ptr_before);
|
||||
void InsertBeforeDLLL(DLLL * ptr_after);
|
||||
void PopDLLL(DLLL ** manager);
|
||||
void DeleteDLLL(DLLL * ptr_cur);
|
||||
void PrintDLLL(DLLL * manager, void Print(Data_Type dt));
|
||||
// --------------------------------- CLLL -----------------------------------------------
|
||||
void InsertLastCLLL(CLLL ** manager);
|
||||
void InsertAfterCLLL(CLLL * ptr_before);
|
||||
void InsertEndCLLL(CLLL ** manager);
|
||||
void DeleteLastCLLL(CLLL ** manager);
|
||||
void DeleteAfterCLLL(CLLL * ptr_before);
|
||||
void DeleteEndCLLL(CLLL ** manager);
|
||||
// --------------------------------- CDLLL ----------------------------------------------
|
||||
void InsertLastCDLLL(CDLLL ** manager);
|
||||
void InsertAfterCDLLL(CDLLL * ptr_before);
|
||||
void InsertBeforeCDLLL(CDLLL * ptr_after);
|
||||
void InsertEndCDLLL(CDLLL ** manager);
|
||||
void DeleteCDLLL(CDLLL * ptr_cur);
|
||||
void DeleteLastCDLLL(CDLLL ** manager);
|
||||
void PrintCDLLL(CDLLL * manager, void Print(Data_Type dt));
|
||||
// ---------------------------- Sparce Matrix -------------------------------------------
|
||||
void InitSM(SM ** manager);
|
||||
void AddRowSM(SM * manager);
|
||||
void AddColSM(SM * manager);
|
||||
SM * GetItemSM(SM * manager, int row, int col);
|
||||
SM * FindAbove(SM * ptr_sm);
|
||||
SM * FindBefore(SM * ptr_sm);
|
||||
unsigned int RowsCountSM(SM * manager);
|
||||
unsigned short ColsCountSM(SM * manager);
|
||||
void AddItemSM(SM * manager, int row, int col);
|
||||
void RemoveItemSM(SM * manager, int row, int col);
|
||||
void RemoveColSM(SM * manager);
|
||||
void RemoveRowSM(SM * manager);
|
||||
void PrintIntDataType(Data_Type dt);
|
||||
void PrintSM(SM * manager, void print(Data_Type));
|
||||
// -------------------------------------- Binary Tree -----------------------------------
|
||||
void MakeBinaryTree(BinaryTree ** tree);
|
||||
void AddLeftAfterBinaryTree(BinaryTree * tree);
|
||||
void AddRightAfterBinaryTree(BinaryTree * tree);
|
||||
BOOLEAN IsLeafBinaryTree(BinaryTree * tree);
|
||||
// -------------------------------------- Third Tree -----------------------------------
|
||||
void MakeThirdTree(ThirdTree ** tree);
|
||||
void AddLeftAfterThirdTree(ThirdTree * tree);
|
||||
void AddRightAfterThirdTree(ThirdTree * tree);
|
||||
void AddMiddleAfterThirdTree(ThirdTree * tree);
|
||||
BOOLEAN IsLeafThirdTree(ThirdTree * tree);
|
||||
// -------------------------------------- General Tree -----------------------------------
|
||||
void MakeGeneralTree(GeneralTree ** tree);
|
||||
void AddAfterGeneralTree(GeneralTree * tree);
|
||||
BOOLEAN IsLeafGeneralTree(GeneralTree * tree);
|
||||
// -------------------------------------- Funcations Tree -----------------------------------
|
||||
void PreodererPrintBinaryTree(BinaryTree * root, void print(Data_Type));
|
||||
void PosorderPrintBinaryTree(BinaryTree * root, void print(Data_Type));
|
||||
void InorderPrintBinaryTree(BinaryTree * root, void print(Data_Type));
|
||||
void LevelPrintBinaryTree(BinaryTree * root, void print(Data_Type));
|
||||
BIN
Projects/AtomBomb/a.out
Normal file
BIN
Projects/AtomBomb/a.out
Normal file
Binary file not shown.
480
Projects/AtomBomb/atom_bomb.c
Normal file
480
Projects/AtomBomb/atom_bomb.c
Normal file
@@ -0,0 +1,480 @@
|
||||
#include "General.h"
|
||||
|
||||
#define ATOM 4
|
||||
#define NEUTRON_UP 3
|
||||
#define NEUTRON_DOWN 5
|
||||
#define NEUTRON_LEFT 7
|
||||
#define NEUTRON_RIGHT 9
|
||||
#define FILE_NAME "test.txt"
|
||||
#define OPEN_FILE_TYPE "rt"
|
||||
#define ASCII_COMMA ','
|
||||
#define ASCII_ZERO '0'
|
||||
#define ASCII_HYPHEN '-'
|
||||
#define SM_ROWS 500
|
||||
#define SM_COLUMNS 600
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// ReadStringFromFile
|
||||
// ------------------
|
||||
//
|
||||
// General : Read from file as string and save the string to array.
|
||||
//
|
||||
// Parameters :
|
||||
// char ** ptr_save_data - save data from file in array
|
||||
// unsigned int length - length of array
|
||||
//
|
||||
// Return value : The number (int).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void ReadStringFromFile(char ** ptr_save_data, unsigned int * length)
|
||||
{
|
||||
*length = ZERO;
|
||||
char ch;
|
||||
FILE * fp = fopen(FILE_NAME, OPEN_FILE_TYPE);
|
||||
if (fp)
|
||||
{
|
||||
*ptr_save_data = malloc(sizeof(ch));
|
||||
while ((ch = fgetc(fp)) != EOF)
|
||||
{
|
||||
*((*ptr_save_data) + *length) = ch;
|
||||
(*length)++;
|
||||
*ptr_save_data = realloc(*ptr_save_data, sizeof(ch) * (*length + ONE));
|
||||
}
|
||||
*ptr_save_data = realloc(*ptr_save_data, sizeof(ch) * (*length));
|
||||
fclose(fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Can't read file : \"%s\"\n", FILE_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// StringToNumber
|
||||
// --------------
|
||||
//
|
||||
// General : Convert string to number.
|
||||
//
|
||||
// Parameters :
|
||||
// char * ptr_str - array of char
|
||||
// unsigned int length - length of array
|
||||
//
|
||||
// Return value : The number (int).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
int StringToNumber(char * ptr_str, unsigned int length)
|
||||
{
|
||||
int number = ZERO;
|
||||
unsigned int counter;
|
||||
if (*ptr_str == ASCII_HYPHEN)
|
||||
{
|
||||
number = -(*(++ptr_str));
|
||||
ptr_str++;
|
||||
}
|
||||
for (counter = ZERO; counter < length; counter++)
|
||||
{
|
||||
number = (number * TEN) + (*(ptr_str++) - ASCII_ZERO);
|
||||
}
|
||||
|
||||
return (number);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// FileDataToArray
|
||||
// ---------------
|
||||
//
|
||||
// General : Change the data we get from file as string to numbers array.
|
||||
//
|
||||
// Parameters :
|
||||
// char * ptr_file_data - pointer to char array
|
||||
// unsigned int length_file_data - length of char array
|
||||
// int ** ptr_array - pointer to save data from char array
|
||||
// unsigned int * length_array - pointer of ptr_array length
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void FileDataToArray(char * ptr_file_data, unsigned int length_file_data, int ** ptr_array, unsigned int * length_array)
|
||||
{
|
||||
char * temp_ptr_file_data = ptr_file_data;
|
||||
int temp_number;
|
||||
unsigned int loop_counter;
|
||||
*length_array = ONE;
|
||||
*ptr_array = malloc(sizeof(int));
|
||||
|
||||
for (loop_counter = ZERO; loop_counter < length_file_data; loop_counter++)
|
||||
{
|
||||
if (*temp_ptr_file_data == ASCII_COMMA)
|
||||
{
|
||||
(*ptr_array)[*length_array - ONE] = StringToNumber(ptr_file_data, temp_ptr_file_data - ptr_file_data);
|
||||
(*length_array)++;
|
||||
*ptr_array = realloc(*ptr_array, sizeof(int) * (*length_array));
|
||||
ptr_file_data = ++temp_ptr_file_data + ONE;
|
||||
loop_counter++;
|
||||
}
|
||||
temp_ptr_file_data++;
|
||||
}
|
||||
*ptr_array = realloc(*ptr_array, sizeof(int) * (--(*length_array)));
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// InitBoard
|
||||
// ---------
|
||||
//
|
||||
// General : Initializing simulation board.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// unsigned int rows - Amount of rows in board
|
||||
// unsigned int cols - Amount of cols in board
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void InitBoard(SM * board, unsigned int rows, unsigned int cols)
|
||||
{
|
||||
unsigned int counter;
|
||||
for (counter = ZERO; counter < rows; counter++)
|
||||
{
|
||||
AddRowSM(board);
|
||||
}
|
||||
for (counter = ZERO; counter < cols; counter++)
|
||||
{
|
||||
AddColSM(board);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// ArrayDataToSM
|
||||
// -------------
|
||||
//
|
||||
// General : Copies all points to a sparse matrix from the array.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// int * ptr_arr - pointer of points array (x1, y1, x2, y2, ..)
|
||||
// unsigned int length - length of points array
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void ArrayDataToSM(SM * board, int * ptr_arr, unsigned int length)
|
||||
{
|
||||
unsigned int counter;
|
||||
for (counter = ZERO; counter < length; counter += TWO)
|
||||
{
|
||||
AddItemSM(board, ptr_arr[counter], ptr_arr[counter+ONE]);
|
||||
GetItemSM(board, ptr_arr[counter], ptr_arr[counter+ONE])->info.int_ = ATOM;
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// MoveUp
|
||||
// ------
|
||||
//
|
||||
// General : Creates to the up of the node, a neutron facing the up side
|
||||
// of the simulation board.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// SM * node - address of node in simulation board
|
||||
// queue * qt - queue of active objects
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void MoveUp(SM * manager, SM * node, queue * qt)
|
||||
{
|
||||
SM * temp = NULL;
|
||||
if (node->row > ZERO)
|
||||
{
|
||||
temp = GetItemSM(manager, node->row - ONE, node->col);
|
||||
if (!temp)
|
||||
{
|
||||
AddItemSM(manager, node->row - ONE, node->col);
|
||||
temp->info.int_ = ONE;
|
||||
}
|
||||
temp->info.int_ *= node->info.int_;
|
||||
}
|
||||
if (temp != NULL)
|
||||
{
|
||||
InsertQueue(qt, (Data_Type)(void *)temp);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// MoveDown
|
||||
// --------
|
||||
//
|
||||
// General : Creates to the down of the node, a neutron facing the down side
|
||||
// of the simulation board.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// SM * node - address of node in simulation board
|
||||
// queue * qt - queue of active objects
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void MoveDown(SM * manager, SM * node, queue * qt)
|
||||
{
|
||||
SM * temp = NULL;
|
||||
if ((node->row + ONE) < RowsCountSM(manager))
|
||||
{
|
||||
temp = GetItemSM(manager, node->row + ONE, node->col);
|
||||
if (!temp)
|
||||
{
|
||||
AddItemSM(manager, node->row + ONE, node->col);
|
||||
temp->info.int_ = ONE;
|
||||
}
|
||||
temp->info.int_ *= NEUTRON_DOWN;
|
||||
}
|
||||
if (temp != NULL)
|
||||
{
|
||||
InsertQueue(qt, (Data_Type)(void *)temp);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// MoveLeft
|
||||
// --------
|
||||
//
|
||||
// General : Creates to the left of the node, a neutron facing the left side
|
||||
// of the simulation board.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// SM * node - address of node in simulation board
|
||||
// queue * qt - queue of active objects
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void MoveLeft(SM * manager, SM * node, queue * qt)
|
||||
{
|
||||
SM * temp = NULL;
|
||||
if (node->col > ZERO)
|
||||
{
|
||||
temp = GetItemSM(manager, node->row, node->col - ONE);
|
||||
if (!temp)
|
||||
{
|
||||
AddItemSM(manager, node->row, node->col - ONE);
|
||||
temp->info.int_ = ONE;
|
||||
}
|
||||
temp->info.int_ *= NEUTRON_LEFT;
|
||||
}
|
||||
if (temp != NULL)
|
||||
{
|
||||
InsertQueue(qt, (Data_Type)(void *)temp);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// MoveRight
|
||||
// ---------
|
||||
//
|
||||
// General : Creates to the right of the node, a neutron facing the right side
|
||||
// of the simulation board.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// SM * node - address of node in simulation board
|
||||
// queue * qt - queue of active objects
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void MoveRight(SM * board, SM * node, queue * qt)
|
||||
{
|
||||
SM * temp = NULL;
|
||||
if ((node->col + ONE) < ColsCountSM(board))
|
||||
{
|
||||
temp = GetItemSM(board, node->row, node->col + ONE);
|
||||
if (!temp)
|
||||
{
|
||||
AddItemSM(board, node->row, node->col + ONE);
|
||||
temp->info.int_ = ONE;
|
||||
}
|
||||
temp->info.int_ *= NEUTRON_RIGHT;
|
||||
}
|
||||
if (temp != NULL)
|
||||
{
|
||||
InsertQueue(qt, (Data_Type)(void *)temp);
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Explode
|
||||
// -------
|
||||
//
|
||||
// General : Handles atomic fission mode.
|
||||
//
|
||||
// Parameters :
|
||||
// SM * board - Simulation board
|
||||
// SM * node - address of node in simulation board
|
||||
// queue * qt - queue of active objects
|
||||
//
|
||||
// Return value : None.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void Explode(SM * board, SM * node, queue * qt)
|
||||
{
|
||||
MoveUp(board, node, qt);
|
||||
MoveDown(board, node, qt);
|
||||
MoveLeft(board, node, qt);
|
||||
MoveRight(board, node, qt);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Steps
|
||||
// -----
|
||||
//
|
||||
// General : Handles all objects within the queue..
|
||||
//
|
||||
// Parameters :
|
||||
// SM * manager - pointer of simulation board
|
||||
// queue * qt - queue of objects
|
||||
//
|
||||
// Return value : Amount atoms explode.
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
unsigned int Steps(SM * manager, queue * qt)
|
||||
{
|
||||
unsigned int booms_count = ZERO;
|
||||
SM * main_node;
|
||||
while (!IsEmptyQueue(qt))
|
||||
{
|
||||
main_node = (SM *)RemoveQueue(qt).pointer;
|
||||
switch (main_node->info.int_)
|
||||
{
|
||||
case NEUTRON_UP:
|
||||
MoveUp(manager, main_node, qt);
|
||||
break;
|
||||
case NEUTRON_DOWN:
|
||||
MoveDown(manager, main_node, qt);
|
||||
break;
|
||||
case NEUTRON_LEFT:
|
||||
MoveLeft(manager, main_node, qt);
|
||||
break;
|
||||
case NEUTRON_RIGHT:
|
||||
MoveRight(manager, main_node, qt);
|
||||
break;
|
||||
|
||||
default:
|
||||
if (!(main_node->info.int_ % TWO));
|
||||
{
|
||||
Explode(manager, main_node, qt);
|
||||
booms_count++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
RemoveItemSM(manager, main_node->row, main_node->col);
|
||||
}
|
||||
|
||||
return (booms_count);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Atomic explosion
|
||||
// -------------------
|
||||
//
|
||||
// General : The program is essentially an atomic explosion
|
||||
// simulation
|
||||
//
|
||||
// Input : A location where we want to blow up the atom
|
||||
//
|
||||
// Process : The program reads atoms the locations of the atoms
|
||||
// and presents them in the matrix. Then we choose a
|
||||
// place where we want to blow up the atom in the matrix
|
||||
// and count how many nuclear fission happened
|
||||
//
|
||||
// Output : None
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 26.01.2020
|
||||
//---------------------------------------------------------------------------------------
|
||||
void main(void)
|
||||
{
|
||||
SM * board;
|
||||
SM * temp;
|
||||
queue active_objects;
|
||||
char * chars_from_file;
|
||||
unsigned int length_chars_from_file;
|
||||
int * atoms_position;
|
||||
unsigned int length_atoms_position;
|
||||
unsigned int chosen_row;
|
||||
unsigned int chosen_col;
|
||||
unsigned int booms_count;
|
||||
ReadStringFromFile(&chars_from_file, &length_chars_from_file);
|
||||
FileDataToArray(chars_from_file, length_chars_from_file, atoms_position, &length_atoms_position);
|
||||
free(chars_from_file);
|
||||
InitSM(&board);
|
||||
InitBoard(board, SM_ROWS, SM_COLUMNS);
|
||||
InitQueue(&active_objects);
|
||||
ArrayDataToSM(board, atoms_position + ONE, length_atoms_position - ONE);
|
||||
free(atoms_position);
|
||||
while (!temp)
|
||||
{
|
||||
printf("Enter X of atom you want to explode: ");
|
||||
scanf(" %u", &chosen_col);
|
||||
printf("Enter Y of atom you want to explode: ");
|
||||
scanf(" %u", &chosen_row);
|
||||
temp = GetItemSM(board, chosen_row, chosen_col);
|
||||
}
|
||||
InsertQueue(&active_objects, (Data_Type)(void *)temp);
|
||||
booms_count = Steps(board, &active_objects);
|
||||
|
||||
printf("Energy: %d\n", booms_count);
|
||||
|
||||
}
|
||||
1
Projects/AtomBomb/test.txt
Normal file
1
Projects/AtomBomb/test.txt
Normal file
@@ -0,0 +1 @@
|
||||
idan cohen
|
||||
BIN
Projects/AtomBomb/בעיות.odg
Normal file
BIN
Projects/AtomBomb/בעיות.odg
Normal file
Binary file not shown.
3
Projects/AtomBomb/פונקציות.txt
Normal file
3
Projects/AtomBomb/פונקציות.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
1) לעשות פונקציה שתבדוק את מקום ההתנגשות ברגע ששני אטומים מתפוצצים באותו רגע.
|
||||
|
||||
2) ג
|
||||
Reference in New Issue
Block a user