实现Luhn检验和实现c++实现

实验代码一

 1 #include <iostream>
 2 using namespace std;
 3 
 4 //<<像程序员一样思考>> 实现Luhn检验和实现 
 5 //Luhn原理 标识号 最后一位是检验数字  
 6 //回车 ASC码值 10 
 7 int method(int temp);
 8 bool luhnMethod(int temp);
 9 int main()
10 {
11 //  const int array_size = 6;
12     const int array_size = 7;
13     int sum = 0;
14     //步骤划分 先实现数组为偶数 大小为6的数组 
15     //乘以2的数字分别为 1 5 9 乘以2后分别为 2 1 9
16     //2 + 1 + 9 + 3 + 7 + 8 = 30 
17     //数组索引0 2 4 
18 //    int age[] = {1,3,5,7,9,8};
19 //    for(int i=0; i<array_size; i++)
20 //    {
21 //        if(i % 2 == 0)
22 //        {
23 //            sum += method(age[i]);
24 //        }
25 //        else
26 //        {
27 //            sum += age[i];
28 //        }
29 //    }
30 //    cout << "sum: " << sum << endl;
31 //    bool flag = luhnMethod(sum);
32 //    cout << "flag: " << flag << endl;
33     //实现数组为奇数 大小为7的数组
34     //1,3,5,7,9,8,2,
35     //乘以2的数字 3 7 8 乘以2的结果为 6 5 7  
36     //在数组中的索引位置 分别为 1 3 5 
37     //相加的结果为 1 + 6 + 5 + 5 + 9 + 7 + 2 = 35 
38     int age[] = {1,3,5,7,9,8,2};
39     for(int i=0; i<array_size; i++)
40     {
41         if(i % 2 != 0)
42         {
43             sum += method(age[i]);
44         }
45         else
46         {
47             sum += age[i];
48         }
49     }
50     cout << "sum: " << sum << endl;
51     bool flag = luhnMethod(sum);
52     cout << "flag: " << flag << endl;
53     return 0;
54 }
55 //如果传入的数值乘以2小于10 返回乘以2之后的数 若大于10 返回个位 十位数 数字相加之和 
56 int method(int temp)
57 {
58     int doubleValue;
59     temp = 2 * temp;
60     if(temp < 10)
61     {
62         doubleValue = temp;
63     }
64     if(temp >= 10)
65     {
66         doubleValue = 1 + temp%10;
67     }
68     return doubleValue;
69 }
70 //Luhn 相加的和能被10整除 则说明标识号正确 否则错误 
71 bool luhnMethod(int temp)
72 {
73     if(temp % 10 == 0)
74     {
75         return true;
76     }
77     else
78     {
79         return false;
80     }
81 }
实验代码二
 1 #include <iostream>
 2 using namespace std;
 3 
 4 //如何处理数字输入  统计输入的数值键的个数 
 5 int main()
 6 {
 7     int temp;
 8     int count = 0;
 9     cout << "input number please" << endl;
10     temp = cin.get();
11     while(true)
12     {
13         //回车键的ASC码值为10  
14         if(temp == 10)
15         {
16             //回车键不算输入的数值键 
17             break;
18         }
19         else
20         {
21             count++;
22             temp = cin.get();
23         }
24     }
25     cout << "输入的数值键的个数为" << count << endl;
26     return 0;
27 }

实验代码三

每输入一个字符就要计算 然而要等到输入完所有数值时,才可以判断输入的数值是奇数还是偶数 

如何用代码实现这个功能

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int inputMethod();
 5 int method(int temp);
 6 int main()
 7 {
 8     int result = inputMethod();
 9     cout << "result: " << result << endl;
10     return 0;
11 }
12 //方法改进 还要对每一个输入的数值调用检验方法 
13 //int inputMethod()
14 //{
15 //    int inputValue;
16 //    //统计输入的数值键的个数 
17 //    int count = 0;
18 //    cout << "请输入标识号" << endl;
19 //    inputValue = cin.get();
20 //    while(true)
21 //    {
22 //        if(inputValue == 10)
23 //        {
24 //            break;
25 //        }
26 //        else
27 //        {
28 //            count++;
29 //            inputValue = cin.get();
30 //        }
31 //    }
32 //    return count;
33 //}
34 int inputMethod()
35 {
36     int inputValue;
37     int count = 1;
38     int jishuLength = 0;
39     int oushuLength = 0;
40     
41     int totalSum = 0;
42     cout << "请输入标识号" << endl;
43     inputValue = cin.get();
44     while(true)
45     {
46         //回车键不算入数字键
47         //回车键ASC码值为10 
48         if(inputValue == 10)
49         {
50             break;
51         }
52         else
53         {
54             //统计输入的数字键个数  
55             //如果输入的数字键的个数为偶数个 则
56             if(count % 2 == 0)
57             {
58                 jishuLength += method(inputValue - '0');
59                 oushuLength += inputValue - '0';
60             }
61                 
62                 //统计输入的数字键的个数为奇数个 则 
63             else
64             {
65                 oushuLength += method(inputValue - '0');
66                 jishuLength += inputValue - '0';
67             }
68 
69             //输入下一个数字键 
70             inputValue = cin.get();
71             count++;
72         }
73     }
74     if(count%2 == 0)
75     {
76         totalSum = jishuLength;
77     }
78     else
79     {
80         totalSum = oushuLength;
81     }
82     return totalSum;
83 }
84 int method(int temp)
85 {
86     int doubleValue;
87     temp = 2 * temp;
88     if(temp < 10)
89     {
90         doubleValue = temp;
91     }
92     if(temp >= 10)
93     {
94         doubleValue = 1 + temp%10;
95     }
96     return doubleValue;
97 }

最后 完整代码

 1 #include <iostream>
 2 using namespace std;
 3 
 4 //<<像程序员一样思考>> 实现Luhn检验和实现 
 5 //Luhn原理 标识号 最后一位是检验数字  
 6 //回车 ASC码值 10 
 7 int method(int temp);
 8 bool luhnMethod(int temp);
 9 int inputMethod(); 
10 int main()
11 {
12 
13 
14     int sum = inputMethod();
15     int result = luhnMethod(sum);
16     cout << "result: " << result << endl;  
17     return 0;
18 }
19 //如果传入的数值乘以2小于10 返回乘以2之后的数 若大于10 返回个位 十位数 数字相加之和 
20 int method(int temp)
21 {
22     int doubleValue;
23     temp = 2 * temp;
24     if(temp < 10)
25     {
26         doubleValue = temp;
27     }
28     if(temp >= 10)
29     {
30         doubleValue = 1 + temp%10;
31     }
32     return doubleValue;
33 }
34 int inputMethod()
35 {
36     int inputValue;
37     int count = 1;
38     int jishuLength = 0;
39     int oushuLength = 0;
40     
41     int totalSum = 0;
42     cout << "请输入标识号" << endl;
43     inputValue = cin.get();
44     while(true)
45     {
46         //回车键不算入数字键
47         //回车键ASC码值为10 
48         if(inputValue == 10)
49         {
50             break;
51         }
52         else
53         {
54             //统计输入的数字键个数  
55             //如果输入的数字键的个数为偶数个 则
56             if(count % 2 == 0)
57             {
58                 jishuLength += method(inputValue - '0');
59                 oushuLength += inputValue - '0';
60             }
61                 
62                 //统计输入的数字键的个数为奇数个 则 
63             else
64             {
65                 oushuLength += method(inputValue - '0');
66                 jishuLength += inputValue - '0';
67             }
68 
69             //输入下一个数字键 
70             inputValue = cin.get();
71             count++;
72         }
73     }
74     if(count%2 == 0)
75     {
76         totalSum = jishuLength;
77     }
78     else
79     {
80         totalSum = oushuLength;
81     }
82     return totalSum;
83 }
84 //Luhn 相加的和能被10整除 则说明标识号正确 否则错误 
85 bool luhnMethod(int temp)
86 {
87     if(temp % 10 == 0)
88     {
89         return true;
90     }
91     else
92     {
93         return false;
94     }
95 }
重新实现
#include <iostream>
using namespace std;

//c++实现Luhn检验 
int DoubleMethod(int i);
int InputMethod();
void CheckMethod(int i);
int main()
{
    int result = InputMethod();
    CheckMethod(result);
    system("pause");
    return 0;
}
//列如 123456 6是检验位 则 1 3 5 数值扩大一倍 若所在位数值扩大一倍大于10 列如 6*2=12 则取 1 + 2 = 3
int DoubleMethod(int i)
{
    i *= 2;
    if (i < 10)
    {
        return i;
    }
    else
    {
        return 1 + i % 10;
    }
}
int InputMethod()
{
    int numberLength = 1;
    int jishuLengthSum = 0;
    int oushuLengthSum = 0;
    cout << "请输入检验数字" << endl;
    char input = cin.get();
    //回车键ASC码10
    while (input != 10)
    {
        if (numberLength % 2 != 0)
        {
            jishuLengthSum += input - '0';
            oushuLengthSum += DoubleMethod(input - '0');
        }
        else
        {
            jishuLengthSum += DoubleMethod(input - '0');
            oushuLengthSum += input - '0';
        }
        input = cin.get();
        numberLength++;
    }
    if (numberLength % 2 != 0)
    {
        return oushuLengthSum;
    }
    else
    {
        return jishuLengthSum;
    }
}
//检验数是否能被10整除
void CheckMethod(int i)
{
    if (i % 10 == 0)
    {
        cout << "该标识号合法" << endl;
    }
    else
    {
        cout << "该标识号非法" << endl;
    }
}

 

 

 

posted @ 2019-04-06 22:23  littlelittleprince  阅读(419)  评论(0编辑  收藏  举报