seems to work

This commit is contained in:
ldlda committed 2024-10-23 22:40:17 +07:00
1 parent 8aa73bde5e
commit 2930b7217d
9 files changed
+280 -105

No files matched your search

+123 -37
View File
@@ -20,7 +20,7 @@ typedef struct Train
// rust is perfect for this let me tell you
CarInfo *init(int cap, char *id)
CarInfo *CarInit(int cap, char *id)
{
CarInfo *c = (CarInfo *)calloc(1, sizeof(CarInfo));
if (cap > 0)
@@ -29,7 +29,8 @@ CarInfo *init(int cap, char *id)
return c;
}
int check(CarInfo *c)
int trainLength(Train *train) { return (train->Cars).size; }
int car_check(CarInfo *c)
{
if (c->reserved <= 0)
return -1;
@@ -40,17 +41,72 @@ int check(CarInfo *c)
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);
}
void printhere(Train *train)
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));
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]) &&
@@ -59,31 +115,35 @@ void printhere(Train *train)
printf(", "))
;
printf("]\n");
return 1;
}
// HUGE
int addPassenger(Train *train, int p)
{ // +- supported
if (p == 0)
return 0; // hey what if we return the excess
train->passengers += p;
dllda *cars = &train->Cars;
int len = dllda_len(cars);
// so should this be a fifo or a what
int flag = 1;
int startPoint = 0, endIncl = len - 1;
if (p < 0)
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
{
flag = -1;
startPoint = endIncl, endIncl = 0;
} // its find all over again
// so if -4 is smaller than -2 and 4 is smaller than 23
for (int i = startPoint; flag * i <= flag * endIncl; i += flag)
{ CarInfo *curcar;
dllda_query(cars, i, (uintptr_t*) curcar);
if (true);
};
return 0;
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[])
@@ -91,19 +151,45 @@ int main(int argc, char const *argv[])
Train train;
train.passengers = 0;
dllda *Cars = &train.Cars;
Cars = initlda();
CarInfo *Car1 = init(55, "gay101");
dllda_push(Cars, (uintptr_t)Car1);
CarInfo *Car2 = init(73, "top3");
dllda_pushleft(Cars, (uintptr_t)Car2);
CarInfo *Car3 = init(25, "r1chk1dz");
dllda_pushleft(Cars, (uintptr_t)Car3);
CarInfo *Car4 = init(14, "prez");
dllda_insert(Cars, 2, (uintptr_t)Car4);
uintptr_t *array = (uintptr_t *)malloc(dllda_len(Cars) * sizeof(CarInfo));
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;
}
+5 -1
View File
@@ -1,5 +1,7 @@
#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)
@@ -14,7 +16,7 @@ ldaArrayList number_to_list(long long int e)
else
ldalistpush(&c, 1);
for (; e > 0; e /= 10)
ldalistadd(&c, 1, (int) (e % 10));
ldalistadd(&c, 1, (int)(e % 10));
// SHIT IN (array rotato (it means bad))
ldalistprint(&c);
return c;
@@ -48,6 +50,8 @@ long long int list_to_number(ldaArrayList c)
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);
+30
View File
@@ -0,0 +1,30 @@
# lab 3
## ex 1
Suppose we would like to implement a queue system for a commercial website.
Given that the website offers a list of items, and each item has a limited quantity in stock, implement a queue of customers, such that each customer can buy only a few products among the available items.
- Determine the item name, quantity, and price in stock.
- Specify a queue of n customers; each customer can buy k products from one
item (k is different for each customer).
- Customers take turns to enter (enqueue) and leave (dequeue) the queue according to the FIFO order to purchase wanted products.
- If a customer successfully purchases products, display a message and reduce the number of products in stock. Otherwise, display a warning message if the item has been run out.
Implement the above problems in C/C++ using a Queue data structure. Write
a main function for testing all written functions (init(), display(), enqueue(), dequeue(),...).
## ex 2
In this problem, we try to implement the navigation system in a web browser to back/forward a visiting website using a stack for Backward and another for Forward.
Choose one method to implement a Stack data structure (Array-based or Stack using Linked List) to implement this problem.
- A website may contain a url and a title; suppose that we have visited a list of websites.
- The order of visited websites is stored in a Backward stack according to FILO.
- If we return to the previous website, we pop the Backward stack and push it into the Forward stack.
- And otherwise, when we forward to the next website, we retrieve the top
website from the Forward stack and push it into the Backward stack.
Write a main function to test all written functions (init(), display(), push(), pop(),...).
+21
View File
@@ -0,0 +1,21 @@
# lab 4
## ex 1
In this problem, we would like to implement the algorithm to calculate the digit sum of a given natural number that can be used in detecting errors in message transmission or data storage.
For example:
N = 103509, the digit sum = 1 + 0 + 3 + 5 + 0 + 9 = 18.
N = 9512, the digit sum = 9 + 5 + 1 + 2 = 17
- Write a pseudo-code to solve the above problem using Iteration.
- Write a program from the pseudo-code and solve the Problem using Iteration.
- Calculate the complexity. Justify your answer.
- Write a program to solve the Problem using Recursion (with Iteration if necessary).
- Calculate the complexity. Justify your answer.
## exercise 2
Write a program in C/C++ to enter a natural number n and find all sphenic numbers from 1 to n using Recursion. Calculate the complexity of the proposed algorithm.
Note: A sphenic number is a product of $p \times q \times r$ where p, q, and r are three distinct prime numbers.
Example: $30 = 2 \times 3 \times 5$; $42 = 2 \times 3 \times 7$; $66 = 2 \times 3 \times 11$
+27
View File
@@ -0,0 +1,27 @@
# shit
## ex 1
- Implement one of the three elementary sorting algorithms (Selection, Bubble,
or Insert Sort) in C/C++.
- Implement the selected sorting algorithm using a recursive function.
- Implement the selected sorting algorithm using a data structure (List, Stack,
or Queue). Justify your choice.
## ex 2
A binary tree can sort n elements of an array of data. First, create a complete binary tree with all leaves at one level, whose height h = (log2 n) + 1, and store all array elements in the first n leaves. In each empty leaf, store an element E greater than any element in the array.
Figure (a) shows an example for data = {8, 20, 41, 7, 2}, h = (log2(5)) + 1 = 3, and E = 42. Then, starting from the bottom of the tree, assign the minimum of its two children values to each node, as in Figure (b), so that the smallest element emin in the tree is assigned to the root.
If a leaf node is to be removed, this node is replaced by a new node with the same value as its parent node.
If a node is added to the tree, it will be a leaf node. Normally, a node with value E is replaced with a new value. It’s necessary to verify recursively all values of its parent and make any possible modification, if necessary, so that the tree rules are respected.
Implement this tree structure in C/C++ with the necessary functions.
- write a function to initialize an array with n random values
- write a function to build this binary tree with the above definition with any data structure learned in lectures
- write a function to display the tree information
- write a function to search an input value using recursion. If found, display all the subtrees with the found node as the root of this subtree or return -1.
- write a function to insert new nodes into the tree and another one to remove nodes from the tree
+1 -1
View File
@@ -2,7 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "arraylist.h"
int CAPACITY = 24, ADDITIONAL = 16; //id rather just <<=1
int CAPACITY = 24, ADDITIONAL = 16; // id rather just <<=1
// stack list
void ldalistinit(ldaArrayList *l)
+2 -2
View File
@@ -8,12 +8,12 @@ typedef struct LdaArrayList
{
int size;
int capacity; // size big then capacity big 2
int* list;
int *list;
} ldaArrayList;
void ldalistinit(ldaArrayList *l);
int ldalistlen(ldaArrayList *l);
int ldalistcheck(ldaArrayList *l);
// int ldalistcheck(ldaArrayList *l);
int ldalistpush(ldaArrayList *l, int value);
int ldalistadd(ldaArrayList *l, int at, int value);
int ldalistpop(ldaArrayList *l, int *put_here);
+65 -61
View File
@@ -5,6 +5,8 @@
// BE INCREDIBLY FR WITH NODEPTR
// am i doing it right
#define elif else if
// removehidden
dllda *initlda()
@@ -90,8 +92,9 @@ int structure_fixups(dllda *l)
start = start->prev;
count++;
}
if (count > 1)
l->start = start;
l->start = start;
if (count == 1)
l->end = l->start->next;
l->size = count;
return 2;
}
@@ -166,8 +169,6 @@ void dllda_push(dllda *c, uintptr_t ptr)
c->size++;
}
// TODO!
int dllda_len(dllda *c) { return c->size; }
nodeld *adll_find(dllda *l, int i)
@@ -203,25 +204,18 @@ int dllda_rem(dllda *l, int pos, uintptr_t *puthere)
adll_remove(l, c);
return 0;
}
int adll_findHandler(dllda *l, int i, nodeld *c)
int dllda_query(dllda *l, int i, uintptr_t *copy_here)
{
c = adll_find(l, i);
if (c == NULL)
{ // start now is No Longer Null
nodeld *g = adll_find(l, i);
if (g == NULL)
{
if (dllda_len(l) == 0)
return -1;
else
return -2;
};
return 0;
}
int dllda_query(dllda *l, int i, uintptr_t *copy_here)
{
nodeld *g = NULL;
int c = adll_findHandler(l, i, g); // ma
if (c != 0)
return c;
// printf("%p, %p", g, &g);
*copy_here = g->ptr;
return 0;
}
@@ -245,17 +239,18 @@ int dllda_pop(dllda *l, uintptr_t *puthere)
nodeld *endMayBeNull = adll_find(l, dllda_len(l) - 1);
if (endMayBeNull == NULL)
{
if (dllda_len(l) == 0)
return -1;
else
return -2;
int len = dllda_len(l);
if (len == 1)
return dllda_popleft(l, puthere);
elif (len == 0) return -1;
else return -2;
};
*puthere = endMayBeNull->ptr;
adll_remove(l, endMayBeNull);
return 0;
}
// no add to size, also bugs when edges (i mean this is a interceptor)
int adll_intercept(nodeld *tobeplaced, uintptr_t ptr)
int adll_insert(nodeld *tobeplaced, uintptr_t ptr)
{
nodeld *next = tobeplaced;
nodeld *prev = next->prev;
@@ -263,7 +258,10 @@ int adll_intercept(nodeld *tobeplaced, uintptr_t ptr)
new->prev = prev;
new->next = next;
if (prev->next != next || next->prev != prev)
{
free(new);
return -1;
}
prev->next = new;
next->prev = new;
return 0;
@@ -277,12 +275,12 @@ int dllda_insert(dllda *l, int pos, uintptr_t ptr)
nodeld *replaced = adll_find(l, pos);
if (replaced == NULL)
return -1;
if (adll_intercept(replaced, ptr) != 0)
if (adll_insert(replaced, ptr) != 0)
return -1;
l->size++;
return 0;
}
else if (pos == len)
else if (pos == len) // funny you can do that
dllda_push(l, ptr);
else if (pos == 0)
dllda_pushleft(l, ptr);
@@ -350,42 +348,48 @@ int dllda_to_list(dllda *l, uintptr_t *arr)
return len;
}
// int main(int argc, char const *argv[])
// {
// dllda *new = initlda();
// int dsa = 17;
// dllda_push(new, (uintptr_t)&dsa);
// // traverse(new);
// int item1 = 1;
// dllda_push(new, (uintptr_t)&item1);
// // traverse(new);
int _main(int argc, char const *argv[])
{
dllda *new = initlda();
int dsa = 17;
dllda_push(new, (uintptr_t)&dsa);
// traverse(new);
printItOut(new);
int item4 = -2;
dllda_pushleft(new, (uintptr_t)&item4);
printItOut(new);
// traverse(new);
int item1 = 1;
dllda_pushleft(new, (uintptr_t)&item1);
// traverse(new);
printItOut(new);
int item5 = 9;
dllda_insert(new, 2, (uintptr_t)&item5);
printItOut(new);
// traverse(new);
// int item2 = 4;
// dllda_push(new, (uintptr_t)&item2);
// // traverse(new);
// int item3 = 13;
// dllda_push(new, (uintptr_t)&item3);
// // traverse(new);
// int item4 = -2;
// dllda_pushleft(new, (uintptr_t)&item4);
// int item5 = 9;
// dllda_insert(new, 2, (uintptr_t)&item5);
// int item6 = 25;
// dllda_insert(new, 0, (uintptr_t)&item6);
// printItOut(new);
// traverse(new);
// uintptr_t this;
// int code;
// code = dllda_rem(new, 3, &this);
// printf(" %d ", *(int *)this);
// printItOut(new);
// code = dllda_popleft(new, &this);
// printf(" %d ", *(int *)this);
// printItOut(new);
// code = dllda_pop(new, &this);
// printf(" %d ", *(int *)this);
// printItOut(new);
// traverse(new);
int item2 = 4;
dllda_push(new, (uintptr_t)&item2);
// traverse(new);
int item3 = 13;
dllda_push(new, (uintptr_t)&item3);
// traverse(new);
int item6 = 25;
dllda_insert(new, 0, (uintptr_t)&item6);
// printItOut(new);
// traverse(new);
uintptr_t this;
int code;
code = dllda_rem(new, 3, &this);
printf(" %d ", *(int *)this);
// printItOut(new);
code = dllda_popleft(new, &this);
printf(" %d ", *(int *)this);
// printItOut(new);
code = dllda_pop(new, &this);
printf(" %d ", *(int *)this);
// printItOut(new);
// traverse(new);
// return 0;
// }
return 0;
}
+6 -3
View File
@@ -23,20 +23,23 @@ void dllda_pushleft(dllda *c, uintptr_t ptr);
void dllda_push(dllda *c, uintptr_t ptr);
int dllda_insert(dllda *l, int pos, uintptr_t ptr);
// nodeld *adll_find(dllda *l, int i);
// semi hidden node method
nodeld *adll_find(dllda *l, int i);
int dllda_len(dllda *c);
int dllda_query(dllda *l, int i, uintptr_t *copy_here);
int dllda_peekleft(dllda *l, uintptr_t *copy_here);
int dllda_peek(dllda *l, uintptr_t *copy_here);
// int adll_intercept(nodeld *tobeplaced, uintptr_t ptr);
// int adll_insert(nodeld *tobeplaced, uintptr_t ptr);
// int adll_remove(dllda *whatif, nodeld *node);
int dllda_rem(dllda *l, int pos, uintptr_t *puthere);
int dllda_popleft(dllda *l, uintptr_t *puthere);
int dllda_pop(dllda *l, uintptr_t *puthere);
// void traverse(dllda *l);
// void printItOut(dllda *new);
int dllda_to_list(dllda *l, uintptr_t *arr);
#endif