migf1 έγραψε:Ναι, αυτό το [48]= δεν θυμόμουν (σε 2D array πως λειτουργεί; ).
- Κώδικας: Επιλογή όλων
int table[5][5] = { {3, 4}, [2][1] = 12, 27, [4][0] = 14 };
Συντονιστής: konnn
migf1 έγραψε:Ναι, αυτό το [48]= δεν θυμόμουν (σε 2D array πως λειτουργεί; ).
int table[5][5] = { {3, 4}, [2][1] = 12, 27, [4][0] = 14 };
enum Piece { EMPTY=0, P, N, B, R, Q, K }; /* pawn, knight, bishop, rook, queen, king*/
enum Color { BL=0, WH };
typedef struct Square {
enum Piece piece;
enum Color color;
} Square;
int main( void )
{
Square board[8][8] = {
{ {R,BL}, {N,BL}, {B,BL}, {K,BL}, {Q,BL}, {B,BL}, {N,BL}, {R,BL} },
{ {P,BL}, {P,BL}, {P,BL}, {P,BL}, {P,BL}, {P,BL}, {P,BL}, {P,BL} },
{ {EMPTY,BL} },
{ {EMPTY,BL} },
{ {EMPTY,BL} },
{ {EMPTY,BL} },
{ {P,WH}, {P,WH}, {P,WH}, {P,WH}, {P,WH}, {P,WH}, {P,WH}, {P,WH} },
{ {R,WH}, {N,WH}, {B,WH}, {K,WH}, {Q,WH}, {B,WH}, {N,WH}, {R,WH} }
};

#include <stdio.h>
#include <stdlib.h>
enum Piece {EMPTY=0, P, N, B, R, Q, K}; /* pawn, knight, bishop, rook, queen, king*/
enum Color { BL=0, WH };
typedef struct Square {
enum Piece piece;
enum Color color;
char cout;
} Square;
/* ------------------------------------------------------
*
* ------------------------------------------------------
*/
void board_print( Square board[][8] )
{
if ( !board ) {
puts("*** internal error: invalid parameter...");
return;
}
for (int i=0; i < 8; i++)
{
for (int j=0; j < 8; j++ ) {
char c = board[i][j].cout;
if ( '\0' == c )
printf( ". " );
else
printf( "%c ", c );
}
puts("\b");
}
return;
}
/* ------------------------------------------------------
*
* ------------------------------------------------------
*/
int main( void )
{
Square board[8][8] = {
{ /* black back-row*/
{R,BL,'R'}, {N,BL,'K'}, {B,BL,'B'}, {K,BL,'K'},
{Q,BL,'Q'}, {B,BL,'B'}, {N,BL,'N'}, {R,BL,'R'}
},
{ /* black fornt-row */
{P,BL,'P'}, {P,BL,'P'}, {P,BL,'P'}, {P,BL,'P'},
{P,BL,'P'}, {P,BL,'P'}, {P,BL,'P'}, {P,BL,'P'}
},
{ {EMPTY,BL,'\0'} },
{ {EMPTY,BL,'\0'} },
{ {EMPTY,BL,'\0'} },
{ {EMPTY,BL,'\0'} },
{ /* white front-row */
{P,WH,'p'}, {P,WH,'p'}, {P,WH,'p'}, {P,WH,'p'},
{P,WH,'p'}, {P,WH,'p'}, {P,WH,'p'}, {P,WH,'p'}
},
{ /* white back-row */
{R,WH,'r'}, {N,WH,'n'}, {B,WH,'b'}, {K,WH,'k'},
{Q,WH,'q'}, {B,WH,'b'}, {N,WH,'n'}, {R,WH,'r'}
}
};
board_print( board );
system("pause"); /* windows only */
exit( EXIT_SUCCESS );
}


stamatiou έγραψε:Πήγα να κάνω μια υλοποίηση με έναν άλλο αλγόριθμο αλλά για κάποιο λόγο δεν μου εμφανίζει ούτε την έξοδο :/
- Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_VALUE 2000000001L
int main(void) {
register long int counter, main_num, num_to_add,num_to_stop, i1, i2;
long int *array, smallest[3], size;
FILE *input, *output;
if(!(input = fopen("operators.in","r"))) {
printf("Failed to open operators.in\n");
exit(EXIT_FAILURE);
}
if(!(output = fopen("operators.out","w"))) {
printf("Failed to open operators.out\n");
exit(EXIT_FAILURE);
}
smallest[0] = MAX_VALUE;
fscanf(input,"%ld",&size);
if(!(array = (long int *) malloc(size * sizeof(long int)))) {
printf("Failed to allocate memory for input\n");
exit(EXIT_FAILURE);
}
array[0] = 0;
counter = 1;
fscanf(input, "%ld", &array[0]);
printf("Inputting:\t%ld\n",array[0]);
do {
fscanf(input, "%ld", &array[counter]);
printf("Inputing:\t%ld\n",array[counter]);
printf("Inputing:\t%ld\n",array[counter + 1]);
i1 = labs(array[0] + array[counter]);
i2 = labs(array[0] + array[counter + 1]);
counter += 2;
}while(counter - 2 < size && i1 > i2);
array = (long int *) realloc((long int *) array, ((size = counter) * sizeof(long int)));
for(main_num = 0; main_num < size; main_num++) {
for(num_to_add = main_num + 1; num_to_add < size && num_to_add <= num_to_stop; num_to_add++) {
printf("Processing:\t%ld %ld\n",array[counter],array[counter + 1]);
if((labs((array[main_num] + array[num_to_add]))) < (labs(array[main_num] + array[num_to_add + 1]))) {
smallest[0] = labs(array[main_num] + array[num_to_add]);
smallest[1] = array[main_num];
smallest[2] = array[num_to_add];
num_to_stop = num_to_add;
}
}
}
fprintf(output,"/---------------\nMinsum:\t%ld %ld",smallest[1],smallest[2]);
free(array);
exit(EXIT_SUCCESS);
}

alkismavridis έγραψε:Καλησπέρα!
Έχω την εξής απορία: τι γίνεται με τα σφάλματα (exceptions) στη c;
Πρέπει κάθε φορά να βάζω if blocks για κάθε διαίρεση, άνοιγμα αρχείου κτλ κτλ ή υπάρχει τρόπος να "συνεχίσω" το πρόγραμμα από κάπου όταν συμβεί κάτι περίεργο;;
Ψάχνοντας στο google βρίκα για τις συναρτήσεις setjmp κτλ, αλλά και πάλι τα if blocks δε τα γλιτώνεις..
Ευχαριστώ!




