如何从大数字中获取指定的数字?

时间:2018-04-16 03:33:26

标签: c++

如何获得指定的号码?

例如,用户输入的号码是4568962358。

我想在变量中保存“96”;我怎么得到96或23?

这是真正的问题:

一家重要的航运公司正在对他们仓库中的货物进行年度库存,为此他们已经实施了分配给所述仓库中每种商品的代码系统。此代码由16位数字组成,包含以下信息:唯一编号,是否易碎的指示,属性的原产地和财产的到期日期。 指定的代码结构为UUUFPPPPDDMMAAAA,其中:

  • UUU:商品的唯一编号。
  • F:表示商品是否易碎的数字。如果它为0则意味着它很脆弱。
  • PPPP:两个字母的ASCII代码,用于标识商品的原产国。
  • DD:好到期日。
  • MM:商品到期的一个月。
  • AAAA:商品到期年份。

系统会要求您创建一个C ++程序,该程序将分配的代码作为数据接收,然后打印以下数据,如示例所示。

输入密码:1120677212042015

然后程序必须打印:唯一编号:112。 脆弱的(N:不; S:是):S。 原产国:CH。 到期日,月份和年份:2015年12月4日。 到目前为止它已经过时了(N:不; S:是):S。

在问题的解决方案中,您将无法使用选择性结构。

版本1

#include "stdafx.h"
#include "conio.h"
#include "iostream"


using namespace System;
using namespace std;

int main()
{
    int code;
    int UUU;
    int F;
    int PP1,PP2;
    int DD;
    int MM;
    int AAAA;


    cout << "Enter the code: "; cin >> code; // 1120677212042015

    // ...code needed here

    UUU = int(code / 10000000000000);

    cout << "\nRESULTS" << endl;
    cout << "\nUnique number: " << UUU << endl; // prints 112

    _getch();
    return 0;
}

第2版

我无法做下一部分;请帮忙!

#include "stdafx.h"
#include "conio.h"
#include "iostream"

using namespace System;
using namespace std;

int main()
{
    double code;
    double UUU;
    double F;
    double PP1,PP2;
    double DD;
    double MM;
    double AAAA;


    cout << "Enter the code: "; cin >> code; // 1120677212042015 


    UUU = int(code / 10000000000000);
    PP1 = int(code / 10000000000) % 100;
    PP2 = int(code / 100000000) % 100;
    DD = int(code / 1000000) % 100;


    cout << "\nRESULTS" << endl;
    cout << "\nUnique number: " << UUU << endl; // 112
    cout << "Country of origin: " << (char)PP1 << (char)PP2<<endl; //CH
    cout << "Day, Month and Year of expiration: " << DD; // 12/../....


    _getch();
    return 0;
}

3 个答案:

答案 0 :(得分:2)

一种方法是使用 substr 概念。

例如,如果用户输入输入为1120677212042015,则将其读入字符串并根据格式UUUFPPPPDDMMAAAA将字符串分成子字符串。

std::string sUserCode = "1120677212042015";
std::string sUniqueNumber = str.substr (0,3);
int iUniqueNumber = stoi(sUniqueNumber);

答案 1 :(得分:1)

这是C库派上用场的场合之一:

    int UUU, F, PP1, PP2, DD, MM, AAAA;
    cout << "Enter the code: "; // 1120677212042015
    scanf("%3d %1d %2d %2d %2d %2d %4d", &UUU, &F, &PP1, &PP2, &DD, &MM, &AAAA);

这会捕获所有值。格式字符串中的空格不是必需的,但可以更容易阅读。

答案 2 :(得分:0)

你可以使用%和/来做到这一点 4568962358/10000 = 456896%100 = 96 或者你可以将它放在一个数组中并显示你想要的整数部分。

$res=$mysqli->query('select first, last from 240Insert');
$records = array();
if($res){
    while($rowHolder = mysqli_fetch_array($res,MYSQLI_ASSOC)){
        $records[] = $rowHolder;
    }
}

在堆栈中搜索您的问题。 这些问题都有很好的答案。