华科CPP慕课 源课程 第七章 编程题2

手机营运商的识别(35分)

题目内容:

  给出一个手机号,依据其前 3 位,判断其为:中国电信、中国联通、中国移动、不确定。
    中国手机号码一共 11 位数字,前 3 位代表网络识别号,也是移动接入码,可用来判断其营运商。
        中国电信号码段:133,149,153,173,177,180,181,189,191,199;
        中国联通号码段:130,131,132,145,155,156,166,171,175,176,185,186;
        中国移动号码段:134,135,136,137,138,139,147,150,151,152,157,158,
               159,172,178,182,183,184,187,188,198;

  问题:请编写函数 detection,完成手机营运商的判断,如营运商不是电信、联通或移动,则返回假值,否则返回真值。

  代码:(编写 detection 函数,完善以下代码)

#include <iostream>

#include <cstring>

using namespace std;

int main()

{

    char number[12];

    char merchant[9];

    cin>>number;

    if(detection(number, merchant)) 

        cout<<merchant<<endl;

    else

        cout<<"不确定"<<endl;

    return 0;

}

输入格式:

11位的手机号码

输出格式:

运营商的名称或者“不确定”

输入样例:

13871434786

输出样例:

中国移动

代码如下:

#include <iostream>
#include <cstring>
using namespace std;

bool telecom(int a)
{
	if(a==133||a==149||a==153||a==173||a==177)
	return true;
	else if(a==180||a==181||a==189||a==191||a==199)
	return true;
	else return false;	
}//判断电信 

bool unicom(int a)
{
	if(a==130||a==131||a==132||a==145)
	return true;
	else if(a==155||a==156||a==166||a==171)
	return true;
	else if(a==175||a==176||a==185||a==186)
	return true;
	else return false;	
}
//判断联通 

bool mobile(int a)
{
	if(a==134||a==135||a==136||a==137)
	return true;
	else if(a==138||a==139||a==147||a==150)
	return true;
	else if(a==151||a==152||a==157||a==158)
	return true;
	else if(a==159||a==172||a==178||a==182)
	return true;
	else if(a==183||a==184||a==187||a==188||a==198)
	return true;
	else return false;	
}
//判断移动 

bool detection(char *num, char *mer)
{
	int a=0;
	for(int i=0;i<3;i++)
	{
		a=a*10+(*num-'0');
		num++;
	}
	if(telecom(a))
	{
		strcpy(mer,"中国电信"); //拷贝函数实现字符常量传值给字符数组 
		return true;
	}
	else if(unicom(a))
	{
		strcpy(mer,"中国联通"); 
		return true;
	}
	else if(mobile(a))
	{
		strcpy(mer,"中国移动"); 
		return true;
	}
	else return false;
	
}

int main()
{
    char number[12];
    char merchant[9];
    cin>>number;
    if(detection(number, merchant)) 
        cout<<merchant<<endl;
    else
        cout<<"不确定"<<endl;
    return 0;
} 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

季风13

谢谢认可

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值