Compare commits
9 Commits
cf145e880b
...
7aa1aa5423
Author | SHA1 | Date |
---|---|---|
Tibor Bizjak | 7aa1aa5423 | |
Tibor Bizjak | a3d3f02d7b | |
Tibor Bizjak | 53a06aa213 | |
Tibor Bizjak | a95842850d | |
Tibor Bizjak | 9243fd9764 | |
Tibor Bizjak | afc4c14e54 | |
Tibor Bizjak | d8252a8967 | |
Tibor Bizjak | 5723e1d29b | |
Tibor Bizjak | 1bdbe712fd |
|
@ -18,3 +18,15 @@
|
|||
!k\&r/*/[0-9]*.c
|
||||
# and header files
|
||||
!k\&r/*/*.h
|
||||
|
||||
# ---- udemy c++ .gitignore ----
|
||||
# except udemy cpp
|
||||
!udemy-cpp/
|
||||
# except chapters
|
||||
!udemy-cpp/[0-9]*/
|
||||
# except excersise solutions
|
||||
!udemy-cpp/exc[0-9]*/
|
||||
# except .cc files
|
||||
!udemy-cpp/*/*.cc
|
||||
# except .h files
|
||||
!udemy-cpp/*/*.h
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
# Solutions to varius excersises
|
||||
Solutions to varius excersises from different sources.
|
||||
- k&r - [The C Programming Language](https://en.wikipedia.org/wiki/The_C_Programming_Language)
|
||||
- udemy-cpp - [Quick Start to Modern C++ for Programmers](https://www.udemy.com/course/quick-start-to-modern-c-for-programmers/)
|
||||
|
|
|
@ -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];
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef STACK_H
|
||||
|
||||
#define STACK_H
|
||||
#define printerr(format, ...) printf("error: " format, ##__VA_ARGS__)
|
||||
#define printerr(format, ...) printf("error: " format "\n", ##__VA_ARGS__)
|
||||
|
||||
#define EMPTY_STACK_ERR "stack empty"
|
||||
#define SHORT_STACK_ERR "not enough elements on stack"
|
||||
|
@ -10,8 +10,8 @@
|
|||
#define MAX_STACK 100
|
||||
#endif // MAX_STACK
|
||||
|
||||
static int sp = 0;
|
||||
static double val[MAX_STACK];
|
||||
int sp = 0;
|
||||
double val[MAX_STACK];
|
||||
|
||||
/* length: current length of stack */
|
||||
#define length() sp
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#include <cstdint>
|
||||
#include <cinttypes>
|
||||
#include <iostream>
|
||||
|
||||
/* somewhat overcomplicated, could use macros provided by cstdint */
|
||||
/* take advantage of two's complement */
|
||||
#define INT_TEST(BITS) \
|
||||
std::int##BITS##_t i##BITS = ~(~UINT##BITS##_C(0) << (BITS-1)); \
|
||||
std::uint##BITS##_t u##BITS = -1; \
|
||||
std::printf("%-10s %" PRId##BITS "\n", "int"#BITS"_t" , i##BITS); \
|
||||
std::printf("%-10s %" PRIu##BITS "\n", "uint"#BITS"_t" , u##BITS)
|
||||
|
||||
#define BITS X(8) X(16) X(32) X(64)
|
||||
|
||||
/* prints sizes of varius (u)intX_t types */
|
||||
int main()
|
||||
{
|
||||
std::cout << "max values of intx_t";
|
||||
#define X(B) std::cout << '\n'; INT_TEST(B);
|
||||
BITS
|
||||
#undef X
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace Ad
|
||||
{
|
||||
namespace Constants
|
||||
{
|
||||
|
||||
constexpr std::int32_t EGO_VEHICLE_ID = -1;
|
||||
|
||||
} // namespace Constants
|
||||
} // namespace Ad
|
|
@ -0,0 +1,47 @@
|
|||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
|
||||
#include "AdConstants.h"
|
||||
#include "AdFunctions.h"
|
||||
|
||||
namespace Ad
|
||||
{
|
||||
namespace Utils
|
||||
{
|
||||
|
||||
constexpr float kph_to_mps(const float kph)
|
||||
{
|
||||
return 0.2778 * kph;
|
||||
}
|
||||
|
||||
} // namespace Ad::Utils
|
||||
|
||||
namespace Data
|
||||
{
|
||||
|
||||
Types::Vehicle init_ego_vehicle()
|
||||
{
|
||||
return Types::Vehicle{
|
||||
.id = Constants::EGO_VEHICLE_ID,
|
||||
.lane = Types::Lane::Center,
|
||||
.speed_mps = Utils::kph_to_mps(135),
|
||||
.rel_distance_m = 0,
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace Ad::Data
|
||||
|
||||
namespace Visualize
|
||||
{
|
||||
|
||||
void print_vehicle(const Types::Vehicle vehicle)
|
||||
{
|
||||
std::cout << "Vehicle " << vehicle.id << '\n';
|
||||
std::cout << "speed : " << vehicle.speed_mps << " m/s\n";
|
||||
std::cout << "distance : " << vehicle.rel_distance_m << " m\n";
|
||||
}
|
||||
|
||||
} // namespace Ad::Visualize
|
||||
|
||||
} // namespace Ad
|
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include "AdTypes.h"
|
||||
|
||||
namespace Ad
|
||||
{
|
||||
namespace Utils
|
||||
{
|
||||
constexpr float kph_to_mps(const float kph);
|
||||
} // namespace Ad::Utils
|
||||
|
||||
namespace Data
|
||||
{
|
||||
Types::Vehicle init_ego_vehicle();
|
||||
} // namespace Ad::Data
|
||||
|
||||
namespace Visualize
|
||||
{
|
||||
void print_vehicle(const Types::Vehicle vehicle);
|
||||
} // namespace Ad::Visualize
|
||||
|
||||
} // namespace Ad
|
|
@ -0,0 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "AdConstants.h"
|
||||
|
||||
namespace Ad
|
||||
{
|
||||
namespace Types
|
||||
{
|
||||
|
||||
enum class Lane
|
||||
{
|
||||
Unknown,
|
||||
Left,
|
||||
Center,
|
||||
Right,
|
||||
};
|
||||
|
||||
struct Vehicle
|
||||
{
|
||||
std::int32_t id;
|
||||
Lane lane;
|
||||
double speed_mps;
|
||||
double rel_distance_m;
|
||||
};
|
||||
|
||||
} // namespace Ad::Types
|
||||
} // namespace Ad
|
|
@ -0,0 +1,14 @@
|
|||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
|
||||
#include "AdFunctions.h"
|
||||
#include "AdTypes.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
auto ego_vehicle = Ad::Data::init_ego_vehicle();
|
||||
Ad::Visualize::print_vehicle(ego_vehicle);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue