我希望我能找到正确的逻辑方向。
<<p> 背景信息/strong>应用程序的每日pin是根据已知5个月后的时间表上的月、日、年的一组计算生成的。这个调度操作日期的哪些部分(月、日、年)包含在计算中,以及如何计算(加、减、乘)。
而不是打开我的计算器来计算这个引脚,参考文档,然后从文档中计算,我想做一个程序来为我做这件事,谁知道甚至我的同事,如果他们一直喂我。
我的c#或其他编程经验:"Hello World"经验水平。
研究总结
*找到以下问题的答案:如何在c#中创建表单如何在c#中从一个文本框传输信息到另一个文本框如何关闭应用程序如何获取当前日期。如何在文本框中只获取日期而不显示时间。如何将信息从一个文本框传输到另一个文本框。
*没有找到我要找的东西:如何解析另一个文本框中的文本框中的日期。如何计算转台上解析的信息。如何使用日期时间解析日期,以便在另一个文本框中计算日期。
我的目标让应用程序不需要用户交互,这样我的同事就不会因为他们没有输入正确的方法而生气。
根据需要每5个月调整一次应用程序(我将考虑是否可以自动推出更新,而不是每5个月安装一次)。
找到我缺失的逻辑,以便我可以从那里建立并改进这个应用程序。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Pin_Generator
{
public partial class Home : Form
{
public Home()
{
InitializeComponent();
TodayDateTextBox.Text = DateTime.Now.ToString("MM/dd/yyyy");
TodayPasswordTextBox.Text = TodayDateTextBox.Text;
}
private void Home_Load(object sender, EventArgs e)
{
}
private void CloseButton_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
// private void TodayPasswordTextBox_TextChanged(object sender, EventArgs e)
// {
// int MM;
// int dd;
// int yyyy;
调试时屏幕显示:
今日日期:5/19/2015
今日Pin: 5/19/2015
屏幕应该显示如下内容:今天日期:5/19/2015今日Pin: 33289306093
我知道我还有很长的路要走,但是我看到了很多关于如何计算的资源,而不是关于如何计算我所拥有的"今天的日期"转换为基于日期的不同数字的资源。我也刚刚尝试搜索"我如何将日期转换为计算字符串"
我认为有了正确的问题,我应该能够找到正确的答案,但是到目前为止我的逻辑不一致,我还没有完全记住关键词。也可能是我完全走错了路,我也明白这一点。
提前感谢大家的时间,如果你有任何问题请告诉我。我希望这些截图能帮助我想象我的思想在哪里,希望我能在你的帮助下回到正确的轨道上。
从文本框中解析日期非常简单
DateTime dateInTextbox = DateTime.Parse(TodayDateTextBox.Text);
据此,我建议您创建一个方法,使用DateTime
对象的属性来生成密码,然后像这样调用它:
TodayPasswordTextBox.Text = CalculatePassword(dateInTextbox);
从你的问题听起来你的CalculatePassword()方法看起来像这样:
string CalculatePassword(DateTime target)
{
const int startingPin = 123; // the number your rotation is based on
DateTime current = DateTime.Parse("1/1/2015"); // the date your rotation starts from
int currentPin = startingPin;
while(current < target)
{
currentPin = CalculateNextPin(
currentPin, current.Year, current.Month, current.Day);
current = current.AddDays(1);
}
return currentPin.ToString();
}
当然,您必须自己填写CalculateNextPin()
实现的细节。
如果您创建了如下方法来计算您的PIN。
public string CalculatePIN()
{
DateTime today = DateTime.Now;
int year = today.Year;
int month = today.Month;
int day = today.Day;
string pin = // Do some calculation using the year month and day variables.
return pin;
}
你可以这样修改你的Home方法:
public Home()
{
InitializeComponent();
TodayDateTextBox.Text = DateTime.Now.ToString("MM/dd/yyyy");
TodayPasswordTextBox.Text = CalculatePIN();
}
我知道这是一个很晚的回复,过去几周很忙!谢谢大家的意见。希望我能在答案中多标记一个,因为每一点都对我有帮助。我将amdermott标记为答案,但可以使用StriplingWarrior的信息来处理其他一些概念。我能够让它工作得很好,与以下其他花絮的信息,我发现的逻辑。
下面是我最终想出的让事情真正开始的东西。一旦我理解了分割DateTime的逻辑,以及如何将字符串变成整数,然后再变成字符串,事情就开始真正地结合在一起了。
// Declares string values for each part of the DateTime format to be later calculated.
string sYear = DateTime.Now.ToString("yyyy");
string sMonth = DateTime.Now.ToString("MM");
string sDay = DateTime.Now.ToString("dd");
// Takes string values of the parsed DateTime values and converts them into integers.
int iYear = Convert.ToInt32(sYear);
int iMonth = Convert.ToInt32(sMonth);
int iDay = Convert.ToInt32(sDay);
// Takes the integer values and performs equations with them to get pin.
int pass = iYear * iMonth * (iDay * iDay);
// Takes calculation from above line to pass it back to a string.
string today = pass.ToString();
// Passes the above line into the PinTextBox for display to the operator.
TodayPinTextBox.Text = today;
这可能会为事务生成代码。
public string generateCode()
{
DateTime today = DateTime.Now;
int year = today.Year;
int month = today.Month;
int day = today.Day;
int i = 0;
do
{
i += 1;
string code = "PUL" + Convert.ToString(year) + Convert.ToString(month) + Convert.ToString(day) + Convert.ToString(i);
//you can change the format code as you want
return code;
} while (today == dtpTrans.Value);
}