0% found this document useful (0 votes)
92 views

Assignment 7

This document contains 3 C++ programs written by Sumra Kanwal for her CIS285 C++ Programming course, taught by Professor Hashem Tabrizi at the American College of Commerce and Technology. The first program defines functions to convert numbers to their English word equivalents. The second program finds the product with the best value based on price and score from user input. The third program defines a Product class to encapsulate the data for each product and includes functions to read in data, compare products, and print details.

Uploaded by

Nahian Kabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Assignment 7

This document contains 3 C++ programs written by Sumra Kanwal for her CIS285 C++ Programming course, taught by Professor Hashem Tabrizi at the American College of Commerce and Technology. The first program defines functions to convert numbers to their English word equivalents. The second program finds the product with the best value based on price and score from user input. The third program defines a Product class to encapsulate the data for each product and includes functions to read in data, compare products, and print details.

Uploaded by

Nahian Kabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Sumra Kanwal

Course: CIS285 C++ Programming

Instructor: Professor Hashem Tabrizi

Assignment 7

American College of Commerce and Technology

Program 1
#include <iostream>
#include <string>
using namespace std;

double balance;

/**
Turns digit into its English name,
@param n integer between 1 and 9
@ return th ename of n ("one"... ("nine")
*/

string digit_name (int n)


{
if (n == 1) return "one";
if (n == 2) return "two";
if (n == 3) return "three";
if (n == 4) return "four";
if (n == 5) return "five";
if (n == 6) return "six";
if (n == 7) return "seven";
if (n == 8) return "eight";
if (n == 9) return "nine";
return " ";
}
/**
Turns a number between 10 and 19 into its english names,
@param n an integer between 10 an d19
@return the name of n ("ten"... ("nineteen")
*/

string teen_name (int n)


{
if (n == 10) return "ten";
if (n == 11) return "eleven";
if (n == 12) return "twelve";
if (n == 13) return "thirteen";
if (n == 14) return "fourteen";
if (n == 15) return "fifteen";
if (n == 16) return "sixteen";
if (n == 17) return "seventeen";
if (n == 18) return "eighteen";
if (n == 19) return "nineteen";
return "";

/**
Given the English name of a multiple of 10,
@param n an integer between 2 and 9
@return the name of n ("twenty"... ("ninety")
*/
string tens_name (int n)

{
if (n == 2) return " twenty";
if (n == 3) return "thirty";
if (n == 4) return "forty";
if (n == 5) return "fifty";
if (n == 6) return "sixty";
if (n == 7) return "seventy";
if (n == 8) return "eighty";
if (n == 9) return "ninety";
return "";

/** Turns the number into its English name ,


@param n a positive integer <1,000,000
@return the name of n (e.g. "two hundred seventy four")

*/
string int_name (int n)
{

int c = n; // The part still needs to be converted


string r; // The return value
if (c>= 1000)

{
r = int_name (c/1000) + " thousand";
c = c % 1000;
}

if (c >= 100)

{
r = r + " " + digit_name(c/ 100) + "hundred";
c = c % 100;

}
if (c >= 10)

{
r = r + " " +tens_name (c/ 10);
c = c % 10;
}
if (c>=10)
{
r = r + " " + teen_name (c);
c = 0;
}

if (c > 0)
r = r + " " + digit_name(c);

return r;

}
int main ()
{
int n;
cout << "please enter a positive integer: " ;
cin >> n;
cout << int_name (n);

return 0;
}

Program 2

#include <iostream>
#include <string>

using namespace std;

int main ()

{
string best_name = " ";
double best_price = 1;
int best_score = 0;

bool more = true;


while (more)
{
string next_name;
double next_price;
int next_score;

cout << "Please enter the model name: ";


getline (cin, next_name);
cout << " Please enter the price: ";
cin >> next_price;
cout << " Please enter the score: ";
cin >> next_score;
string remainder; // Read remainder of line
getline (cin, remainder);

if (next_score / next_price, best_score / best_price)


{
best_name = next_name;
best_score = next_score;
best_price = next_price;

cout << " More date? (y/n)";


string answer;
getline (cin, answer);

if (answer != "y") more = false;

cout << " The best value is " << best_name

<< " Price: " << best_price


<< " Score: " << best_score << "/n";

return 0;

Program 3

#include <iostream>

#include <string>

using namespace std;

class Product

public:

/**

Constructs a product with score 0 and price 1.

*/

Product();
/**

Reads in this product object.

*/

void read();

/**

Compares two product objects.

@param b the object to compare with this object

@return true if this object is better than b

*/

bool is_better_than(Product b) const;

/**

Prints this product object.

*/

void print() const;

private:

string name;

double price;

int score;

};

Product::Product()

{
price = 1;

score = 0;

}
void Product::read()

cout << "Please enter the model name: ";

getline(cin, name);

cout << "Please enter the price: ";

cin >> price;

cout << "Please enter the score: ";

cin >> score;

string remainder; // Read remainder of line

getline(cin, remainder);

bool Product::is_better_than(Product b) const

if (price == 0) return true;

if (b.price == 0) return false;

return score / price > b.score / b.price;

}
void Product::print() const

cout << name

<< " Price: " << price

<< " Score: " << score;


}
int main()

Product best;

bool more = true;

while (more)

Product next;

next.read();

if (next.is_better_than(best))

best = next;

cout << "More data? (y/n) ";

string answer;

getline(cin, answer);

if (answer != "y")

more = false;

cout << "The best value is ";

best.print();

return 0;

You might also like