等价类划分的应用
-
EditBox允许1到6个英文字符或数字,按OK结束
-
有效等价类:
长度:1到6 字符:a-z,A-Z,0-9 -
无效等价类: 长度:0,7 字符:英文/数字以外字符,控制字符,标点符号
-
|编号|输入|输出|
|-|-|-|
|1|abcdef|符合要求|
|2|abc123|符合要求|
|3|354301|符合要求|
|4||不符合要求|
|5|akdfnafks|不符合要求|
|6|,.,.*&|不符合要求|
使用 Xcode 编写的 Objective-C 程序
//
// ViewController.m
// testApp
//
// Created by trigger on 15/3/22.
// Copyright (c) 2015年 trigger. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITextField *textBoxText;
- (IBAction)buttonPress:(id)sender;
- (BOOL)isValid:(NSString*)str;
@end
@implementation ViewController
@synthesize textBoxText;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonPress:(id)sender {
BOOL validity;
validity = [self isValid:textBoxText.text];
NSString *message;
if (validity == true){
message = [NSString stringWithFormat:@"输入信息符合要求!"];
}
else{
message = [NSString stringWithFormat:@"输入信息不符合要求!"];
}
[[[UIAlertView alloc]initWithTitle:@"等价类检查" message:message delegate: nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil]show];
}
- (BOOL)isValid:(NSString*)str{
if (str.length == 0 || str.length >= 7) {
return false;
}
char * cha = [str cStringUsingEncoding:NSASCIIStringEncoding];
for (int i = 0; i < str.length; i++) {
if ((cha[i] >= 'a' && cha[i] <='z') || (cha[i] >= 'A' || cha[i] <= 'Z') || (cha[i] >= '0' || cha[i] <= '9'));
else{
return false;
}
}
return true;
}
@end