//cc -lm -o gg ~/gg6.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <stdbool.h>
struct Grid_Graph {
unsigned int cols_m; //number of columns as m
struct {
bool cols_m_parity; //columns m parity bit
unsigned int cols_m_plus_1;
unsigned int cols_m_minus_1;
unsigned int cols_m_minus_2;
};
unsigned int rows_n; //number of rows as n
struct {
bool rows_n_parity; //rows n parity bit
unsigned int rows_n_plus_1;
unsigned int rows_n_minus_1;
};
unsigned int spaces; //m*n as vertex spaces
} gg;
void clear_screen() {
int system(const char *command);
system("clear");
return;
}
unsigned int cli_prompts(unsigned int stage, unsigned int type) {
unsigned int passes = 0; //loop safety control
switch (stage) {
case 0: //get the dimensions of the grid graph
clear_screen();
printf("Grid Graph Dimensions (range 3-to-2000)\n");
printf("columns: %u, rows: %u, spaces: %u\n", gg.cols_m, gg.rows_n, gg.spaces);
printf("%s: ", type == 0 ? "Enter number of columns (m)": "Enter number of rows (n)");
while(1) {
if (passes > 2) {
clear_screen();
printf("gg: Too many attempts without a valid response.\nGoodbye.\n");
break;
}
int input_status = scanf("%u", &type);
if (input_status == 1) {
if (type > 2 && type <= 2000) { break; }
}
while(getchar() != '\n');
type = 0; passes++;
}
break;
}
return type;
}
void calculate_center() {
unsigned int half = floor((gg.spaces + 1) / 2);
unsigned int halves = half + 1;
unsigned int tile1 = half - floor(half / gg.rows_n);
unsigned int tile2 = tile1 + 1;
unsigned int tile3 = tile1 + gg.cols_m;
unsigned int tile4 = tile3 + 1;
printf("Medial spaces\n");
if (gg.cols_m_parity) { //warning: bool with switch.
if (gg.rows_n_parity) {
printf(" count: 4\n id: %u, %u, %u, %u\n position: stack\n", tile1, tile2, tile3, tile4);
} else {
printf(" count: 2\n id here: %u, %u\n position: horizontal\n", half, halves);
}
} else {
if (gg.rows_n_parity) {
printf(" count: 2\n id: %u, %u\n position: vertical\n", tile1, tile3);
} else {
printf(" count: 1\n id: %u\n position: center\n", half);
}
}
return;
}
int main(void) {
gg.cols_m = cli_prompts(0, 0); if (!gg.cols_m) { return 1; }
gg.rows_n = cli_prompts(0, 1); if (!gg.rows_n) { return 1; }
gg.cols_m_plus_1 = gg.cols_m + 1;
gg.cols_m_minus_1 = gg.cols_m - 1;
gg.cols_m_minus_2 = gg.cols_m - 2;
gg.rows_n_plus_1 = gg.rows_n + 1;
gg.rows_n_minus_1 = gg.rows_n - 1;
gg.spaces = gg.cols_m * gg.rows_n;
gg.cols_m_parity = gg.cols_m %2 ? 0: 1;
gg.rows_n_parity = gg.rows_n %2 ? 0: 1;
clear_screen();
printf("columns: %u, rows: %u, spaces: %u\n", gg.cols_m, gg.rows_n, gg.spaces);
printf("m parity: %d\nn parity: %d\n", gg.cols_m_parity, gg.rows_n_parity);
printf("\n");
calculate_center();
printf("\nm+1= %u, n+1= %u\n", gg.cols_m_plus_1, gg.rows_n_plus_1);
printf("m-1= %u, n-1= %u\n", gg.cols_m_minus_1, gg.rows_n_minus_1);
//to test exit status from sh/bourne shell: echo $?
return 0;
}