我应该使用解析器吗?如果不是我能做什么? C ++帮助!? [英] Should I use a parser? If not what can I do? C++ Help!?
问题描述
***我正在为我的cs1类编写一个程序,该程序应该从输入文件中获取日期并在日期错误或写入时输出。如果格式错误,如果日期错误(不可能)则会显示格式无效,然后显示无效日期。我的日期格式和日期正确,然后它应显示输入文件中的日期等。我是新手,我遇到了问题。任何人都可以帮助我到目前为止我写的代码? ***
***I am Writing a program for my cs1 class that is supposed to take dates from an input file and output if the date is wrong or write. If the format is wrong it will put "Invalid format" if the date is wrong(not possible) then it will display "Invalid date". I the date has correct format and date then it should display the date from the input file ect. I am new to functions and am having problems. Can anyone help me with the code I have written so far below? ***
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string header (void);
bool ValidFormat (int, int, int);
bool ValidDate (string);
void parser (string, int *, int *, int *);
int length = 10;
int month, day, year;
int main()
{
string dates; // this string will be used for any dates in the program
ifstream infile;
ofstream outfile;
infile.open("glasgow_ian_prog4.dat");
outfile.open("glasgow_ian_dates.txt");
outfile << header(); // this is used in displaying my header
while (infile >> dates)
{
if (ValidDate(dates) == false)
outfile << "Invalid Format" << endl;
if (ValidFormat(month, day, year) == false && ValidDate(dates) == true)
outfile << "Invalid Date" << endl;
if (ValidFormat(month, day, year) == true && ValidDate(dates) == true)
outfile << dates << endl;
parser(dates, & month, & day, & year); // this is here for the parser and will read in the string and integers
}
outfile << header();// this is used in displaying my header2
system("pause");
return 0;
}
string header(void)
{
return "********************************Ian Glasgow********************************\n";
}
bool ValidDate(string dates)
{
bool correctFormat = false;//must be set to false because we are always finding anything wrong even if there is something true.
if (dates[2] == '/' && dates[5] == '/' && length == 10)
if (correctFormat == true);
return correctFormat;
}
bool ValidFormat(int month, int day, int year)
{
if (year < 1582)
return false;
if (year == 1582 && month < 10)
return false;
if (year == 1582 && month == 10 && day < 15)
return false;
if (month > 12 || month < 1)
return false;
if ((day > 28 || day < 1) && month == 2 && year % 4 != 0) //this checks not on leap year for february
return false;
if ((day > 29 || day < 1) && year % 4 == 0 && month == 2)// this checks to see if days are correct for leap years
return false;
if ((day > 30 || day < 1) && (month == 4))// checking these months for days over 30
return false;
if ((day > 30 || day < 1) && (month == 6))
return false;
if ((day > 30 || day < 1) && (month == 9))
return false;
if ((day > 30 || day < 1) && (month == 11))
return false;
if ((day > 31 || day < 1) && month == 1)// checking if days are more than 31 for these months
return false;
if ((day > 31 || day < 1) && month == 3)
return false;
if ((day > 31 || day < 1) && month == 5)
return false;
if ((day > 31 || day < 1) && month == 7)
return false;
if ((day > 31 || day < 1) && month == 8)
return false;
if ((day > 31 || day < 1) && month == 10)
return false;
if ((day > 31 || day < 1) && month == 12)
return false;
else
return true;
}
void parser(string date, int *month, int *day, int *year)
{
int m1, m2;
int d1, d2;
int y1, y2, y3, y4;
m1 = date[0] - '0';//setting the space values to place ignoring the /
m2 = date[1] - '0';
*month = m1 * 10 + m2;//
d1 = date[3] - '0';
d2 = date[4] - '0';
*day = d1 * 10 + d2;
y1 = date[6] - '0';
y2 = date[7] - '0';
y3 = date[8] - '0';
y4 = date[9] - '0';
*year = y1 * 1000 + y2 * 100 + y3 * 10 + y4;
}
推荐答案
ValidDate
函数显然没有做什么它应该做。使用缩进你应该看到它(当你设置为更高的警告级别时,你的编译器也应该显示警告):
TheValidDate
function is obviously not doing what it should do. Using indenting you should see it (and your compiler should also show a warning when set to higher warning level):
bool correctFormat = false;
if (dates[2] == '/' && dates[5] == '/' && length == 10)
if (correctFormat == true);
return correctFormat;
<$的名称c $ c> ValidDate 和 ValidFormat
函数互换(每个函数都完成另一个函数)。
您的闰年计算不正确。每100和400年都有特殊规则。
总体而言,您可能会考虑部分程序的不同解决方案。例如。有标准库函数可以将字符串转换为数字。
The names of the ValidDate
and ValidFormat
functions are interchanged (each does the job of the other).
Your leap year calculation is not correct. There are special rules for every 100th and 400th year.
Overall you might think about different solutions to parts of your program. E.g. there are standard library functions to convert strings to numbers.
请参阅此Stack Overflow问题:C ++检查日期是否有效 [ ^ ]。
See this Stack Overflow question: "C++ check if a date is valid"[^].
这篇关于我应该使用解析器吗?如果不是我能做什么? C ++帮助!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!