Skip to content

Instantly share code, notes, and snippets.

@kabeer11000
Created September 7, 2023 06:54
Show Gist options
  • Save kabeer11000/76ff4629e74339410735873ef9c1ee9c to your computer and use it in GitHub Desktop.
Save kabeer11000/76ff4629e74339410735873ef9c1ee9c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
// Function implementation, not important for this question
double Task3Evaluate(const std::string& expression) {
std::stack<double> operands;
std::istringstream iss(expression);
std::string token;
while (iss >> token) {
if (isdigit(token[0]) || (token[0] == '-' && token.length() > 1)) {
// If the token is a number (including negative numbers)
operands.push(std::stod(token));
} else {
// If the token is an operator
double operand2 = operands.top();
operands.pop();
double operand1 = operands.top();
operands.pop();
if (token == "+") {
operands.push(operand1 + operand2);
} else if (token == "-") {
operands.push(operand1 - operand2);
} else if (token == "*") {
operands.push(operand1 * operand2);
} else if (token == "/") {
if (operand2 == 0) {
throw std::runtime_error("Division by zero is not allowed.");
}
operands.push(operand1 / operand2);
} else {
throw std::runtime_error("Invalid operator: " + token);
}
}
}
if (operands.size() != 1) {
throw std::runtime_error("Invalid expression: " + expression);
}
return operands.top();
}
// Expression solving calculator, based on the operator-array algorithm I discussed
void Task3() {
std::string expression;
// Input an expression
std::cout << "Enter an expression: \n";
std::cin.clear();
std::cin.ignore();
std::getline(std::cin, expression); // The problem, this takes the last 'enter' press form the input buffer and so the code executes with basically a blank expression
try {
// Evaluate the expression and give a result in double form
double result = Task3Evaluate(expression);
std::cout << "Result: " << result << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
int main() {
int a;
std::cin >> a;
Task3();
return 0;
}
@kabeer11000
Copy link
Author

/**

  • Define the Book class with private members the title of the book, its code, and its price. It should also
    contain the following public functions:
    • void review(float grd) which grades the book with the grade grd. The review() can be called
    multiple times for the same book. The final score is the average of the grades.
    • a constructor that accepts as parameters the data of the book and assigns them to the corresponding
    members of the object.
    Add to the class any other function or member you think is needed.
    The following test program declares an array of five objects of type Book and initializes it. It calls the
    review() of the objects few times. Then, the program read the code of a book from user and, if the book
    exists, the program display its score and price. If the book does not exist or it exists but has not been
    graded, the program display a related message.

**/
#include "iostream"
class Book {
private:
static int reviews[] = {};
int price, code;
std::string title;
double getLength() {
return (sizeof(this->reviews) / sizeof(int));
}

public: 
    Book(std::string title, int code, int price) {
        this->title = title;
        this->code = code;
        this->price = price;
    }
    void review(int n) {
        this->reviews[this->getLength()] = n;
    }
    double getScore() {
        for(i = 0; i < (this->getLength()); ++i) sum += this->reviews[i];
        return sum / (this->getLength());
    }
    int getCode() {
        return this->code;
    }
    int getPrice() {
        return this->price;
    }

};
int main() {
Book books[5] = {
Book("The C++ Programming Language", 1, 60),
Book("The Mythical Man-month", 2, 40),
Book("The Pragmatic Programmer: Your Journey to Mastery", 3, 50),
Book("The Art of Computer Programming", 4, 50),
Book("C++ For Dummies", 5, 30)
};
books[0].review(5);
books[0].review(4);
books[1].review(4);
books[2].review(2);
books[3].review(5);
int code;
std::cout << "Enter code: ";
std::cin >> code;
for (int i = 0; i < 5; i++) if (books[i].getCode() == code) {
if (books[i].getScore() == 0) {
std::cout << "The book has not been graded yet" << std::endl;
else {
std::cout << "The book has score " << books[i].getScore() << " and price " <<
books[i].getPrice() << std::endl;
}
return 0;
}
}
std::cout << "The book does not exist" << std::endl
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment