Loading...
More C++ Posts
#include <iostream>using namespace std;/*Description: uses switch case statements to determine whether it is hot or not outside.Also uses toupper() function which forces user input char to be uppercase in order to work for the switch statement*/int main() {char choice;cout << "S = Summer, F = Fall, W = Winter, P = Spring" << endl;cout << "Enter a character to represent a season: ";asdasdasdasdcin >> choice;enum Season {SUMMER='S', FALL='F', WINTER='W', SPRING='P'};switch(toupper(choice)) // This switch statement compares a character entered with values stored inside of an enum{case SUMMER:cout << "It's very hot outside." << endl;break;case FALL:cout << "It's great weather outside." << endl;break;case WINTER:cout << "It's fairly cold outside." << endl;break;case SPRING:cout << "It's rather warm outside." << endl;break;default:cout << "Wrong choice" << endl;break;}return 0;}
#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;}
#include <iostream>#include <string> //Should already be in iostream#include <cstdlib>//A word score adds up the character values. a-z gets mapped to 1-26 for the values of the characters.//wordScore [wordValue]//Pipe in the input into stdin, or type the words yourself.//Lowercase words onlyint characterValue(const char b){return ((b >= 'a') && (b <= 'z'))? ((b - 'a') + 1) : 0;}int main(int argc, char** argv){//The first argument specifies if you are trying to look for a certain word scoreint wordValue = (argc > 1)? std::atoi(argv[1]) : 0;std::string line;while(std::getline(std::cin, line)){int sum = 0;for(const char c : line){sum += characterValue(c);}if(wordValue){ //If wordValue is 0 or the sum is the correct valueif(wordValue == sum){std::cout << line << std::endl;}} else {std::cout << sum << "\t" << line << std::endl;}}return 0;}
/*Algorithm:Step 1: Get radius of the cylinder from the user and store in variable rStep 2: Get height of the cylinder from the user and store in variable hStep 3: Multiply radius * radius * height * pi and store in vStep 4: Display the volume*/#include <iostream>using namespace std;int main(){float r; //define variable for radiusfloat h; //define variable for heightfloat v;float pi;pi=3.1416;cout<<"Enter radius:";cin>>r;cout<<"Enter height:";cin>>h;v=r*r*h*pi; //compute volumecout<<"Radius:"<<r<<"\tHeight:"<<h<<endl; //display radius and heightcout<<"\n************************\n";cout<<"Volume:"<<v<<endl;//display volumereturn 0;}
using namespace std;class Hash{int BUCKET; // No. of buckets// Pointer to an array containing bucketslist<int> *table;public:Hash(int V); // Constructor// inserts a key into hash tablevoid insertItem(int x);// deletes a key from hash tablevoid deleteItem(int key);// hash function to map values to keyint hashFunction(int x) {return (x % BUCKET);}void displayHash();};Hash::Hash(int b){this->BUCKET = b;table = new list<int>[BUCKET];}void Hash::insertItem(int key){int index = hashFunction(key);table[index].push_back(key);}void Hash::deleteItem(int key){// get the hash index of keyint index = hashFunction(key);// find the key in (inex)th listlist <int> :: iterator i;for (i = table[index].begin();i != table[index].end(); i++) {if (*i == key)break;}// if key is found in hash table, remove itif (i != table[index].end())table[index].erase(i);}// function to display hash tablevoid Hash::displayHash() {for (int i = 0; i < BUCKET; i++) {cout << i;for (auto x : table[i])cout << " --> " << x;cout << endl;}}// Driver programint main(){// array that contains keys to be mappedint a[] = {15, 11, 27, 8, 12};int n = sizeof(a)/sizeof(a[0]);// insert the keys into the hash tableHash h(7); // 7 is count of buckets in// hash tablefor (int i = 0; i < n; i++)h.insertItem(a[i]);// delete 12 from hash tableh.deleteItem(12);// display the Hash tableh.displayHash();return 0;}
#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)) != 0return (num & (1 << 31));}void printBinaryNumber(const int& num, const int numBits){for(int i = numBits; i > 0; --i){//8..1int bitMask = 1 << (i-1);if(num & bitMask){ //Test the bitcout << '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..1int bitMask = 1 << i;bool aBit = a & bitMask;bool bBit = b & bitMask;if(aBit && bBit || aBit && carry || bBit && carry){ //Carry bit is true nextif(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..1int bitMask = 1 << i;bool aBit = a & bitMask;bool bBit = b & bitMask;if((!(aBit ^ carry)) && bBit){ //Carry bit is true nextif(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;}