diff options
Diffstat (limited to 'ee1301/wk5')
-rw-r--r-- | ee1301/wk5/hw5_directory/maze.txt | 10 | ||||
-rw-r--r-- | ee1301/wk5/hw5_directory/strap012_HW5A.cpp | 183 | ||||
-rw-r--r-- | ee1301/wk5/hw5_directory/strap012_HW5B.cpp | 51 | ||||
-rw-r--r-- | ee1301/wk5/hw5_directory/strap012_HW5C.cpp | 130 | ||||
-rw-r--r-- | ee1301/wk5/lab4/arrayCat.cpp | 23 | ||||
-rw-r--r-- | ee1301/wk5/lab4/mysteryBox.cpp | 18 | ||||
-rw-r--r-- | ee1301/wk5/lab4/pOc.cpp | 12 | ||||
-rw-r--r-- | ee1301/wk5/lab4/partner.cpp | 52 | ||||
-rw-r--r-- | ee1301/wk5/lab4/partner2.cpp | 53 | ||||
-rw-r--r-- | ee1301/wk5/lab4/partner3.cpp | 63 | ||||
-rw-r--r-- | ee1301/wk5/lab4/strap012_lab4_w_2.cpp | 54 | ||||
-rw-r--r-- | ee1301/wk5/lab4/swappy.cpp | 24 | ||||
-rw-r--r-- | ee1301/wk5/lab4/time1.cpp | 24 | ||||
-rw-r--r-- | ee1301/wk5/lab4/time2.cpp | 36 |
14 files changed, 733 insertions, 0 deletions
diff --git a/ee1301/wk5/hw5_directory/maze.txt b/ee1301/wk5/hw5_directory/maze.txt new file mode 100644 index 0000000..aa3c598 --- /dev/null +++ b/ee1301/wk5/hw5_directory/maze.txt @@ -0,0 +1,10 @@ +WWWW-O-WWW
+---W------
+--WWWWWW-W
+----------
+WWWWWWWWW-
+----------
+-WW-WW-WW-
+----------
+WW-WW-WW-W
+X---------
\ No newline at end of file diff --git a/ee1301/wk5/hw5_directory/strap012_HW5A.cpp b/ee1301/wk5/hw5_directory/strap012_HW5A.cpp new file mode 100644 index 0000000..768e8e2 --- /dev/null +++ b/ee1301/wk5/hw5_directory/strap012_HW5A.cpp @@ -0,0 +1,183 @@ +/*
+Date: April 4 2019
+Name: Matthew Strapp
+Student ID number: 5449340
+Course number: EE 1301
+Term: Spring 2019
+Lab/assignment number: HW 5A
+Short Program Description: Maze Runner 2.0
+*/
+
+#include <iostream>
+#include <iomanip>
+#include <fstream>
+#include <string>
+using namespace std;
+ifstream fin;
+ofstream fout;
+
+const char BLANK = '-';
+const char ROBOT = 'X';
+const char GOAL = 'O';
+const char WALL = 'W';
+const int screen_num_lines = 25;
+const int lengthX = 10; // for now we only support square boards
+const int lengthY = 10; // it "should" work with non-square, YMMV
+// Board is described by the following coordinate system:
+// 0 --> +X
+// |
+// |
+// V
+// +Y
+
+void initBoardDefault(char board[lengthX][lengthY], int &xPos, int &yPos);
+void initBoardCustom(char board[lengthX][lengthY], int &xPos, int &yPos);
+void clearScreen();
+void showGrid(char board[lengthX][lengthY]);
+
+bool hasChar(char board[lengthX][lengthY], char findMe);
+void updateGrid(char board[lengthX][lengthY], int &xPos, int &yPos, char action);
+
+int main()
+{
+ srand(time(NULL));
+ char board[lengthX][lengthY] = {0};
+ int xPos = 0, yPos = 0;
+ fin.open("maze.txt"); //maze.txt is in the directory that it is run in
+ if (!fin.is_open()) //If a custom maze does not exist, generate one automatically
+ {
+ initBoardDefault(board, xPos, yPos);
+ }
+ else
+ {
+ initBoardCustom(board, xPos, yPos);
+ }
+ while (hasChar(board, GOAL))
+ {
+ char action;
+ clearScreen();
+ showGrid(board);
+ cout << "Where would you like to go? (l, r, u, d) " << endl;
+ cin >> action;
+
+ updateGrid(board, xPos, yPos, action);
+ }
+
+ clearScreen();
+ showGrid(board);
+ cout << "You found the cookie! Congratulations!\n";
+
+ return 0;
+}
+
+//The default (no custom file) maze is procedurally generated, sometimes rendering levels impossible to solve.
+//This bug is not as important as finishing the monster that is 5C.
+void initBoardDefault(char board[lengthX][lengthY], int &xPos, int &yPos)
+{
+ for (int curRow = 0; curRow < lengthY; curRow++)
+ {
+ for (int curCol = 0; curCol < lengthX; curCol++)
+ {
+ if ((rand() % 5) + 1 == 4 || (rand() % 10) + 1 == 7)
+ {
+ board[curCol][curRow] = WALL;
+ }
+ else
+ {
+ board[curCol][curRow] = BLANK;
+ }
+ }
+ }
+
+ board[rand() % lengthX][rand() % lengthY] = GOAL;
+ xPos = rand() % lengthX;
+ yPos = rand() % lengthY;
+ board[xPos][yPos] = ROBOT;
+}
+
+void initBoardCustom(char board[lengthX][lengthY], int &xPos, int &yPos)
+{
+ for (int curRow = 0; curRow < lengthY; curRow++)
+ {
+ for (int curCol = 0; curCol < lengthX; curCol++)
+ {
+ fin >> board[curCol][curRow];
+ if (board[curCol][curRow] == 'X')
+ {
+ board[curCol][curRow] = ROBOT;
+ xPos = curCol;
+ yPos = curRow;
+ }
+ }
+ }
+}
+
+bool hasChar(char board[lengthX][lengthY], char findMe)
+{
+ for (int curRow = 0; curRow < lengthY; curRow++)
+ {
+ for (int curCol = 0; curCol < lengthX; curCol++)
+ {
+ if (board[curCol][curRow] == findMe)
+ {
+ return true;
+ }
+ } //end curCol loop
+ } // end curRow loop
+
+ return false;
+}
+
+void clearScreen()
+{
+ for (int i = 0; i < screen_num_lines; i++)
+ {
+ cout << endl;
+ }
+}
+
+void showGrid(char board[lengthX][lengthY])
+{
+ for (int curRow = 0; curRow < lengthY; curRow++)
+ {
+ for (int curCol = 0; curCol < lengthX; curCol++)
+ {
+ cout << board[curCol][curRow];
+ } // end curCol loop
+ cout << endl;
+ } // end curRow loop
+}
+
+void updateGrid(char board[lengthX][lengthY], int &xPos, int &yPos, char action)
+{
+ int oldX = xPos;
+ int oldY = yPos;
+ if (action == 'l' && xPos > 0)
+ {
+ xPos--;
+ }
+ else if (action == 'r' && xPos < lengthX - 1)
+ {
+ xPos++;
+ }
+ else if (action == 'u' && yPos > 0)
+ {
+ yPos--;
+ }
+ else if (action == 'd' && yPos < lengthY - 1)
+ {
+ yPos++;
+ }
+ //Hit detection
+ if (board[xPos][yPos] == WALL)
+ {
+ xPos = oldX;
+ yPos = oldY;
+ board[xPos][yPos] = WALL;
+ }
+ else
+ {
+ board[oldX][oldY] = BLANK;
+ }
+ board[xPos][yPos] = ROBOT;
+}
diff --git a/ee1301/wk5/hw5_directory/strap012_HW5B.cpp b/ee1301/wk5/hw5_directory/strap012_HW5B.cpp new file mode 100644 index 0000000..f1e0c2c --- /dev/null +++ b/ee1301/wk5/hw5_directory/strap012_HW5B.cpp @@ -0,0 +1,51 @@ +/*
+Date: April 4 2019
+Name: Matthew Strapp
+Student ID number: 5449340
+Course number: EE 1301
+Term: Spring 2019
+Lab/assignment number: HW 5B
+Short Program Description: Random Array
+*/
+
+#include <iostream>
+#include <cstdlib>
+using namespace std;
+
+int main(int argc, char *argv[])
+{
+ if (argc != 4) {
+ cout << "Invalid input!" << endl << " USAGE: ./rand-array m n max" << endl;
+ return 2;
+ }
+
+ srand(time(NULL));
+ int m, n, max;
+ m = atoi(argv[1]); //number of rows
+ n = atoi(argv[2]); //number of columns
+ int randArray[m][n];
+ max = atoi(argv[3]); //Maximum number in array
+
+ //Loop to make the random array
+ for (int curRow = 0; curRow < m; curRow++)
+ {
+ for (int curCol = 0; curCol < n; curCol++)
+ {
+ if (rand() % 2 + 1 == 2) { //Decide the sign of the number
+ randArray[curCol][curRow] = -1 * (rand() % (max+1));
+ } else {
+ randArray[curCol][curRow] = (rand() % (max+1));
+ }
+ }
+ }
+
+ //Loop to output the array in the console
+ for (int curRow = 0; curRow < m; curRow++)
+ {
+ for (int curCol = 0; curCol < n; curCol++)
+ {
+ cout << randArray[curCol][curRow] << " ";
+ }
+ cout << endl;
+ }
+}
\ No newline at end of file diff --git a/ee1301/wk5/hw5_directory/strap012_HW5C.cpp b/ee1301/wk5/hw5_directory/strap012_HW5C.cpp new file mode 100644 index 0000000..2bdda92 --- /dev/null +++ b/ee1301/wk5/hw5_directory/strap012_HW5C.cpp @@ -0,0 +1,130 @@ +/*
+Date: April 4 2019
+Name: Matthew Strapp
+Student ID number: 5449340
+Course number: EE 1301
+Term: Spring 2019
+Lab/assignment number: HW 5C
+Short Program Description: Pixel Averaging
+*/
+#include <iostream>
+#include <string>
+#include <sstream>
+using namespace std;
+
+#define SIZE 100
+int maxRow = -1, maxColumn = 0;
+void getInput(int input[SIZE][SIZE]);
+void makeNewArray(int oldArray[SIZE][SIZE], int newArray[SIZE][SIZE]);
+void pixelAverage(int inArray[SIZE][SIZE], int outArray[SIZE][SIZE]);
+void printArray(int array[SIZE][SIZE]);
+
+int main() {
+ int inputArray[SIZE][SIZE] = {0},
+ outputArray[SIZE][SIZE] = {0};
+ getInput(inputArray);
+ makeNewArray(inputArray, outputArray);
+ pixelAverage(inputArray, outputArray);
+ printArray(outputArray);
+}
+
+// Function: getInput
+// ---------------------------
+// Takes the stream of stdin, puts that into a stringsream and puts that stringsteam into an integer array
+// input: an empty array of SIZE, as defined in line 15.
+void getInput(int input[SIZE][SIZE]) {
+ string test, temp1; int temp2=0;
+ //I would mainly like to thank whatever user on StackOverflow solved my problem
+ while (getline(cin,test,'\n')) {
+ temp2 = -1;
+ istringstream ss(test);
+ while(getline(ss,temp1,' ')) {
+ temp2++;
+ input[maxRow][temp2] = stoi(temp1);
+ }
+ if (temp2>maxColumn) {
+ maxColumn = temp2+1; //1 is added to make the math work.
+ }
+ maxRow++;
+ }
+ maxRow++;
+}
+
+// Function: makeNewArray
+// ---------------------------
+// Takes the array made in getInput and puts that same array into a new array to manipulate
+// oldArray: Array generated in getInput of SIZE
+// newArray: Array of same dimensions and data
+void makeNewArray(int oldArray[SIZE][SIZE], int newArray[SIZE][SIZE]) {
+ for (int row = 0; row < maxRow; row++) {
+ for (int column = 0; column < maxColumn; column++) {
+ newArray[row][column] = oldArray[row][column];
+ }
+ }
+}
+
+// Function: pixelAverage
+// ---------------------------
+// The meat of the program, taking the surrounding values, adding them, and divides by the number of surrounding values
+// inArray: Generated in getInput
+// outArray: Generated in makeNewArray
+void pixelAverage(int inArray[SIZE][SIZE], int outArray[SIZE][SIZE]) {
+ int sum = 0, n = 0, average; //n is the number of surrounding pixels
+ for (int row = -1; row < maxRow-1; row++) {
+ for (int column = -1; column < maxColumn-1; column++) {
+ sum=0; n=0; //Sum and n are reset every loop
+ if ( !((row)<0) ) { //True when array is in lower bounds row-wise
+ sum += inArray[row-1][column];
+ n++;
+ }
+ if ( (row+1)<maxRow-1 ) { //True when array is in upper bounds row-wise
+ sum += inArray[row+1][column];
+ n++;
+ }
+ if ( !((column)<0) ) { //True when array is in lower bounds column-wise
+ sum += inArray[row][column-1];
+ n++;
+ }
+ if ( (column+1)<maxColumn-1 ) {//True when array is in upper bounds column-wise
+ sum += inArray[row][column+1];
+ n++;
+ }
+ //All of these if statements are combinations are previous statements for the diagonal aspects
+ if ( !((row)<0) && !((column)<0) ) {
+ sum += inArray[row-1][column-1];
+ n++;
+ }
+ if ( !((row)<0) && ( (column+1)<maxColumn-1 ) ) {
+ sum += inArray[row-1][column+1];
+ n++;
+ }
+ if ( (row+1)<maxRow-1 && (column+1)<maxColumn-1 ) {
+ sum += inArray[row+1][column+1];
+ n++;
+ }
+ if ( (!((column)<0) && (row+1)<maxRow-1) ) {
+ sum+=inArray[row+1][column-1];
+ n++;
+ }
+ sum+=inArray[row][column];
+ n++;
+ average = sum/n;
+ outArray[row+1][column+1]=average;
+ }
+ }
+}
+
+// Function: printArray
+// ---------------------------
+// Prints the new array after the pixel averaging
+// array: Array generated in pixelAverage
+void printArray (int array[SIZE][SIZE]) {
+ for (int row = 0; row < maxRow; row++)
+ {
+ for (int column = 0; column < maxColumn; column++)
+ {
+ cout << array[row][column] << " ";
+ }
+ cout << endl;
+ }
+}
diff --git a/ee1301/wk5/lab4/arrayCat.cpp b/ee1301/wk5/lab4/arrayCat.cpp new file mode 100644 index 0000000..d711ba6 --- /dev/null +++ b/ee1301/wk5/lab4/arrayCat.cpp @@ -0,0 +1,23 @@ +#include <iostream>
+using namespace std;
+void append (char first[], int l1, char second[], int l2, char result[200]);
+
+int main () {
+ char first[] = {'I', ' ', 'a', 'm', ' '};
+ char second[] = {'i', 'r', 'o', 'n', 'm', 'a', 'n', '\0'};
+ char result[200];
+ append(first, 5, second, 8, result);
+ cout << result << endl;
+}
+
+void append (char first[],int l1, char second[],int l2, char result[200]) {
+ int length=0;
+ for (int i=0; i<l1; i++) {
+ result[length]=first[i];
+ length++;
+ }
+ for (int i=0; i<l2; i++) {
+ result[length]=second[i];
+ length++;
+ }
+}
diff --git a/ee1301/wk5/lab4/mysteryBox.cpp b/ee1301/wk5/lab4/mysteryBox.cpp new file mode 100644 index 0000000..c0c58ab --- /dev/null +++ b/ee1301/wk5/lab4/mysteryBox.cpp @@ -0,0 +1,18 @@ +#include <iostream>
+#include <string>
+using namespace std;
+
+bool mystery(string fstr);
+int main () {
+ string e = "Sargent Pepper's Lonely Hearts Club Band";
+ cout << mystery(e) << endl;
+}
+
+bool mystery(string fstr) {
+ string rstr; // a string is like an array of chars, e.g., char[]
+ for(int i=fstr.length()-1; i>=0 ;i--){
+ rstr += fstr[i]; // fstr[i] gets the char at index i
+ }
+ cout << fstr << endl << rstr << endl;
+ return rstr == fstr;
+}
diff --git a/ee1301/wk5/lab4/pOc.cpp b/ee1301/wk5/lab4/pOc.cpp new file mode 100644 index 0000000..919806f --- /dev/null +++ b/ee1301/wk5/lab4/pOc.cpp @@ -0,0 +1,12 @@ +#include <iostream>
+using namespace std;
+
+int main(int argc, char* argv[]) {
+
+ int i;
+ cout << "You typed the following on the command line:" << endl;
+ for (i = 0; i < argc ; i++) {
+ cout << argv[i] << ", ";
+ }
+ cout << endl;
+}
diff --git a/ee1301/wk5/lab4/partner.cpp b/ee1301/wk5/lab4/partner.cpp new file mode 100644 index 0000000..538163d --- /dev/null +++ b/ee1301/wk5/lab4/partner.cpp @@ -0,0 +1,52 @@ +#include <iostream>
+
+using namespace std;
+
+string requestName();
+double requestHeight(string fullName);
+int requestNumberOfPartners();
+
+
+int main()
+{
+ string fullName[2]; //fullName1, fullName2;
+ double height[2]; //height1, height2;
+
+ fullName[0] = requestName();
+ height[0] = requestHeight(fullName[0]);
+ fullName[1] = requestName();
+ height[1] = requestHeight(fullName[1]);
+
+ cout << "If " << fullName[0] << " and " << fullName[1]
+ << " form a human tower, their combined height will be "
+ << (height[0] + height[1]) << endl;
+
+}
+
+string requestName()
+{
+ string name;
+ cout << "Please enter full name: ";
+ getline(cin, name);
+ return name;
+}
+
+double requestHeight(string fullName)
+{
+ double height;
+ cout << "Please enter " << fullName << "'s height: ";
+ cin >> height;
+ cin.ignore(2, '\n'); // gets rid of \n in the buffer
+
+ return height;
+}
+
+int requestNumberOfPartners()
+{
+ int numberOfPartners;
+ cout << "How many partners are there?";
+ cin >> numberOfPartners;
+
+ return numberOfPartners;
+}
+
diff --git a/ee1301/wk5/lab4/partner2.cpp b/ee1301/wk5/lab4/partner2.cpp new file mode 100644 index 0000000..2b6bd36 --- /dev/null +++ b/ee1301/wk5/lab4/partner2.cpp @@ -0,0 +1,53 @@ +#include <iostream>
+
+using namespace std;
+
+string requestName();
+double requestHeight(string fullName);
+int requestNumberOfPartners();
+void NotinMain(string fullName[], const int length1, double height[], const int length2);
+
+int main()
+{
+ string fullName[2]; //fullName1, fullName2;
+ double height[2]; //height1, height2;
+
+ fullName[0] = requestName();
+ height[0] = requestHeight(fullName[0]);
+ fullName[1] = requestName();
+ height[1] = requestHeight(fullName[1]);
+ NotinMain(fullName, 2, height, 2);
+}
+
+string requestName()
+{
+ string name;
+ cout << "Please enter full name: ";
+ getline(cin, name);
+ return name;
+}
+
+double requestHeight(string fullName)
+{
+ double height;
+ cout << "Please enter " << fullName << "'s height: ";
+ cin >> height;
+ cin.ignore(2, '\n'); // gets rid of \n in the buffer
+
+ return height;
+}
+
+int requestNumberOfPartners()
+{
+ int numberOfPartners;
+ cout << "How many partners are there?";
+ cin >> numberOfPartners;
+
+ return numberOfPartners;
+}
+
+void NotinMain(string fullName[], const int length1, double height[], const int length2) {
+ cout << "If " << fullName[0] << " and " << fullName[1]
+ << " form a human tower, their combined height will be "
+ << (height[0] + height[1]) << endl;
+ }
\ No newline at end of file diff --git a/ee1301/wk5/lab4/partner3.cpp b/ee1301/wk5/lab4/partner3.cpp new file mode 100644 index 0000000..75d6d7b --- /dev/null +++ b/ee1301/wk5/lab4/partner3.cpp @@ -0,0 +1,63 @@ +#include <iostream>
+
+using namespace std;
+
+string requestName();
+double requestHeight(string fullName);
+int requestNumberOfPartners();
+void NotinMain(string array1[], double array2[], int numberOfPartners);
+
+int main()
+{
+ int numberOfPartners = requestNumberOfPartners();
+ string fullName[100]; //fullName1, fullName2;
+ double height[100]; //height1, height2;
+
+ // fullName[0] = requestName();
+ // height[0] = requestHeight(fullName[0]);
+ for (int i=0; i<numberOfPartners; i++) {
+ fullName[i] = requestName();
+ height[i] = requestHeight(fullName[i]);
+ }
+ NotinMain(fullName, height, numberOfPartners);
+}
+
+string requestName()
+{
+ string name;
+ cout << "Please enter full name: ";
+ getline(cin, name);
+ return name;
+}
+
+double requestHeight(string fullName)
+{
+ double height;
+ cout << "Please enter " << fullName << "'s height: ";
+ cin >> height;
+ cin.ignore(2, '\n'); // gets rid of \n in the buffer
+
+ return height;
+}
+
+int requestNumberOfPartners()
+{
+ int numberOfPartners;
+ cout << "How many partners are there? ";
+ cin >> numberOfPartners;
+ cin.ignore(2, '\n');
+
+ return numberOfPartners;
+}
+
+void NotinMain(string array1[100], double array2[100], int numberOfPartners) {
+ double heightTotal=0;
+ cout << "If " << array1[0];
+ for (int i=1; i<numberOfPartners-1; i++) {
+ cout << ", " << array1[i];
+ heightTotal += array2[i];
+ }
+ cout << " and " << array1[numberOfPartners-1]
+ << " form a human tower, their combined height will be "
+ << (array2[0] + heightTotal + array2[numberOfPartners-1]) << endl;
+ }
diff --git a/ee1301/wk5/lab4/strap012_lab4_w_2.cpp b/ee1301/wk5/lab4/strap012_lab4_w_2.cpp new file mode 100644 index 0000000..5b82cd4 --- /dev/null +++ b/ee1301/wk5/lab4/strap012_lab4_w_2.cpp @@ -0,0 +1,54 @@ +#include <iostream>
+using namespace std;
+
+void bsort(int list[], int length);
+void swap (int &a, int &b);
+void printArray (int list[], int length);
+void makeArray (int list[], int length);
+const int n=50;
+
+int main () {
+int list[n];
+makeArray(list,n);
+bsort(list, n);
+printArray(list, n);
+}
+
+//This function makes the array used for the rest of the program.
+void makeArray(int list[], int length) {
+ int temp=100;
+ for (int i=0; i<n; i++) {
+ list[i]=temp;
+ temp--;
+ }
+}
+
+//This function is the algorithm.
+void bsort(int list[], int length) {
+ for (int i=0; i<n; i++) {
+ for (int j=0; j<n-1; j++) {
+ if (list[j]>list[j+1]) {
+ //This is so the bubble can happen
+ swap(list[j], list[j+1]);
+ }
+ }
+ }
+}
+
+//This function simply swaps two values so the bubbling can commence
+void swap (int &a, int &b) {
+ int temp;
+ temp=a; a=b; b=temp;
+}
+
+//This function prints the array at 5 per line.
+void printArray (int list[], int length) {
+ int k=0;
+ for (int j=0; j<(n/10)*2; j++) {
+ for (int i=0; i<5; i++) {
+ cout << list[k] << " ";
+ k++;
+ }
+ cout << endl;
+ }
+}
diff --git a/ee1301/wk5/lab4/swappy.cpp b/ee1301/wk5/lab4/swappy.cpp new file mode 100644 index 0000000..a2810a6 --- /dev/null +++ b/ee1301/wk5/lab4/swappy.cpp @@ -0,0 +1,24 @@ +#include <iostream>
+using namespace std;
+
+void swap (int &a, int &b);
+int main(int argc, char* argv[]) {
+ if (argc !=2) {
+ cout << "Incorrect number of arguments! USAGE:" << endl
+ << " swappy <string>" << endl;;
+ return 3;
+ }
+ int i;
+ for (i=0; i<10000; i++) {
+ if (argv[1][i]=='\0') {
+ break;
+ }
+ }
+ swap(argv[1][0],argv[1][(i-1)]);
+ cout << argv[1] << endl;
+
+}
+void swap (char &a, char &b) {
+ char temp;
+ temp=a; a=b; b=temp;
+}
diff --git a/ee1301/wk5/lab4/time1.cpp b/ee1301/wk5/lab4/time1.cpp new file mode 100644 index 0000000..0bb9eaa --- /dev/null +++ b/ee1301/wk5/lab4/time1.cpp @@ -0,0 +1,24 @@ +#include <iostream>
+#include <string>
+
+using namespace std;
+
+int timeToMinutes(int hours, int mins);
+int main() {
+ int hours, minutes;
+ char colon, cont = 'y';
+ do {
+ cout << "Enter a time duration (hh:mm) ";
+ cin >> hours >> colon >> minutes;
+ cout << "Total minutes: " << timeToMinutes(hours, minutes) << endl;
+ do {
+ cout << "Continue? (y/n):";
+ cin >> cont;
+ } while ( !( !(cont!='n') != !(cont!='y') ) );
+ //The last logic is a XNOR gate of magic, please do not touch
+ } while ((cont=='y'));
+}
+
+int timeToMinutes(int hours, int mins) {
+ return mins + hours * 60;
+}
diff --git a/ee1301/wk5/lab4/time2.cpp b/ee1301/wk5/lab4/time2.cpp new file mode 100644 index 0000000..0126e23 --- /dev/null +++ b/ee1301/wk5/lab4/time2.cpp @@ -0,0 +1,36 @@ +#include <iostream>
+#include <string>
+
+using namespace std;
+
+void minutesToTime(int minute_value, int& hours, int& mins);
+int main() {
+ int hours, mins;
+ char cont = 'y';
+ do {
+ cout << "Enter a number of minutes: ";
+ cin >> mins;
+ minutesToTime(mins, hours, mins);
+ cout << hours << ":";
+ if (mins<10) {
+ cout << '0' << mins << endl;
+ } else {
+ cout << mins << endl;
+ }
+ do {
+ cout << "Continue? (y/n):";
+ cin >> cont;
+ } while ( !( !(cont!='n') != !(cont!='y') ) );
+ //The last logic is a XNOR gate of magic, touching will disturb the magic
+ } while ((cont=='y'));
+}
+
+void minutesToTime(int minute_value, int& hours, int& mins) {
+ mins=minute_value%60;
+ hours=0;
+ for (int i=minute_value/60; i>0; i--) {
+ hours++;
+ if (hours>12)
+ hours=1;
+ }
+}
|