Untitled
unknown
plain_text
a year ago
13 kB
7
Indexable
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<cctype>
#include<iomanip>
#define k 6.67e-11
//Define the size of bytes
constexpr long double KB = 1024L;
constexpr long double MB = KB*1024L;
constexpr long double GB = MB * 1024LL;
constexpr long double TB = GB*1024LL;
constexpr long double PB = TB * 1024LL;
constexpr long double EB = PB*1024LL;
//Define the math size
constexpr long double K = 1000L;
constexpr long double M = K * 1000L;
constexpr long double G = M * 1000L;
constexpr long double T = G * 1000L;
constexpr long double P = T * 1000L;
constexpr long double E = P * 1000L;
void build_menu(int&);
void problem1(int&);
void problem2(int&, int&, int&);
void problem3(int&);
void problem4(long double& distance, long double& m1, long double& m2);
void problem5(int& num);
void problem6(float&, float&, float&);
void problem7(float& num);
void problem8(float& num1, float& num2);
void problem9();
void problem10();
void convert_upper(std::string&);
int main() {
int opt;
build_menu(opt);
return 0;
}
void convert_upper(std::string& str) {
for (char& ch : str) {
ch = toupper(ch);
}
}
void problem1 (int &num) {
int sum=0;
do {
std::cout << "\n\tPlease enter a 3-digit number: ";
std::cin >> num;
if (num < 100 || num >999) std::cout << "\tInvalid input. Please enter again.\n";
} while (num < 100 || num >999);
const int n = num;
while (num != 0) {
sum += num % 10;
num /= 10;
}
std::cout << "\tThe sum of 3 digits of " << n << " is " << sum << ".";
}
void problem2(int &a, int &b, int &c) {
int num = 0;
do {
std::cout << "\n\tPlease enter the 1st 1-digit number: ";
std::cin >> a;
if (a < 1 || a >9) std::cout << "\tInvalid input. Please enter again.\n";
} while (a < 1 || a >9);
do {
std::cout << "\n\tPlease enter the 2nd 1-digit number: ";
std::cin >> b;
if (b < 0 || b >9) std::cout << "\tInvalid input. Please enter again.\n";
} while (b < 0 || b >9);
do {
std::cout << "\n\tPlease enter the 3rd 1-digit number: ";
std::cin >> c;
if (c < 0 || c >9) std::cout << "\tInvalid input. Please enter again.\n";
} while (c < 1 || c >9);
num = a * 100 + b * 10 + c;
std::cout << "\tThe 3-digit number formed by " << a << ", "
<< b << ", " << c << " is: " << num << ".";
}
void problem3(int& num) {
int s = 0;
do {
std::cout << "\n\tPlease enter a 3-digit number: ";
std::cin >> num;
if (num < 100 || num >999) std::cout << "\tInvalid input. Please enter again.\n";
} while (num < 100 || num >999);
const int n = num;
while (num != 0) {
s *= 10;
s += num % 10;
num /= 10;
}
std::cout << "\tThe reverse number of " << n << " is: " << s << ".";
}
void problem4(long double& distance, long double& m1, long double& m2) {
std::cout << "\n\tPlease enter the distance between 2 bodies (in meters): ";
std::cin >> distance;
std::cout << "\tPlease enter the mass of body 1 (in kg): ";
std::cin >> m1;
std::cout << "\tPlease enter the mass of body 2 (in kg): ";
std::cin >> m2;
long double force = (k * (m1 * m2)) / pow(distance, 2);
std::cout << "\tThe gravitational force between the 2 bodies: " << force << " (Newton).";
}
void problem5(int& num) {
long double num2 = 0;
std::string size;
do {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\n\tPlease enter the unit size of your hard drive(KB -> EB): ";
getline(std::cin, size);
convert_upper(size);
if (size != "KB" && size != "MB" && size != "GB" && size != "TB" && size != "PB" && size != "EB") {
std::cout << "\tInvalid input. Please enter again.";
}
} while (size != "KB" && size != "MB" && size != "GB" && size != "TB" && size != "PB" && size != "EB");
std::cout << "\tPlease enter the storage of your hard drive in (" << size << "): ";
std::cin >> num;
if (size == "KB") {
num2 = num * (K / KB);
}
else if (size == "MB") {
num2 = num * (M / MB);
}
else if (size == "GB") {
num2 = num * (G / GB);
}
else if (size == "TB") {
num2 = num * (T / TB);
}
else if (size == "EB") {
num2 = num * (E / EB);
}
else if (size == "PB") {
num2 = num * (P / PB);
}
std::cout << "\n\tThe actual storage of the hard drive is: " << num2 << " " << size << ", in contrast to "
<< num << " " << size << " .";
}
void problem6(float& a, float& b, float& c) {
float x1 = 0;
float x2 = 0;
float delta = 0;
std::cout << "\n\tPlease enter the real number a: ";
std::cin >> a;
std::cout << "\n\tPlease enter the real number b: ";
std::cin >> b;
std::cout << "\n\tPlease enter the real number c: ";
std::cin >> c;
if (a == 0) {
if (b != 0) {
std::cout << "\n\tThis is a linear equation " << b << "x + " << c << " = 0 :"
<< "\n\tSolutiuon to this equation is " << -c / b;
}
else {
std::cout << "\n\tThis equation has no solutions because both a & b = 0.";
}
std::cout << "\n";
return;
}
delta = pow(b, 2) - (4 * a * c);
if (delta > 0) {
x1 = (-b + sqrt(delta)) / (2*a);
x2 = (-b - sqrt(delta)) / (2*a);
std::cout << "\n\tThere are 2 solutions to the quadratic equation " <<
a << "*x^2 + " << b << "x + " << c << " = 0 " << ":\n"
<< "\t\tx1 = " << x1 << "\n\t\t" << "x2 = " << x2;
}
else if (delta == 0) {
x1 = -b / (2 * a);
std::cout << "\n\tThere is only 1 solution to the quadratic equation " <<
a << "*x^2 + " << b << "x + " << c << " = 0 " << ":\n"
<< "\t\tx = " << x1;
}
else {
std::cout << "\n\tThere is no real solution to the quadratic equation "
<< a << "*x^2 + " << b << "x + " << c << " = 0.";
}
}
void problem7(float& num) {
int fee = 0;
do {
std::cout << "\n\tPlease enter the number of hours that the car parked at the airport(0-24 hrs): ";
std::cin >> num;
if (num < 0 || num >24) std::cout << "\tInvalid input. Please enter again.\n";
} while (num < 0 || num >24);
if (0 <= num && num <= 3) fee = 5;
else if (3 < num && num <= 9) fee = 6 * int(num + 1);
else if (9 < num && num <= 24) fee = 60;
std::cout << "\n\tThe parking fee for " << num << " hours: " << "$" << fee << ".";
}
void problem8(int& num1, int& num2) {
do {
std::cout << "\n\tPlease enter the starting temperature(int): ";
std::cin >> num1;
std::cout << "\n\tPlease enter the ending temperature(int): ";
std::cin >> num2;
if (num2 <= num1) std::cout << "\n\tInvalid inputs. Please neter again.\n";
} while (num2 <= num1);
for (int i = num1; i <= num2; i++) {
float v = 331.3 + 0.61 * i;
std::cout << "\n\tAt " << i << " degrees Celsius the velocity of sound is " << v << " (m/s).\n";
}
}
void problem9() {
struct point {
double x;
double y;
};
point p1, p2;
std::cout << "\n\tPlease enter the x-coordinate of point 1: ";
std::cin >> p1.x;
std::cout << "\n\tPlease enter the y-coordinate of point 1: ";
std::cin >> p1.y;
std::cout << "\n\tPlease enter the x-coordinate of point 2: ";
std::cin >> p2.x;
std::cout << "\n\tPlease enter the y-coordinate of point 2: ";
std::cin >> p2.y;
double m = 0;
m = (p2.y - p1.y) / (p2.x - p1.x);
double c = 0;
c = p1.y - p1.x * m;
std::cout << "\n\tThe linear function of the 2 points (" << p1.x << "," << p1.y << "), "
<< "(" << p2.x << ", " << p2.y << "): " << "y = " << m << "x + " << c;
}
void problem10() {
//Zodiac structure
struct zodiac {
std::string name;
int starting_month;
int ending_month;
int starting_date;
int ending_date;
};
zodiac zodiac_sign[] = {
{"Capricorn", 12, 1, 22, 19},
{"Aquarius", 1, 2, 20, 18},
{"Pisces", 2, 3, 19, 20},
{"Aries", 3, 4, 21, 19},
{"Taurus", 4, 5, 20, 20},
{"Gemini", 5, 6, 21, 21},
{"Cancer", 6, 7, 22, 22},
{"Leo", 7, 8, 23, 22},
{"Virgo", 8, 9, 23, 22},
{"Libra", 9, 10, 23, 22},
{"Scorpio", 10, 11, 23, 21},
{"Sagittarius", 11, 12, 22, 21},
};
//Birthday structure
std::string s;
bool opt = true;
bool check;
struct birthday {
int day, month, year;
};
birthday u;
do {
do {
check = true;
do {
std::cout << "\n\tPlease enter the day of your birth: ";
std::cin >> u.day;
if (u.day < 1 || u.day >31) std::cout << "\n\tInvalid input. Please enter again.\n";
} while (u.day < 1 || u.day >31);
do {
std::cout << "\n\tPlease enter the month of your birth: ";
std::cin >> u.month;
if (u.month < 1 || u.month >12) std::cout << "\n\tInvalid input. Please enter again.\n";
} while (u.month < 1 || u.month >12);
do {
std::cout << "\n\tPlease enter the year of your birth: ";
std::cin >> u.year;
if (u.year < 1924 || u.year >2024) std::cout << "\n\tInvalid input. Please enter again.\n";
} while (u.year < 1924 || u.year >2024);
if (u.month == 4 || u.month == 6 || u.month == 9 || u.month == 11) {
if (u.day > 30) std::cout << "\n\tYou have entered invalid day of birth. Please enter again.\n";
}
if (u.month == 2) {
if (u.year % 4 == 0) {
if (u.day > 29) std::cout << "\n\tYou have entered invalid day of birth. Please enter again.\n";
check = false;
}
else {
if (u.day > 28) std::cout << "\n\tYou have entered invalid day of birth. Please enter again.\n";
check = false;
}
}
if (check == true) {
for (const auto& zodiac : zodiac_sign) {
if ((u.month == zodiac.starting_month && u.day >= zodiac.starting_date) ||
(u.month == zodiac.ending_month && u.day <= zodiac.ending_date)) {
std::cout << "\n\tYour zodiac sign is " << zodiac.name << ".\n";
break;
}
}
}
}while (check == false);
do {
std::cout << "\n\tDo you want to continue calculating (Yes/No): ";
std::cin.ignore();
getline(std::cin, s);
convert_upper(s);
if (s == "YES") opt = true;
else if (s == "NO") opt = false;
else {
std::cout << "\n\tInvalid option. Please enter again.\n";
opt = true;
}
} while (s != "YES" && s != "NO");
} while (opt == true);
}
void build_menu(int& opt) {
do {
do {
system("cls");
std::cout << "\t\tCHU VAN NHAT MINH - MSSV:20232506\n\n";
std::cout << "\tThe homework consists of 10 problems. Enter 11 to EXIT program.\n";
std::cout << "\tPlease enter an integer to choose the problems (1-11): ";
std::cin >> opt;
if (opt < 1 || opt >11) {
std::cout << "\n\tInvalid input. PLease enter again.\n\n\t";
system("pause");
}
} while (opt < 1 || opt >11);
std::string list[11] = {
"Calculate the sum of digits of a three-digit number",
"Merge three number into one" ,
"Print the reverse of a 3-digit number",
"Calculate the gravitational force between the bodies.",
"Storage capacity of the hard drive.",
"Solve the quadratic equation ax^2 + bx + c = 0 with a,b,c are real numbers provided by users.",
"Calculate the parking fee by inputing number of hours.",
"Calculate the velocity and temperature in 1 degree increment.",
"Calculate the slope between 2 points (x1,y1) and (x2,y2).",
"Write an astrology program.",
"PROGRAM ENDED. THANK YOU!\n",
};
int* arr;
switch (opt) {
case 1: {
std::cout << "\n\t" << list[0] << "\n";
arr = new int;
problem1(*arr);
std::cout << "\n\n\t";
delete arr;
} break;
case 2: {
std::cout << "\n\t" << list[1] << "\n";
arr = new int[3];
problem2(*(arr), *(arr + 1), *(arr + 2));
std::cout << "\n\n\t";
delete[] arr;
} break;
case 3: {
std::cout << "\n\t" << list[2] << "\n";
arr = new int;
problem3(*arr);
std::cout << "\n\n\t";
delete arr;
} break;
case 4: {
std::cout << "\n\t" << list[3] << "\n";
long double* a = new long double[3];
problem4(a[0], a[1], a[2]);
std::cout << "\n\n\t";
delete[] a;
}break;
case 5: {
std::cout << "\n\t" << list[4] << "\n";
arr = new int;
problem5(*arr);
std::cout << "\n\n\t";
delete arr;
} break;
case 6: {
std::cout << "\n\t" << list[5] << "\n";
float* b = new float[3];
problem6(b[0], b[1], b[2]);
std::cout << "\n\n\t";
delete[] b;
} break;
case 7: {
std::cout << "\n\t" << list[6] << "\n";
float hours;
problem7(hours);
std::cout << "\n\n\t";
}break;
case 8: {
std::cout << "\n\t" << list[7] << "\n";
arr = new int[2];
problem8(arr[0], arr[1]);
std::cout << "\n\n\t";
delete arr;
} break;
case 9: {
std::cout << "\n\t" << list[8] << "\n";
problem9();
std::cout << "\n\n\t";
} break;
case 10: {
std::cout << "\n\t" << list[9] << "\n";
problem10();
std::cout << "\n\n\t";
}break;
case 11: {
std::cout << "\n\t" << list[10] << "\n";
std::cout << "\n\t";
}break;
}
system("pause");
} while (opt != 11);
}Editor is loading...
Leave a Comment