Question: PLEASE HELP ME WITH C++! YOU CAN COPY THEM AND PASTE THEM IN YOUR PROGRAM. I CANNOT RUN THEM IN MY PROGRAM BECAUSE I CANNOT
PLEASE HELP ME WITH C++! YOU CAN COPY THEM AND PASTE THEM IN YOUR PROGRAM. I CANNOT RUN THEM IN MY PROGRAM BECAUSE I CANNOT SEE THE OUTPUT. I WILL RATE YOU IF YOU POST THE OUTPUTS.
#include "StackOfIntegers.h" //constructor of StackofIntegers StackOfIntegers::StackOfIntegers() { size = 0; } //returns true if the stack is empty bool StackOfIntegers::isEmpty() const { return size == 0; } //returns peak value of stack int StackOfIntegers::peek() const { return elements[size-1]; } //add elements in stack void StackOfIntegers::push(int value) { elements[size++] = value; } //retrieve elements from stack int StackOfIntegers::pop() { return elements[--size]; } //returns size of stack int StackOfIntegers::getSize() const { return size; }
#ifndef STACK_H #define STACK_H
class StackOfIntegers { public: StackOfIntegers(); bool isEmpty() const; //the functions that stack uses like, push, pop, and peek int peek () const; void push (int value); int pop(); int getSize() const;
private: int elements[100]; int size; }; #endif
#ifndef Stock_H #define Stock_H #include
class Stock { public: //delcaring variables string symbol, name; double previousClosingPrice, currentPrice; //constructor with symbol and name Stock(string Symbol, string Name) { symbol = Symbol; name = Name; currentPrice = 0; } //constant accessor function for getting Name of stock const string getName() { return name; } //constant accessor function for getting symbol of stock const string getSymbol() { return symbol; } //accessor function for getting previous closing price const double getPreviousClosingPrice() { return previousClosingPrice; } //constant accessor function for getting current //price of stock const double getCurrentPrice() { return currentPrice; } const double getChangePercent() { return (currentPrice*100/previousClosingPrice) - 100; } //mutator function for setting previous closing price void setPreviousClosingPrice(double price) { previousClosingPrice = price; } //mutator function for setting current price void setCurrentPrice(double price) { currentPrice = price; } }; #endif
#ifndef MyInteger_H #define MyInteger_H #include
using namespace std;
//implement the class MyInteger class MyInteger { //value assigned as integer type in private private: int value; public: //constructor as argument MyInteger(int newValue) { value = newValue; } //return the int value int getValue() const//the get function is asked to be constant { return value; } //functions are used for checking even, ood, prime using the values of the object //and return boolean type bool isEven() const//constant function { if (value % 2 == 0) { return true; } else { return false; } } bool isOdd()const //asked for constant function { if (value % 2 != 0)
{ return true; } else { return false; } } bool isPrime() const { for (int factor = 2; factor <= value / 2; factor++) { if (value % factor == 0) { return false; break; } } return true; }
//static functions for checking even,odd or prime using the specified //values and return boolean type static bool isEven(int value) { if (value % 2 == 0) { return true; } else { return false; } } static bool isOdd(int value) { if (value % 2 != 0) { return true; } else { return false; } } static bool isPrime(int value) { for (int factor = 2; factor <= value / 2; factor++) { if (value % factor == 0) { return false; break; } } return true; } //static functions for checking even,odd, or prime by passing object //as argument and return boolean type static bool isEven(const MyInteger& myInt) { return myInt.isEven(myInt.getValue()); } static bool isOdd(const MyInteger& myInt) { return myInt.isOdd(myInt.getValue()); } static bool isPrime(const MyInteger& myInt) { return myInt.isPrime(myInt.getValue()); } //function for checking value of the object equals //to specified value or not bool equals(int intValue)const//needed to be a const function { return value == intValue; } //function for checking equality values between two objects bool equals(const MyInteger& myInt)const { return equals(myInt.getValue()); } //function converts string to an integer value static int parseInt(string s) { return (atoi(s.c_str())); } }; #endif
#include
//declaration of global scope void Prog10_1(); void Prog10_2(); void Prog10_4(); void Prog10_5(); void Prog10_6(); void Prog10_7(); void Prog10_10(); void Prog10_12(); void Prog10_14(); void Prog10_16();
//declare the functions bool isAnagram(const string & s1, const string &s2); string commonChars(const string& s1, const string& s2); bool isPalindrome(const string& s); int countLetters(const string& s); void count(string& s, int counts[26], int length); string sort(string& s);
void Prog10_1() { //declare the variables string i1, i2; //read the input for string 1 and string 2 //display the output cout << " Enter the first string: "; cin >> i1; cout << " Enter the second string: "; cin >> i2; //invoke the function if(isAnagram(i1,i2)) cout< } bool isAnagram(const string & s1, const string &s2) { int num1[26]={0}; int num2[26]={0}; //this function has been changed. The overview is //we first check if both strings are same. if so then return true if(s1==s2) { return true; } else//othewise { for(int i=0;i for(int i=0;i<26;i++) { if(num1[i]!=num2[i]) return false; } return true; } } void Prog10_2() { string s1,s2,s3; //prompt user to enter two strings s1 and s2 cout << "Enter a string s1: "; cin.ignore(); getline(cin, s1); cout << "Enter a string s2: "; getline(cin, s2); //invoke commmonChars function s3 = commonChars(s1,s2); //checking the condition whether the resultant string is empty if (s3.empty() == 1) { cout << "No Common Characters" << endl; } else { cout << "The common characters are: " << s3 << " "<< endl; } } string commonChars(const string& s1, const string& s2) { //delcare the variable of string datatype string s; //lterate the for loop and check whether the value of i //is less than the string length and increment the value of i for (int i = 0; i< s1.length(); i++) { //lterate the for loop and check whether the value //of j is less than the string length and increment the value //of j for (int j = 0; j < s2.length(); ++j) { //checking condition using the if statement if (s1[i] == s2[j])//here we need to check if s has already the letter and then append { int l=s.find(s1[i]); if(l==-1) s += s1[i]; } } } return s; } string sort(string& s) { char temp; for(int i=0;i } void Prog10_5() { //declare the string variables string s; cin.ignore(); cout << "Enter a string: "; //get the string getline(cin,s); //invoke the isPalindrome function bool Palindrome = isPalindrome(s); //print if the string is palindrome or not if (Palindrome) { cout << s << " is a palindrome" << endl; } else { cout << s << " is not a palindrome" << endl; } } bool isPalindrome(const string& s) { //for loop is declared which loops equal to the //length of the string for (int i = 0; i < s.length(); i++) { //if-else statement checks if the characters //beginning from the front end is equal to //the characters beginning from the rear end //in the input string if (toupper(s[i]) != toupper(s[s.length()-1-i])) { return false; } } return true; } void Prog10_6() { string s; cin.ignore(); cout << "Enter a string: "; getline(cin,s); //invoke countLetters function int p = countLetters(s); //substract the count, returned by the function //string length and display to the console cout << "The number of letters in " << s << " is " << s.length()-p << endl; } int countLetters(const string& s) { int m = 0, n = 0; for (int i = 0; i < s.length(); i++) { //isdigit function returns the number of digits //in the input string if (isdigit(s[i])) { i++; } //isspace function returns the number of the input string if (isspace(s[i])) { n++; } } return (m+n); } void Prog10_7() { // string is delcared string s; //counts[] array stroes 26 positions for counting 26 letters //and initalized by 0 counts[0] to count [25] count the occurrence of // a to z respectively int counts[26] = {0}; cin.ignore(); cout << "Enter a string:"; getline(cin,s); //accept the string with blankspaces //int size = s.length(); // finds the length of the string 's'//this line is really not needed count(s, counts, 26);//it is given in the instrution that size is the size of counts array, 26 //so let us pass 26 as value to the function instead of size } void count(string& s, int counts[26], int length)//the length is no where used in the function { int i = 0; //traverse the string from first to last character //I am changing this loop from while to for loop, we have s length in our hangs //so let us use it. I am making this changes because, the while loop below //is a never ending loop for(int i=0;i void Prog10_10() { //creating three objects with passing value MyInteger intd1(10); MyInteger intd2(21); MyInteger intd3(37); cout << "Values for the objects are" << endl; cout << "For object1:" << intd1.getValue()< cout << "Checking even for Object1"; bool x = intd1.isEven(); if (x==1) { cout << "TRUE" << endl; } else { cout << "FALSE"<< endl; } cout << "Checking odd for Object2"; bool y = intd2.isOdd(); if ( y == 1) { cout << "TRUE" << endl; } else { cout << "FALSE" << endl; } cout << "Checking prime for Object3"; bool z = intd3.isPrime(); if ( z == 1) { cout << "TRUE" << endl; } else { cout << "FALSE" << endl; } //calling static function, no object is required, called only using ckassname cout << "Checking even, odd, or prime by specified value:" << endl; bool p = MyInteger::isEven(25); if ( p == 1) { cout << "TRUE" << endl; } else { cout << "FALSE" << endl; } bool q = MyInteger::isOdd(28); if (q == 1) { cout << "TRUE" << endl; } else { cout << "FALSE" << endl; } bool r = MyInteger::isPrime(23); if (r == 1) { cout << "TRUE" << endl; } else { cout << "FALSE" << endl; } cout << "Checking even, odd, prime by passing object as argument."<< endl; bool s = MyInteger::isOdd(intd2); if (s == 1) { cout << "TRUE" << endl; } else { cout << "FALSE" << endl; } bool t = MyInteger::isEven(intd1); if (t ==1) { cout << "TRUE" << endl; } else { cout << "FALSE" << endl; } bool u = MyInteger::isPrime(intd1); if (u == 1) { cout << "TRUE" << endl; } else { cout << "FALSE" << endl; } cout << "Checking the value of the object equals to the specified value or not" << endl; bool v = intd1.equals(10); if ( v == 1) { cout << "TRUE" << endl; } else { cout << "FALSE" << endl; } cout << "Checking equality as object as argument:" << endl; bool w = intd1.equals(intd2); if ( w==1) { cout << "TRUE" << endl; } else { cout << "FALSE" << endl; } cout << "Converts string to integer value" << endl; cout << intd1.parseInt("245689") << endl; } void Prog10_12() { //creating object Stock stock("MSFT", "Microsoft Corporation"); //setting previous closing price stock.setPreviousClosingPrice(27.5); //setting current Price stock.setCurrentPrice(27.6); //displaying price change percentage cout << endl<<"The price change in percentage of product " << stock.getName()<< " of symbol " << stock.symbol << " is: " << stock.getChangePercent(); } bool isPrime(int value) { for (int factor = 2; factor <= value / 2; factor++) { if (value % factor == 0) { return false; break; } } return true; } void Prog10_14() { //returns true if the number is prim StackOfIntegers Stack; //if number is prime push it in to stack for (int i = 1; i < 120; i++) { if (isPrime(i)) { Stack.push(i); } } cout << "Display the prime numbers in reverse order" << endl; //pop the prime numbers from stack in reverse order while (!Stack.isEmpty()) { cout << Stack.pop()<< " "; } } void Prog10_16() { //declare an object StackOfIntegers s; //delcare the variables int value, i, temp; //read the input for 'value' //get positive integer from the user cout << "Enter a positive integer:"; cin >> value; //factor value i = 2; //temporary variable for the operation temp = value; //compute the prime factor, and push it into the stack. //loop statement to calculate the prime factors while (i <= temp) { if (temp % i == 0) { //push a factor value into the stack s.push(i); temp = temp / i; } else { i++; } } //pop the values from the stack and display it //display the prime factors cout << "The prime factor for the number "<< value << " are "; while (!s.isEmpty()) { //pop a factor value from the stack cout << s.pop() << " "; } } int main() { while (true) { system("cls"); cout << " Main Menu - Chapter 10 "; cout << "================================== "; cout << " 1: Programming Exercise 10.1 "; cout << " 2: Programming Exercise 10.2 "; cout << " 4: Programming Exercise 10.4 "; cout << " 5: Programming Exercise 10.5 "; cout << " 6: Programming Exercise 10.6 "; cout << " 7: Programming Exercise 10.7 "; cout << " 10: Programming Exercise 10.10 "; cout << " 12: Programming Exercise 10.12 "; cout << " 14: Programming Exercise 10.14 "; cout << " 16: Programming Exercise 10.16 "; cout << "other: Exit "; cout << "================================== "; cout << "Enter an exercise: "; char exercise[2]; cin >> exercise; cout << endl; switch (atoi(exercise)) { case 1: Prog10_1(); break; case 2: Prog10_2(); break; case 4: Prog10_4(); break; case 5: Prog10_5(); break; case 6: Prog10_6(); break; case 7: Prog10_7(); break; case 10: Prog10_10(); break; case 12: Prog10_12(); break; case 14: Prog10_14(); break; case 16: Prog10_16(); break; default: exit(0); } cout << endl; system("pause"); cin.clear(); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
