Loading...
More C++ Posts
#include <iostream>#include <fstream>#include <string>#include <cstring>using namespace std;//This program makes a new text file that contains all combinations of two letters.// aa, ab, ..., zy, zzint main(){string filename = "two_letters.txt";ofstream outFile;outFile.open(filename.c_str());if(!outFile.is_open()){cout << "Something's wrong. Closing..." << endl;return 0;}for(char first = 'a'; first <= 'z'; first++){for(char second = 'a'; second <= 'z'; second++){outFile << first << second << " ";}outFile << endl;}return 0;}
#include <iostream>using namespace std;/* Function: get_coeffParameters: double& coeff, int pos passed from bb_4acReturn: type is void so no return, but does ask for user to input data that establishes what a b and c are.*/void get_coeff(double& coeff, int pos) {char position;if(pos == 1) {position = 'a';} else if(pos == 2) { //a simple system to determine what coefficient the program is asking for.position = 'b';} else {position = 'c';}cout << "Enter the co-efficient " << position << ":"; //prompt to input coeffcoeff = 5; //input coeff}/* Function: bb_4acParameters: no parameters passed from main, but 3 params established in function, double a, b, c.Return: b * b - 4 * a * c*/double bb_4ac() {double a, b, c; //coefficients of a quadratic equationget_coeff(a, 1); // call function 1st timeget_coeff(b, 2); // call function 2nd timeget_coeff(c, 3); // call function 3rd timereturn b * b - 4 * a * c; //return b * b - 4 * a * c}int main() {cout << "Function to calculate the discriminant of the equation. . . " << endl;double determinate = bb_4ac(); //assign double determinate to bb_4ac functioncout << "The discriminant for given values is: " << determinate << endl; //output the determinate!}
#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;}}}
#include <iostream>#include <vector>using namespace std;void swap(int *a, int *b){int temp = *b;*b = *a;*a = temp;}void heapify(vector<int> &hT, int i){int size = hT.size();int largest = i;int l = 2 * i + 1;int r = 2 * i + 2;if (l < size && hT[l] > hT[largest])largest = l;if (r < size && hT[r] > hT[largest])largest = r;if (largest != i){swap(&hT[i], &hT[largest]);heapify(hT, largest);}}void insert(vector<int> &hT, int newNum){int size = hT.size();if (size == 0){hT.push_back(newNum);}else{hT.push_back(newNum);for (int i = size / 2 - 1; i >= 0; i--){heapify(hT, i);}}}void deleteNode(vector<int> &hT, int num){int size = hT.size();int i;for (i = 0; i < size; i++){if (num == hT[i])break;}swap(&hT[i], &hT[size - 1]);hT.pop_back();for (int i = size / 2 - 1; i >= 0; i--){heapify(hT, i);}}void printArray(vector<int> &hT){for (int i = 0; i < hT.size(); ++i)cout << hT[i] << " ";cout << "\n";}int main(){vector<int> heapTree;insert(heapTree, 3);insert(heapTree, 4);insert(heapTree, 9);insert(heapTree, 5);insert(heapTree, 2);cout << "Max-Heap array: ";printArray(heapTree);deleteNode(heapTree, 4);cout << "After deleting an element: ";printArray(heapTree);}
//Leif Messinger//Compile with C++ 20#include <iostream>#include <ranges>#include <vector>#include <functional>#include <cctype> //toupper#include <cxxabi.h>template <typename T>void printType(){std::cout << abi::__cxa_demangle(typeid(T).name(), NULL, NULL, NULL) << std::endl;}template <typename T>class Slicer{public:T begin_;T end_;T trueEnd;Slicer(T begin, T end): begin_(begin), end_(begin), trueEnd(end){}template<typename U>Slicer(U&& vec) : begin_(vec.begin()), end_(vec.begin()), trueEnd(vec.end()){}Slicer& finish(){begin_ = end_;end_ = trueEnd;return (*this);}Slicer& to(long int index){begin_ = end_;if(index > 0){end_ = (begin_ + index);}else{index *= -1;end_ = (trueEnd - index);}return (*this);}Slicer& operator[](long int index){return to(index);}T begin(){return this->begin_;}T end(){return this->end_;}Slicer& operator()(std::function<void(decltype(*begin_))> func) {for(decltype(*begin_) thing : (*this)){func(thing);}return (*this);}};static_assert(std::ranges::range< Slicer<std::vector<int>::const_iterator> >);int main(){std::string vec = "abcdefghijklmnopqrstuvwxyz";Slicer<std::string::const_iterator> bruh(vec);//printType<decltype(bruh)>();bruh.to(3)([](char yeet){std::cout << yeet;}).to(-1)([](char yeet){std::cout << char(std::toupper(yeet));}).finish()([](char yeet){std::cout << yeet << yeet << yeet << yeet << yeet;});std::cout << std::endl << std::endl;int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};Slicer<int*> arrSlicer(arr, arr + (sizeof(arr)/sizeof(int)));std::cout << "[";arrSlicer.to(-1)([](int yeet){std::cout << yeet << ", ";}).finish()([](int yeet){std::cout << yeet << "]" << std::endl;});return 0;}
//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;}