Repository navigation
Expand file tree
/
Copy pathstreamManipulators.cpp
More file actions
162 lines (131 loc) · 6.06 KB
/
Copy pathstreamManipulators.cpp
File metadata and controls
162 lines (131 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// streamManipulators.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
/*
* C++ Functions and Streams: The program can perform 4 functions taht are
presented to the user in an initial menu. The menu presents the options of finding
cosines, finding logarithms, converting between hexadecimal and decimal, and formatting
a c-string date. Within these inital options there are further menus that ask about the
specific function with the intial general category selected. ie. natural log vs log
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>
using namespace std;
int main() {
//inital choice from the main menu
int initalChoice;
//do while loop to allow for the multiple things in a single run
do {
//initial main menu for all of the choices
cout << "\nMain Menu\n1) Find Cosines\n2) Find Logarithms\n3) Convert Decimal/Hexadecimal\n4) Format C-String Date\nEnter Any Other Key To Exit\nEnter your choice: ";
cin >> initalChoice;
if (initalChoice == 1) {
int cosChoice;
double radians;
cout << "\n1) Cosine\n2) Arc Cosine\n3) Hyperbolic Cosine\nChoose Type of Cosine: ";
cin >> cosChoice;
cout << "Enter a Floating Point Number in Radians: ";
cin >> radians;
//setf is set to a fixed decimal point number
cout.setf(ios::fixed | ios::showpoint);
//rouds the number to 3 decimal places
cout << setprecision(3);
if (cosChoice == 1) {
cout << "Cosine: " << cos(radians) << endl;
}
else if (cosChoice == 2) {
cout << "Arc Cosine: " << acos(radians) << endl;
}
else if (cosChoice == 3) {
cout << "Hyperbolic Cosine: " << cosh(radians) << endl;
}
//clearing modifiers
cout.unsetf(ios::fixed | ios::showpoint);
}
else if (initalChoice == 2) {
int logChoice;
double num;
cout << "\n1) Common Logarithm\n2) Natural Logarithm\nChoose Type of Logarithm: ";
cin >> logChoice;
cout << "Enter a Floating Point Number: ";
cin >> num;
//setf for a fixed point decimal that shows that +/- sign and is rounded to 3 decimal places
cout.setf(ios::fixed | ios::showpoint | ios::showpos);
cout << setprecision(3);
if (logChoice == 1) {
cout << "Common Logarithm: " << log10(num) << endl;
}
else if (logChoice == 2) {
cout << "Natural Logarithm: " << log(num) << endl;
}
//clearing modifiers
cout.unsetf(ios::fixed | ios::showpoint | ios::showpos);
}
else if (initalChoice == 3) {
int convChoice;
cout << "\n1) Decimal to Hexadecimal\n2) Hexadecimal to Decimal\nChoose Conversion: ";
cin >> convChoice;
if (convChoice == 1) {
bool isLower;
int decimalNum;
cout << "Use Lowercase Letters for Hexadecimal? (true/false): ";
//boolalpha allows for either "true/false"
cin >> boolalpha >> isLower;
cout << "Enter a Decimal Whole Number: ";
cin >> dec >> decimalNum;
if (isLower) {
cout << "Input Number: " << dec << decimalNum << " -> Hexadecimal: "
<< showbase << hex << nouppercase << decimalNum << dec << endl; //showbase for '0x'/'0X'
}
else {
cout << "Input Number: " << dec << decimalNum << " -> Hexadecimal: "
<< showbase << hex << uppercase << decimalNum << dec << nouppercase << endl;
}
}
else if (convChoice == 2) {
int hexNum;
cout << "Enter a Hexadecimal Number: ";
cin >> hex >> hexNum;
cout << "Input Number: " << showbase << hex << hexNum
<< " -> Decimal: " << dec << hexNum << endl;
}
}
else if (initalChoice == 4) {
char date[12];
char newDate[12] = "";
cout << "\nEnter a C-String Date (MM/DD/YYYY): ";
cin >> date;
//changes / to null through using strtok
char* token = strtok(date, "/");
if (token != NULL) {
//takes the first part before the / and then adds a - and repeats.
strcpy(newDate, token);
strcat(newDate, "-");
token = strtok(NULL, "/");
if (token != NULL) {
strcat(newDate, token);
strcat(newDate, "-");
token = strtok(NULL, "/");
if (token != NULL) {
strcat(newDate, token);
}
}
}
cout << "Newly Formatted Date: " << newDate << endl;
}
} while (initalChoice == 1 || initalChoice == 2 || initalChoice == 3 || initalChoice == 4); //will terminate anything other than 1-4
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file