Skip to main content

set hostname syscall

0 likes • Oct 7, 2023 • 10 views
C++
Loading...

More C++ Posts

Big O(n^2) Ascending Sort

0 likes • Nov 18, 2022 • 6 views
C++
#include <iostream>
using namespace std;
int main() {
int arr[5];
for(int i = 0; i < 5; i++) {
arr[i] = i;
}
for(int i = 0; i < 5; i++) {
cout << "Outputting array info at position " << i + 1 << ": " << arr[i] << endl;
}
for(int i=0;i<5;i++)
{
for(int j=i+1;j<5;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout << endl;
for(int i = 0; i < 5; i++) {
cout << "Outputting sorted array info at position " << i + 1 << ": " << arr[i] << endl;
}
return 0;
}

Bit arithmetic + and -

0 likes • Sep 1, 2023 • 2 views
C++
#define NUM_BITS 8
#include <iostream>
struct Number{
int num : NUM_BITS;
Number(){}
Number(const int& bruh){
num = bruh;
}
operator int() const { return num; }
Number& operator=(const int& bruh){
num = bruh;
return (*this);
}
};
using namespace std;
bool isNegative(const int& num){
//This gets the bitwise and of num and 10000000000000000000000000000000
//This implicit casts to bool, which means (num & (1 << 31)) != 0
return (num & (1 << 31));
}
void printBinaryNumber(const int& num, const int numBits){
for(int i = numBits; i > 0; --i){
//8..1
int bitMask = 1 << (i-1);
if(num & bitMask){ //Test the bit
cout << '1';
}else{
cout << '0';
}
}
}
void printCarryBits(const int& a, const int& b, const int numBits){
int answer = 0;
bool carry = false;
for(int i = 0; i < numBits; ++i){
//8..1
int bitMask = 1 << i;
bool aBit = a & bitMask;
bool bBit = b & bitMask;
if(aBit && bBit || aBit && carry || bBit && carry){ //Carry bit is true next
if(carry)
answer |= bitMask;
carry = true;
}else{
if(carry)
answer |= bitMask;
carry = false;
}
}
printBinaryNumber(answer, 8);
}
void printBorrowBits(const int& a, const int& b, const int numBits){
int answer = 0;
bool carry = false;
for(int i = 0; i < numBits; ++i){
//8..1
int bitMask = 1 << i;
bool aBit = a & bitMask;
bool bBit = b & bitMask;
if((!(aBit ^ carry)) && bBit){ //Carry bit is true next
if(carry)
answer |= bitMask;
carry = true;
}else{
if(carry)
answer |= bitMask;
carry = false;
}
}
printBinaryNumber(answer, 8);
}
void doProblem(const int& a, const int& b, const char& sign, const int& result, const int& numBits){
if(sign == '+'){
cout << ' '; printCarryBits(a, b, numBits); cout << endl;
}else{
cout << ' '; printBorrowBits(a, b, numBits); cout << endl;
}
cout << ' '; printBinaryNumber(a, numBits); cout << endl;
cout << sign; printBinaryNumber(b, numBits); cout << endl;
cout << "----------" << endl;
cout << ""; printBinaryNumber(result, numBits + 1); cout << " = " << result;
cout << endl;
}
int main(){
Number a = 0b110;
Number b = 0b011;
cout<< a << endl << b << endl;
doProblem(a, b, '+', a + b, NUM_BITS);
doProblem(a, b, '-', a - b, NUM_BITS);
doProblem(-a, b, '+', -a + b, NUM_BITS);
doProblem(a, b, '-', -a - b, NUM_BITS);
return 0;
}

Stock Options Analyzer

0 likes • Nov 18, 2022 • 0 views
C++
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
int main() {
string tickerName;
int numOfContracts;
float currentOptionValue;
cout << "Enter a stock ticker: ";
getline(cin, tickerName);
cout << "Enter the current number of " << tickerName << " contracts you are holding: ";
cin >> numOfContracts;
cout << "Enter the current price of the option: ";
cin >> currentOptionValue;
cout << "The value of your " << tickerName << " options are: $" << (currentOptionValue * 100.00) * (numOfContracts);
cout << endl;
return 0;
}

CSCE 1040 Lab 9

0 likes • Nov 18, 2022 • 2 views
C++
#include <iostream>
#include "PlaylistNode.h"
using namespace std;
void PrintMenu(string title);
int main() {
string plTitle;
cout << "Enter playlist's title:" << endl;
getline(cin, plTitle);
PrintMenu(plTitle);
return 0;
}
void PrintMenu(string title) {
Playlist list;
string id;
string sname;
string aname;
int length;
int oldPos;
int newPos;
char choice;
while(true) {
cout << endl << title << " PLAYLIST MENU" << endl;
cout << "a - Add song" << endl;
cout << "d - Remove song" << endl;
cout << "c - Change position of song" << endl;
cout << "s - Output songs by specific artist" << endl;
cout << "t - Output total time of playlist (in seconds)" << endl;
cout << "o - Output full playlist" << endl;
cout << "q - Quit" << endl << endl;
cout << "Choose an option:" << endl;
cin >> choice;
cin.ignore();
if (choice == 'q') {
exit(1);
}
else if (choice == 'a') {
cout << "\nADD SONG" << endl;
cout << "Enter song's unique ID: ";
cin >> id;
cin.ignore();
cout << "Enter song's name: ";
getline(cin,sname);
cout << "Enter artist's name: ";
getline(cin,aname);
cout << "Enter song's length (in seconds): ";
cin >> length;
list.AddSong(id, sname, aname, length);
}
else if (choice == 'd') {
cout << "\nREMOVE SONG" << endl;
cout << "Enter song's unique ID: ";
cin >> id;
list.RemoveSong(id);
}
else if (choice == 'c') {
cout << "\nCHANGE POSITION OF SONG" << endl;
cout << "Enter song's current position: ";
cin >> oldPos;
cout << "Enter new position for song: ";
cin >> newPos;
list.ChangePosition(oldPos, newPos);
}
else if (choice == 's') {
cout << "\nOUTPUT SONGS BY SPECIFIC ARTIST" << endl;
cout << "Enter artist's name: ";
getline(cin, aname);
list.SongsByArtist(aname);
}
else if (choice == 't') {
cout << "\nOUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)" << endl;
cout << "Total time: " << list.TotalTime() << " seconds" << endl;
}
else if (choice == 'o') {
cout << endl << title << " - OUTPUT FULL PLAYLIST" << endl;
list.PrintList();
}
else {
cout << "Invalid menu choice! Please try again." << endl;
}
}
}

Hello, World!

0 likes • Nov 18, 2022 • 2 views
C++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
return 0;
}

Literal Bruh

0 likes • Jul 30, 2023 • 5 views
C++
//Constant prefix notation solver using bruh
//Could make it infix or postfix later
#include<string>
#include<vector>
#include<iostream>
std::vector<long double> bruhBuff;
long double operator ""bruh(long double a){
bruhBuff.push_back(a);
return a;
}
long double operator ""bruh(const char op){
if(bruhBuff.size() < 2) throw "Bruh weak";
long double b = bruhBuff.back();
bruhBuff.pop_back();
long double a = bruhBuff.back();
bruhBuff.pop_back();
switch(op){
case (int)('+'):
return a + b;
case (int)('-'):
return a - b;
case (int)('*'):
return a * b;
case (int)('/'):
return a / b;
}
return 69l;
}
int main(){
1.0bruh;
2.0bruh;
std::cout << '+'bruh << std::endl;
return 0;
}