first comm in cpp
This commit is contained in:
1 parent
5968efc884
commit
9e742f0d79
22 files changed
+154
-1379
No files matched your search
-195
@@ -1,195 +0,0 @@
|
||||
#include "../common/linkedlist.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // reimport!!! as
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct CarInfo
|
||||
{
|
||||
int capacity;
|
||||
char *id; // i follow the thing
|
||||
// (0, cap]
|
||||
int reserved;
|
||||
} CarInfo;
|
||||
|
||||
typedef struct Train
|
||||
{
|
||||
dllda Cars;
|
||||
int passengers;
|
||||
} Train;
|
||||
|
||||
// rust is perfect for this let me tell you
|
||||
|
||||
CarInfo *CarInit(int cap, char *id)
|
||||
{
|
||||
CarInfo *c = (CarInfo *)calloc(1, sizeof(CarInfo));
|
||||
if (cap > 0)
|
||||
c->capacity = cap;
|
||||
c->id = strdup(id);
|
||||
return c;
|
||||
}
|
||||
|
||||
int trainLength(Train *train) { return (train->Cars).size; }
|
||||
int car_check(CarInfo *c)
|
||||
{
|
||||
if (c->reserved <= 0)
|
||||
return -1;
|
||||
else if (c->reserved > c->capacity)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
int getPassengers(CarInfo *c) { return c->reserved; }
|
||||
int getCapacity(CarInfo *c) { return c->capacity; }
|
||||
|
||||
// negative passengers
|
||||
int CarModifyPassengers(CarInfo *car, int passengers)
|
||||
{
|
||||
int current = getPassengers(car);
|
||||
int capa = getCapacity(car);
|
||||
int added = current + passengers;
|
||||
if (added > capa)
|
||||
{
|
||||
car->reserved = capa;
|
||||
return added - capa; // added - newCurrent
|
||||
}
|
||||
else if (added < 0)
|
||||
{
|
||||
car->reserved = 0;
|
||||
return added;
|
||||
}
|
||||
else
|
||||
{
|
||||
car->reserved = added;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int TrainModifyPassengers(Train *train, int passengers)
|
||||
{
|
||||
if (passengers == 0)
|
||||
return 0;
|
||||
int len = trainLength(train);
|
||||
dllda *Cars = &train->Cars;
|
||||
int in_or_out = passengers > 0 ? 1 : -1;
|
||||
int number = in_or_out == 1
|
||||
? 0
|
||||
: in_or_out == -1
|
||||
? len - 1
|
||||
: false;
|
||||
for (int i = number;
|
||||
in_or_out == -1
|
||||
? i >= 0
|
||||
: in_or_out == 1
|
||||
? i < len
|
||||
: false;
|
||||
i += in_or_out)
|
||||
{
|
||||
CarInfo *thisCar;
|
||||
dllda_query(Cars, i, (uintptr_t *)&thisCar);
|
||||
passengers = CarModifyPassengers(thisCar, passengers);
|
||||
// I DONT FREE cars fuckleyoos
|
||||
if (passengers == 0)
|
||||
break;
|
||||
}
|
||||
return passengers > 0 ? passengers : 0;
|
||||
}
|
||||
|
||||
int printTheCarThing(CarInfo *c)
|
||||
{
|
||||
return printf("CarInfo (id: %s): %d/%d", c->id, c->reserved, c->capacity);
|
||||
}
|
||||
|
||||
int printhere(Train *train)
|
||||
{
|
||||
dllda *new = &train->Cars;
|
||||
int sz = dllda_len(new);
|
||||
if (sz <= 0)
|
||||
return 0;
|
||||
printf("size: %d [", sz);
|
||||
uintptr_t *c = (uintptr_t *)calloc(sz, sizeof(uintptr_t));
|
||||
for (int i = sz - dllda_to_list(new, c);
|
||||
printf("(%d: ", i) &&
|
||||
printTheCarThing((CarInfo *)c[i]) &&
|
||||
printf(")") &&
|
||||
(++i < sz); // how to get value
|
||||
printf(", "))
|
||||
;
|
||||
printf("]\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
void cartraverse(Train *train)
|
||||
{
|
||||
dllda *l = &train->Cars;
|
||||
printf("\nthis is cars with size %d, start being uint %p, and end we do not know %p\n", l->size, l->start, l->end);
|
||||
printf("array repr: ");
|
||||
printhere(train) || printf("None\n");
|
||||
int leng = dllda_len(l);
|
||||
|
||||
nodeld *someNode = l->start;
|
||||
for (int i = 0; (i < leng); i++,
|
||||
(i < leng) && (printf("next node is\n") == 0)) // funnily
|
||||
{
|
||||
printf("with find:\n");
|
||||
nodeld *thisNode = adll_find(l, i);
|
||||
printf("node %p at %d, ", thisNode, i);
|
||||
printf("with ptr %p val ", thisNode->ptr);
|
||||
printTheCarThing((CarInfo *)thisNode->ptr);
|
||||
printf(", prev %p and next %p\n", thisNode->prev, thisNode->next);
|
||||
printf("with node traversal:\n");
|
||||
printf("node %p at %d, ", thisNode, i);
|
||||
printf("with ptr %p val ", someNode->ptr);
|
||||
printTheCarThing((CarInfo *)someNode->ptr);
|
||||
printf(", prev %p and next %p\n", someNode->prev, someNode->next);
|
||||
someNode = someNode->next;
|
||||
}
|
||||
printf("that is all.\n");
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
Train train;
|
||||
train.passengers = 0;
|
||||
|
||||
train.Cars = *initlda(); // some fuck
|
||||
|
||||
dllda *Cars = &train.Cars;
|
||||
// cartraverse(&train);
|
||||
|
||||
CarInfo *Car1 = CarInit(55, "gay101");
|
||||
|
||||
// dllda *Cars = &train.Cars;
|
||||
dllda_push(Cars, (uintptr_t)Car1);
|
||||
|
||||
CarInfo *Car2 = CarInit(73, "top3");
|
||||
dllda_pushleft(Cars, (uintptr_t)Car2);
|
||||
// cartraverse(&train);
|
||||
|
||||
CarInfo *Car3 = CarInit(25, "r1chk1dz");
|
||||
dllda_pushleft(Cars, (uintptr_t)Car3);
|
||||
// cartraverse(&train);
|
||||
|
||||
CarInfo *Car4 = CarInit(14, "prez");
|
||||
dllda_insert(Cars, 2, (uintptr_t)Car4);
|
||||
cartraverse(&train);
|
||||
|
||||
uintptr_t *array = (uintptr_t *)calloc(dllda_len(Cars), sizeof(CarInfo));
|
||||
dllda_to_list(Cars, array);
|
||||
// printhere(&train);
|
||||
|
||||
int pass = 183;
|
||||
printf("pass in: %d\n", pass);
|
||||
printhere(&train);
|
||||
pass = TrainModifyPassengers(&train, pass);
|
||||
printf("pass left: %d\n", pass);
|
||||
// cartraverse(&train);
|
||||
printhere(&train);
|
||||
int out = -221;
|
||||
printf("pass out: %d\n", out);
|
||||
out = TrainModifyPassengers(&train, out);
|
||||
printhere(&train);
|
||||
printf("pass all out?: %d\n", out);
|
||||
printhere(&train);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
#include "../common/arraylist.h"
|
||||
// haha i will have to compile manually EVERY TIME HAHAHAHAHAHAHAHA
|
||||
|
||||
// gcc ./2/unboundedIntegers.c ./common/arraylist.c -o ./exe/inboundedIntegers && ./exe/inboundedIntegers
|
||||
#include <stdio.h>
|
||||
|
||||
ldaArrayList number_to_list(long long int e)
|
||||
{
|
||||
ldaArrayList c;
|
||||
ldalistinit(&c);
|
||||
if (e < 0)
|
||||
{
|
||||
ldalistpush(&c, -1);
|
||||
e = -e;
|
||||
}
|
||||
else
|
||||
ldalistpush(&c, 1);
|
||||
for (; e > 0; e /= 10)
|
||||
ldalistadd(&c, 1, (int)(e % 10));
|
||||
// SHIT IN (array rotato (it means bad))
|
||||
ldalistprint(&c);
|
||||
return c;
|
||||
}
|
||||
|
||||
void displayNumber(ldaArrayList *n)
|
||||
{
|
||||
for (int i = 0, cur; i < ldalistlen(n); i++)
|
||||
{
|
||||
if (ldalistlookup(n, i, &cur) != 0)
|
||||
return;
|
||||
if (cur == -1)
|
||||
printf("-");
|
||||
else
|
||||
printf("%d", cur);
|
||||
}
|
||||
}
|
||||
|
||||
long long int list_to_number(ldaArrayList c)
|
||||
{
|
||||
// SHIT OUT
|
||||
long long ret = 0;
|
||||
int e;
|
||||
for (long long int f = 1; ldalistpop(&c, &e) == 0; f *= 10)
|
||||
{
|
||||
if (e >= 0)
|
||||
ret = ret + e * f;
|
||||
else if (e == -1)
|
||||
ret = -ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// i wont bother with add rem stuff use the list methods directly
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
ldaArrayList ef = number_to_list(-276482364786342);
|
||||
displayNumber(&ef);
|
||||
printf("\n");
|
||||
printf(" %lld ", list_to_number(ef));
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user