Need some Junk code

Posts 1–6 of 6 · Page 1 of 1
Need some Junk code
Hello, it would be very nice if someone could post some junk codes, no matter what, thank you
Here's some junk code. Something I threw together when I was first learning about the algorithm header. Sloppy, nasty, and could have been done a lot better but you can add and subtract on one line
Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>

using std::cin; using std::cout; using std::endl; using std::string; using std::vector;
using std::accumulate; using std::equal;

long long doMath(string, char);	//	does the math

void main()
{
	string temp;	//	the math problem
	char sign = '+';	//	char to hold the first sign
	long long answer;	//	Making it long long to support very large numbers and decimal value
	bool first_is_char = false;
	cout << "Give me a math problem (ex. -1992 + 1920):" << endl;
	while (getline(cin, temp)){
		if (temp[0] == '-' || temp[0] == '+'){	//	if there is a sign at the first spot (i.e., -1992, +44)
			sign = temp[0];	//	store the sign in sign
			temp.erase(temp.begin());	//	get rid of it from the string
		}
		answer = doMath(temp, sign);	//	get the answer
		cout << "The answer is: " << answer << endl;	//	print the answer

	}
}

long long doMath(string input, char sign)
{
	vector<long long> variables;	//	vector to put all the variables in
	string temp;	//	just using for substr
	int operation_spot;	//	numeric value to hold the current spot of the first opeation
	input.erase(remove_if(input.begin(), input.end(), isspace), input.end());	//	get rid of the spaces

	while (input.size()) {	//	while there are still variables to process in the problem
		operation_spot = input.find_first_of("-+");	//	looking for the operation. Note: Does not support multiply or divide
		if (operation_spot){	//	operation_spot != input[0]
			temp = sign + input.substr(0, operation_spot);	//	combine the sign and the first variable
			variables.push_back(stod(temp));	//	put it in the vector
			temp.clear();	//	temp is just a temporary string to hold the substr, clear
			input.erase(0, operation_spot);	//	get rid of the variable since it's already stored in the vec
		}
		else {	//	operation_spot is at input[0], which means opeation_spot = 0
			sign = input[0];	//	store the sign at input[0] in sign
			input.erase(input.begin());	//	get rid of the sign in input
		}
	}
	return accumulate(variables.begin(), variables.end(), 0.00);	//	returns everything added together
}
And here's something I made when I was practicing more with memory/pointers/allocator class (It's essentially a vector of strings)
Code:
#include <string>
#include <iostream>
#include <memory>

// Append data to the end of the container
void push_back(std::allocator<std::string>&, std::string*&, const std::string&);
// Retrieve the string stored in the container at position
std::string& value_at_position(std::string*, size_t);
// size of the container, how many strings are currently constructed
const size_t size(const std::string*, const std::string *);
// capacity of the container, how much data has been allocated
const size_t capacity(const std::string*, const std::string*);
// reallocate more memory
void reallocate(std::allocator<std::string>&, std::string *&, std::string*&, std::string*&);
// check container, reallocate is necessary
void check_and_allocate(std::allocator<std::string>&, std::string*&, std::string*&, std::string*&);
// Release the old data constructed and allocated by str_allocator after reallocate
void free(std::allocator<std::string>&, std::string*&, std::string*&, std::string*&);

void main()
{
	// string allocator that can allocate and construct strings
	std::allocator<std::string> str_allocator;

	// container_begin is a pointer to a block of memory that is allocated by the str_allocator, it is essentially the "start" of a container
	std::string *container_begin = nullptr;

	// container_end is a pointer to a block of memory that is allocated by the str_allocator, it is always going to be one past the end of the constructed memory
	std::string *container_end = nullptr;

	// container_capacity is a pointer to a block of memory that is allocated by str_allocator, it points to the very last piece of memory that has been allocated
	std::string *container_capacity = nullptr;

	for (int i = 0; i != 129; ++i) {
		check_and_allocate(str_allocator, container_begin, container_end, container_capacity);
		push_back(str_allocator, container_end, "A default string at position <" + std::to_string(i) + "> in the container");
	}

	std::cout << value_at_position(container_begin, 42) << std::endl;
	std::cout << "Number of constructed elements: " << size(container_begin, container_end) << std::endl;
	std::cout << "Number of allocated pieces of memory: " << capacity(container_begin, container_capacity) << std::endl;
}

void push_back(std::allocator<std::string> &str_allocator, std::string *&end, const std::string &data)
{
	// Construct a string in the piece of memory at position end, then increment end
	str_allocator.construct(end++, std::move(data));
}

std::string& value_at_position(std::string *begin, size_t n)
{
	// Return the value at position n
	return *(begin + n);
}

const size_t size(const std::string *begin, const std::string *end)
{
	// Subtract two pointers getting the number of constructed elements
	return end - begin;
}

const size_t capacity(const std::string *begin, const std::string *container_cap)
{
	// Subtract two pointers getting the number of allocated pieces of memory, includes unconstructed elements
	return container_cap - begin;
}

void free(std::allocator<std::string> &str_allocator, std::string *&begin, std::string *&end, std::string *&container_cap)
{
	// Check that begin is a valid pointer, not a nullptr
	if (begin) {
		// Destroy the constructed element
		while (end != begin) {
			str_allocator.destroy(--end);
		}

		// Deallocate that block of memory, 
		str_allocator.deallocate(begin, container_cap - begin);
	}
}

void reallocate(std::allocator<std::string> &str_allocator, std::string *&begin, std::string *&end, std::string *&container_cap)
{
	// Get the max size
	size_t new_max_size = (size(begin, end) ? size(begin, end) * 2 : 1);
	// Create a pointer from the pointer returned from .allocate(new_max_size), it points to the first piece of memory allocated
	std::string *new_container_begin = str_allocator.allocate(new_max_size);
	// Create a pointer that starts at new_container_begin
	std::string *new_container_end = new_container_begin;
	// Temporary pointer used to move the strings from the old container to the new one
	std::string *current_data = begin;

	// Move elements from the old block of memory to the new one
	for (size_t i = 0; i != size(begin, end); ++i) {
		str_allocator.construct(new_container_end++, std::move(*current_data++));
	}

	// Destroy the old strings and free the memory from the old container
	free(str_allocator, begin, end, container_cap);

	// Update the old pointers with the new values
	begin = new_container_begin;
	end = new_container_end;
	container_cap = begin + new_max_size;
}

void check_and_allocate(std::allocator<std::string> &str_allocator, std::string* &begin, std::string* &end, std::string* &container_cap)
{
	if (size(begin, end) >= capacity(begin, container_cap))  {
		/*
		 * Uncomment this to see the old size/capacity vs the new one after reallocate
		 */
		// std::cout << "Old Size = " << size(begin, end) << " and Old Capacity = " << capacity(begin, container_cap) << std::endl;
		reallocate(str_allocator, begin, end, container_cap);
		// std::cout << "New Size = " << size(begin, end) << " and New Capacity = " << capacity(begin, container_cap) << std::endl;
	}
}
Quote Originally Posted by InunoTaishou View Post
Here's some junk code. Something I threw together when I was first learning about the algorithm header. Sloppy, nasty, and could have been done a lot better but you can add and subtract on one line
Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>

using std::cin; using std::cout; using std::endl; using std::string; using std::vector;
using std::accumulate; using std::equal;

long long doMath(string, char);	//	does the math

void main()
{
	string temp;	//	the math problem
	char sign = '+';	//	char to hold the first sign
	long long answer;	//	Making it long long to support very large numbers and decimal value
	bool first_is_char = false;
	cout << "Give me a math problem (ex. -1992 + 1920):" << endl;
	while (getline(cin, temp)){
		if (temp[0] == '-' || temp[0] == '+'){	//	if there is a sign at the first spot (i.e., -1992, +44)
			sign = temp[0];	//	store the sign in sign
			temp.erase(temp.begin());	//	get rid of it from the string
		}
		answer = doMath(temp, sign);	//	get the answer
		cout << "The answer is: " << answer << endl;	//	print the answer

	}
}

long long doMath(string input, char sign)
{
	vector<long long> variables;	//	vector to put all the variables in
	string temp;	//	just using for substr
	int operation_spot;	//	numeric value to hold the current spot of the first opeation
	input.erase(remove_if(input.begin(), input.end(), isspace), input.end());	//	get rid of the spaces

	while (input.size()) {	//	while there are still variables to process in the problem
		operation_spot = input.find_first_of("-+");	//	looking for the operation. Note: Does not support multiply or divide
		if (operation_spot){	//	operation_spot != input[0]
			temp = sign + input.substr(0, operation_spot);	//	combine the sign and the first variable
			variables.push_back(stod(temp));	//	put it in the vector
			temp.clear();	//	temp is just a temporary string to hold the substr, clear
			input.erase(0, operation_spot);	//	get rid of the variable since it's already stored in the vec
		}
		else {	//	operation_spot is at input[0], which means opeation_spot = 0
			sign = input[0];	//	store the sign at input[0] in sign
			input.erase(input.begin());	//	get rid of the sign in input
		}
	}
	return accumulate(variables.begin(), variables.end(), 0.00);	//	returns everything added together
}
And here's something I made when I was practicing more with memory/pointers/allocator class (It's essentially a vector of strings)
Code:
#include <string>
#include <iostream>
#include <memory>

// Append data to the end of the container
void push_back(std::allocator<std::string>&, std::string*&, const std::string&);
// Retrieve the string stored in the container at position
std::string& value_at_position(std::string*, size_t);
// size of the container, how many strings are currently constructed
const size_t size(const std::string*, const std::string *);
// capacity of the container, how much data has been allocated
const size_t capacity(const std::string*, const std::string*);
// reallocate more memory
void reallocate(std::allocator<std::string>&, std::string *&, std::string*&, std::string*&);
// check container, reallocate is necessary
void check_and_allocate(std::allocator<std::string>&, std::string*&, std::string*&, std::string*&);
// Release the old data constructed and allocated by str_allocator after reallocate
void free(std::allocator<std::string>&, std::string*&, std::string*&, std::string*&);

void main()
{
	// string allocator that can allocate and construct strings
	std::allocator<std::string> str_allocator;

	// container_begin is a pointer to a block of memory that is allocated by the str_allocator, it is essentially the "start" of a container
	std::string *container_begin = nullptr;

	// container_end is a pointer to a block of memory that is allocated by the str_allocator, it is always going to be one past the end of the constructed memory
	std::string *container_end = nullptr;

	// container_capacity is a pointer to a block of memory that is allocated by str_allocator, it points to the very last piece of memory that has been allocated
	std::string *container_capacity = nullptr;

	for (int i = 0; i != 129; ++i) {
		check_and_allocate(str_allocator, container_begin, container_end, container_capacity);
		push_back(str_allocator, container_end, "A default string at position <" + std::to_string(i) + "> in the container");
	}

	std::cout << value_at_position(container_begin, 42) << std::endl;
	std::cout << "Number of constructed elements: " << size(container_begin, container_end) << std::endl;
	std::cout << "Number of allocated pieces of memory: " << capacity(container_begin, container_capacity) << std::endl;
}

void push_back(std::allocator<std::string> &str_allocator, std::string *&end, const std::string &data)
{
	// Construct a string in the piece of memory at position end, then increment end
	str_allocator.construct(end++, std::move(data));
}

std::string& value_at_position(std::string *begin, size_t n)
{
	// Return the value at position n
	return *(begin + n);
}

const size_t size(const std::string *begin, const std::string *end)
{
	// Subtract two pointers getting the number of constructed elements
	return end - begin;
}

const size_t capacity(const std::string *begin, const std::string *container_cap)
{
	// Subtract two pointers getting the number of allocated pieces of memory, includes unconstructed elements
	return container_cap - begin;
}

void free(std::allocator<std::string> &str_allocator, std::string *&begin, std::string *&end, std::string *&container_cap)
{
	// Check that begin is a valid pointer, not a nullptr
	if (begin) {
		// Destroy the constructed element
		while (end != begin) {
			str_allocator.destroy(--end);
		}

		// Deallocate that block of memory, 
		str_allocator.deallocate(begin, container_cap - begin);
	}
}

void reallocate(std::allocator<std::string> &str_allocator, std::string *&begin, std::string *&end, std::string *&container_cap)
{
	// Get the max size
	size_t new_max_size = (size(begin, end) ? size(begin, end) * 2 : 1);
	// Create a pointer from the pointer returned from .allocate(new_max_size), it points to the first piece of memory allocated
	std::string *new_container_begin = str_allocator.allocate(new_max_size);
	// Create a pointer that starts at new_container_begin
	std::string *new_container_end = new_container_begin;
	// Temporary pointer used to move the strings from the old container to the new one
	std::string *current_data = begin;

	// Move elements from the old block of memory to the new one
	for (size_t i = 0; i != size(begin, end); ++i) {
		str_allocator.construct(new_container_end++, std::move(*current_data++));
	}

	// Destroy the old strings and free the memory from the old container
	free(str_allocator, begin, end, container_cap);

	// Update the old pointers with the new values
	begin = new_container_begin;
	end = new_container_end;
	container_cap = begin + new_max_size;
}

void check_and_allocate(std::allocator<std::string> &str_allocator, std::string* &begin, std::string* &end, std::string* &container_cap)
{
	if (size(begin, end) >= capacity(begin, container_cap))  {
		/*
		 * Uncomment this to see the old size/capacity vs the new one after reallocate
		 */
		// std::cout << "Old Size = " << size(begin, end) << " and Old Capacity = " << capacity(begin, container_cap) << std::endl;
		reallocate(str_allocator, begin, end, container_cap);
		// std::cout << "New Size = " << size(begin, end) << " and New Capacity = " << capacity(begin, container_cap) << std::endl;
	}
}
Thanks, you really helped me
Quote Originally Posted by DadDelta View Post
Hello, it would be very nice if someone could post some junk codes, no matter what, thank you
hmm . junk code. do you want my source hmm ? ) jk .
Quote Originally Posted by Radu97 View Post
hmm . junk code. do you want my source hmm ? ) jk .
I got my own, but sure lol
Code:
#include <windows.h>
int	nRandWidth = 150, nRandHeight = 15, nSpeed = 1;
int	nScreenWidth, nScreenHeight;

LRESULT WINAPI MelterProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg){
		case WM_CREATE:
			{
				HDC hdcDesktop = GetDC(HWND_DESKTOP);
				HDC hdcWindow  = GetDC(hWnd);	
				BitBlt(hdcWindow, 0, 0, nScreenWidth, nScreenHeight, hdcDesktop, 0, 0, SRCCOPY);
				ReleaseDC(hWnd, hdcWindow);
				ReleaseDC(HWND_DESKTOP, hdcDesktop);
				SetTimer(hWnd, 0, nSpeed, NULL);
				ShowWindow(hWnd, SW_SHOW);
			}
			return 0;
		case WM_ERASEBKGND:
			return 0;
		case WM_PAINT:
			ValidateRect(hWnd, NULL);
			return 0;
		case WM_TIMER:
			{
				HDC hdcWindow  = GetDC(hWnd);
				int	nXPos  = (rand() % nScreenWidth) - (nRandWidth / 2),
					nYPos  = (rand() % nRandHeight),
					nWidth = (rand() % nRandWidth);
				BitBlt(hdcWindow, nXPos, nYPos, nWidth, nScreenHeight, hdcWindow, nXPos, 0, SRCCOPY);
				ReleaseDC(hWnd, hdcWindow);
			}
			return 0;
		case WM_CLOSE:
		case WM_DESTROY:
			{
				KillTimer(hWnd, 0);
				PostQuitMessage(0);
			}
			return 0;				
	}
	return DefWindowProc(hWnd, Msg, wParam, lParam);
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd)
{
	nScreenWidth  = GetSystemMetrics(SM_CXSCREEN);
	nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
	
	WNDCLASS wndClass = { 0, MelterProc, 0, 0, hInstance, NULL, LoadCursor(NULL, IDC_ARROW), 0, NULL, "Melter" };
	if(!RegisterClass(&wndClass)) return MessageBox(HWND_DESKTOP, "Cannot register class!", NULL, MB_ICONERROR | MB_OK);
	
	HWND hWnd = CreateWindow("Melter", NULL, WS_POPUP, 0, 0, nScreenWidth, nScreenHeight, HWND_DESKTOP, NULL, hInstance, NULL);
	if(!hWnd) return MessageBox(HWND_DESKTOP, "Cannot create window!", NULL, MB_ICONERROR | MB_OK);
	
	srand(GetTickCount());
	MSG Msg = { 0 };
	while(Msg.message != WM_QUIT){
		if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)){
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
		if(GetAsyncKeyState(VK_ESCAPE) & 0x8000)
			//DestroyWindow(hWnd);
	}
	return 0;
}
Not mine, I keep it for the lols
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?