Wind咨讯南京高校算法竞赛试题:输入ID号,提取出生日期

本来5分钟就写完的程序,后来在输入设计的测试用例时发现了严重的BUG,即年月日没有判断。

好吧,这又花了我十几分钟构思逻辑结构,考虑了所有的情况之后,将逻辑结构简化到最精简,程序如下:

#include <iostream>
#include <cstring>

using std::cin;
using std::cout;
using std::endl;

#define N 100
#define ID_LEN 18

int main(int argc, char *argv[])
{

	char str[N];
	int str_len,i=0;
	
	cout<<"Input your ID numer, 18-word string: " <<endl;
	cin>>str;

	str_len = strlen(str);
	if(str_len != ID_LEN)
	{
		cout<<"Input string number is not 18!"<<endl;
		exit(1);
	}

	while(i<ID_LEN)
	{
		// 合法字符识别
		if( (i <= ID_LEN-1 && (str[i] >='0' && str[i] <='9')) || (i == ID_LEN-1 && str[i] =='X') )
		{
			// ID合法并输出日期
			if( i==ID_LEN-1 )
			{
				cout<<"Your Birthday Info is: "<<std::endl;
				for(int j=6;j<=13;j++)
				{
					cout<<str[j];
				}
				cout<<endl;
			}

			// 月判别
			if( (i==10 && str[i]>'1') || (i==11 && str[i-1]=='0' &&  str[i]=='0') || (i==11 && str[i-1]=='1' &&  str[i]>'2') )
			{
				cout<<"Wrong Month Info!"<<endl;
				exit(1);
			}

			// 日判别
			if( (i==12 && str[i]>'3') || (i==13 && str[i-1]=='0' &&  str[i]=='0') || (i==13 && str[i-1]=='3' &&  str[i]>'1') )
			{
				cout<<"Wrong Data Info!"<<endl;
				exit(1);
			}

			++i;
		}
		else
		{
			cout<<"Exist Unavailable Words!"<<endl;
			exit(1);
		}
	}

	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值