Uploaded by Izuchukwu OMENIFE

COS1512 Assignment2solutions

advertisement
Student No; 14277352
Name: Izuchukwu Omenife
Assignment 2
QUESTION 1
#include <iostream>
#include <iomanip>
const double GLOBALMAIL_COST_ZONE_1_3 = 108.0;
const double GLOBALMAIL_COST_ZONE_4_6 = 130.0;
const double DHL_COST_PER_KG = 70.0;
const int VOLUMETRIC_DIVISOR = 5000;
double calcPostage(double weight, int zone) {
if (zone >= 1 && zone <= 3) {
return weight * GLOBALMAIL_COST_ZONE_1_3;
} else if (zone >= 4 && zone <= 6) {
return weight * GLOBALMAIL_COST_ZONE_4_6;
} else {
std::cerr << "Invalid zone. Zone must be between 1 and 6." << std::endl;
return 0.0;
}
}
double calcPostage(double actualWeight, double length, double width, double
height) {
double volumetricWeight = (length * width * height) / VOLUMETRIC_DIVISOR;
double weightToUse = (actualWeight > volumetricWeight) ? actualWeight :
volumetricWeight;
return weightToUse * DHL_COST_PER_KG;
}
int main() {
char serviceType;
std::cout << "Enter 'd' for DHL or 'g' for GlobalMail: ";
std::cin >> serviceType;
if (serviceType == 'g') {
int zone;
double weight;
std::cout << "Enter the zone (1-6): ";
std::cin >> zone;
if (zone < 1 || zone > 6) {
std::cerr << "Invalid zone. Please enter a zone between 1 and 6." <<
std::endl;
return 1;
}
std::cout << "Enter the weight of the parcel in kg: ";
std::cin >> weight;
double cost = calcPostage(weight, zone);
std::cout << "The cost to send the parcel via GlobalMail is: R" << std::fixed <<
std::setprecision(2) << cost << std::endl;
} else if (serviceType == 'd') {
double actualWeight, length, width, height;
std::cout << "Enter the actual weight of the parcel in kg: ";
std::cin >> actualWeight;
std::cout << "Enter the length of the box in cm: ";
std::cin >> length;
std::cout << "Enter the width of the box in cm: ";
std::cin >> width;
std::cout << "Enter the height of the box in cm: ";
std::cin >> height;
double cost = calcPostage(actualWeight, length, width, height);
std::cout << "The cost to send the parcel via DHL is: R" << std::fixed <<
std::setprecision(2) << cost << std::endl;
} else {
std::cerr << "Invalid service type. Please enter 'd' for DHL or 'g' for GlobalMail."
<< std::endl;
return 1;
}
return 0;
}
Enter 'd' for DHL or 'g' for GlobalMail: d
Enter the actual weight of the parcel in kg: 111
Enter the length of the box in cm: 24
Enter the width of the box in cm: 12
Enter the height of the box in cm: 10
The cost to send the parcel via DHL is: R7770.00
Question2
#include <iostream>
#include <cassert>
#include <ctime>
int getCurrentYear() {
time_t t = time(0);
struct tm* now = localtime(&t);
return now->tm_year + 1900;
}
void checkVotingEligibility(int yearOfBirth) {
int currentYear = getCurrentYear();
assert(yearOfBirth != currentYear && "Year of birth should not be the current
year.");
assert(yearOfBirth <= currentYear && "Year of birth should not be in the future.");
int age = currentYear - yearOfBirth;
if (age >= 18) {
std::cout << "You are allowed to vote." << std::endl;
} else {
std::cout << "You are not allowed to vote." << std::endl;
}
}
int main() {
int yearOfBirth;
std::cout << "Enter your year of birth: ";
std::cin >> yearOfBirth;
checkVotingEligibility(yearOfBirth);
return 0;
}
Run 1: Year of Birth = 2011 (Younger than 18)
Enter your year of birth: 2011
You are not allowed to vote.
Run 2: Year of Birth = 2000 (At least 18)
Enter your year of birth: 2000
You are allowed to vote.
Question 3
#include <iostream>
#include <fstream>
#include <iomanip>
int main() {
std::ifstream inputFile("orders.dat",std::ios:: binary);
std::ofstream outputFile("result.dat");
if (!inputFile.is_open()) {
std::cerr << "Error opening input file." << std::endl;
return 1;
}
if (!outputFile.is_open()) {
std::cerr << "Error opening output file." << std::endl;
return 1;
}
int totalFamilies = 0;
int specialFamilies = 0;
float totalCommission = 0.0;
float totalSpent = 0.0;
int totalPeople = 0;
int familyMembers, specialOrders, wineBottles;
float billAmount;
while (inputFile >> familyMembers >> specialOrders >> wineBottles >>
billAmount) {
totalFamilies++;
totalPeople += familyMembers;
totalSpent += billAmount;
if (familyMembers >= 5 && specialOrders >= 4 && wineBottles >= 2) {
specialFamilies++;
totalCommission += 0.03 * billAmount;
}
}
float averageSpentPerPerson = totalSpent / totalPeople;
outputFile << "Number of families that ordered the special: " << specialFamilies <<
std::endl;
outputFile << "Commission earned from the special meal: R" << std::fixed <<
std::setprecision(2) << totalCommission << std::endl;
outputFile << "Average spent per person for the evening: R" << std::fixed <<
std::setprecision(2) << averageSpentPerPerson << std::endl;
inputFile.close();
outputFile.close();
return 0;
}
Output
Number of families that ordered the special: 3
Commission earned from the special meal: R102.67
Average spent per person for the evening: R137.59
Question 4
#include <iostream>
#include <fstream>
#include <unordered_map>
void encodeLetter(const std::string& inputFile, const std::string& outputFile) {
std::ifstream inFile(inputFile);
std::ofstream outFile(outputFile);
if (!inFile) {
std::cerr << "Error opening input file: " << inputFile << std::endl;
return;
}
if (!outFile) {
std::cerr << "Error opening output file: " << outputFile << std::endl;
return;
}
std::unordered_map<char, std::string> encodingMap = {
{'t', "1Y"}, {'T', "1Y"},
{'h', "1O"}, {'H', "1O"},
{'j', "1X"}, {'J', "1X"},
{'d', "1B"}, {'D', "1B"},
{'a', "1S"}, {'A', "1S"},
{'p', "1M"}, {'P', "1M"},
{'I', "1Q"}
};
char ch;
while (inFile.get(ch)) {
if (encodingMap.find(ch) != encodingMap.end()) {
outFile << encodingMap[ch];
} else {
outFile.put(ch);
}
}
inFile.close();
outFile.close();
}
int main() {
encodeLetter("letter.txt", "encode.txt");
std::cout << "Encoding complete. Check encode.txt for the encoded letter." <<
std::endl;
return 0;
}
Output
1B1S1O1B 1X1QS1O1,
You 1S1O1Y 1Y1O1Y 1QS1O m1Y1S 1BS1O1U1Y1Iu1Ql g1IQ1Y 1Y1S1Y 1Q
h1S1Oe e1Y1Q 1Y1O1I. 1Q w1S1S wo1Q1Oe1Qi1Q
1Q yo1U wo1U1M1Ql li1Y1O co1M1S 1S1Q 1vi1Q1Y 1Me. 1M1Q mo1Y1O1Y
wi1Ml m1S1Qe u1S
1M1S1Q1Q1S1Oe wi1Y1Q i1Ce c1Qe1S1M. 1M1Q 1B1O1X, 1Be1Q1S, 1X1U1S1Y
h1S1O 1Y1h1Q1Oe 1BS1S1U1Y1Q1I1Ml 1M1U1MM1Qe1S.
1Mo1M 1S1Q1Ys 1Q m1S1Y 1Q1Q1Qy 1Me1Q 1Qo1S 1Y1hem. 1Q wo1U1M1Ql
li1Y1O yo1U 1Y1O 1Q1O1M m1Q
c1ho1O1S1Q o1Q1O, 1Be1c1S1U1Se 1Y1h1Q 1S1Q1Q1S1l1Q c1U1Ye 1S1Q1O
1S1Q1S1B1Q1S. 1S1Q1B 1X1U1S1Y
1Be1c1S1U1Se yo1U 1S1Q1Q1S m1Q 1SP1QS1Q1S1l 1F1O1Qi1Q1Q, yo1U
m1S1Q 1S1S1Q h1S1O o1Q1Q 1Q yo1U w1S1Q1Y.
Yo1U1Q 1F1Qi1Q1Q,
1H1O1C1Y1O1Q
Download