Skip to main content

Command line game

0 likes • Nov 19, 2022 • 1 view
C++
Loading...

More C++ Posts

Wing Project 1

0 likes • Oct 31, 2021 • 1 view
C++
//Get data file at https://codecatch.net/post.php?postID=91e87d73
//Iteration 1 of Wing Project. Solution breaks down around n=35
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
int getSum(map<int, int> list);
void readData(map<int, float>* data);
void lowestPrice();
void findSums(int n, vector<map<int, int>>* sumsList, map<int, float>* data);
//void findSum(map<int, int> currList, int x, int n, vector<map<int, int>>* sumsList);
void findSum(map<int, int> currList, int x, int n, vector<map<int, int>>* sumsList, map<int, float>* data);
float getPrice(map<int, int> set, map<int, float>* data);
template <typename S>
ostream& operator<<(ostream& os, const vector<S>& vector)
{
// Printing all the elements using <<
for (auto element : vector) {
os << element << " ";
}
return os;
}
bool operator==(map<int, int> m1, map<int, int> m2)
{
if(m1.size() != m2.size())
return false;
bool ret = true;
for(auto it = m1.begin(); it !=m1.end() && ret; it++)
{
if(ret && m1.count(it->first) != m2.count(it->first))
ret = false;
if(ret && m1.count(it->first) == 1)
{
if(m1.at(it->first) != m2.at(it->first))
ret = false;
}
}
return ret;
}
int main()
{
map<int, float> data;
readData(&data);
vector<map<int, int>> *sumsList;
sumsList = new vector<map<int, int>>;
findSums(40, sumsList, &data);
for(auto el : *sumsList)
{
for(auto it = el.begin(); it != el.end(); it++)
{
cout << it->first << "->" << it->second << " ";
}
cout << getPrice(el, &data) << endl;
}
return 0;
}
/* Returns the price of wings given a set of numbers of wings to buy.
* Returns -1 if the set contains a number that is not possible to buy.
*/
float getPrice(map<int, int> set, map<int, float>* data)
{
float price = 0;
for(auto it = set.begin(); it != set.end(); it++)
{
//If data doesn't contain an element of set, return -1
if(data->count(it->first) == 0)
return -1;
price += data->at(it->first) * it->second; //pricePerPacket * qtyOfPackets
}
return price;
}
/* Adds the elements of list.
* Suppose mapping is <num, qty>.
* Returns sum(num*qty)
*/
int getSum(map<int, int> list)
{
int sum = 0;
for(auto it = list.begin(); it != list.end(); it++)
sum += it->first * it->second;
return sum;
}
void findSums(int n, vector<map<int, int>>* sumsList, map<int, float>* data)
{
map<int, int> currList;
//Recur when currSum < n
auto it = data->begin();
while(it->first <= n && it != data->end())
{
findSum(currList, it->first, n, sumsList, data);
it++;
}
}
void findSum(map<int, int> currList, int x, int n, vector<map<int, int>>* sumsList, map<int, float>* data)
{
//Append x to currList
if(currList.count(x) == 0)
currList.emplace(x, 1);
else
{
int val = 1+ currList.at(x);
currList.erase(x);
currList.emplace(x, val);
}
//Determine current sum, check for return cases
int currSum = getSum(currList);
if(currSum > n)
return;
else if(currSum == n)
{
//Check to make sure no duplicates
for(auto list : *sumsList)
{
if(list == currList)
return;
}
sumsList->push_back(currList);
return;
}
//Recur when currSum < n
auto it = data->begin();
while(it->first <= n-x && it != data->end())
{
findSum(currList, it->first, n, sumsList, data);
it++;
}
}
void readData(map<int, float>* data)
{
ifstream file ("./data", ifstream::in);
if(file.is_open())
{
int i = 0;
while(!file.eof())
{
float wings, price;
string skipnl;
file >> wings;
file >> price;
data->emplace(wings, price);
getline(file, skipnl);
i++;
}
}
}

Get Coefficient

0 likes • Nov 18, 2022 • 4 views
C++
#include <iostream>
using namespace std;
/* Function: get_coeff
Parameters: double& coeff, int pos passed from bb_4ac
Return: 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 coeff
coeff = 5; //input coeff
}
/* Function: bb_4ac
Parameters: 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 equation
get_coeff(a, 1); // call function 1st time
get_coeff(b, 2); // call function 2nd time
get_coeff(c, 3); // call function 3rd time
return 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 function
cout << "The discriminant for given values is: " << determinate << endl; //output the determinate!
}

PlaylistNode.cpp (lab 9)

0 likes • Nov 18, 2022 • 0 views
C++
#include <string>
#include <iostream>
#include "PlaylistNode.h"
using namespace std;
PlaylistNode::PlaylistNode() {
uniqueID = "none";
songName = "none";
artistName = "none";
songLength = 0;
nextNodePtr = 0;
}
PlaylistNode::PlaylistNode(string uniqueID_, string songName_, string artistName_, int songLength_) {
uniqueID = uniqueID_;
songName = songName_;
artistName = artistName_;
songLength = songLength_;
nextNodePtr = 0;
}
void PlaylistNode::InsertAfter(PlaylistNode* ptr) {
this->SetNext(ptr->GetNext());
ptr->SetNext(this);
}
void PlaylistNode::SetNext(PlaylistNode* ptr) {
nextNodePtr = ptr;
}
string PlaylistNode::GetID() {
return uniqueID;
}
string PlaylistNode::GetSongName() {
return songName;
}
string PlaylistNode::GetArtistName() {
return artistName;
}
int PlaylistNode::GetSongLength() {
return songLength;
}
PlaylistNode* PlaylistNode::GetNext() {
return nextNodePtr;
}
void PlaylistNode::PrintPlaylistNode() {
cout << "Unique ID: " << uniqueID << endl;
cout << "Song Name: " << songName << endl;
cout << "Artist Name: " << artistName << endl;
cout << "Song Length (in seconds): " << songLength << endl;
}
Playlist::Playlist() {
head = tail = 0;
}
void Playlist::AddSong(string id, string songname, string artistname, int length) {
PlaylistNode* n = new PlaylistNode(id, songname, artistname, length);
if (head == 0) {
head = tail = n;
}
else {
n->InsertAfter(tail);
tail = n;
}
}
bool Playlist::RemoveSong(string id) {
if (head == NULL) {
cout << "Playlist is empty" << endl;
return false;
}
PlaylistNode* curr = head;
PlaylistNode* prev = NULL;
while (curr != NULL) {
if (curr->GetID() == id) {
break;
}
prev = curr;
curr = curr->GetNext();
}
if (curr == NULL) {
cout << "\"" << curr->GetSongName() << "\" is not found" << endl;
return false;
}
else {
if (prev != NULL) {
prev ->SetNext(curr->GetNext());
}
else {
head = curr->GetNext();
}
if (tail == curr) {
tail = prev;
}
cout << "\"" << curr->GetSongName() << "\" removed." << endl;
delete curr;
return true;
}
}
bool Playlist::ChangePosition(int oldPos, int newPos) {
if (head == NULL) {
cout << "Playlist is empty" << endl;
return false;
}
PlaylistNode* prev = NULL;
PlaylistNode* curr = head;
int pos;
if (head == NULL || head == tail) {
return false;
}
for (pos = 1; curr != NULL && pos < oldPos; pos++) {
prev = curr;
curr = curr->GetNext();
}
if (curr != NULL) {
string currentSong = curr->GetSongName();
if (prev == NULL) {
head = curr->GetNext();
}
else {
prev->SetNext(curr->GetNext());
}
if (curr == tail) {
tail = prev;
}
PlaylistNode* curr1 = curr;
prev = NULL;
curr = head;
for (pos = 1; curr != NULL && pos < newPos; pos++) {
prev = curr;
curr = curr->GetNext();
}
if (prev == NULL) {
curr1->SetNext(head);
head = curr1;
}
else {
curr1->InsertAfter(prev);
}
if (curr == NULL) {
tail = curr1;
}
cout << "\"" << currentSong << "\" moved to position " << newPos << endl;
return true;
}
else {
cout << "Song's current position is invalid" << endl;
return false;
}
}
void Playlist::SongsByArtist(string artist) {
if (head == NULL) {
cout << "Playlist is empty" << endl;
}
else {
PlaylistNode* curr = head;
int i = 1;
while (curr != NULL) {
if (curr->GetArtistName() == artist) {
cout << endl << i << "." << endl;
curr->PrintPlaylistNode();
}
curr = curr->GetNext();
i++;
}
}
}
int Playlist::TotalTime() {
int total = 0;
PlaylistNode* curr = head;
while (curr != NULL) {
total += curr->GetSongLength();
curr = curr->GetNext();
}
return total;
}
void Playlist::PrintList() {
if (head == NULL) {
cout << "Playlist is empty" << endl;
}
else {
PlaylistNode* curr = head;
int i = 1;
while (curr != NULL) {
cout << endl << i++ << "." << endl;
curr->PrintPlaylistNode();
curr = curr->GetNext();
}
}
}

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;
}

Audio Frequency Amplitudes

0 likes • Aug 27, 2021 • 1 view
C++
//From https://create.arduino.cc/projecthub/abhilashpatel121/easyfft-fast-fourier-transform-fft-for-arduino-9d2677
#include <cmath>
#include <iostream>
const unsigned char sine_data[] = { //Quarter a sine wave
0,
4, 9, 13, 18, 22, 27, 31, 35, 40, 44,
49, 53, 57, 62, 66, 70, 75, 79, 83, 87,
91, 96, 100, 104, 108, 112, 116, 120, 124, 127,
131, 135, 139, 143, 146, 150, 153, 157, 160, 164,
167, 171, 174, 177, 180, 183, 186, 189, 192, 195, //Paste this at top of program
198, 201, 204, 206, 209, 211, 214, 216, 219, 221,
223, 225, 227, 229, 231, 233, 235, 236, 238, 240,
241, 243, 244, 245, 246, 247, 248, 249, 250, 251,
252, 253, 253, 254, 254, 254, 255, 255, 255, 255
};
float sine(int i){ //Inefficient sine
int j=i;
float out;
while(j < 0) j = j + 360;
while(j > 360) j = j - 360;
if(j > -1 && j < 91) out = sine_data[j];
else if(j > 90 && j < 181) out = sine_data[180 - j];
else if(j > 180 && j < 271) out = -sine_data[j - 180];
else if(j > 270 && j < 361) out = -sine_data[360 - j];
return (out / 255);
}
float cosine(int i){ //Inefficient cosine
int j = i;
float out;
while(j < 0) j = j + 360;
while(j > 360) j = j - 360;
if(j > -1 && j < 91) out = sine_data[90 - j];
else if(j > 90 && j < 181) out = -sine_data[j - 90];
else if(j > 180 && j < 271) out = -sine_data[270 - j];
else if(j > 270 && j < 361) out = sine_data[j - 270];
return (out / 255);
}
//Example data:
//-----------------------------FFT Function----------------------------------------------//
float* FFT(int in[],unsigned int N,float Frequency){ //Result is highest frequencies in order of loudness. Needs to be deleted.
/*
Code to perform FFT on arduino,
setup:
paste sine_data [91] at top of program [global variable], paste FFT function at end of program
Term:
1. in[] : Data array,
2. N : Number of sample (recommended sample size 2,4,8,16,32,64,128...)
3. Frequency: sampling frequency required as input (Hz)
If sample size is not in power of 2 it will be clipped to lower side of number.
i.e, for 150 number of samples, code will consider first 128 sample, remaining sample will be omitted.
For Arduino nano, FFT of more than 128 sample not possible due to mamory limitation (64 recomended)
For higher Number of sample may arise Mamory related issue,
Code by ABHILASH
Documentation:https://www.instructables.com/member/abhilash_patel/instructables/
2/3/2021: change data type of N from float to int for >=256 samples
*/
unsigned int sampleRates[13]={1,2,4,8,16,32,64,128,256,512,1024,2048};
int a = N;
int o;
for(int i=0;i<12;i++){ //Snapping N to a sample rate in sampleRates
if(sampleRates[i]<=a){
o = i;
}
}
int in_ps[sampleRates[o]] = {}; //input for sequencing
float out_r[sampleRates[o]] = {}; //real part of transform
float out_im[sampleRates[o]] = {}; //imaginory part of transform
int x = 0;
int c1;
int f;
for(int b=0;b<o;b++){ // bit reversal
c1 = sampleRates[b];
f = sampleRates[o] / (c1 + c1);
for(int j = 0;j < c1;j++){
x = x + 1;
in_ps[x]=in_ps[j]+f;
}
}
for(int i=0;i<sampleRates[o];i++){ // update input array as per bit reverse order
if(in_ps[i]<a){
out_r[i]=in[in_ps[i]];
}
if(in_ps[i]>a){
out_r[i]=in[in_ps[i]-a];
}
}
int i10,i11,n1;
float e,c,s,tr,ti;
for(int i=0;i<o;i++){ //fft
i10 = sampleRates[i]; // overall values of sine/cosine :
i11 = sampleRates[o] / sampleRates[i+1]; // loop with similar sine cosine:
e = 360 / sampleRates[i+1];
e = 0 - e;
n1 = 0;
for(int j=0;j<i10;j++){
c=cosine(e*j);
s=sine(e*j);
n1=j;
for(int k=0;k<i11;k++){
tr = c*out_r[i10 + n1]-s*out_im[i10 + n1];
ti = s*out_r[i10 + n1]+c*out_im[i10 + n1];
out_r[n1 + i10] = out_r[n1]-tr;
out_r[n1] = out_r[n1]+tr;
out_im[n1 + i10] = out_im[n1]-ti;
out_im[n1] = out_im[n1]+ti;
n1 = n1+i10+i10;
}
}
}
/*
for(int i=0;i<sampleRates[o];i++)
{
std::cout << (out_r[i]);
std::cout << ("\t"); // un comment to print RAW o/p
std::cout << (out_im[i]); std::cout << ("i");
std::cout << std::endl;
}
*/
//---> here onward out_r contains amplitude and our_in conntains frequency (Hz)
for(int i=0;i<sampleRates[o-1];i++){ // getting amplitude from compex number
out_r[i] = sqrt(out_r[i]*out_r[i]+out_im[i]*out_im[i]); // to increase the speed delete sqrt
out_im[i] = i * Frequency / N;
std::cout << (out_im[i]); std::cout << ("Hz");
std::cout << ("\t"); // un comment to print freuency bin
std::cout << (out_r[i]);
std::cout << std::endl;
}
x = 0; // peak detection
for(int i=1;i<sampleRates[o-1]-1;i++){
if(out_r[i]>out_r[i-1] && out_r[i]>out_r[i+1]){
in_ps[x] = i; //in_ps array used for storage of peak number
x = x + 1;
}
}
s = 0;
c = 0;
for(int i=0;i<x;i++){ // re arraange as per magnitude
for(int j=c;j<x;j++){
if(out_r[in_ps[i]]<out_r[in_ps[j]]){
s=in_ps[i];
in_ps[i]=in_ps[j];
in_ps[j]=s;
}
}
c=c+1;
}
float* f_peaks = new float[sampleRates[o]];
for(int i=0;i<5;i++){ // updating f_peak array (global variable)with descending order
f_peaks[i]=out_im[in_ps[i]];
}
return f_peaks;
}
//------------------------------------------------------------------------------------//
//main.cpp
int data[64]={
14, 30, 35, 34, 34, 40, 46, 45, 30, 4, -26, -48, -55, -49, -37,
-28, -24, -22, -13, 6, 32, 55, 65, 57, 38, 17, 1, -6, -11, -19, -34,
-51, -61, -56, -35, -7, 18, 32, 35, 34, 35, 41, 46, 43, 26, -2, -31, -50,
-55, -47, -35, -27, -24, -21, -10, 11, 37, 58, 64, 55, 34, 13, -1, -7
};
int main(){
const unsigned int SAMPLE_RATE = 48*1000; //48khz
auto result = FFT(data,64,SAMPLE_RATE);
std::cout << result[0] << " " << result[1] << " " << result[2] << " " << result[3] << std::endl;
delete[] result;
return 0;
}

Hello World!

0 likes • Aug 31, 2020 • 2 views
C++
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!\n";
// Prints out "Hello World"
return 0;
}