某城市电话号码由三部分组成。它们的名称和内容分别是: 地区码:空白或三位数字; 前 缀:非 '0'或'1'的三位数字;后 缀:4 位数字。假定被测程序能接受一切符合上述规定 的电话号码,拒绝所有不符合规定的电话号码。根据该程序的规格说明,作等价类的划分,并 设计测试方案,用C++编写C++编写测试代码
时间: 2024-02-05 20:11:03 浏览: 28
等价类划分:
1. 合法电话号码:符合规定的电话号码
2. 地区码不符规定:地区码为空或不是三位数字
3. 前缀不符规定:前缀为'0'或'1',或者不是三位数字
4. 后缀不符规定:后缀不是四位数字
测试方案:
```
#include <iostream>
#include <string>
using namespace std;
bool isValidPhoneNumber(string phoneNumber) {
// 地区码不符规定
if (phoneNumber.substr(0, 3) == "" || !isdigit(phoneNumber[0]) || !isdigit(phoneNumber[1]) || !isdigit(phoneNumber[2])) {
return false;
}
// 前缀不符规定
for (int i = 3; i < 6; i++) {
if (phoneNumber[i] == '0' || phoneNumber[i] == '1' || !isdigit(phoneNumber[i])) {
return false;
}
}
// 后缀不符规定
for (int i = 6; i < 10; i++) {
if (!isdigit(phoneNumber[i])) {
return false;
}
}
return true;
}
int main() {
// 合法电话号码
string phone1 = "02188889999";
string phone2 = "01066668888";
string phone3 = "075512345678";
string phone4 = "10011112222";
// 地区码不符规定
string phone5 = "0";
string phone6 = "12345";
string phone7 = "A123";
// 前缀不符规定
string phone8 = "02108889999";
string phone9 = "01006668888";
string phone10 = "075501234567";
string phone11 = "021a889999";
// 后缀不符规定
string phone12 = "0218888999";
string phone13 = "0218888999999";
string phone14 = "02188889a9b";
// 测试合法电话号码
cout << isValidPhoneNumber(phone1) << endl; // true
cout << isValidPhoneNumber(phone2) << endl; // true
cout << isValidPhoneNumber(phone3) << endl; // true
cout << isValidPhoneNumber(phone4) << endl; // true
// 测试地区码不符规定
cout << isValidPhoneNumber(phone5) << endl; // false
cout << isValidPhoneNumber(phone6) << endl; // false
cout << isValidPhoneNumber(phone7) << endl; // false
// 测试前缀不符规定
cout << isValidPhoneNumber(phone8) << endl; // false
cout << isValidPhoneNumber(phone9) << endl; // false
cout << isValidPhoneNumber(phone10) << endl; // false
cout << isValidPhoneNumber(phone11) << endl; // false
// 测试后缀不符规定
cout << isValidPhoneNumber(phone12) << endl; // false
cout << isValidPhoneNumber(phone13) << endl; // false
cout << isValidPhoneNumber(phone14) << endl; // false
return 0;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)