AfanasevGad4

This is a task for our favourite professor
git clone git://git.stellar-nexus.ru/AfanasevGad4
Log | Files | Refs

something.c (1720B)


      1 #include <stdio.h>
      2 
      3 #include "die.h"
      4 
      5 #define NUMBER_AMOUNT		4
      6 #define SENTINEL_NUMBER		-1
      7 #define SET_EMPTY_ARRAY(x)	x[0] = SENTINEL_NUMBER
      8 
      9 struct Number {
     10 	unsigned int n;
     11 	int p[NUMBER_AMOUNT];
     12 	unsigned int status; /* 0, 1, 2 */
     13 };
     14 
     15 const int
     16 GetLastIndex(int arr[])
     17 {
     18 	int i;
     19 	for (i = 0; arr[i] != SENTINEL_NUMBER; ++i);
     20 	return i;
     21 }
     22 
     23 void
     24 ProcessUserInput(struct Number p)
     25 {
     26 	for (int i = 0; i < NUMBER_AMOUNT; ++i) {
     27 		if (N[i].n == p.n) {
     28 			switch (p.status) {
     29 			case 0:
     30 				die("Double nothing");
     31 			case 1:
     32 				switch (N[i].status) {
     33 				case 0:
     34 					die("Nothing is turning into a cow");
     35 				case 2:
     36 					die("Bull is turning into a cow");
     37 				}
     38 				
     39 				N[i].p[GetLastIndex(N[i].p) + 1] = SENTINEL_NUMBER;
     40 				N[i].p[GetLastIndex(N[i].p)] = p.p[0];
     41 				if (GetLastIndex(N[i].p) == 3)
     42 					N[i].status = 2;
     43 				break;
     44 			case 2:
     45 				switch (N[i].status) {
     46 				case 0:
     47 					die("Null is bull!");
     48 				case 2:
     49 					die("Double bull");
     50 				}
     51 				
     52 				for (int j = 0, index = 0; j < NUMBER_AMOUNT; ++j, ++index) {
     53 					if (j == p.p[0]) {
     54 						N[i].p[index--] = j;
     55 					}
     56 					N[i].p[index] = j;
     57 				}
     58 				break;
     59 			default:
     60 				die("Status unknown, index exists");
     61 			}
     62 		}
     63 	}
     64 	
     65 	for (int i = 0; i < NUMBER_AMOUNT; ++i) {
     66 		if (GetLastIndex(N[i].p) == 0) {
     67 			switch (p.status) {
     68 			case 0:
     69 				for (int j = 0; j < NUMBER_AMOUNT; ++j)
     70 					N[i].p[j] = j;
     71 				break;
     72 			case 1:
     73 				N[i].p[0] = p.p[0];
     74 				N[i].p[1] = SENTINEL_NUMBER;
     75 				break;
     76 			case 2:
     77 				for (int j = 0, index = 0; j < NUMBER_AMOUNT; ++j, ++index) {
     78 					if (j == p.p[0]) {
     79 						N[i].p[index--] = j;
     80 						continue;
     81 					}
     82 					N[i].p[index] = j;
     83 				}
     84 				break;
     85 			default:
     86 				die("Status unknown, index nonexistent");
     87 			}
     88 		}
     89 	}
     90 }