请帮忙。
以下是问题:编写Qt图形用户界面(GUI)程序以生成用户名和初始密码 用户的全名。
以下是生成有效用户名的规则:
它由5个字符组成,其中第一个字符为大写字母,其余为四个字符 字符较小的情况。
通过将姓氏的前4个字符与第一个字符组合来创建用户名 第一个名字。
通过组合随机选择的5个字符来生成初始密码 用户。
请注意,用户名或密码不允许使用空格。
应使用QInputDialog获取用户输入。
您可以将全名视为单个字符串,其中每个单词都使用空格分隔。例如:Mike William Owen。
应使用QMessageBox显示输出(用户名和密码)。
您无需对用户输入进行任何验证。
当我运行程序时,密码并不反映5个随机字符 - 只有相同的字符 - 每次运行程序时都会有所不同。我也无法弄清楚如何包含第4条规则。
任何帮助将不胜感激
以下是代码:
#include <QtGui>
#include <QDebug>
int randInt(int low, int high)
{
QTime time = QTime::currentTime(); qsrand((uint)time.msec());
return qrand() % ((high + 1) - low) + low;
}
int main (int argc, char* argv[])
{
QApplication app(argc, argv);
QString userFullName = QInputDialog::getText(0, "User Full Name", "Enter your name and surname (first name last name)");
QStringList userInfo = userFullName.split(" ");
QString password;
QString firstName = userInfo[0].trimmed();
QString lastName = userInfo[1].trimmed();
QString FullName = firstName.toLower() + lastName.toLower();
if (FullName.size() < 5)
{
FullName.insert(4,"0");
}
int userL = FullName.length();
for (int i = 0; i < 5; i++)
{
int num = randInt(0,userL);
password[i] = FullName[num];
}
if(userInfo.size() >= 2)
{
QString lastNameChars;
QString firstNameChars;
if (lastName.size() >= 4)
{
QString firstL = QString(lastName.at(0));
firstL.toUpper();
QString secondL = QString(lastName.at(1));
secondL.toLower();
QString thirdL = QString(lastName.at(2));
thirdL.toLower();
QString fourthL = QString(lastName.at(3));
fourthL.toLower();
lastNameChars = firstL + secondL + thirdL + fourthL;
firstNameChars = QString(firstName.at(0));
}
else if (lastName.size() == 3)
{
QString firstL = QString(lastName.at(0));
firstL.toUpper();
QString secondL = QString(lastName.at(1));
secondL.toLower();
QString thirdL = QString(lastName.at(2));
thirdL.toLower();
lastNameChars = firstL + secondL + thirdL;
firstNameChars = QString(firstName.at(0)) +
QString(firstName.at(1));
}
else if (lastName.size() == 2 )
{
QString firstL = QString(lastName.at(0));
firstL.toUpper();
QString secondL = QString(lastName.at(1));
secondL.toLower();
lastNameChars = firstL + secondL;
firstNameChars = QString(firstName.at(0)) + QString(firstName.at(1)) + QString(firstName.at(2));
}
else if (lastName.size() == 1 )
{
QString firstL = QString(lastName.at(0));
firstL.toUpper();
lastNameChars = firstL;
firstNameChars = QString(firstName.at(0)) + QString(firstName.at(1)) + QString(firstName.at(2)) + QString(firstName.at(3));
}
QString UserName = lastNameChars + firstNameChars.toLower();
QString response = QString("The password is %1 for %2 %3 and the username is %4 and the full name is %5").arg(password).arg(firstName).arg(lastName).arg(UserName).arg(FullName);
QMessageBox::information(0, "Results", response);
}
else
{
QMessageBox::information(0, "Error", "Incorrect input data");
}
return 0;
}
答案 0 :(得分:2)
你应该在程序开始时只调用一次srand(或者在这种情况下为qsrand)。否则你不会得到随机数。
//remove qsrand from randInt(), put in main
qsrand(QTime::currentTime().msec());
int userL = FullName.length();
if (userL < 5)
{
for (int i = userL; i < 5; i++)
{
int n = randInt('a', 'z');
QString buf;
buf.sprintf("%c", n);
FullName += buf;
}
}