C++有關ID生產器的輸出問題(急20點)(程式碼)

using namespace std;

int a;

a=0;

int main();{

randomize();

while(a=0){

int n1[]={10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33};

int idstr[10];

int total=0;

int cn=random(26);

idstr[0]=n1[cn];

idstr[1]=random(2)+1;

for(int i=2;i<10;i++)

idstr[i]=random(10);

total=idstr[0]/10+(idstr[0]%10)*9;

for(int k=1;k<9;k++)

total += (idstr[k])*(9-k);

int checkNum = (10 - total % 10) % 10;

if(checkNum == (idstr[9])){

cout<<char(cn+65);

for(int j=1;j<10;j++)

cout<<idstr[j];

return;

}

}

}

2 Answers

Rating
  • 生鏽
    Lv 5
    1 decade ago
    Favorite Answer

    #include <cstdlib>

    #include <ctime>

    #include <iostream>

    using namespace std;

    bool Validate(char *p)

    {

    int wt[10]={1,9,8,7,6,5,4,3,2,1};

    int i=0;

    int temp[10]={0};

    int sum=0;

    int check=0;

    int count=0;

    int alpha[26]={10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33};

    count=toupper(*p)-65;

    temp[0]=alpha[count]/10;

    temp[1]=alpha[count]%10;

    for(i=2;i<=9;i++)

    temp[i]=*(p+i-1)-48;

    for(i=0;i<10;i++)

    sum=sum+temp[i]*wt[i];

    if(sum%10==0)

    check=0;

    else

    check=10-sum%10;

    if(check==(*(p+9)-48))

    return true;

    else

    return false;

    }

    int main()

    {

    ::srand( (unsigned int)time(NULL) );

    int area[]={10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33};

    char id[11];

    int index = ::rand() % 26;

    id[0] = index + 'A';

    id[1] = ::rand() % 2 + '1';

    for(int i=2; i< 9; i++)

    id[i] = ::rand() % 10 + '0';

    int checksum = area[index] / 10 + (area[index] % 10) * 9;

    for(int i=1; i< 9; i++)

    checksum += (id[i] - '0') * (9 - i);

    checksum %= 10;

    if(checksum == 0)

    id[9] = '0';

    else

    {

    checksum = 10 - checksum;

    id[9] = checksum + '0';

    }

    id[10] = 0;

    cout << id << endl;

    //cout << (Validate(id) ? "True" : "False") << endl;

    system("pause");

    }

    Validate 函數跟你的問題無關,純粹是為了驗證用。

    假如你要驗證,把 cout << (Validate(id)... 這一行的 remark 拿掉。

  • Anonymous
    5 years ago

    到下面的網址看看吧

    ▶▶http://*****

Still have questions? Get your answers by asking now.