#include #include #include #include #include #include // For numeric_limits used in input buffer clearing using namespace std;
/*
- This program offers a menu based interface to perform four different types of operations:
-
- Cosine Functions: Compute cosine, arccosine, or hyperbolic cosine of a given radian value.
-
- Logarithmic Functions: Calculate common logarithm (base 10) or natural logarithm (base e) of a positive number.
-
- Decimal/Hexadecimal Conversion: Convert numbers between decimal and hexadecimal, allowing lowercase or uppercase hex output.
-
- CString Date Format: Reformat dates from mm/dd/yyyy to mm-dd-yyyy format using C-style strings.
- To use, select an option by entering the number (1-4), then follow the prompts to input your data.
- After completing an operation, you can choose to perform another or exit the program. */
int main() { int choice; bool continueProgram = true;
while (continueProgram) {
cout << "\nSelect an option:\n";
cout << "1. Cosine Functions\n";
cout << "2. Logarithmic Functions\n";
cout << "3. Decimal/Hexadecimal Conversion\n";
cout << "4. CString Date Format\n";
cout << "Enter your choice (1-4): ";
cin >> choice;
// Clear input buffer after reading an integer to avoid leftover '\n' affecting subsequent getline calls
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// Set all floating-point output to fixed-point notation with 3 decimal places
cout << fixed << setprecision(3);
if (choice == 1) {
string cosType;
double value;
// Use getline to read full line, supporting inputs like "arccos"
cout << "Choose type: cos, arccos, or hcos: ";
getline(cin, cosType);
cout << "Enter a number in radians: ";
cin >> value;
// Clear buffer after numeric input to prevent input issues later
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// Compute and print based on user choice using cmath functions
if (cosType == "cos")
cout << "Cosine: " << cos(value) << endl;
else if (cosType == "arccos")
cout << "Arc Cosine: " << acos(value) << endl;
else if (cosType == "hcos")
cout << "Hyperbolic Cosine: " << cosh(value) << endl;
else
cout << "Invalid cosine type.\n";
}
else if (choice == 2) {
string logType;
double value;
cout << "Choose log type: common log or natural log: ";
// Using getline to support inputs with spaces like "common log"
getline(cin, logType);
cout << "Enter a positive number: ";
cin >> value;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// showpos forces the '+' sign for positive numbers (optional display detail)
cout << showpos;
if (logType == "common log")
cout << "Common Logarithm: " << log10(value) << endl; // log base 10
else if (logType == "natural log")
cout << "Natural Logarithm: " << log(value) << endl; // natural log base e
else
cout << "Invalid log type.\n";
// Reset showpos to default (no '+' sign)
cout << noshowpos;
}
else if (choice == 3) {
string direction;
cout << "Convert: dec to hex or hex to dec? ";
// Read full line to support inputs with spaces
getline(cin, direction);
if (direction == "dec to hex") {
bool useLowercase;
int decimalNumber;
cout << "Use lowercase hex output? (true/false): ";
// Read boolean input as 'true' or 'false'
cin >> boolalpha >> useLowercase;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter a whole decimal number: ";
cin >> decimalNumber;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\nOriginal Decimal: " << dec << decimalNumber << endl;
// Output hexadecimal with 0x prefix, either lowercase or uppercase letters
if (useLowercase)
cout << "Hexadecimal (lowercase): " << showbase << hex << nouppercase << decimalNumber << endl;
else
cout << "Hexadecimal (UPPERCASE): " << showbase << hex << uppercase << decimalNumber << endl;
// Reset output formatting to default
cout << dec << nouppercase << noshowbase;
}
else if (direction == "hex to dec") {
int hexNumber;
cout << "Enter hexadecimal number (no prefix, e.g., 5d): ";
// Read hex input (base 16)
cin >> hex >> hexNumber;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\nOriginal Hexadecimal: " << showbase << hex << hexNumber << endl;
cout << "Converted Decimal: " << dec << hexNumber << endl;
// Reset formatting back to decimal, no base prefix, lowercase letters
cout << dec << nouppercase << noshowbase;
}
else {
cout << "Invalid conversion type entered.\n";
}
}
else if (choice == 4) {
char original[20];
char copy[20];
char newDate[20] = "";
cout << "Enter a date in mm/dd/yyyy format: ";
cin >> original;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// Copy the original string safely before tokenizing
strcpy_s(copy, sizeof(copy), original);
// Tokenize the string by '/' delimiter
char* next_token = nullptr;
char* month = strtok_s(copy, "/", &next_token);
char* day = strtok_s(nullptr, "/", &next_token);
char* year = strtok_s(nullptr, "/", &next_token);
// Build new date format with dashes instead of slashes
strcat_s(newDate, sizeof(newDate), month);
strcat_s(newDate, sizeof(newDate), "-");
strcat_s(newDate, sizeof(newDate), day);
strcat_s(newDate, sizeof(newDate), "-");
strcat_s(newDate, sizeof(newDate), year);
cout << "Formatted Date: " << newDate << endl;
}
else {
cout << "Invalid choice.\n";
}
// Ask the user if they want to run another operation or exit
char again;
cout << "\nWould you like to run another operation? (y/n): ";
cin >> again;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear input buffer for next iteration
if (again != 'y' && again != 'Y') {
continueProgram = false;
}
}
cout << "Program ended.\n";
return 0;
}