Compare commits

...

2 Commits

Author SHA1 Message Date
Tibor Bizjak bf7bc9e4a3 Fixed namespace termination comments in AdTypes.h, simplified init_ego_vehicle in AdFunctions.cc 2023-07-14 12:56:19 +02:00
Tibor Bizjak f11eef6a7a solutions to excercises 2023-07-14 12:56:19 +02:00
7 changed files with 185 additions and 8 deletions

View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include <string.h>
#define MIN(A, B) ((A < B) ? (A) : (B))
/* -------- INPUT WITH PUSHBACK */
int _getchar()
{
extern int buffi;
extern char buffer[];
return buffi ? buffer[--buffi] : getchar();
}
#undef getchar
#define getchar() _getchar()
/* --------- MAIN PROGRAM */
#define SLEN 10
void ungets(char *s);
/* this program copies its input to its output,
* if ungets wokrks as expected */
int main()
{
int i, c;
char s[SLEN+1];
for (i = 0; i < SLEN && (c = getchar()) != EOF; i++)
s[i] = c;
s[i] = '\0';
ungets(s);
while ((c = getchar()) != EOF)
putchar(c);
}
#define BUFF_SIZE 100
int buffi;
char buffer[BUFF_SIZE];
/* ungets: pushes string s back to input */
void ungets(char *s)
{
int i = strlen(s);
for (i = MIN(i, BUFF_SIZE-buffi)-1; i >= 0; i--, buffi++)
buffer[buffi] = s[i];
}

View File

@ -0,0 +1,22 @@
#include <stdio.h>
int main()
{
;
}
/* this handles EOF as wall */
int buffc = -1;
char getch()
{
char temp;
return (buffc < 0) ? getchar() : (temp = buffc, buffc = -1, temp);
}
void ungetch(int c)
{
if (buffc >= 0)
printf("warning: overriding character buffer\n");
buffc = c;
}

View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void itoar(char *s, int x);
/* converts first arg to int, puts it to output with itoar */
int main(int argc, char *argv[])
{
if (argc < 2)
return 1;
int x = atoi(argv[1]);
char s[strlen(argv[1])+1];
itoar(s, x);
printf("%s\n", s);
return 0;
}
char * utoar(char *s, unsigned x);
/* itoa: writes int x to string s in decimal
* recursive implementation */
void itoar(char *s, int x)
{
unsigned y = (x < 0) ? *(s++) = '-', -x : x;
s = utoar(s, y);
*s = '\0';
}
/* utoar: helper for itoa */
char * utoar(char *s, unsigned x)
{
if (x / 10)
s = utoar(s, x / 10);
*s = x % 10 + '0';
return s+1;
}

View File

@ -0,0 +1,28 @@
#include <stdio.h>
int reverse(char string[], int i);
/* reverses command line argument */
int main(int argc, char *argv[])
{
if (argc < 2)
return 1;
printf("length : %d\n", reverse(argv[1], 0));
printf("reversed : %s\n", argv[1]);
return 0;
}
/* reverse: reverses string from index i, returns length
* recursive implementation */
int reverse(char s[], int i)
{
if (s[i] == '\0') { /* base case */
return 0;
}
int j = reverse(s, i+1);
if (j > i) { /* swap elements */
char c;
c = s[i], s[i] = s[j], s[j] = c;
}
return j+1;
}

View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
void qsort_book(int v[], int left, int right);
/* sorts command line arguments */
int main(int argc, char *argv[])
{
int i, v[argc-1];
for (i = 1; i < argc; i++)
v[i-1] = atoi(argv[i]);
qsort_book(v, 0, argc-2);
for (i = 0; i < argc-1; i++)
printf("%d ", v[i]);
putchar('\n');
return 0;
}
/* swap: swap v[i] and v[j] */
void swap(int *v, int i, int j)
{
int temp = v[i];
v[i] = v[j], v[j] = temp;
}
/* qsort: sort v[left]...v[right] into increasing order */
void qsort_book(int v[], int left, int right)
{
int i, last;
if (left >= right) /* do nothing if array contains */
return; /* fewer than two elements */
swap(v, left, (left + right)/2); /* move partition elem */
last = left; /* to v[0] */
for (i = left+1; i <= right; i++) /* partition */
if (v[i] < v[left])
swap(v, ++last, i);
swap(v, left, last); /* restore partition elem */
qsort_book(v, left, last-1);
qsort_book(v, last+1, right);
}

View File

@ -22,20 +22,19 @@ namespace Data
Types::Vehicle init_ego_vehicle()
{
Types::Vehicle ev =
{.id = Constants::EGO_VEHICLE_ID,
.lane = Types::Lane::Center,
.speed_mps = Utils::kph_to_mps(135),
.rel_distance_m = 0,
return Types::Vehicle{
.id = Constants::EGO_VEHICLE_ID,
.lane = Types::Lane::Center,
.speed_mps = Utils::kph_to_mps(135),
.rel_distance_m = 0,
};
return ev;
}
} // namespace Ad::Data
namespace Visualize
{
void print_vehicle(const Types::Vehicle vehicle)
{
std::cout << "Vehicle " << vehicle.id << '\n';

View File

@ -25,5 +25,5 @@ struct Vehicle
double rel_distance_m;
};
} // namespace Types
} // namespace Ad::Types
} // namespace Ad