mirror of
https://github.com/Kaehvaman/OAIP.git
synced 2025-01-31 01:08:25 +04:00
commit everything lol
This commit is contained in:
parent
f2428d8bb6
commit
0c32b4dd13
@ -105,6 +105,7 @@
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)\include</AdditionalIncludeDirectories>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@ -122,7 +123,7 @@
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)\include;</AdditionalIncludeDirectories>
|
||||
<LanguageStandard_C>Default</LanguageStandard_C>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
@ -139,13 +140,16 @@
|
||||
<ClInclude Include="..\include\raymath.h" />
|
||||
<ClInclude Include="..\include\resource_dir.h" />
|
||||
<ClInclude Include="..\include\tinyfiledialogs.h" />
|
||||
<ClInclude Include="..\src\dynamic_memory.h" />
|
||||
<ClInclude Include="..\src\game.h" />
|
||||
<ClInclude Include="..\src\parallel_for.h" />
|
||||
<ClInclude Include="..\src\memory_arena.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\src\dynamic_memory.c" />
|
||||
<ClCompile Include="..\src\game.c" />
|
||||
<ClCompile Include="..\src\gol_threading.c" />
|
||||
<ClCompile Include="..\src\main.c" />
|
||||
<ClCompile Include="..\src\parallel_for.c" />
|
||||
<ClCompile Include="..\src\memory_arena.c" />
|
||||
<ClCompile Include="..\src\tinyfiledialogs.c" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
|
@ -33,7 +33,10 @@
|
||||
<ClInclude Include="..\src\game.h">
|
||||
<Filter>Файлы заголовков</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\src\parallel_for.h">
|
||||
<ClInclude Include="..\src\dynamic_memory.h">
|
||||
<Filter>Файлы заголовков</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\src\memory_arena.h">
|
||||
<Filter>Файлы заголовков</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
@ -47,7 +50,13 @@
|
||||
<ClCompile Include="..\src\game.c">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\parallel_for.c">
|
||||
<ClCompile Include="..\src\dynamic_memory.c">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\memory_arena.c">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\gol_threading.c">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
|
31
Game of Life/src/dynamic_memory.c
Normal file
31
Game of Life/src/dynamic_memory.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void* SafeMalloc(size_t size)
|
||||
{
|
||||
void* buffer = malloc(size);
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "Error in SafeMalloc: failed to allocate %zu bytes.\n", size);
|
||||
abort();
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void* SafeCalloc(size_t count, size_t size)
|
||||
{
|
||||
void* buffer = calloc(count, size);
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "Error in SafeCalloc: failed to allocate %zu bytes.\n", count * size);
|
||||
abort();
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void* SafeRealloc(void* block, size_t size) {
|
||||
void* buffer = realloc(block, size);
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "Error in SafeRealloc: failed to reallocate block %p of %zu bytes.\n", block, size);
|
||||
abort();
|
||||
}
|
||||
return buffer;
|
||||
}
|
5
Game of Life/src/dynamic_memory.h
Normal file
5
Game of Life/src/dynamic_memory.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
void* SafeMalloc(size_t size);
|
||||
void* SafeCalloc(size_t count, size_t size);
|
||||
void* SafeRealloc(void* block, size_t size);
|
53
Game of Life/src/gol_threading.c
Normal file
53
Game of Life/src/gol_threading.c
Normal file
@ -0,0 +1,53 @@
|
||||
//#include <stdbool.h>
|
||||
//#include <string.h>
|
||||
//#include <iso646.h>
|
||||
//
|
||||
//typedef struct {
|
||||
// bool** map;
|
||||
// bool** tempMap;
|
||||
// int startX;
|
||||
// int endX;
|
||||
// int startY;
|
||||
// int endY;
|
||||
//} golArgs;
|
||||
|
||||
//static inline int checkCell(int x, int y) {
|
||||
// if (x < 0 or y < 0 or x > MAP_X - 1 or y > MAP_Y - 1) {
|
||||
// return 0;
|
||||
// }
|
||||
// else {
|
||||
// return map[x][y];
|
||||
// }
|
||||
//}
|
||||
|
||||
//void celluralAutomata()
|
||||
//{
|
||||
// for (int x = 0; x < MAP_X; x++) {
|
||||
// for (int y = 0; y < MAP_Y; y++) {
|
||||
// int neighbours = 0;
|
||||
//
|
||||
// neighbours += checkCell(x - 1, y);
|
||||
// neighbours += checkCell(x - 1, y + 1);
|
||||
// neighbours += checkCell(x - 1, y - 1);
|
||||
// neighbours += checkCell(x + 1, y);
|
||||
// neighbours += checkCell(x + 1, y + 1);
|
||||
// neighbours += checkCell(x + 1, y - 1);
|
||||
// neighbours += checkCell(x, y + 1);
|
||||
// neighbours += checkCell(x, y - 1);
|
||||
//
|
||||
// if (neighbours == 3) {
|
||||
// tempMap[x][y] = true;
|
||||
// }
|
||||
// else if (neighbours == 2) {
|
||||
// tempMap[x][y] = map[x][y];
|
||||
// }
|
||||
// else {
|
||||
// tempMap[x][y] = false;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// for (int x = 0; x < MAP_X; x++) {
|
||||
// memcpy(map[x], tempMap[x], MAP_Y * sizeof(bool));
|
||||
// }
|
||||
//}
|
@ -3,21 +3,20 @@
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
#include "resource_dir.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <iso646.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <threads.h>
|
||||
|
||||
#include "parallel_for.h"
|
||||
#include "dynamic_memory.h"
|
||||
#include "memory_arena.h"
|
||||
|
||||
#define RAYGUI_IMPLEMENTATION
|
||||
#include "raygui.h"
|
||||
|
||||
#define MAP_X 400
|
||||
#define MAP_Y 200
|
||||
#define CELL_SIZE 6
|
||||
#define MAP_X 1200
|
||||
#define MAP_Y 600
|
||||
#define CELL_SIZE 2
|
||||
#define FCELL_SIZE (float)CELL_SIZE
|
||||
|
||||
#define BOTTOM_BAR_HEIGHT 60
|
||||
|
||||
#define PUREBLUE CLITERAL(Color){ 0, 0, 255, 255 }
|
||||
@ -25,275 +24,379 @@
|
||||
#define VSGREEN CLITERAL(Color){78, 201, 176, 255}
|
||||
#define WATERBLUE CLITERAL(Color){200, 240, 255, 255}
|
||||
|
||||
//static bool map[MAP_X][MAP_Y] = { 0 };
|
||||
//static bool tempMap[MAP_X][MAP_Y] = { 0 };
|
||||
|
||||
bool** map;
|
||||
bool** tempMap;
|
||||
|
||||
void* SafeMalloc(size_t size)
|
||||
typedef struct {
|
||||
bool** map;
|
||||
bool** tempMap;
|
||||
int startX;
|
||||
int endX;
|
||||
int startY;
|
||||
int endY;
|
||||
} golArgs;
|
||||
|
||||
thrd_t threads[32];
|
||||
golArgs thread_args[32];
|
||||
int numThreads = 5;
|
||||
|
||||
static inline int checkCell(int x, int y)
|
||||
{
|
||||
void* buffer = malloc(size);
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "Error in SafeMalloc: failed to allocate %zu bytes.\n", size);
|
||||
abort();
|
||||
}
|
||||
return buffer;
|
||||
if (x < 0 or y < 0 or x > MAP_X - 1 or y > MAP_Y - 1) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return map[x][y];
|
||||
}
|
||||
}
|
||||
|
||||
void* SafeCalloc(size_t count, size_t size)
|
||||
static inline single_gol(int x, int y)
|
||||
{
|
||||
void* buffer = calloc(count, size);
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "Error in SafeCalloc: failed to allocate %zu bytes.\n", count * size);
|
||||
abort();
|
||||
}
|
||||
return buffer;
|
||||
int neighbours = 0;
|
||||
|
||||
neighbours += checkCell(x - 1, y);
|
||||
neighbours += checkCell(x - 1, y + 1);
|
||||
neighbours += checkCell(x - 1, y - 1);
|
||||
neighbours += checkCell(x + 1, y);
|
||||
neighbours += checkCell(x + 1, y + 1);
|
||||
neighbours += checkCell(x + 1, y - 1);
|
||||
neighbours += checkCell(x, y + 1);
|
||||
neighbours += checkCell(x, y - 1);
|
||||
|
||||
if (neighbours == 2) {
|
||||
tempMap[x][y] = map[x][y];
|
||||
}
|
||||
else if (neighbours == 3) {
|
||||
tempMap[x][y] = true;
|
||||
}
|
||||
else {
|
||||
tempMap[x][y] = false;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int checkCell(int x, int y) {
|
||||
if (x < 0 or y < 0 or x > MAP_X - 1 or y > MAP_Y - 1) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return map[x][y];
|
||||
}
|
||||
int gol_thread(void* arg_ptr)
|
||||
{
|
||||
golArgs args = *(golArgs*)(arg_ptr);
|
||||
|
||||
for (int x = args.startX; x < args.endX; x++) {
|
||||
for (int y = args.startY; y < args.endY; y++) {
|
||||
int neighbours = 0;
|
||||
|
||||
neighbours += checkCell(x - 1, y);
|
||||
neighbours += checkCell(x - 1, y + 1);
|
||||
neighbours += checkCell(x - 1, y - 1);
|
||||
neighbours += checkCell(x + 1, y);
|
||||
neighbours += checkCell(x + 1, y + 1);
|
||||
neighbours += checkCell(x + 1, y - 1);
|
||||
neighbours += checkCell(x, y + 1);
|
||||
neighbours += checkCell(x, y - 1);
|
||||
|
||||
if (neighbours == 2) {
|
||||
tempMap[x][y] = map[x][y];
|
||||
}
|
||||
else if (neighbours == 3) {
|
||||
tempMap[x][y] = true;
|
||||
}
|
||||
else {
|
||||
tempMap[x][y] = false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
single_gol(args.startX, args.startY);
|
||||
single_gol(args.startX, args.endY);
|
||||
single_gol(args.endX, args.startY);
|
||||
single_gol(args.endX, args.endY);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void gol_thread_init() {
|
||||
int deltaX = MAP_X / numThreads;
|
||||
int remainder = MAP_X % numThreads;
|
||||
|
||||
for (int i = 0; i < numThreads - 1; i++) {
|
||||
thread_args[i] = (golArgs){
|
||||
.startX = deltaX * i,
|
||||
.endX = deltaX * (i + 1),
|
||||
.startY = 0,
|
||||
.endY = MAP_Y
|
||||
};
|
||||
}
|
||||
thread_args[numThreads - 1] = (golArgs){
|
||||
.startX = deltaX * (numThreads - 1),
|
||||
.endX = MAP_X,
|
||||
.startY = 0,
|
||||
.endY = MAP_Y
|
||||
};
|
||||
}
|
||||
|
||||
void gol_thread_start()
|
||||
{
|
||||
for (int i = 0; i < numThreads; i++) {
|
||||
thrd_create(&threads[i], gol_thread, &thread_args[i]);
|
||||
}
|
||||
for (int i = 0; i < numThreads; i++) {
|
||||
thrd_join(threads[i], NULL);
|
||||
}
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
memcpy(map[x], tempMap[x], MAP_Y * sizeof(bool));
|
||||
}
|
||||
}
|
||||
|
||||
void celluralAutomata()
|
||||
{
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
for (int y = 0; y < MAP_Y; y++) {
|
||||
int neighbours = 0;
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
for (int y = 0; y < MAP_Y; y++) {
|
||||
int neighbours = 0;
|
||||
|
||||
neighbours += checkCell(x - 1, y);
|
||||
neighbours += checkCell(x - 1, y + 1);
|
||||
neighbours += checkCell(x - 1, y - 1);
|
||||
neighbours += checkCell(x + 1, y);
|
||||
neighbours += checkCell(x + 1, y + 1);
|
||||
neighbours += checkCell(x + 1, y - 1);
|
||||
neighbours += checkCell(x, y + 1);
|
||||
neighbours += checkCell(x, y - 1);
|
||||
neighbours += checkCell(x - 1, y);
|
||||
neighbours += checkCell(x - 1, y + 1);
|
||||
neighbours += checkCell(x - 1, y - 1);
|
||||
neighbours += checkCell(x + 1, y);
|
||||
neighbours += checkCell(x + 1, y + 1);
|
||||
neighbours += checkCell(x + 1, y - 1);
|
||||
neighbours += checkCell(x, y + 1);
|
||||
neighbours += checkCell(x, y - 1);
|
||||
|
||||
if (neighbours == 3) {
|
||||
tempMap[x][y] = true;
|
||||
}
|
||||
else if (neighbours == 2) {
|
||||
tempMap[x][y] = map[x][y];
|
||||
}
|
||||
else {
|
||||
tempMap[x][y] = false;
|
||||
}
|
||||
if (neighbours == 3) {
|
||||
tempMap[x][y] = true;
|
||||
}
|
||||
else if (neighbours == 2) {
|
||||
tempMap[x][y] = map[x][y];
|
||||
}
|
||||
else {
|
||||
tempMap[x][y] = false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
memcpy(map[x], tempMap[x], MAP_Y * sizeof(bool));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
memcpy(map[x], tempMap[x], MAP_Y * sizeof(bool));
|
||||
}
|
||||
}
|
||||
|
||||
//int compute_cell(int x) {
|
||||
// int neighbours = 0;
|
||||
//
|
||||
// neighbours += checkCell(x - 1, y);
|
||||
// neighbours += checkCell(x - 1, y + 1);
|
||||
// neighbours += checkCell(x - 1, y - 1);
|
||||
// neighbours += checkCell(x + 1, y);
|
||||
// neighbours += checkCell(x + 1, y + 1);
|
||||
// neighbours += checkCell(x + 1, y - 1);
|
||||
// neighbours += checkCell(x, y + 1);
|
||||
// neighbours += checkCell(x, y - 1);
|
||||
//
|
||||
// if (neighbours == 3) {
|
||||
// tempMap[x][y] = true;
|
||||
// }
|
||||
// else if (neighbours == 2) {
|
||||
// tempMap[x][y] = map[x][y];
|
||||
// }
|
||||
// else {
|
||||
// tempMap[x][y] = false;
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//void* compute_cell_forp(void* arg)
|
||||
//{
|
||||
// int* pa = (int*)arg;
|
||||
// int* result = malloc(sizeof(*result));
|
||||
// *result = mult2(*pa);
|
||||
// return result;
|
||||
//}
|
||||
|
||||
void ClearMap() {
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
memset(map[x], 0, MAP_Y * sizeof(bool));
|
||||
}
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
memset(map[x], 0, MAP_Y * sizeof(bool));
|
||||
}
|
||||
}
|
||||
|
||||
void drawMap()
|
||||
{
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
for (int y = 0; y < MAP_Y; y++) {
|
||||
if (map[x][y]) {
|
||||
int posX = x * CELL_SIZE;
|
||||
int posY = y * CELL_SIZE;
|
||||
DrawRectangle(posX, posY, CELL_SIZE, CELL_SIZE, BLACK);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
for (int y = 0; y < MAP_Y; y++) {
|
||||
if (map[x][y]) {
|
||||
int posX = x * CELL_SIZE;
|
||||
int posY = y * CELL_SIZE;
|
||||
DrawRectangle(posX, posY, CELL_SIZE, CELL_SIZE, BLACK);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void drawNet() {
|
||||
for (int i = 0; i <= MAP_X * CELL_SIZE; i += CELL_SIZE) {
|
||||
DrawLine(i, 0, i, MAP_Y * CELL_SIZE, GRAY);
|
||||
DrawLine(i+1, 0, i+1, MAP_Y * CELL_SIZE, GRAY);
|
||||
}
|
||||
for (int i = 0; i <= MAP_X * CELL_SIZE; i += CELL_SIZE) {
|
||||
DrawLine(i, 0, i, MAP_Y * CELL_SIZE, GRAY);
|
||||
DrawLine(i+1, 0, i+1, MAP_Y * CELL_SIZE, GRAY);
|
||||
}
|
||||
|
||||
for (int i = 0; i <= MAP_Y * CELL_SIZE; i += CELL_SIZE) {
|
||||
DrawLine(0, i, MAP_X * CELL_SIZE, i, GRAY);
|
||||
DrawLine(0, i-1, MAP_X * CELL_SIZE, i-1, GRAY);
|
||||
}
|
||||
for (int i = 0; i <= MAP_Y * CELL_SIZE; i += CELL_SIZE) {
|
||||
DrawLine(0, i, MAP_X * CELL_SIZE, i, GRAY);
|
||||
DrawLine(0, i-1, MAP_X * CELL_SIZE, i-1, GRAY);
|
||||
}
|
||||
}
|
||||
|
||||
void drawBottomBar()
|
||||
{
|
||||
DrawRectangle(0, MAP_Y * CELL_SIZE, MAP_X * CELL_SIZE, BOTTOM_BAR_HEIGHT, BLACKGRAY);
|
||||
DrawRectangle(0, MAP_Y * CELL_SIZE, MAP_X * CELL_SIZE, BOTTOM_BAR_HEIGHT, BLACKGRAY);
|
||||
}
|
||||
|
||||
#define CPSIZE 213
|
||||
int main()
|
||||
{
|
||||
map = (bool**)SafeMalloc(MAP_X * sizeof(bool*));
|
||||
tempMap = (bool**)SafeMalloc(MAP_X * sizeof(bool*));
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
map[x] = (bool*)SafeCalloc(MAP_Y, sizeof(bool));
|
||||
tempMap[x] = (bool*)SafeCalloc(MAP_Y, sizeof(bool));
|
||||
}
|
||||
map = (bool**)SafeMalloc(MAP_X * sizeof(bool*));
|
||||
tempMap = (bool**)SafeMalloc(MAP_X * sizeof(bool*));
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
map[x] = (bool*)SafeCalloc(MAP_Y, sizeof(bool));
|
||||
tempMap[x] = (bool*)SafeCalloc(MAP_Y, sizeof(bool));
|
||||
}
|
||||
|
||||
const int screenWidth = MAP_X * CELL_SIZE;
|
||||
const int screenHeight = MAP_Y * CELL_SIZE + BOTTOM_BAR_HEIGHT;
|
||||
gol_thread_init();
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "Game of Life");
|
||||
/*Arena arena = ArenaCreate(SafeCalloc(1024 * 1024, 1), 1024 * 1024);
|
||||
map = (bool**)ArenaAlloc(&arena, MAP_X * sizeof(bool*));
|
||||
tempMap = (bool**)ArenaAlloc(&arena, MAP_X * sizeof(bool*));
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
map[x] = (bool*)ArenaAlloc(&arena, MAP_Y * sizeof(bool));
|
||||
tempMap[x] = (bool*)ArenaAlloc(&arena, MAP_Y * sizeof(bool));
|
||||
}*/
|
||||
|
||||
SearchAndSetResourceDir("resources");
|
||||
const int screenWidth = MAP_X * CELL_SIZE;
|
||||
const int screenHeight = MAP_Y * CELL_SIZE + BOTTOM_BAR_HEIGHT;
|
||||
|
||||
int codepoints[CPSIZE] = { 0 };
|
||||
for (int i = 0; i < 127 - 32; i++) codepoints[i] = 32 + i; // Basic ASCII characters
|
||||
for (int i = 0; i < 118; i++) codepoints[95 + i] = 1024 + i; // Cyrillic characters
|
||||
SetConfigFlags(FLAG_VSYNC_HINT);
|
||||
|
||||
Font InconsolataBold = LoadFontEx("Inconsolata-LGC-Bold.ttf", 36, codepoints, CPSIZE);
|
||||
SetTextureFilter(InconsolataBold.texture, TEXTURE_FILTER_BILINEAR);
|
||||
InitWindow(screenWidth, screenHeight, "Game of Life");
|
||||
|
||||
GuiSetFont(InconsolataBold);
|
||||
GuiSetStyle(DEFAULT, TEXT_SIZE, (int)(24));
|
||||
GuiSetStyle(DEFAULT, TEXT_SPACING, 0);
|
||||
GuiSetStyle(DEFAULT, TEXT_LINE_SPACING, (int)(24));
|
||||
GuiSetStyle(STATUSBAR, BORDER_WIDTH, 2);
|
||||
int monitor = GetCurrentMonitor();
|
||||
int monitorFPS = GetMonitorRefreshRate(monitor);
|
||||
|
||||
int monitor = GetCurrentMonitor();
|
||||
int monitorFPS = GetMonitorRefreshRate(monitor);
|
||||
SetTargetFPS(monitorFPS);
|
||||
|
||||
SetTargetFPS(monitorFPS);
|
||||
SearchAndSetResourceDir("resources");
|
||||
|
||||
Vector2 mousePos = { 0 };
|
||||
int mouseCellX = 0;
|
||||
int mouseCellY = 0;
|
||||
int codepoints[CPSIZE] = { 0 };
|
||||
for (int i = 0; i < 127 - 32; i++) codepoints[i] = 32 + i; // Basic ASCII characters
|
||||
for (int i = 0; i < 118; i++) codepoints[95 + i] = 1024 + i; // Cyrillic characters
|
||||
|
||||
bool editMap = true;
|
||||
bool netToggle = false;
|
||||
Font InconsolataBold = LoadFontEx("Inconsolata-LGC-Bold.ttf", 36, codepoints, CPSIZE);
|
||||
SetTextureFilter(InconsolataBold.texture, TEXTURE_FILTER_BILINEAR);
|
||||
|
||||
float simSpeed = 1.0f;
|
||||
int frameCounter = 0;
|
||||
Vector2 mousePos = { 0 };
|
||||
Vector2 mouseWorldPos = { 0 };
|
||||
int mouseWorldCellX = 0;
|
||||
int mouseWorldCellY = 0;
|
||||
|
||||
int guyScreenWidth = 720;
|
||||
bool editMap = true;
|
||||
bool netToggle = false;
|
||||
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
frameCounter++;
|
||||
float simSpeed = 1.0f;
|
||||
int frameCounter = 0;
|
||||
|
||||
if (IsKeyPressed(KEY_SPACE)) {
|
||||
editMap = !editMap;
|
||||
}
|
||||
if (IsKeyPressed(KEY_N)) {
|
||||
netToggle = !netToggle;
|
||||
}
|
||||
if (IsKeyPressed(KEY_EQUAL)) {
|
||||
simSpeed *= 2.0f;
|
||||
}
|
||||
if (IsKeyPressed(KEY_MINUS)) {
|
||||
simSpeed *= 0.5f;
|
||||
}
|
||||
if (IsKeyPressed(KEY_C)) {
|
||||
ClearMap();
|
||||
}
|
||||
int guiScreenWidth = 720;
|
||||
|
||||
if (editMap)
|
||||
{
|
||||
mousePos = GetMousePosition();
|
||||
mouseCellX = (int)(Clamp(mousePos.x, 0, (float)(MAP_X * CELL_SIZE - 1)) / CELL_SIZE);
|
||||
mouseCellY = (int)(Clamp(mousePos.y, 0, (float)(MAP_Y * CELL_SIZE - 1)) / CELL_SIZE);
|
||||
Camera2D camera = {
|
||||
.target = (Vector2){ screenWidth / 2.0f, screenHeight / 2.0f },
|
||||
.offset = (Vector2){ screenWidth / 2.0f, screenHeight / 2.0f },
|
||||
.rotation = 0.0f,
|
||||
.zoom = 1.0f
|
||||
};
|
||||
int cameraSpeed;
|
||||
float frametime;
|
||||
//float mouseWheel;
|
||||
|
||||
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
|
||||
map[mouseCellX][mouseCellY] = true;
|
||||
}
|
||||
else if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
|
||||
map[mouseCellX][mouseCellY] = false;
|
||||
}
|
||||
}
|
||||
else if (FloatEquals(simSpeed, 1.0f)) {
|
||||
celluralAutomata();
|
||||
}
|
||||
else if (simSpeed < 1.0f and frameCounter >= 1 / simSpeed) {
|
||||
celluralAutomata();
|
||||
//printf("%d %d\n", frameCounter, (int)(1 / simSpeed));
|
||||
frameCounter = 0;
|
||||
}
|
||||
else if (simSpeed > 1.0f) {
|
||||
for (int i = 0; i < (int)simSpeed; i++) {
|
||||
celluralAutomata();
|
||||
}
|
||||
//printf("%f speed\n", simSpeed);
|
||||
}
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
frameCounter++;
|
||||
frametime = GetFrameTime();
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(WHITE);
|
||||
if (IsKeyPressed(KEY_SPACE)) {
|
||||
editMap = !editMap;
|
||||
}
|
||||
if (IsKeyPressed(KEY_N)) {
|
||||
netToggle = !netToggle;
|
||||
}
|
||||
if (IsKeyPressed(KEY_EQUAL)) {
|
||||
simSpeed *= 2.0f;
|
||||
}
|
||||
if (IsKeyPressed(KEY_MINUS)) {
|
||||
simSpeed *= 0.5f;
|
||||
}
|
||||
if (IsKeyPressed(KEY_C)) {
|
||||
ClearMap();
|
||||
}
|
||||
|
||||
drawMap();
|
||||
drawBottomBar();
|
||||
if (editMap)
|
||||
{
|
||||
mousePos = GetMousePosition();
|
||||
mouseWorldPos = GetScreenToWorld2D(mousePos, camera);
|
||||
mouseWorldCellX = (int)(Clamp(mouseWorldPos.x, 0, (float)(MAP_X * CELL_SIZE - 1)) / CELL_SIZE);
|
||||
mouseWorldCellY = (int)(Clamp(mouseWorldPos.y, 0, (float)(MAP_Y * CELL_SIZE - 1)) / CELL_SIZE);
|
||||
|
||||
if (netToggle) drawNet();
|
||||
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
|
||||
map[mouseWorldCellX][mouseWorldCellY] = true;
|
||||
}
|
||||
else if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
|
||||
map[mouseWorldCellX][mouseWorldCellY] = false;
|
||||
}
|
||||
}
|
||||
else if (FloatEquals(simSpeed, 1.0f)) {
|
||||
//celluralAutomata();
|
||||
gol_thread_start();
|
||||
}
|
||||
else if (simSpeed < 1.0f and frameCounter >= 1 / simSpeed) {
|
||||
//celluralAutomata();
|
||||
gol_thread_start();
|
||||
frameCounter = 0;
|
||||
}
|
||||
else if (simSpeed > 1.0f) {
|
||||
for (int i = 0; i < (int)simSpeed; i++) {
|
||||
//celluralAutomata();
|
||||
gol_thread_start();
|
||||
}
|
||||
//printf("%f speed\n", simSpeed);
|
||||
}
|
||||
|
||||
if (editMap)
|
||||
{
|
||||
Rectangle rec = { mouseCellX * FCELL_SIZE, mouseCellY * FCELL_SIZE, FCELL_SIZE, FCELL_SIZE };
|
||||
DrawRectangleLinesEx(rec, 2, GREEN);
|
||||
}
|
||||
|
||||
//Rectangle settingsBox = { 400, 400, 400, 200 };
|
||||
//GuiGroupBox(settingsBox, u8"Init game");
|
||||
//Rectangle valueBox1 = { 620, 420, 100, 40 };
|
||||
//GuiValueBox(valueBox1, u8"screen Width ", &guyScreenWidth, 120, 2560, true);
|
||||
|
||||
DrawFPS(0, MAP_Y * CELL_SIZE);
|
||||
DrawText(TextFormat("%.4fx", simSpeed), 0, MAP_Y * CELL_SIZE + 20, 20, ORANGE);
|
||||
DrawText(TextFormat("%.1f TPS", GetFPS() * simSpeed), 0, MAP_Y * CELL_SIZE + 40, 20, BLUE);
|
||||
if (IsKeyDown(KEY_LEFT_SHIFT)) {
|
||||
cameraSpeed = 1000;
|
||||
}
|
||||
else {
|
||||
cameraSpeed = 200;
|
||||
}
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
if (IsKeyDown(KEY_W)) {
|
||||
camera.target.y -= cameraSpeed * frametime;
|
||||
}
|
||||
if (IsKeyDown(KEY_S)) {
|
||||
camera.target.y += cameraSpeed * frametime;
|
||||
}
|
||||
if (IsKeyDown(KEY_A)) {
|
||||
camera.target.x -= cameraSpeed * frametime;
|
||||
}
|
||||
if (IsKeyDown(KEY_D)) {
|
||||
camera.target.x += cameraSpeed * frametime;
|
||||
}
|
||||
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
free(map[x]);
|
||||
free(tempMap[x]);
|
||||
}
|
||||
free(map);
|
||||
free(tempMap);
|
||||
BeginDrawing();
|
||||
|
||||
UnloadFont(InconsolataBold);
|
||||
ClearBackground(WHITE);
|
||||
|
||||
CloseWindow();
|
||||
BeginMode2D(camera);
|
||||
|
||||
return 0;
|
||||
drawMap();
|
||||
Rectangle rec = { -2, -2, MAP_X * CELL_SIZE + 4, MAP_Y * CELL_SIZE + 4 };
|
||||
|
||||
DrawRectangleLinesEx(rec, 2.0f, RED);
|
||||
|
||||
if (netToggle) drawNet();
|
||||
|
||||
if (editMap)
|
||||
{
|
||||
Rectangle rec = { mouseWorldCellX * FCELL_SIZE, mouseWorldCellY * FCELL_SIZE, FCELL_SIZE, FCELL_SIZE };
|
||||
DrawRectangleLinesEx(rec, 2, GREEN);
|
||||
}
|
||||
|
||||
EndMode2D();
|
||||
|
||||
drawBottomBar();
|
||||
|
||||
DrawFPS(0, MAP_Y * CELL_SIZE);
|
||||
DrawText(TextFormat("%.4fx", simSpeed), 0, MAP_Y * CELL_SIZE + 20, 20, ORANGE);
|
||||
DrawText(TextFormat("%.1f TPS", GetFPS() * simSpeed), 0, MAP_Y * CELL_SIZE + 40, 20, BLUE);
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
for (int x = 0; x < MAP_X; x++) {
|
||||
free(map[x]);
|
||||
free(tempMap[x]);
|
||||
}
|
||||
free(map);
|
||||
free(tempMap);
|
||||
|
||||
//ArenaDestroy(&arena);
|
||||
|
||||
UnloadFont(InconsolataBold);
|
||||
|
||||
CloseWindow();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WinMain() {
|
||||
return main();
|
||||
return main();
|
||||
}
|
64
Game of Life/src/memory_arena.c
Normal file
64
Game of Life/src/memory_arena.c
Normal file
@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "memory_arena.h"
|
||||
|
||||
static bool is_power_of_two(uintptr_t x) {
|
||||
return (x & (x - 1)) == 0;
|
||||
}
|
||||
|
||||
static uintptr_t AlignForward(uintptr_t ptr, size_t alignment) {
|
||||
uintptr_t p, a, modulo;
|
||||
|
||||
assert(is_power_of_two(alignment));
|
||||
|
||||
p = ptr;
|
||||
a = (uintptr_t)alignment;
|
||||
// Same as (p % a) but faster as 'a' is a power of two
|
||||
modulo = p & (a - 1);
|
||||
|
||||
if (modulo != 0) {
|
||||
// If 'p' address is not aligned, push the address to the
|
||||
// next value which is aligned
|
||||
p += a - modulo;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
void* ArenaAllocAligned(Arena* arena, size_t size, size_t alignment)
|
||||
{
|
||||
uintptr_t aligned_offset_ptr = AlignForward(arena->offset_ptr, alignment);
|
||||
uintptr_t next_offset_ptr = aligned_offset_ptr + size;
|
||||
|
||||
if (next_offset_ptr - (uintptr_t)arena->buffer <= arena->size)
|
||||
{
|
||||
arena->offset_ptr = next_offset_ptr;
|
||||
return (void*)aligned_offset_ptr;
|
||||
}
|
||||
|
||||
fprintf(stderr, "\nError in ArenaAllocAligned: reached arena end.\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
void* ArenaAlloc(Arena* arena, size_t size)
|
||||
{
|
||||
return ArenaAllocAligned(arena, size, DEFAULT_ALIGNMENT);
|
||||
}
|
||||
|
||||
Arena ArenaCreate(void* buffer, size_t buffer_size)
|
||||
{
|
||||
return (Arena) {
|
||||
.buffer = buffer,
|
||||
.size = buffer_size,
|
||||
.offset_ptr = (uintptr_t)buffer
|
||||
};
|
||||
}
|
||||
|
||||
void ArenaDestroy(Arena* arena)
|
||||
{
|
||||
free(arena->buffer);
|
||||
arena->offset_ptr = 0;
|
||||
arena->size = 0;
|
||||
}
|
21
Game of Life/src/memory_arena.h
Normal file
21
Game of Life/src/memory_arena.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define ARENA_ALIGNMENT(type) offsetof(struct { char c; type x; }, x) // generates C4116 warning
|
||||
|
||||
#ifndef DEFAULT_ALIGNMENT
|
||||
#define DEFAULT_ALIGNMENT (2 * sizeof(void*))
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
void* buffer;
|
||||
size_t size;
|
||||
uintptr_t offset_ptr;
|
||||
} Arena;
|
||||
|
||||
Arena ArenaCreate(void* buffer, size_t buffer_size);
|
||||
void* ArenaAlloc(Arena* arena, size_t size);
|
||||
void* ArenaAllocAligned(Arena* arena, size_t size, size_t alignment);
|
||||
void ArenaDestroy(Arena* arena);
|
@ -1,463 +0,0 @@
|
||||
#include "parallel_for.h"
|
||||
|
||||
#if defined(__APPLE__) || defined(__linux__)
|
||||
#define POSIX
|
||||
#elif defined(_WIN32)
|
||||
|
||||
#else
|
||||
#error "Platform not supported."
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef POSIX
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
/******************************************************************************
|
||||
* A task descriptor specifying the input element and the address at which the *
|
||||
* output element should be stored. *
|
||||
******************************************************************************/
|
||||
typedef struct task_descriptor {
|
||||
void* input_element;
|
||||
void** output_element_address;
|
||||
} task_descriptor;
|
||||
|
||||
/************************************************************
|
||||
* This structure implements a concurrent array-based queue. *
|
||||
************************************************************/
|
||||
typedef struct concurrent_queue {
|
||||
|
||||
#ifdef POSIX
|
||||
pthread_mutex_t mutex;
|
||||
#elif defined(_WIN32)
|
||||
CRITICAL_SECTION criticalSection;
|
||||
#endif
|
||||
|
||||
task_descriptor** array;
|
||||
size_t begin_index;
|
||||
size_t end_index;
|
||||
size_t size;
|
||||
size_t len;
|
||||
} concurrent_queue;
|
||||
|
||||
/************************************************************
|
||||
* Initializes the input concurrent queue to an empty state. *
|
||||
************************************************************/
|
||||
static int concurrent_queue_init(concurrent_queue* queue, size_t len)
|
||||
{
|
||||
int ret;
|
||||
queue->array = malloc(len * sizeof(*queue->array));
|
||||
|
||||
if (queue->array == NULL)
|
||||
{
|
||||
return ERROR_FORP_MALLOC_FAIL;
|
||||
}
|
||||
|
||||
queue->begin_index = 0;
|
||||
queue->end_index = 0;
|
||||
queue->size = 0;
|
||||
queue->len = len;
|
||||
|
||||
#ifdef POSIX
|
||||
|
||||
ret = pthread_mutex_init(&queue->mutex, NULL);
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
return ERROR_FORP_NO_MUTEX_INIT;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
InitializeCriticalSection(&queue->criticalSection);
|
||||
|
||||
#endif
|
||||
|
||||
return ERROR_FORP_SUCCESS;
|
||||
}
|
||||
|
||||
/******************************************************
|
||||
* Appends a task descriptor to the tail of the queue. *
|
||||
******************************************************/
|
||||
static void concurrent_queue_enqueue(concurrent_queue* queue,
|
||||
task_descriptor* descriptor)
|
||||
{
|
||||
queue->array[queue->end_index] = descriptor;
|
||||
queue->end_index++;
|
||||
queue->size++;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Removes the head element from the queue. Unlike all other functions related *
|
||||
* to the queue, this is one is thread-safe. *
|
||||
******************************************************************************/
|
||||
static task_descriptor* concurrent_queue_dequeue(concurrent_queue* queue)
|
||||
{
|
||||
task_descriptor* descriptor;
|
||||
|
||||
#ifdef POSIX
|
||||
pthread_mutex_lock(&queue->mutex);
|
||||
#else
|
||||
EnterCriticalSection(&queue->criticalSection);
|
||||
#endif
|
||||
|
||||
if (queue->size > 0)
|
||||
{
|
||||
descriptor = queue->array[queue->begin_index];
|
||||
queue->begin_index++;
|
||||
queue->size--;
|
||||
}
|
||||
else
|
||||
{
|
||||
descriptor = NULL;
|
||||
}
|
||||
|
||||
#ifdef POSIX
|
||||
pthread_mutex_unlock(&queue->mutex);
|
||||
#else
|
||||
LeaveCriticalSection(&queue->criticalSection);
|
||||
#endif
|
||||
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Releases all the resources occupied by the queue, or namely, the mutex and *
|
||||
* the array. *
|
||||
*****************************************************************************/
|
||||
static int concurrent_queue_destroy(concurrent_queue* queue)
|
||||
{
|
||||
int ret;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < queue->len; i++)
|
||||
{
|
||||
free(queue->array[i]);
|
||||
}
|
||||
|
||||
free(queue->array);
|
||||
|
||||
#ifdef POSIX
|
||||
ret = pthread_mutex_destroy(&queue->mutex);
|
||||
return ret == 0 ? ERROR_FORP_SUCCESS : ERROR_FORP_NO_MUTEX_DESTROY;
|
||||
#else
|
||||
DeleteCriticalSection(&queue->criticalSection);
|
||||
return ERROR_FORP_SUCCESS;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
/*******************************************************
|
||||
* Returns the number of processors on Mac OS or Linux. *
|
||||
*******************************************************/
|
||||
static int get_number_of_processors_apple_linux(size_t* p_number_of_processors)
|
||||
{
|
||||
#ifdef POSIX
|
||||
* p_number_of_processors = (size_t)sysconf(_SC_NPROCESSORS_ONLN);
|
||||
#endif
|
||||
|
||||
return ERROR_FORP_SUCCESS;
|
||||
}
|
||||
|
||||
/***********************************************
|
||||
* Returns the number of processors on Windows. *
|
||||
***********************************************/
|
||||
static int get_number_of_processors_windows(size_t* p_number_of_processors)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SYSTEM_INFO si;
|
||||
GetSystemInfo(&si);
|
||||
*p_number_of_processors = (size_t)2 * si.dwNumberOfProcessors;
|
||||
#endif
|
||||
|
||||
return ERROR_FORP_SUCCESS;
|
||||
}
|
||||
|
||||
/**************************************************************
|
||||
* A portable function for returning the number of processors. *
|
||||
**************************************************************/
|
||||
static int get_number_of_processors(size_t* p_number_of_processors)
|
||||
{
|
||||
#ifdef POSIX
|
||||
return get_number_of_processors_apple_linux(p_number_of_processors);
|
||||
#else
|
||||
return get_number_of_processors_windows(p_number_of_processors);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Specifies the worker thread arguments. Holds the queue and the function to *
|
||||
* be applied to each queue element. *
|
||||
*****************************************************************************/
|
||||
typedef struct worker_thread_proc_args {
|
||||
concurrent_queue* queue;
|
||||
void* (*func)(void*);
|
||||
int return_status;
|
||||
} worker_thread_proc_args;
|
||||
|
||||
/*********************************
|
||||
* Implements the worker threads. *
|
||||
*********************************/
|
||||
static void* worker_thread_proc(void* args)
|
||||
{
|
||||
worker_thread_proc_args* worker_thread_proc_arguments =
|
||||
(worker_thread_proc_args*)args;
|
||||
|
||||
concurrent_queue* queue = worker_thread_proc_arguments->queue;
|
||||
void* (*func)(void*) = worker_thread_proc_arguments->func;
|
||||
task_descriptor* task_desc;
|
||||
int ret = 0;
|
||||
|
||||
#ifdef POSIX
|
||||
ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
||||
#endif
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
worker_thread_proc_arguments->return_status = ret;
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
worker_thread_proc_arguments->return_status = 0;
|
||||
}
|
||||
|
||||
while ((task_desc = concurrent_queue_dequeue(queue)) != NULL)
|
||||
{
|
||||
*task_desc->output_element_address = func(task_desc->input_element);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/***************************************
|
||||
* Cancels all the first 'len' threads. *
|
||||
***************************************/
|
||||
#ifdef POSIX
|
||||
static void cancel_threads(pthread_t* pthreads, size_t len)
|
||||
#else
|
||||
static void cancel_threads(HANDLE* threads, size_t len)
|
||||
#endif
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < len; ++i)
|
||||
{
|
||||
|
||||
#ifdef POSIX
|
||||
pthread_cancel(pthreads[i]);
|
||||
#else
|
||||
TerminateThread(threads[i], 0);
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************
|
||||
* The actual implementation of the parallel for construct. *
|
||||
***********************************************************/
|
||||
int forp(void** input, void** output, size_t len, void* (*func)(void*))
|
||||
{
|
||||
size_t number_of_cores;
|
||||
size_t szi;
|
||||
int ret;
|
||||
int join_ret = ERROR_FORP_SUCCESS;
|
||||
concurrent_queue queue;
|
||||
task_descriptor* task_desc;
|
||||
worker_thread_proc_args* wtpa;
|
||||
|
||||
#ifdef POSIX
|
||||
|
||||
pthread_t* threads;
|
||||
|
||||
#else
|
||||
|
||||
HANDLE* threads;
|
||||
|
||||
#endif
|
||||
|
||||
if (input == NULL || output == NULL || func == NULL)
|
||||
{
|
||||
return ERROR_FORP_NO_ARGS;
|
||||
}
|
||||
|
||||
if (len == 0)
|
||||
{
|
||||
/*****************
|
||||
* Nothing to do. *
|
||||
*****************/
|
||||
return ERROR_FORP_SUCCESS;
|
||||
}
|
||||
|
||||
ret = get_number_of_processors(&number_of_cores);
|
||||
|
||||
if (ret != ERROR_FORP_SUCCESS)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (number_of_cores == 0)
|
||||
{
|
||||
return ERROR_FORP_UNKNOWN_CORES;
|
||||
}
|
||||
|
||||
if ((ret = concurrent_queue_init(&queue, len)) != ERROR_FORP_SUCCESS)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**************************************
|
||||
* Create a concurrent queue of tasks. *
|
||||
**************************************/
|
||||
for (szi = 0; szi < len; szi++)
|
||||
{
|
||||
task_desc = malloc(sizeof * task_desc);
|
||||
|
||||
if (task_desc == NULL)
|
||||
{
|
||||
concurrent_queue_destroy(&queue);
|
||||
return ERROR_FORP_MALLOC_FAIL;
|
||||
}
|
||||
|
||||
task_desc->input_element = input[szi];
|
||||
task_desc->output_element_address = &output[szi];
|
||||
concurrent_queue_enqueue(&queue, task_desc);
|
||||
|
||||
if (ret != ERROR_FORP_SUCCESS)
|
||||
{
|
||||
concurrent_queue_destroy(&queue);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************
|
||||
* Create the worker threads. *
|
||||
*****************************/
|
||||
threads = malloc(number_of_cores * sizeof(*threads));
|
||||
|
||||
if (threads == NULL)
|
||||
{
|
||||
concurrent_queue_destroy(&queue);
|
||||
return ERROR_FORP_MALLOC_FAIL;
|
||||
}
|
||||
|
||||
wtpa = malloc(number_of_cores * sizeof(*wtpa));
|
||||
|
||||
if (wtpa == NULL)
|
||||
{
|
||||
free(threads);
|
||||
concurrent_queue_destroy(&queue);
|
||||
return ERROR_FORP_MALLOC_FAIL;
|
||||
}
|
||||
|
||||
for (szi = 0; szi < number_of_cores; szi++)
|
||||
{
|
||||
wtpa[szi].queue = &queue;
|
||||
wtpa[szi].func = func;
|
||||
wtpa[szi].return_status = 0;
|
||||
|
||||
#ifdef POSIX
|
||||
ret = pthread_create(&threads[szi],
|
||||
NULL,
|
||||
worker_thread_proc,
|
||||
&wtpa[szi]);
|
||||
#else
|
||||
threads[szi] = CreateThread(NULL,
|
||||
100000,
|
||||
(LPTHREAD_START_ROUTINE)worker_thread_proc,
|
||||
(LPVOID)&wtpa[szi],
|
||||
0,
|
||||
NULL);
|
||||
#endif
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
cancel_threads(threads, szi);
|
||||
concurrent_queue_destroy(&queue);
|
||||
return ERROR_FORP_NO_THREAD;
|
||||
}
|
||||
|
||||
if (wtpa[szi].return_status != 0)
|
||||
{
|
||||
cancel_threads(threads, szi + 1);
|
||||
concurrent_queue_destroy(&queue);
|
||||
return ERROR_FORP_NO_SETCANCELTYPE;
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************
|
||||
* Wait for all the worker threads to complete. *
|
||||
***********************************************/
|
||||
for (szi = 0; szi < number_of_cores; szi++)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
|
||||
if (WaitForSingleObject(threads[szi], INFINITE) != 0 && join_ret == 0)
|
||||
{
|
||||
join_ret = ERROR_FORP_NO_JOIN;
|
||||
}
|
||||
#else
|
||||
join_ret = pthread_join(threads[szi], NULL);
|
||||
|
||||
if (ret != 0 && join_ret == ERROR_FORP_SUCCESS)
|
||||
{
|
||||
join_ret = ERROR_FORP_NO_JOIN;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return join_ret;
|
||||
}
|
||||
|
||||
const char* forp_error(int error_code)
|
||||
{
|
||||
switch (error_code)
|
||||
{
|
||||
case ERROR_FORP_SUCCESS:
|
||||
return "forp succeeded.";
|
||||
|
||||
case ERROR_FORP_NO_ARGS:
|
||||
return "Some arguments missing.";
|
||||
|
||||
case ERROR_FORP_NO_JOIN:
|
||||
return "Could not join a thread.";
|
||||
|
||||
case ERROR_FORP_CPU_FEOF:
|
||||
return "Reached EOF while reading the number of processors.";
|
||||
|
||||
case ERROR_FORP_NO_THREAD:
|
||||
return "Could create a thread.";
|
||||
|
||||
case ERROR_FORP_CPU_FERROR:
|
||||
return "An error occured while reading the number of processors.";
|
||||
|
||||
case ERROR_FORP_POPEN_FAIL:
|
||||
return "Could not execute a program in popen.";
|
||||
|
||||
case ERROR_FORP_MALLOC_FAIL:
|
||||
return "A call to malloc returned NULL.";
|
||||
|
||||
case ERROR_FORP_SSCANF_FAIL:
|
||||
return "sscanf failed.";
|
||||
|
||||
case ERROR_FORP_NO_MUTEX_INIT:
|
||||
return "Could not initialize a mutex.";
|
||||
|
||||
case ERROR_FORP_NO_MUTEX_DESTROY:
|
||||
return "Could not destroy a mutex.";
|
||||
|
||||
case ERROR_FORP_UNKNOWN_CORES:
|
||||
return "Could not determine the number of processors.";
|
||||
|
||||
case ERROR_FORP_NO_SETCANCELTYPE:
|
||||
return "setcanceltype failed.";
|
||||
|
||||
default:
|
||||
return "Unknown error code.";
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
#ifndef PARALLEL_FOR_H
|
||||
#define PARALLEL_FOR_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ERROR_FORP_SUCCESS 0
|
||||
#define ERROR_FORP_NO_ARGS 1
|
||||
#define ERROR_FORP_UNKNOWN_CORES 2
|
||||
#define ERROR_FORP_NO_MUTEX_INIT 3
|
||||
#define ERROR_FORP_NO_MUTEX_DESTROY 4
|
||||
#define ERROR_FORP_MALLOC_FAIL 5
|
||||
#define ERROR_FORP_SSCANF_FAIL 6
|
||||
#define ERROR_FORP_POPEN_FAIL 7
|
||||
#define ERROR_FORP_CPU_FEOF 8
|
||||
#define ERROR_FORP_CPU_FERROR 9
|
||||
#define ERROR_FORP_NO_THREAD 10
|
||||
#define ERROR_FORP_NO_SETCANCELTYPE 11
|
||||
#define ERROR_FORP_NO_JOIN 12
|
||||
|
||||
/*******************************************************************************
|
||||
* Runs a multithreaded for loop over the input array producing the results and *
|
||||
* storing them in the output array. *
|
||||
*******************************************************************************/
|
||||
int forp(void** input,
|
||||
void** output,
|
||||
size_t len,
|
||||
void* (*func)(void*));
|
||||
|
||||
/*************************************************************************
|
||||
* Returns a human-readable description of an error code related to forp. *
|
||||
*************************************************************************/
|
||||
const char* forp_error(int error_code);
|
||||
|
||||
#endif /* PARALLEL_FOR_H */
|
BIN
Iso_C_1999_definition.pdf
Normal file
BIN
Iso_C_1999_definition.pdf
Normal file
Binary file not shown.
28
exam test variant/exam test variant.sln
Normal file
28
exam test variant/exam test variant.sln
Normal file
@ -0,0 +1,28 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.12.35527.113 d17.12
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "exam test variant", "exam test variant\exam test variant.vcxproj", "{4BD82D12-E491-4ACC-9A31-E269F2A6F346}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{4BD82D12-E491-4ACC-9A31-E269F2A6F346}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{4BD82D12-E491-4ACC-9A31-E269F2A6F346}.Debug|x64.Build.0 = Debug|x64
|
||||
{4BD82D12-E491-4ACC-9A31-E269F2A6F346}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{4BD82D12-E491-4ACC-9A31-E269F2A6F346}.Debug|x86.Build.0 = Debug|Win32
|
||||
{4BD82D12-E491-4ACC-9A31-E269F2A6F346}.Release|x64.ActiveCfg = Release|x64
|
||||
{4BD82D12-E491-4ACC-9A31-E269F2A6F346}.Release|x64.Build.0 = Release|x64
|
||||
{4BD82D12-E491-4ACC-9A31-E269F2A6F346}.Release|x86.ActiveCfg = Release|Win32
|
||||
{4BD82D12-E491-4ACC-9A31-E269F2A6F346}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
135
exam test variant/exam test variant/exam test variant.vcxproj
Normal file
135
exam test variant/exam test variant/exam test variant.vcxproj
Normal file
@ -0,0 +1,135 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{4bd82d12-e491-4acc-9a31-e269f2a6f346}</ProjectGuid>
|
||||
<RootNamespace>examtestvariant</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.c" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Исходные файлы">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Файлы заголовков">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Файлы ресурсов">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.c">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
199
exam test variant/exam test variant/main.c
Normal file
199
exam test variant/exam test variant/main.c
Normal file
@ -0,0 +1,199 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdio.h>
|
||||
#include <Windows.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int N1;
|
||||
int N2;
|
||||
int M2;
|
||||
|
||||
#define MAX_N2 20
|
||||
#define MAX_M2 20
|
||||
|
||||
int A1[20];
|
||||
int A2[MAX_N2][MAX_M2];
|
||||
|
||||
void loadLists(char filepath[])
|
||||
{
|
||||
FILE* fin = fopen(filepath, "r");
|
||||
|
||||
if (fin == NULL) {
|
||||
printf("File didn't open!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fscanf(fin, "%d", &N1);
|
||||
|
||||
for (int i = 0; i < N1; i++) {
|
||||
fscanf(fin, "%d", &A1[i]);
|
||||
}
|
||||
|
||||
fscanf(fin, "%d", &N2);
|
||||
fscanf(fin, "%d", &M2);
|
||||
|
||||
for (int n = 0; n < N2; n++) {
|
||||
for (int m = 0; m < M2; m++) {
|
||||
fscanf(fin, "%d", &A2[n][m]);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fin);
|
||||
}
|
||||
|
||||
void saveLists(char filepath[])
|
||||
{
|
||||
FILE* fout = fopen(filepath, "w");
|
||||
if (fopen == NULL) {
|
||||
printf("File didn't create!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(fout, "%d\n", N1);
|
||||
for (int i = 0; i < N1; i++) {
|
||||
fprintf(fout, "%d ", A1[i]);
|
||||
}
|
||||
fprintf(fout, "\n");
|
||||
|
||||
fprintf(fout, "%d %d\n", N2, M2);
|
||||
|
||||
for (int n = 0; n < N2; n++) {
|
||||
for (int m = 0; m < M2; m++) {
|
||||
fprintf(fout, "%d ", A2[n][m]);
|
||||
}
|
||||
fprintf(fout, "\n");
|
||||
}
|
||||
|
||||
fclose(fout);
|
||||
}
|
||||
|
||||
void printLists()
|
||||
{
|
||||
printf("===== Òåêóùåå ñîñòîÿíèå ìàññèâîâ =====\n");
|
||||
printf("A1[%d]: ", N1);
|
||||
for (int i = 0; i < N1; i++) {
|
||||
printf("%d ", A1[i]);
|
||||
}
|
||||
printf("\n\n");
|
||||
|
||||
printf("A2[%d][%d]:\n", N2, M2);
|
||||
for (int n = 0; n < N2; n++) {
|
||||
for (int m = 0; m < M2; m++) {
|
||||
printf("%2d ", A2[n][m]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("=====================================\n");
|
||||
}
|
||||
|
||||
void task2()
|
||||
{
|
||||
int firstEvenId = 0;
|
||||
int lastEvenId = 0;
|
||||
|
||||
for (int i = 0; i < N1; i++) {
|
||||
if (A1[i] % 2 == 0) {
|
||||
firstEvenId = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < N1; i++) {
|
||||
if (A1[i] % 2 == 0) {
|
||||
lastEvenId = i;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = firstEvenId + 1; i < lastEvenId; i++) {
|
||||
if (A1[i] % 2 == 0) {
|
||||
A1[i] += 100;
|
||||
}
|
||||
}
|
||||
|
||||
printf("> Âûïîëíåíî çàäàíèå 2\n");
|
||||
}
|
||||
|
||||
void deleteRow(int row)
|
||||
{
|
||||
for (int n = row; n < N2 - 1; n++) {
|
||||
for (int m = 0; m < M2; m++) {
|
||||
A2[n][m] = A2[n + 1][m];
|
||||
}
|
||||
}
|
||||
N2 -= 1;
|
||||
}
|
||||
|
||||
void addColumn() {
|
||||
if (M2 + 1 <= MAX_M2) {
|
||||
for (int n = 0; n < N2; n++) {
|
||||
A2[n][M2] = 0;
|
||||
}
|
||||
M2++;
|
||||
}
|
||||
else {
|
||||
printf("Äîñòèãíóò ìàêñèìóì êîëîíîê!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void insertColumn(int column) {
|
||||
if (M2 + 1 <= MAX_M2) {
|
||||
for (int n = 0; n < N2; n++) {
|
||||
for (int m = M2; m > column; m--) {
|
||||
A2[n][m] = A2[n][m - 1];
|
||||
}
|
||||
}
|
||||
for (int n = 0; n < N2; n++) {
|
||||
A2[n][column] = 0;
|
||||
}
|
||||
M2++;
|
||||
}
|
||||
else {
|
||||
printf("Äîñòèãíóò ìàêñèìóì êîëîíîê!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void insertRow(int row) {
|
||||
if (N2 + 1 <= MAX_N2) {
|
||||
for (int n = N2; n > row; n--) {
|
||||
for (int m = 0; m < M2; m++) {
|
||||
A2[n][m] = A2[n - 1][m];
|
||||
}
|
||||
}
|
||||
for (int m = 0; m < M2; m++) {
|
||||
A2[row][m] = 0;
|
||||
}
|
||||
N2++;
|
||||
}
|
||||
else {
|
||||
printf("Äîñòèãíóò ìàêñèìóì ñòðîê!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void task3() {
|
||||
for (int n = 0; n < N2; n++) {
|
||||
for (int m = 0; m < M2; m++) {
|
||||
if (A2[n][m] < 0) {
|
||||
deleteRow(n);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("> Âûïîëíåíî çàäàíèå 3\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
SetConsoleCP(1251);
|
||||
SetConsoleOutputCP(1251);
|
||||
|
||||
loadLists("test.txt");
|
||||
printLists();
|
||||
|
||||
task2();
|
||||
task3();
|
||||
|
||||
printLists();
|
||||
|
||||
saveLists("out.txt");
|
||||
|
||||
return 0;
|
||||
}
|
5
exam test variant/exam test variant/out.txt
Normal file
5
exam test variant/exam test variant/out.txt
Normal file
@ -0,0 +1,5 @@
|
||||
6
|
||||
1 2 104 106 7 8
|
||||
2 4
|
||||
-2 -1 0 1
|
||||
2 3 4 3
|
6
exam test variant/exam test variant/test.txt
Normal file
6
exam test variant/exam test variant/test.txt
Normal file
@ -0,0 +1,6 @@
|
||||
6
|
||||
1 2 4 6 7 8
|
||||
3 4
|
||||
-2 -1 0 1
|
||||
2 3 4 3
|
||||
2 1 0 -1
|
@ -1,324 +1,324 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <Windows.h>
|
||||
|
||||
#define MAX_M 10
|
||||
#define MAX_N 10
|
||||
|
||||
int m = 3;
|
||||
int n = 3;
|
||||
|
||||
int arr[MAX_M][MAX_N] = {
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9}
|
||||
};
|
||||
|
||||
void printarr() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
printf("%2d ", arr[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void fill_with_indexes() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = (i * 10) + j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fillZero() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fillRand() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = rand() % 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void findMin() {
|
||||
int min = arr[0][0];
|
||||
int iMin = 0;
|
||||
int jMin = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
if (arr[i][j] < min) {
|
||||
printf("min = %d\n", arr[i][j]);
|
||||
printf("imin = %d\n", i);
|
||||
printf("jmin = %d\n", j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void deleteRow(int row) {
|
||||
for (int i = row; i < m - 1; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = arr[i + 1][j];
|
||||
}
|
||||
}
|
||||
m--;
|
||||
}
|
||||
|
||||
void addColumn() {
|
||||
if (n + 1 <= MAX_N) {
|
||||
for (int i = 0; i < m; i++) {
|
||||
arr[i][n] = 0;
|
||||
}
|
||||
n++;
|
||||
}
|
||||
else {
|
||||
printf("Достигнут максимум колонок!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void insertColumn(int column) {
|
||||
if (n + 1 <= MAX_N) {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = n; j > column; j--) {
|
||||
arr[i][j] = arr[i][j - 1];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < m; i++) {
|
||||
arr[i][column] = 0;
|
||||
}
|
||||
n++;
|
||||
}
|
||||
else {
|
||||
printf("Достигнут максимум колонок!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void insertRow(int row) {
|
||||
if (m + 1 <= MAX_M) {
|
||||
for (int i = m; i > row; i--) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = arr[i - 1][j];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
arr[row][i] = 0;
|
||||
}
|
||||
m++;
|
||||
}
|
||||
else {
|
||||
printf("Достигнут максимум строк!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void odd10x() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (arr[i][j] % 2 != 0) arr[i][j] *= 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void crat10() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (arr[i][j] % 10 == 0) arr[i][j] /= 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void inputArr() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
printf("a%d%d = ", i, j);
|
||||
scanf_s("%d", &arr[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void save() {
|
||||
FILE* fout = fopen("savefile.txt", "w");
|
||||
if (fout == NULL) {
|
||||
puts("Невозможно открыть файл");
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(fout, "%d %d\n", m, n);
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
fprintf(fout, "%2d ", arr[i][j]);
|
||||
}
|
||||
fprintf(fout, "\n");
|
||||
}
|
||||
|
||||
fclose(fout);
|
||||
}
|
||||
|
||||
void load() {
|
||||
FILE* fin = fopen("savefile.txt", "r");
|
||||
if (fin == NULL) {
|
||||
puts("Невозможно открыть файл");
|
||||
return;
|
||||
}
|
||||
|
||||
fscanf_s(fin, "%d%d", &m, &n);
|
||||
if (m > MAX_M || n > MAX_N) {
|
||||
printf("Слишком большой массив в файле!");
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
fscanf_s(fin, "%d", &arr[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fin);
|
||||
}
|
||||
|
||||
void duplicateColumn(int column) {
|
||||
insertColumn(column);
|
||||
for (int i = 0; i < m; i++) {
|
||||
arr[i][column] = arr[i][column + 1];
|
||||
}
|
||||
}
|
||||
|
||||
int count(int num, int arr[], int len) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (arr[i] == num) count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void row0() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
if (count(0, arr[i], n) > 2) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void duplicateRow(int row) {
|
||||
insertRow(row);
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[row][j] = arr[row + 1][j];
|
||||
}
|
||||
}
|
||||
|
||||
void dupRow0() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
if (count(0, arr[i], n) > 0) {
|
||||
duplicateRow(i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
SetConsoleCP(1251);
|
||||
SetConsoleOutputCP(1251);
|
||||
|
||||
int tmp;
|
||||
|
||||
int n = 0;
|
||||
do {
|
||||
puts("\n");
|
||||
printf("Содержимое массива:\n");
|
||||
printarr(arr);
|
||||
puts("Выберите программу");
|
||||
puts("1) Заполнение значениями i * 10 + j");
|
||||
puts("2) Заполнение нулями");
|
||||
puts("3) Заполнение случайными числами от 0 до 9");
|
||||
puts("4) Найти минимальный элемент");
|
||||
puts("5) Удалить строку");
|
||||
puts("6) Добавить пустую колонку");
|
||||
puts("7) Все нечетные увеличить в 10 раз");
|
||||
puts("8) Все кратные 10 уменьшить в 10 раз");
|
||||
puts("9) Ввести массив с клавиатуры");
|
||||
puts("10) Сохранить в файл");
|
||||
puts("11) Загрузить из файла");
|
||||
puts("12) Вставить пустую колонку");
|
||||
puts("13) Продублировать заданный столбец массива");
|
||||
puts("14) Обнулить элементы тех строк, в которых встречается более двух нулевых элементов");
|
||||
puts("15) Продублировать те строки, в которых встречаются нулевые элементы");
|
||||
puts("");
|
||||
puts("0) Выйти из программы");
|
||||
|
||||
while (scanf_s(" %d", &n) != 1) {
|
||||
scanf_s("%*[^\n]");
|
||||
scanf_s("%*c");
|
||||
}
|
||||
puts("");
|
||||
|
||||
switch (n)
|
||||
{
|
||||
case 1:
|
||||
fill_with_indexes();
|
||||
break;
|
||||
case 2:
|
||||
fillZero();
|
||||
break;
|
||||
case 3:
|
||||
fillRand();
|
||||
break;
|
||||
case 4:
|
||||
findMin();
|
||||
break;
|
||||
case 5:
|
||||
printf("Какую строку удалить: ");
|
||||
scanf_s("%d", &tmp);
|
||||
deleteRow(tmp);
|
||||
break;
|
||||
case 6:
|
||||
addColumn();
|
||||
break;
|
||||
case 7:
|
||||
odd10x();
|
||||
break;
|
||||
case 8:
|
||||
crat10();
|
||||
break;
|
||||
case 9:
|
||||
inputArr();
|
||||
break;
|
||||
case 10:
|
||||
save();
|
||||
break;
|
||||
case 11:
|
||||
load();
|
||||
break;
|
||||
case 12:
|
||||
printf("На каком индексе вставить колонку: ");
|
||||
scanf_s("%d", &tmp);
|
||||
insertColumn(tmp);
|
||||
break;
|
||||
case 13:
|
||||
printf("На каком индексе одублировать столбец: ");
|
||||
scanf_s("%d", &tmp);
|
||||
duplicateColumn(tmp);
|
||||
break;
|
||||
case 14:
|
||||
row0();
|
||||
break;
|
||||
case 15:
|
||||
dupRow0();
|
||||
break;
|
||||
case 0:
|
||||
puts("Досвидания :3");
|
||||
break;
|
||||
default:
|
||||
puts("Ошибка: неправильное N");
|
||||
break;
|
||||
}
|
||||
} while (n != 0);
|
||||
return 0;
|
||||
}
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <Windows.h>
|
||||
|
||||
#define MAX_M 10
|
||||
#define MAX_N 10
|
||||
|
||||
int m = 3;
|
||||
int n = 3;
|
||||
|
||||
int arr[MAX_M][MAX_N] = {
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9}
|
||||
};
|
||||
|
||||
void printarr() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
printf("%2d ", arr[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void fill_with_indexes() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = (i * 10) + j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fillZero() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fillRand() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = rand() % 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void findMin() {
|
||||
int min = arr[0][0];
|
||||
int iMin = 0;
|
||||
int jMin = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
if (arr[i][j] < min) {
|
||||
printf("min = %d\n", arr[i][j]);
|
||||
printf("imin = %d\n", i);
|
||||
printf("jmin = %d\n", j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void deleteRow(int row) {
|
||||
for (int i = row; i < m - 1; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = arr[i + 1][j];
|
||||
}
|
||||
}
|
||||
m--;
|
||||
}
|
||||
|
||||
void addColumn() {
|
||||
if (n + 1 <= MAX_N) {
|
||||
for (int i = 0; i < m; i++) {
|
||||
arr[i][n] = 0;
|
||||
}
|
||||
n++;
|
||||
}
|
||||
else {
|
||||
printf("Достигнут максимум колонок!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void insertColumn(int column) {
|
||||
if (n + 1 <= MAX_N) {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = n; j > column; j--) {
|
||||
arr[i][j] = arr[i][j - 1];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < m; i++) {
|
||||
arr[i][column] = 0;
|
||||
}
|
||||
n++;
|
||||
}
|
||||
else {
|
||||
printf("Достигнут максимум колонок!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void insertRow(int row) {
|
||||
if (m + 1 <= MAX_M) {
|
||||
for (int i = m; i > row; i--) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = arr[i - 1][j];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
arr[row][i] = 0;
|
||||
}
|
||||
m++;
|
||||
}
|
||||
else {
|
||||
printf("Достигнут максимум строк!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void odd10x() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (arr[i][j] % 2 != 0) arr[i][j] *= 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void crat10() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (arr[i][j] % 10 == 0) arr[i][j] /= 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void inputArr() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
printf("a%d%d = ", i, j);
|
||||
scanf_s("%d", &arr[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void save() {
|
||||
FILE* fout = fopen("savefile.txt", "w");
|
||||
if (fout == NULL) {
|
||||
puts("Невозможно открыть файл");
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(fout, "%d %d\n", m, n);
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
fprintf(fout, "%2d ", arr[i][j]);
|
||||
}
|
||||
fprintf(fout, "\n");
|
||||
}
|
||||
|
||||
fclose(fout);
|
||||
}
|
||||
|
||||
void load() {
|
||||
FILE* fin = fopen("savefile.txt", "r");
|
||||
if (fin == NULL) {
|
||||
puts("Невозможно открыть файл");
|
||||
return;
|
||||
}
|
||||
|
||||
fscanf_s(fin, "%d%d", &m, &n);
|
||||
if (m > MAX_M || n > MAX_N) {
|
||||
printf("Слишком большой массив в файле!");
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
fscanf_s(fin, "%d", &arr[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fin);
|
||||
}
|
||||
|
||||
void duplicateColumn(int column) {
|
||||
insertColumn(column);
|
||||
for (int i = 0; i < m; i++) {
|
||||
arr[i][column] = arr[i][column + 1];
|
||||
}
|
||||
}
|
||||
|
||||
int count(int num, int arr[], int len) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (arr[i] == num) count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void row0() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
if (count(0, arr[i], n) > 2) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void duplicateRow(int row) {
|
||||
insertRow(row);
|
||||
for (int j = 0; j < n; j++) {
|
||||
arr[row][j] = arr[row + 1][j];
|
||||
}
|
||||
}
|
||||
|
||||
void dupRow0() {
|
||||
for (int i = 0; i < m; i++) {
|
||||
if (count(0, arr[i], n) > 0) {
|
||||
duplicateRow(i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
SetConsoleCP(1251);
|
||||
SetConsoleOutputCP(1251);
|
||||
|
||||
int tmp;
|
||||
|
||||
int n = 0;
|
||||
do {
|
||||
puts("\n");
|
||||
printf("Содержимое массива:\n");
|
||||
printarr(arr);
|
||||
puts("Выберите программу");
|
||||
puts("1) Заполнение значениями i * 10 + j");
|
||||
puts("2) Заполнение нулями");
|
||||
puts("3) Заполнение случайными числами от 0 до 9");
|
||||
puts("4) Найти минимальный элемент");
|
||||
puts("5) Удалить строку");
|
||||
puts("6) Добавить пустую колонку");
|
||||
puts("7) Все нечетные увеличить в 10 раз");
|
||||
puts("8) Все кратные 10 уменьшить в 10 раз");
|
||||
puts("9) Ввести массив с клавиатуры");
|
||||
puts("10) Сохранить в файл");
|
||||
puts("11) Загрузить из файла");
|
||||
puts("12) Вставить пустую колонку");
|
||||
puts("13) Продублировать заданный столбец массива");
|
||||
puts("14) Обнулить элементы тех строк, в которых встречается более двух нулевых элементов");
|
||||
puts("15) Продублировать те строки, в которых встречаются нулевые элементы");
|
||||
puts("");
|
||||
puts("0) Выйти из программы");
|
||||
|
||||
while (scanf_s(" %d", &n) != 1) {
|
||||
scanf_s("%*[^\n]");
|
||||
scanf_s("%*c");
|
||||
}
|
||||
puts("");
|
||||
|
||||
switch (n)
|
||||
{
|
||||
case 1:
|
||||
fill_with_indexes();
|
||||
break;
|
||||
case 2:
|
||||
fillZero();
|
||||
break;
|
||||
case 3:
|
||||
fillRand();
|
||||
break;
|
||||
case 4:
|
||||
findMin();
|
||||
break;
|
||||
case 5:
|
||||
printf("Какую строку удалить: ");
|
||||
scanf_s("%d", &tmp);
|
||||
deleteRow(tmp);
|
||||
break;
|
||||
case 6:
|
||||
addColumn();
|
||||
break;
|
||||
case 7:
|
||||
odd10x();
|
||||
break;
|
||||
case 8:
|
||||
crat10();
|
||||
break;
|
||||
case 9:
|
||||
inputArr();
|
||||
break;
|
||||
case 10:
|
||||
save();
|
||||
break;
|
||||
case 11:
|
||||
load();
|
||||
break;
|
||||
case 12:
|
||||
printf("На каком индексе вставить колонку: ");
|
||||
scanf_s("%d", &tmp);
|
||||
insertColumn(tmp);
|
||||
break;
|
||||
case 13:
|
||||
printf("На каком индексе одублировать столбец: ");
|
||||
scanf_s("%d", &tmp);
|
||||
duplicateColumn(tmp);
|
||||
break;
|
||||
case 14:
|
||||
row0();
|
||||
break;
|
||||
case 15:
|
||||
dupRow0();
|
||||
break;
|
||||
case 0:
|
||||
puts("Досвидания :3");
|
||||
break;
|
||||
default:
|
||||
puts("Ошибка: неправильное N");
|
||||
break;
|
||||
}
|
||||
} while (n != 0);
|
||||
return 0;
|
||||
}
|
||||
|
@ -110,7 +110,7 @@
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)\lib</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>raylib.lib;opengl32.lib;winmm.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>raylib33test.lib;opengl32.lib;winmm.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
@ -129,7 +129,7 @@
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)\lib</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>raylib.lib;opengl32.lib;winmm.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>raylib33test.lib;opengl32.lib;winmm.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<EntryPointSymbol>mainCRTStartup</EntryPointSymbol>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
|
BIN
lab16 with raylib/lib/raylib33test.lib
Normal file
BIN
lab16 with raylib/lib/raylib33test.lib
Normal file
Binary file not shown.
@ -57,7 +57,7 @@ void* ArenaAlloc(Arena* arena, size_t size)
|
||||
return ArenaAllocAligned(arena, size, DEFAULT_ALIGNMENT);
|
||||
}
|
||||
|
||||
Arena ArenaInit(size_t buffer_size)
|
||||
Arena ArenaCreate(size_t buffer_size)
|
||||
{
|
||||
void* buffer = SafeMalloc(buffer_size);
|
||||
return (Arena) {
|
||||
|
@ -15,7 +15,7 @@ typedef struct {
|
||||
uintptr_t offset_ptr;
|
||||
} Arena;
|
||||
|
||||
Arena ArenaInit(size_t buffer_size);
|
||||
Arena ArenaCreate(size_t buffer_size);
|
||||
void* ArenaAlloc(Arena* arena, size_t size);
|
||||
void* ArenaAllocAligned(Arena* arena, size_t size, size_t alignment);
|
||||
void ArenaDestroy(Arena* arena);
|
@ -1,15 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <threads.h>
|
||||
|
||||
void thrd_func(int* i) {
|
||||
printf_s("%d\n", *i);
|
||||
}
|
||||
|
||||
int main() {
|
||||
thrd_t* threads[10];
|
||||
for (int i = 0; i < 10; i++) {
|
||||
thrd_create(threads[i], thrd_func, &i);
|
||||
}
|
||||
return 0;
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <iso646.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <threads.h>
|
||||
|
||||
int thrd_func(void* i) {
|
||||
int* arg_ptr = (int*)i;
|
||||
|
||||
unsigned long long y = 1;
|
||||
for (unsigned long long x = 0; x < 1000ULL * 1000 * 1000 * 10; x++) {
|
||||
y += x;
|
||||
}
|
||||
|
||||
printf("lol %d) y=%llu\n", *arg_ptr, y);
|
||||
return *arg_ptr;
|
||||
}
|
||||
|
||||
int main() {
|
||||
thrd_t threads[10];
|
||||
int args[10];
|
||||
int results[10];
|
||||
for (int i = 0; i < 10; i++) {
|
||||
args[i] = i;
|
||||
thrd_create(&threads[i], thrd_func, &args[i]);
|
||||
printf("%d) thread created\n", i);
|
||||
}
|
||||
printf("-----------\n");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
thrd_join(threads[i], &results[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,135 +1,137 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{0d89d366-f1ee-4ff3-af21-0e209449319e}</ProjectGuid>
|
||||
<RootNamespace>threads</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.c" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{0d89d366-f1ee-4ff3-af21-0e209449319e}</ProjectGuid>
|
||||
<RootNamespace>threads</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard_C>stdc17</LanguageStandard_C>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.c" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user