Simple Program

mail@pastecode.io avatar
unknown
c_cpp
19 days ago
2.5 kB
0
Indexable
Never
#include <iostream>
#include <cstdio>

using namespace std;

//Returns an integer specifying whether the input value is positive, negative or zero
int StepFunction(const int &x)
{
	int result = 0;

	if (x < 0)
	{
		result = -1;
	}
	else if (x > 0)
	{
		result = 1;
	}
	else
	{
		result = 0;
	}

	return result;
}

int Absolute(const int& x)
{
	if (x > 0)
	{
		return x;
	}
	else
	{
		return -x;
	}
}

//Creates a break in the terminal, quicker than writing \n when using Intellisense
void NewParagraph()
{
	printf("\n");
	return;
}

void PrintIntSizes() 
{
	printf("8  Bit = %hu\n16 Bit = %u\n32 Bit = %llu\n64 Bit = %llu\n", (unsigned short)pow(2, 8),
		(unsigned int)pow(2, 16),
		(unsigned long long)pow(2, 32),
		(unsigned long long)pow(2, 64));
	NewParagraph();
	return;
}

void PrintStringSize() 
{

	printf("Enter a string:\n");
	std::string input;
	std::cin >> input;
	NewParagraph();

	int stringSize = input.length();
	printf("The size of your string is: %d bytes\n", stringSize);
	NewParagraph();
}

//Called when the program is executed 
int main()
{

	printf("Step Function\n=================\n0: %d, 10: %d, -10: %d\n", StepFunction(0), StepFunction(10), StepFunction(-10));
	NewParagraph();
	int num1 = 1;
	int num2 = -1;
	printf("Absolute Values\n=================\nx = %d -> %d\nx = %d -> %d\n", num1, Absolute(num1), num2, Absolute(num2));
	NewParagraph();


	unsigned int a = 12345678;
	printf("Unsigned Short = %u\nHex Value = %x\n", a, a);
	NewParagraph();

	PrintIntSizes();

	double avogadrosNumber = 6.0221409e23;
	printf("Avogadro's Number:\nDecimal = %lf\nScientific = %le\nAuto = %lg\n", avogadrosNumber, avogadrosNumber, avogadrosNumber);
	NewParagraph();

	while (false)
	{
		printf("Hello World!\n");
	}

	//PrintStringSize();

	int array[] = { 1,2,3,4,5 };

	//Simple For
	for (size_t value : array)
	{
		printf("%zu", value);
	}

	NewParagraph();

	//Not So Simple
	for (size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++) 
	{
		printf("%d", array[i]);
	}

	NewParagraph();

	//Waits for an input before closing the console
	NewParagraph();
	printf("You have reached the end of the application, type \"restart\" to restart or anything else to exit");
	NewParagraph();
	std::string exit;
	std::cin >> exit;

	if (exit == "restart")
	{
		main();
	}
	else
	{
		quick_exit(0);
	}
	
	return 0;
}
Leave a Comment