13test02:信用卡校验

/*#include<iostream>
using namespace std;
void input();
int counter=0,jishu_sum=0,oushu_sum=0,sum=0;
int input_num[20];
int main()
{
cout<<"Please enter 16 to 19 numbers which arrange from 0~9:,input 'e' means end";
cout<<endl;
input();
cout<<"Inputed numbers is: "<<endl;
for(int i=0;i<counter;i++)
cout<<input_num[i];
int jishu_i=counter;
int oushu_i=counter;
do
{


jishu_sum+=input_num[jishu_i];
jishu_i-=2;
}
while(jishu_i>=0);
do
{
int oushu=input_num[oushu_i-1]*2;
if(oushu>9) oushu-=9;
oushu_sum+=oushu;
oushu_i-=2;
}
while(oushu_i>=0);


sum=oushu_sum+jishu_sum;
if(!(sum%10))
cout<<endl<<"The card numbers is ligal."<<endl;
else 
cout<<endl<<"The card numbers is illigal."<<endl;


return 1;
}








void input()
{
char end;
do
{   if(counter<20)


{   cin>>input_num[counter];
while(input_num[counter]>9)
{
cout<<"error,please enter numbers from 0~9:";
cin>>input_num[counter];
}
cout<<"end?";
cin>>end;
    cout<<counter+1<<" numbers have been inputed"<<endl<<"please enter numbers from 0~9"<<endl;
   ++counter;
}


else 
{
cout<<"input again:";
counter=0;
input();
}


}while(end!='e');
}*/
#include<iostream>
using namespace std;
int input(char*);
int sum1(char*,int);
int main()
{
char buffer[30]={'0'};
int numbers=input(buffer);
int sum=sum1(buffer,numbers);//不要让函数名与变量名相同.
if(sum%10==0)cout<<"ligal;"<<endl;
else cout<<"illigal:"<<endl;
return 0;
}








int input(char*p)
{
cout<<"请输入16至19位0~9的数字:"<<endl;
int i=0;
do{
if(i==30)break;
p[i]=(char)getchar();//getchar()返回值是读取字符对应的ASCII值。
//可以将其强制转化为char类型
//cout<<p[i];
}while(p[i++]!='\n');


if(i<17||i>20)
{
cout<<"位数输入错误,请重新输入:"<<endl;
input(p);
}
else cout<<"the card number:"<<p<<endl;
return i;//函数可以返回局部变量但是不能返回局部指针.
}




int sum1(char* p,int x)
{
int oushu_sum=0,jishu_sum=0;
for(int i=x-1;i>=0;i-=2)
jishu_sum+=p[i];
for(i=x-2;i>=0;i-=2)
{
if(p[i]*2>=10)
oushu_sum+=p[i]*2-9;
else oushu_sum+=p[i]*2;
}
return oushu_sum+jishu_sum;
}


/*int main()
{
cout<<100-'a';//计算机对字符常量的处理是将其翻译为对应的ASCII值
              //虽然肉眼看到的是‘a',但是计算机执行的时候看到是其码值.








}*/
posted @ 2014-03-27 18:10  lz3018  阅读(135)  评论(0编辑  收藏  举报