45-常用算法及正则表达式

一 常用算法

#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()
{
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值