First of all, thank you very much for your help. My code finally starts working.
But I have a little problem - I do not entirely understand how. At first, just after opening the file, I set all the outputs to unknown (-1).
Code:
for (i=0;i<=GATES_SIZE;i++)
{
gates[i].out = -1; // -1 is unknown
}
Then I started the loop. But I think I made it a little messy. I made a loop reading it and I evaluated what I could at that time, then I made second loop evaluating it again. I think that's not what you've meant, but I couldn't get it other way. Here's how the reading + evaluating loop looks like:
Code:
while (fscanf(file, "%d %s ", &i, gate_type_string) == 2 ) {
// The gate number, i, is used as the index into the gates array
gates[i].type = gate_type_string[0];
switch (gates[i].type)
{
case 'T':
gates[i].out = 1;
break;
case 'F':
gates[i].out = 0;
break;
case 'N': // if gate is NOT then it needs one input, so I read one more variable
fscanf(file,"%d", &gates[i].in1);
buffn = gates[i].in1; //buff is the number of input gate
if (gates[buffn].out != -1) //if its known then negate the output
{
gates[i].out=!gates[buffn].out;
}
break;
case 'X': //if it's X, A or O it needs to read 2 inputs
fscanf(file,"%d %d",&gates[i].in1, &gates[i].in2);
buffx1 = gates[i].in1;
buffx2 = gates[i].in2;
if (gates[buffx1].out != -1 && gates[buffx2].out != -1)
{
gates[i].out = gates[buffx1].out ^ gates[buffx2].out;
}
break;
case 'O':
fscanf(file,"%d %d",&gates[i].in1, &gates[i].in2);
buffo1 = gates[i].in1;
buffo2 = gates[i].in2;
if ( gates[buffo1].out != -1 && gates[buffo2].out != -1)
{
gates[i].out = gates[buffo1].out || gates[buffo2].out;
}
break;
case 'A':
fscanf(file,"%d %d", &gates[i].in1, &gates[i].in2);
buffa1 = gates[i].in1;
buffa2 = gates[i].in2;
if (gates[buffa1].out != -1 && gates[buffa2].out != -1)
{
gates[i].out = gates[buffa1].out && gates[buffa2].out;
}
break;
default: //if gate is not T/F/N/X/O/A
printf("Bad gate type: %c\n",gates[i].type); //then say which letter wasn't correct
exit(1); //and exit the program
}
}
imax=i; //imax has number of the last gate now
puts("nr tp in1 in2 out\n");
for (i=1;i <= imax; i++) // loop evaluating the values
{
if (gates[i].type == 'N')
{
buffn=gates[i].in1;
if (gates[i].out != -1)
gates[i].out = !gates[buffn].out;
}
if (gates[i].type == 'A')
{
int buffa1,buffa2;
buffa1=gates[i].in1;
buffa2=gates[i].in2;
if (gates[buffa1].out != -1 && gates[buffa2].out != -1)
gates[i].out = gates[buffa1].out && gates[buffa2].out;
}
if (gates[i].type == 'O')
{
int buffo1,buffo2;
buffo1=gates[i].in1;
buffo2=gates[i].in2;
if (gates[buffo1].out != -1 && gates[buffo2].out != -1)
gates[i].out = gates[buffo1].out || gates[buffo2].out;
}
if (gates[i].type == 'X')
{
int buffx1,buffx2;
buffx1=gates[i].in1;
buffx2=gates[i].in2;
if (gates[buffx1].out != -1 && gates[buffx2].out != -1)
gates[i].out = gates[buffx1].out ^ gates[buffx2].out;
}
if (gates[i].out==0) // printing the whole array with gates information
{
printf("%d %3c %3d %3d FALSE\n", i, gates[i].type, gates[i].in1, gates[i].in2);
}
else if (gates[i].out==1){
printf("%d %3c %3d %3d TRUE\n", i, gates[i].type, gates[i].in1, gates[i].in2 );
}
}
Point is: this stuff works only if I delete the part of code where I set everything to -1. It's weird for me. Also, I still don't get why everything can't be evaluated in the first loop. This thing that I've done here works but isn't fully understandable by me. (unless it doesn't, but I checked it on quite complex circuit examples and it evaluated everything properly).
It's strange for me, that I actually evaluate AND/XOR/NOT/OR values at reading loop and evaluating loop at once. Also, I am aware that I need to write some code detecting loops in circuits, or some errors, but that's for later.