Wpis z mikrobloga

Oczy krwawią. Aż się chce im wysłać testowirona: "aaaaAAAAAhhhh khhhhuurrrwaaa!!!" To poniżej jest i tak jest od 3 klasy wyżej niż to co właśnie oglądam na review...

#programista15k #sztucznainteligencja #cojapacze

Prompt AI:

Write code in C that sends request to redis server using hiredis library and parses response. Make the request data to be number of mosquitos per second flying over a keybord in Bangalore. Make it look like written by typical VIM developer from India. Add a lot of objects and copy-paste-code. All shoult take at least 300 lines of code.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <hiredis/hiredis.h>

// ------------------- Mosquito Data Structures -------------------

typedef struct {
char city[32];
int population;
char weather[32];
} Bangalore;

typedef struct {
char brand[32];
int keys;
char type[32];
} Keyboard;

typedef struct {
char species[32];
float speed;
float size;
int buzz;
} Mosquito;

typedef struct {
int count;
} MosquitoCounter;

typedef struct {
int last_count;
} MosquitoSensor;

typedef struct {
Bangalore *location;
Keyboard *keyboard;
int count;
} MosquitoData;

typedef struct {
MosquitoData *data;
} MosquitoData1;
typedef struct {
MosquitoData *data;
} MosquitoData2;
typedef struct {
MosquitoData *data;
} MosquitoData3;
typedef struct {
MosquitoData *data;
} MosquitoData4;
typedef struct {
MosquitoData *data;
} MosquitoData5;
typedef struct {
MosquitoData *data;
} MosquitoData6;
typedef struct {
MosquitoData *data;
} MosquitoData7;
typedef struct {
MosquitoData *data;
} MosquitoData8;
typedef struct {
MosquitoData *data;
} MosquitoData9;
typedef struct {
MosquitoData *data;
} MosquitoData10;

// More copy-paste for fun
typedef struct {
MosquitoData *data;
} MosquitoData11;
typedef struct {
MosquitoData *data;
} MosquitoData12;
typedef struct {
MosquitoData *data;
} MosquitoData13;
typedef struct {
MosquitoData *data;
} MosquitoData14;
typedef struct {
MosquitoData *data;
} MosquitoData15;
typedef struct {
MosquitoData *data;
} MosquitoData16;
typedef struct {
MosquitoData *data;
} MosquitoData17;
typedef struct {
MosquitoData *data;
} MosquitoData18;
typedef struct {
MosquitoData *data;
} MosquitoData19;
typedef struct {
MosquitoData *data;
} MosquitoData20;

// ------------------- Mosquito Utility Functions -------------------

void init_bangalore(Bangalore *b) {
strcpy(b->city, "Bangalore");
b->population = 13000000;
strcpy(b->weather, "humid");
}

void init_keyboard(Keyboard *k) {
strcpy(k->brand, "Logitech");
k->keys = 104;
strcpy(k->type, "mechanical");
}

void init_mosquito(Mosquito *m) {
strcpy(m->species, "Aedes");
m->speed = (float)(rand() % 200) / 100.0f + 1.0f;
m->size = (float)(rand() % 300) / 100.0f + 2.0f;
m->buzz = 1;
}

void init_counter(MosquitoCounter *c) {
c->count = 0;
}

void increment_counter(MosquitoCounter *c, int n) {
c->count += n;
}

void reset_counter(MosquitoCounter *c) {
c->count = 0;
}

int get_counter(MosquitoCounter *c) {
return c->count;
}

void init_sensor(MosquitoSensor *s) {
s->last_count = 0;
}

int read_sensor(MosquitoSensor *s) {
int count = rand() % 10;
s->last_count = count;
return count;
}

MosquitoData *create_mosquito_data(Bangalore *b, Keyboard *k, int count) {
MosquitoData *data = (MosquitoData *)malloc(sizeof(MosquitoData));
data->location = b;
data->keyboard = k;
data->count = count;
return data;
}

void print_mosquito_data(MosquitoData *data) {
printf("Mosquitos/sec: %d at %s on %s keyboard\n",
data->count, data->location->city, data->keyboard->brand);
}

// ------------------- Redis Client Wrapper -------------------

typedef struct {
redisContext *context;
} RedisClient;

void redis_client_init(RedisClient *client, const char *host, int port) {
client->context = redisConnect(host, port);
if (client->context == NULL || client->context->err) {
if (client->context) {
printf("Redis connection error: %s\n", client->context->errstr);
} else {
printf("Redis connection error: can't allocate redis context\n");
}
exit(1);
}
}

void redis_client_close(RedisClient *client) {
if (client->context) {
redisFree(client->context);
client->context = NULL;
}
}

void redis_client_set(RedisClient *client, const char *key, const char *value) {
redisReply *reply = redisCommand(client->context, "SET %s %s", key, value);
if (reply == NULL) {
printf("SET command failed\n");
return;
}
printf("SET %s = %s: %s\n", key, value, reply->str ? reply->str : "(nil)");
freeReplyObject(reply);
}

char *redis_client_get(RedisClient *client, const char *key) {
redisReply *reply = redisCommand(client->context, "GET %s", key);
if (reply == NULL) {
printf("GET command failed\n");
return NULL;
}
char *result = NULL;
if (reply->type == REDIS_REPLY_STRING) {
result = strdup(reply->str);
}
printf("GET %s: %s\n", key, reply->str ? reply->str : "(nil)");
freeReplyObject(reply);
return result;
}

void redis_client_ping(RedisClient *client) {
redisReply *reply = redisCommand(client->context, "PING");
if (reply == NULL) {
printf("PING command failed\n");
return;
}
printf("PING: %s\n", reply->str ? reply->str : "(nil)");
freeReplyObject(reply);
}

// ------------------- Copy-Paste Logger/Analyzer -------------------

typedef struct {
char logs[100][256];
int log_count;
} MosquitoLogger;

void logger_init(MosquitoLogger *logger) {
logger->log_count = 0;
}

void logger_log(MosquitoLogger *logger, const char *msg) {
if (logger->log_count < 100) {
strncpy(logger->logs[logger->log_count], msg, 255);
logger->logs[logger->log_count][255] = '\0';
logger->log_count++;
}
printf("LOG: %s\n", msg);
}

typedef struct {
MosquitoData **history;
int history_count;
} MosquitoAnalyzer;

void analyzer_init(MosquitoAnalyzer *analyzer, MosquitoData **history, int count) {
analyzer->history = history;
analyzer->history_count = count;
}

void analyzer_analyze(MosquitoAnalyzer *analyzer) {
int total = 0;
for (int i = 0; i < analyzer->history_count; ++i) {
total += analyzer->history[i]->count;
}
float avg = analyzer->history_count ? (float)total / analyzer->history_count : 0.0f;
printf("Average mosquitos/sec: %.2f\n", avg);
}

typedef struct {
MosquitoData **history;
int history_count;
} MosquitoPrinter;

void printer_init(MosquitoPrinter *printer, MosquitoData **history, int count) {
printer->history = history;
printer->history_count = count;
}

void printer_print_all(MosquitoPrinter *printer) {
for (int i = 0; i < printer->history_count; ++i) {
print_mosquito_data(printer->history[i]);
}
}

// More copy-paste loggers
typedef MosquitoLogger MosquitoLogger2;
typedef MosquitoLogger MosquitoLogger3;
typedef MosquitoLogger MosquitoLogger4;
typedef MosquitoLogger MosquitoLogger5;

// ------------------- Main Application -------------------

#define HISTORY_SIZE 100

typedef struct {
Bangalore bangalore;
Keyboard keyboard;
MosquitoSensor sensor;
MosquitoCounter counter;
RedisClient redis;
MosquitoData *history[HISTORY_SIZE];
int history_count;
} MosquitoApp;

void mosquito_app_init(MosquitoApp *app) {
init_bangalore(&app->bangalore);
init_keyboard(&app->keyboard);
init_sensor(&app->sensor);
init_counter(&app->counter);
app->history_count = 0;
}

void mosquito_app_connect_redis(MosquitoApp *app) {
redis_client_init(&app->redis, "127.0.0.1", 6379);
redis_client_ping(&app->redis);
}

void mosquito_app_run(MosquitoApp *app, int iterations) {
mosquito_app_connect_redis(app);
for (int i = 0; i < iterations; ++i) {
int count = read_sensor(&app->sensor);
increment_counter(&app->counter, count);
MosquitoData *data = create_mosquito_data(&app->bangalore, &app->keyboard, count);
if (app->history_count < HISTORY_SIZE) {
app->history[app->history_count++] = data;
}
char key[64];
char value[32];
snprintf(key, sizeof(key), "mosquito:bangalore:keyboard:%d", i);
snprintf(value, sizeof(value), "%d", count);
redis_client_set(&app->redis, key, value);
// Sleep for realism
struct timespec ts = {0, 50000000};
nanosleep(&ts, NULL);
}
redis_client_close(&app->redis);
}

void mosquito_app_print_history(MosquitoApp *app) {
for (int i = 0; i < app->history_count; ++i) {
print_mosquito_data(app->history[i]);
}
}

// ------------------- Main Entry Point -------------------

int main() {
srand((unsigned int)time(NULL));
MosquitoApp app;
mosquito_app_init(&app);

MosquitoLogger logger;
MosquitoLogger2 logger2;
MosquitoLogger3 logger3;
MosquitoLogger4 logger4;
MosquitoLogger5 logger5;

logger_init(&logger);
logger_init(&logger2);
logger_init(&logger3);
logger_init(&logger4);
logger_init(&logger5);

logger_log(&logger, "Starting MosquitoApp in Bangalore...");
logger_log(&logger2, "Logger2 active.");
logger_log(&logger3, "Logger3 active.");
logger_log(&logger4, "Logger4 active.");
logger_log(&logger5, "Logger5 active.");

mosquito_app_run(&app, 100);
mosquito_app_print_history(&app);

MosquitoAnalyzer analyzer;
analyzer_init(&analyzer, app.history, app.history_count);
analyzer_analyze(&analyzer);

MosquitoPrinter printer;
printer_init(&printer, app.history, app.history_count);
printer_print_all(&printer);

logger_log(&logger, "MosquitoApp finished.");

// Free memory
for (int i = 0; i < app.history_count; ++i) {
free(app.history[i]);
}

return 0;
}
  • 1
  • Odpowiedz
  • Otrzymuj powiadomienia
    o nowych komentarzach