一 常用算法
#include "widget.h"
#include <QDebug>
using namespace Qt;
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
/*
Qt5 常用算法:
1.double c=gAbs(a):数 qAbs()返回 do e 型数值a的绝对 億:
2.double max=gMax(b,c):函数 qMax()返回两个数值中的最大值;
3.int bn=gRound(b):返回一个与浮点数最接近的整数值(四舍五入)
4.int cn=gSwap(bn,cn):交换两数的值;
*/
//求绝对值
double x = -98.654,y = 25.98;
double result1 = qAbs(x);
qDebug()<<"x="<<result1;
//求两个数的最大值
double maxresult = qMax(x,y);
qDebug()<< "Maxresult" << maxresult;
//取整
int result2 = qRound(y);
qDebug()<< "result2" << result2;
int result3 = qRound(x);
qDebug()<< "result3" << result3;
//两个数交换
qSwap(x,y);
qDebug()<< "x = " << x<<" y = "<<y<< endl;
}
Widget::~Widget()
{
}
二 正则表达式
#include "mainwindow.h"
#include <QDebug>
#include <regex>
#include <QString>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
/*
* 通过正则表达式匹配:手机号码
* 根据国家目前对手机号码规定:11数字,
* 其中各段有不同的编码方式
* 前三位:网络识别号(移动,联通,电信)
* 8-11位为用户号码
*中国移动:134 159 158188
*中国联通:130 133 189 156
*相当以1开头,第2位35 8,共计有11位。
*
*/
QString qMobileNumber = "18923558899";
std::regex reg("^1(3|5|8)\\d{9}$");
std::string UserTelIdString = qMobileNumber.toStdString();
qDebug()<<"Phone Number"<<qMobileNumber;
//进行匹配
bool bResult = std::regex_match(UserTelIdString,reg);
if(!bResult )
{
qDebug()<<qMobileNumber<<"---->>error mobile phone number";
}
else
{
qDebug()<<qMobileNumber<<"----->right";
}
}
MainWindow::~MainWindow()
{
}