#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef enum {
    NPN,
    PNP
} TransistorType;

typedef struct {
    double emitter;
    double base;
    double collector;
    double limit;
    TransistorType type;
} Transistor;

double random_double(double min, double max) {
    return min + ((double)rand() / (double)RAND_MAX) * (max - min);
}

int main(void) {
    srand((unsigned int)time(NULL));

    Transistor T;
    T.emitter   = random_double(0.0, 5.0);
    T.base      = random_double(0.0, 5.0);
    T.collector = random_double(0.0, 5.0);
    T.limit     = 0.7; // Vbe forward bias threshold
    T.type      = (rand() % 2 == 0) ? NPN : PNP;

    int is_on = 0;
    if (T.type == NPN && (T.base - T.emitter) > T.limit) {
        is_on = 1;
    } else if (T.type == PNP && (T.emitter - T.base) > T.limit) {
        is_on = 1;
    }

    printf("=== TRANSISTOR STATE ===\n");
    printf("Type:            %s\n", (T.type == NPN) ? "NPN" : "PNP");
    printf("Base (Vb):       %.2f V\n", T.base);
    printf("Emitter (Ve):    %.2f V\n", T.emitter);
    printf("Collector (Vc):  %.2f V\n", T.collector);
    printf("Vbe (Vb - Ve):   %.2f V\n", T.base - T.emitter);
    printf("Limit (Vth):     %.2f V\n", T.limit);

    if (is_on) {
        printf("Channel:         ----//--->\n");
        printf("Status:          TRANSISTOR IS ON\n");
    } else {
        printf("Channel:         ----/  /----\n");
        printf("Status:          TRANSISTOR IS OFF\n");
    }

    return 0;
}