genyjoannerubiera
06.04.2020 •
Computers and Technology
Time Conversion C++: Given a time in -hour AM/PM format, convert it to military (24-hour) time.
Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.
Input Format
A single string containing a time in -hour clock format (i.e.: or ), where and .
Output Format
Convert and print the given time in -hour format, where .
Sample Input
07:05:45PM
Sample Output
19:05:45
Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.
Input Format
A single string containing a time in -hour clock format (i.e.: or ), where and .
Output Format
Convert and print the given time in -hour format, where .
Sample Input
07:05:45PM
Sample Output
19:05:45
Explanation::
Code in C++ is given below.
Please read all the comments for better understanding of the code.
Screenshots of the output are provided at the end of the code.
Code in C++::
#include
#include
#include
#include
using namespace std;
string timeConversion(string s){
/**
* Following two strings are declared named d and c respectively.
* String c will be storing the converted format time and we will return
* c as the answer.
*/
string d,c="";
/**
* String d stores "PM" or "AM"
*/
d=s.substr(8,2);
/**
* An integer named hr is declared below and it stores
* the hh part of string hh:mm:ssPM in integer format.
*/
int hr=atoi(s.substr(0,2).c_str());
if(hr==12 && d=="AM"){
/**
* Now suppose hr is 12 and its AM then we know that it's
* midnight and so hr must be 0.
*/
hr=0;
}
if(d=="PM" && hr!=12){
/**
* Suppose d is "PM" and hr is not 12 then we add 12 into hr.
*/
hr+=12;
}
if(hr<10){
/**
* Now suppose hr is less then 10 then we know that in final answer
* if hr is 7 then we need "07".
* So if hr < 10 then we add extra 0 at start of c string.
*/
c+="0";
}
/**
* Now we convert hr back to string using stringstream.
* A variable named hour is declared and we convert hr to string as follows.
*/
stringstream hour;
hour< /**
* Finally we update the c string as required and return it at the end.
*/
c=c+hour.str()+s.substr(2,6);
return c;
}
int main(){
/**
* A string named s is declared and using cin we scan the string.
*/
string s;
cin>>s;
/**
* Below we call the function and pass string s as parameter.
* Whatever function returns, it is printed.
*/
cout< return 0;
}
OUTPUT::
Test Case 1::
CAChegg\Clock.exe 07:05:45PM 19:05:45 Process returned ? (0x0) execution time : 28.842 s Press any key to continue.
Could you please give another example of the atoi method? Also, please explain the following line of source code:
int hr=atoi(s.substr(0,2).c_str());
What is c_str()?
Is there a simpler way to convert the int variable hr to a string type? Why use stringstream in the first place?
stringstream hour;
hour<
Why do we need to type .str after the variable name "hour"? Does s.substr(2,6) "capture" or gets the 2nd to the 6th element of the string s? Why did the string s get shorter? Initially, the string has a length of 10 elements and now the string s is only needed from the 2nd element to the 8th element? What happened to "AM" or "PM"?
c=c+hour.str()+s.substr(2,6);
Solved
Show answers
More tips
- G Goods and services How to Choose a Humidifier? Helpful Tips and Recommendations...
- W Work and Career What is the Most In-Demand Profession in the Modern World?...
- A Auto and Moto How Can Parking Sensors Help Drivers?...
- H Health and Medicine What is the Normal Blood Sugar Level in a Healthy Person?...
- F Food and Cooking Red Caviar: How to Choose the Best?...
- C Computers and Internet Clearing Cache: How to Speed Up Your Browser...
- S Style and Beauty How are artificial nails removed?...
- S Style and Beauty Secrets of Tying a Pareo: 5 Ways...
- F Food and Cooking Everything You Need to Know About Pasta...
Answers on questions: Computers and Technology
- C Computers and Technology The command at the Bash or Terminal shell enables you to terminate running programs in Linux or macOS, just like the tools in Command or PowerShell....
- C Computers and Technology What if soy milk is just regular milk introducing himself in spanish?...
- H History All truths are easy to understand once they are discovered the point is to discover them -Galileo Galilei The quotation above best demonstrates...
- M Mathematics Hii if you can please help with these parts :)...
- E English 3) Analysis: Based on the soldiers statements in lines 8 - 10, what do you think motivated him to join the Revolutionary Army? What does this suggest to you about the...
- P Physics Please hurry please What is the height of the counter where a 4 kg fruit basket sits that has 30 J of gravitational potential energy? h= GPE/am...
- E English In what ways does the author react differently to Anne Frank’s diary when she reads it as an adult? Cite evidence from the text to support your answer...
- E English what do these details tell you about who Perrault is as a character? How does Pearl feel about his job? (Call of the Wild) plz hurry...
- A Arts Style Wars I need help with these questions about the art documentary “Style Wars”...
- E English How does paragraph 4 contribute to the development of the themeof the story? Mrs. Pynchum kept the poor-farm, and Gobaly was “town’s poor.” The boys sometimes called...
Ответ:
Code in C++::
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
string timeConversion(string s){
/**
* Following two strings are declared named d and c respectively.
* String c will be storing the converted format time and we will return
* c as the answer.
*/
string d,c="";
/**
* String d stores "PM" or "AM"
*/
d=s.substr(8,2);
/**
* An integer named hr is declared below and it stores
* the hh part of string hh:mm:ssPM in integer format.
*/
int hr=atoi(s.substr(0,2).c_str());
if(hr==12 && d=="AM"){
/**
* Now suppose hr is 12 and its AM then we know that it's
* midnight and so hr must be 0.
*/
hr=0;
}
if(d=="PM" && hr!=12){
/**
* Suppose d is "PM" and hr is not 12 then we add 12 into hr.
*/
hr+=12;
}
if(hr<10){
/**
* Now suppose hr is less then 10 then we know that in final answer
* if hr is 7 then we need "07".
* So if hr < 10 then we add extra 0 at start of c string.
*/
c+="0";
}
/**
* Now we convert hr back to string using stringstream.
* A variable named hour is declared and we convert hr to string as follows.
*/
stringstream hour;
hour<<hr;
/**
* Finally we update the c string as required and return it at the end.
*/
c=c+hour.str()+s.substr(2,6);
return c;
}
int main(){
/**
* A string named s is declared and using cin we scan the string.
*/
string s;
cin>>s;
/**
* Below we call the function and pass string s as parameter.
* Whatever function returns, it is printed.
*/
cout<<timeConversion(s)<<endl;
return 0;
}
Explanation:
see thees attached
Ответ: