Skip to content

Instantly share code, notes, and snippets.

View desperius's full-sized avatar
🙃
Weeks of coding can save you hours of planning

desperius

🙃
Weeks of coding can save you hours of planning
View GitHub Profile
@desperius
desperius / openal_wav_load.cpp
Last active June 8, 2022 13:30
Trivial example of loading and playback of audio file in WAV format using OpenAL library and ALUT.
#include <iostream>
#include <stdlib.h>
#include <alut.h>
const unsigned NUM_BUFFERS = 1;
const unsigned NUM_SOURCES = 1;
int main(int argc, char* argv[])
{
/* Sound buffer variable */
@desperius
desperius / ifstream_read_text.cpp
Last active June 8, 2022 13:36
Reading text file using ifstream.
#inlcude <iostream>
#include <fstream>
int main(void)
{
std::ifstream file;
/* Read file */
file.open("file.txt", std::ios::in);
@desperius
desperius / maximal_odd_divisor.cpp
Last active June 8, 2022 13:52
Simple approach to find Maximal Odd Divisor.
#include <iostream>
int main(void)
{
int x = rand();
std::cout << "Generated number: " << x << std::endl;
/* Count maximal odd divisor */
x /= x & -x;
@desperius
desperius / cin_buffer_cleanup.cpp
Last active June 8, 2022 14:10
The way to clean up a standard input buffer in case of a reading error. If such error appears the buffer does not clean up automatically. If application will try to continue then reading will proceeding from already filled buffer and will lead to one more error.
#include <iostream>
int main(void)
{
int i;
std::cin >> i; // Enter from keyboard e.g. the characters string 'hello'
if (std::cin.fail())
{
std::cin.clear();
@desperius
desperius / astyle
Last active June 8, 2022 14:17
Settings for AStyle code formatter
--style=allman
--indent=spaces=4
--indent-switches
--min-conditional-indent=0
--align-pointer=type
--align-reference=type
--max-code-length=120
--break-blocks
--pad-oper
--pad-header
@desperius
desperius / permutation.cpp
Last active May 13, 2017 17:50
All permutation of N numbers. Complexity: N!
#include <iostream>
int n = 0;
int* arr;
void swap(int* arr, int a, int b)
{
int c = arr[a];
arr[a] = arr[b];
@desperius
desperius / sequence.cpp
Last active May 13, 2017 17:52
All sequences of N-place number in range [1, ... , M]. Complexity: M ^ N
#include <iostream>
int n = 0;
int m = 0;
int* arr;
void increse(int index)
{
if (!(index < n))
@desperius
desperius / string.cpp
Last active May 13, 2017 17:56
Custom string class for SW Certification
class Str
{
public:
Str();
Str(const char* src);
~Str();
Str(const Str& that);
Str& operator= (const Str& that);
@desperius
desperius / hash_for_structure.cpp
Created May 13, 2017 18:06
Hash function for custom structure
#include <iostream>
#include <string>
#include <functional>
template <class T>
inline void hash_combined(std::size_t& res, const T& val)
{
std::hash<T> h;
res ^= h(val) + 0x9e3779b9 + (res << 6) + (res >> 2);
}
@desperius
desperius / modify_bit_via_operator.cpp
Last active September 5, 2018 13:03
If you want overload operator[] in such way that you can modify specific bit of a byte.
#include <iostream>
#include <bitset>
struct bitarr
{
bitarr(unsigned char v) : val(v) {}
void print()
{
std::bitset<8 * sizeof(unsigned char)> bits(val);