确定字符串中有多少单词被格式化为MMDDYY形式的日期,并对其进行计数.(C++)



此程序的目标是读取一行字符,以计数和识别该行中的字数。它必须确定这些单词中有多少是MMDDYY格式的日期。

例如,如果用户输入:"四分和死亡队长前"

输出应如下所示。

总字数:6

有效日期数:0

如果用户输入"四032356团队铁人七231290前"

输出应为

总字数:7

有效日期数:1

我在实现validateDate函数时遇到问题。我也不知道我的日期验证方法是否正确。如果代码一团糟,我最诚挚的道歉,我正在学习基础知识。提前谢谢。这是代码:

// #include "stdafx.h"
// This program demonstrates the use of dynamic variables
// Dean DeFino
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
const int MAXNAME = 10;
//Function prototype
void processCstring(char *);
bool validateDate(char[], int);
int main()
{
    int pos;
    char *name = nullptr;
    int result;
    const int SIZE = 81;    // The maximum size of the C-string.
    char *cstring;          // To hold a C-string
    char goAgain;           // To hold Y or N
    cstring = new char[SIZE]; // Fill in code to allocate the character array pointed to by cstring
    name = new char[SIZE];    // Fill in code to allocate the character array pointed to by name
    cout << "Enter your last name with exactly 10 characters." << endl;
    cout << "If your name has < 10 characters, repeat last letter. " << endl;
    cout << "Blanks at the end do not count." << endl;
    for (pos = 0; pos < MAXNAME; pos++)
        cin >> *(name + pos);   // Fill in code to read a character into the name array
                                // WITHOUT USING a bracketed subscript
    cout << "Hi ";
    for (pos = 0; pos < MAXNAME; pos++)
        cout << *(name + pos);
    do
    {
        cin.ignore();
        // Get a C-string.
        cout << "nEnter a C-string containing 80 or fewer characters:n";
        cin.getline(cstring, SIZE);
        // Display the number of words in the C-string.
        cout << "nThe number of words in the C-string:  ";
        processCstring(cstring);

        // Does the user want to do this again?
        cout << "Process another string? (Y or N) ";
        cin.get(goAgain);
        // Validate the input. If the user enter something diferent of y,Y or n,N
        //then ask again for an answer.
        while ((toupper(goAgain) != 'Y') && (toupper(goAgain) != 'N'))
        {
            cout << "Please enter Y or N: ";
            cin.get(goAgain);
        }
    } while (toupper(goAgain) == 'Y');

    // Fill in code to deallocate cstring and name (two steps each pointer)
    delete cstring;
    cstring = nullptr;
    delete name;
    name = nullptr;
    return 0;
}
//***************************************************
// Function processString                               *
// This function counts the number of words and the  *
// number of valid datres in a string passed into an str.   *
//***************************************************
void processCstring(char *cStrPtr)
{
    char *str = cStrPtr;
    int numWords = 0;
    int numDates = 0;
    int count = 0; // Variable para que el loop no sea infinito.
    //****WARNING
    //Do NOT RUN this while loop until you fixed.
    //This is  infinite loop
    while (*(str + count) != '')
    {
        if (isspace(*(str + count)))    //Complete the code to count the number of words.
            count++;                    //Skip any leading spaces
        else if (isalnum(*(str + count)) && isspace(*(str + count + 1)))
            numWords++;
        count++;
        (validateDate(str, strlen(str));
            cout << "h";
        //Now count the number of valid dates in the form MMDDAA
        //for example 040390 is a valid date, 230490 is not valid
        // 23jun90 is not valid.
        //Use the function validateDate (pass the word you found as
        //parameter) to validate if the word is a valid date
    }
    cout << "nTotal number of words: " << numWords + 1 << endl;
    cout << "Number of valid dates: " << numDates << endl;
}

//***************************************************
// Function validateDate                               *
// This function validate if a word received as a parameter  *
// is a valid date.                                     *
//***************************************************bool 
validateDate(char myPass[], int size)
{
    bool answer = false;
    int i = 0; // Arreglar
​
    while (i < size && !answer)
    {
        /*Validar que sea un string de 6 chars
            Que el primer char1 < 2
            char1 + char2 <= 9
            char3 < 4
            char3 + char4 <= 11*/
​
        if (static_cast<int>(myPass[0]) < 2)
        {
            if (static_cast<int>(myPass[0]) + static_cast<int>(myPass[1]) <= 9)
            {
                if (static_cast<int>(myPass[2]) < 4)
                {
                    if (static_cast<int>(myPass[2]) + static_cast<int>(myPass[3]) <= 11)
                    {
                        answer = true;
                        i++;
                    }
                }
            }
        }
    }
    return answer;
}

对我来说,最简单的方法是使用正则表达式进行日期验证。你可以在这里阅读更多关于它们的信息。C中的正则表达式:示例?并在Inetnet中搜索regexec

还有一种C++方法:http://www.cplusplus.com/reference/regex/

validateDate()的方法看起来不正确,也太复杂了。

我也不同意将regexp用于此目的的建议,因为它会使任务更加复杂。

我的版本想要:

// this method could be improved
int parseDD(const char * in) {
  if (not std::isdigit(in[0]) or not std::isdigit(in[1])) {
    return -1;
  }
  return (in[0] - '0') * 10 + (in[1] - '0');
}
bool validateDate(const char myPass[], int size) {
  if (size != 6) {
    return false;
  }
  static int daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  int mm = parseDD(myPass);
  int dd = parseDD(myPass+2);
  int yy = parseDD(myPass+4);
  if (yy < 0) {
    return false;
  }
  if (not (mm > 0 and mm <= 12)) {
    return false;
  }
  int days = daysInMonth[mm];
  bool leap = yy % 4 == 0;
  if (mm == 2 and leap) {
    days += 1; // 29 Feb                                                                                                                                                             
  }
  if (not (dd > 0 and dd <= days)) {
    return false;
  }
  return true;
}

伙计们,我用了一种不同的方法。感谢您的回复。我肯定知道我的静态广播有问题。

(这是代码)

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
const int MAXNAME = 10;
//Function prototype
void processCstring(char *);
bool validateDate(char[], int);
int main()
{
    int pos;
    char *name = nullptr;
    int result;
    const int SIZE = 81;    // The maximum size of the C-string.
    char *cstring;          // To hold a C-string
    char goAgain;           // To hold Y or N
    cstring = new char[SIZE]; // Fill in code to allocate the character array pointed to by cstring
    name = new char[SIZE];    // Fill in code to allocate the character array pointed to by name

    cout << "Enter your last name with exactly 10 characters." << endl;
    cout << "If your name has < 10 characters, repeat last letter. " << endl;
    cout << "Blanks at the end do not count." << endl;
    for (pos = 0; pos < MAXNAME; pos++)
        cin >> *(name + pos);   // Fill in code to read a character into the name array
                                // WITHOUT USING a bracketed subscript
    cout << "Hi ";
    for (pos = 0; pos < MAXNAME; pos++)
        cout << *(name + pos);
    do
    {
        cin.ignore();
        // Get a C-string.
        cout << "nEnter a C-string containing 80 or fewer characters:n";
        cin.getline(cstring, SIZE);
        // Display the number of words in the C-string.
        cout << "nThe number of words in the C-string:  ";
        processCstring(cstring);

        // Does the user want to do this again?
        cout << "Process another string? (Y or N) ";
        cin.get(goAgain);
        // Validate the input. If the user enter something diferent of y,Y or n,N
        //then ask again for an answer.
        while ((toupper(goAgain) != 'Y') && (toupper(goAgain) != 'N'))
        {
            cout << "Please enter Y or N: ";
            cin.get(goAgain);
        }
    } while (toupper(goAgain) == 'Y');

    // Fill in code to deallocate cstring and name (two steps each pointer)
    delete cstring;
    cstring = nullptr;
    delete name;
    name = nullptr;
    return 0;
}
//***************************************************
// Function processString                               *
// This function counts the number of words and the  *
// number of valid datres in a string passed into an str.   *
//***************************************************
void processCstring(char *cStrPtr)
{
    char *str = cStrPtr;
    int numWords = 0;
    int numDates = 0;
    int size = 0;
    bool started = false;
    //****WARNING
    //Do NOT RUN this while loop until you fixed.
    //This is  infinite loop
    while (*str != '')
    {
        if (started && isspace(*str))
        {
            if (validateDate(str - size, size))
            {
                numWords++;
                numDates++;
            }
            else
                numWords++;
            size = 0;
        }
        else if (!isspace(*str))
        {
            if (started)
                size++;
            else
                started = true;
        }
        str++;
    }
    if (size > 0)
    {
        if (validateDate(str - size, size))
            numDates++;
        else
            numWords++;
    }
    //numDates = validateDate(str, strlen(str)); Test de la variable
    cout << "nTotal number of words: " << numWords << endl;
    cout << "Number of valid dates: " << numDates << endl;
}
//***************************************************
// Function validateDate                               *
// This function validate if a word received as a parameter  *
// is a valid date.                                     *
//***************************************************
bool validateDate(char myPass[], int size)
{
    bool answer = false;
    int i = 0;
    while (i<size && !answer)
    {
        if (i == 0)
        {
            if (myPass[i] == '0')
                i++;
            else if (myPass[i] == '1')
            {
                if (myPass[i + 1] != '0' && myPass[i + 1] != '1' && myPass[i + 1] != '2')
                    i = 7;
                else
                    i++;
            }
            else
                i = 7;
        }
        if (i == 1)
            i++;
        if (i == 2)
        {
            if (myPass[i] == '0' || myPass[i] == '2')
                i++;
            else if (myPass[i] == '3')
            {
                if (myPass[i + 1] != '0' && myPass[i + 1] != '1')
                    i = 7;
                else
                    i++;
            }
            else
                i++;
        }
        if (i == 3)
        {
            if (isdigit(myPass[i]))
                i++;
            else
                i = 7;
        }
        if (i == 4)
        {
            if (isdigit(myPass[i]))
                answer = true;
            else
                i = 7;
        }
    }
    return answer;
}

最新更新