Coffee Shop
Solution
Introductory11.cpp
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
const double SALES_TAX = 3.5;
doubleprice_per_pound = 0.0;
doubletotal_amount = 0.0;
int pounds = 0;
charcharge_tax = ‘ ‘;
cout<< “Number of pounds of coffee ordered: “;
cin>> pounds;
cout<< “Price per pound: “;
cin>>price_per_pound;
cout<< “Should the customer be charged the sales tax (Y/N)? “;
cin>>charge_tax;
total_amount = pounds * price_per_pound;
if (toupper(charge_tax) == ‘Y’) // include sales tax
total_amount += total_amount * SALES_TAX / 100;
cout<< “Total amount owed ” << fixed <<setprecision(2) <<total_amount<<endl;
return 0;
}
Introductory13.cpp
#include <iostream>
using namespace std;
int main()
{
const double FEE_UPTO_5 = 125;
const double FEE_UPTO_20 = 100;
const double FEE_MORE_20 = 75;
doublefee_per_person = 0.0;
doubletotal_amount = 0.0;
int registrants = 0;
cout<< “Number of registrants: “;
cin>> registrants;
if (registrants > 0)
{
if (registrants > 20)
fee_per_person = FEE_MORE_20;
else if (registrants > 5)
fee_per_person = FEE_UPTO_20;
else
fee_per_person = FEE_UPTO_5;
total_amount = fee_per_person * registrants;
cout<< “Total amount owed ” <<total_amount<<endl;
}
else
{
cout<< “The number of registrants should be positive.” <<endl;
}
return 0;
}
lab5-1
1: 0.2
2: 0.15
3: 0.2
4: 0.15
5: 0.15
line 27: setprecision(2)
baecause code has type char and char literals are enclosed in single quotes
(stringlinerals are enclosed in double quotes)
if ((code != ‘1’ && code != ‘3’)
rate = 0.15;
else
rate = 0.2;
sales += sales*rate;
6.
if ((code != ‘1’ && code != ‘3’)
sales += sales*0.15;
else
sales += sales*0.2;
lab6-1
1.
11.46
if (ID == 1)
price = 50.55;
else if (ID == 2 | ID == 9)
price = 12.35;
else if (ID == 5 | ID == 7 | ID == 11)
price = 11.46;
else
price = -1;
3.
price = -1;
if (ID == 1)
price = 50.55;
if (ID == 2 | ID == 9)
price = 12.35;
if (ID == 5 | ID == 7 | ID == 11)
price = 11.46;
switch (ID)
{
case 1:
price = 50.55;
break;
case 2: case 9:
price = 12.35;
break;
case 5: case 7: case 11:
price = 11.46;
break;
default:
price = -1;
break;
}
5.
switch (ID)
{
case 1:
cout<< 50.55;
break;
case 2: case 9:
cout<< 12.35;
break;
case 5: case 7: case 11:
cout<< 11.46;
break;
default:
cout<< “Invalid ID”;
break;
}