如何将字符串形式的日期 ( "Dec 25, 2012" ) 转换为一组整数 (12/25/12)?



我在编程方面很新,我的教授希望该类编写一个函数GetDate(int *dptr, int *mptr, int *yptr),提示用户输入一个字符串并将其输出为整数,其中它通过dptrmptryptr返回这些值。 我还不明白这个级别的指针(我了解基础知识),我只需要任何愿意提供帮助的人的支持。 我的代码效率也非常低,是的,我知道我没有使用指针并且输出完全错误,但我正在尝试。 我是新来的,所以我可能看起来很业余,但有人请帮助我。

#include <iostream>
#include <string>
using namespace std;
void GetMonth() {
string month;
cout << "Please enter month: ";
cin >> month;
if (month == "jan")
{
cout << "01/" << endl;
}
else if (month == "feb")
{
cout << "02/" << endl;
}
else if (month == "mar")
{
cout << "03/"<< endl;
}
else if (month == "apr")
{
cout << "04/" << endl;
}
else if (month == "may")
{
cout << "05/" << endl;
}
else if (month == "june")
{
cout << "06/" << endl;
}
else if (month == "july")
{
cout << "07/" << endl;
}
else if (month == "aug")
{
cout << "08/" << endl;
}
else if (month == "sept")
{
cout << "09/" << endl;
}
else if (month == "oct")
{
cout << "10/" << endl;
}
else if (month == "nov")
{
cout << "11/" << endl;
}
else if (month == "dec")
{
cout << "12/" << endl;
}
}
int main()
{
string month;
int day, year;
GetMonth();
cout << "Enter a day: ";
cin >> day;
cout << "Enter a Year: ";
cin >> year;
cout << "The date in int is: " << month << day << year << endl;
return 0;
}

**********编辑******* 我重做了整个代码并使用数组完成了它,但我仍然需要帮助!!

#include <iostream>
#include <string>
using namespace std;
void readDate (char array[]);
void GetDate (char array[]);
const int size = 11;
int main ()
{
char original[size];
readDate(original); // call readDate
GetDate(original); // call GetDate
return 0;
}
void readDate (char array[])
{
string month;
cout << "Enter a date (Ex: Jan/20/2003): ";
cin.getline (array, size); // read size characters into the character array
cin >> month;
}
void getDate (char array[])
{   string month;
string Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sept, Oct, Nov, Dec;
cout << "Date in converted format is: ";
if (month == Jan) {
cout << "01";
}
else if (month == Feb) {
cout << "02";
}
else if (month == Mar) {
cout << "03";
}
else if (month == Apr) {
cout << "04";
}
else if (month == May) {
cout << "05";
}
else if (month == Jun) {
cout << "05";
}
else if (month == July) {
cout << "07";
}
else if (month == Aug) {
cout << "08";
}
else if (month == Sept) {
cout << "09";
}
else if (month == Oct) {
cout << "10";
}
else if (month == Nov) {
cout << "11";
}
else if (month == Dec) {
cout << "12";
}
cout << array[3] << array[4] << array[6] << array[7] << array[8] << array[9] << endl;
}

这里有一些代码。 它旨在帮助您,而不是为您做。

尝试研究它并发展它。仅当您输入正确的日期格式时,代码才能正常工作。

您可以添加一些可以验证输入的函数,并处理不同的格式可能性,例如大写和小写输入。

注意:GetDatebool,当前仅返回true。您需要工作并在需要时使其返回 false。

#include "stdafx.h"
#include <conio.h>
#include <string>
#include <iostream>
#include <cctype>
using namespace std;
bool GetDate(int *dptr, int *mptr, int *yptr)
{
string date, day, month, year;
cout << "n Please enter a date: ";
getline(cin, date);
month = date.substr(0, 3);
day = date.substr(4, 2);
year = date.substr(8, 4);
string month_array[12] = { "jan","feb","mar","apr","may","jun",
"jul","aug","sep","oct","nov","dec" };
for (int i = 0; i < 12; ++i)
if (month == month_array[i])
*mptr = i + 1;
*dptr = stoi(day);
*yptr = stoi(year);
return true;
}
int main()
{
int day, month, year;
int *d = &day, *m = &month, *y = &year;
if (GetDate(d, m, y))
cout << "nnn date is:  " << *m << "/" << *d << "/" << *y;
else
cout << "incorrect date";
_getch();
return 0;
}

最新更新