@Asuna
2016-10-27T12:42:21.000000Z
字数 2468
阅读 768
题意:输入父亲和母亲的身高,点击相应button后输出儿子或者女儿的期望身高。
实现方法:几个textfield和两个button,button中处理算期望升高(课堂都具体实现过类似的功能,应该不难吧。。)
部分代码:
//儿子的期望身高
- (IBAction)clickSonButton:(id)sender {
double fatherHeight = self.fatherHeightTextField.text.doubleValue;
double motherHeight = self.motherHeightTextField.text.doubleValue;
double sonHeight = (fatherHeight + motherHeight) * 1.08 / 2;
NSNumber *number = [NSNumber numberWithDouble:sonHeight];
NSString *sonHeightString = [number stringValue];
self.sonHeightTextField.text = sonHeightString;
}
//女儿的期望身高
- (IBAction)clickDaughterButton:(id)sender {
double fatherHeight = self.fatherHeightTextField.text.doubleValue;
double motherHeight = self.motherHeightTextField.text.doubleValue;
double daughterHeight = (fatherHeight * 0.923 + motherHeight) / 2;
NSNumber *number = [NSNumber numberWithDouble:daughterHeight];
NSString *daughterHeightString = [number stringValue];
self.daughterHeightTextField.text = daughterHeightString;
}
题意:给定一个字符串,按他的奇偶位分别存入两个子串中,最后将两个子串拼接在一起实现加密。
实现方法:用一个i从字符串第一位开始向后推,用一个j从字符串一半的位置开始向后推,每次各取i和j这一位的分别加入密串中即可完成加密,解密的话同上操作(逆向而已)。难点在判断长度奇偶这种细枝末节上,暴力实现没什么困难。
部分代码:
//加密(分奇偶)
- (IBAction)addCipherButton:(id)sender {
NSString *text = self.informationTextField.text;
NSMutableString *ans1 = [[NSMutableString alloc]init];
NSMutableString *ans2 = [[NSMutableString alloc]init];
NSUInteger l = text.length;
for (int i = 0; i < l; i++){
char c = [text characterAtIndex:i];
if (i%2==0) [ans1 appendFormat:@"%c",c];
if (i%2!=0) [ans2 appendFormat:@"%c",c];
}
[ans1 appendFormat:@"%@",ans2];
self.addCipherTextField.text = ans1;
}
//解密(分奇偶操作)
- (IBAction)reduceCipherButton:(id)sender {
NSString *text = self.addCipherTextField.text;
NSMutableString *ans = [[NSMutableString alloc]init];
NSUInteger l = text.length;
NSUInteger l1;
if (l % 2!=0){
l1 = text.length/2 + 1;
for (int i = 0; i <= l / 2; i++){
char c = [text characterAtIndex:i];
[ans appendFormat:@"%c",c];
if (l1 < l){
//NSLog(@"%d",l1);
char c1 = [text characterAtIndex:l1];
[ans appendFormat:@"%c",c1];
l1++;
}
}
}
else{
l1 = text.length/2;
for (int i = 0; i <= l / 2; i++){
if (l1 < l){
char c = [text characterAtIndex:i];
[ans appendFormat:@"%c",c];
}
if (l1 < l){
//NSLog(@"%d",l1);
char c1 = [text characterAtIndex:l1];
[ans appendFormat:@"%c",c1];
l1++;
}
}
}
self.reduceCipherTextField.text = ans;
}
题意:大标题都给了。。。
实现方法:用三个Uislider分别代表红蓝绿三种颜色,首先要在.h文件中提到他们,然后在m文件中用一个.view.backgroundColor就可以进行设置了,还有一个重点就是m中定义的uibutton不是直接拉进去的,而是要对应三个uislider。就是uibutton前面那个小点要拉三次,分别拉到三个uislider上。
部分代码:
- (IBAction)setViewBackgroundColor:(id)sender {
self.view.backgroundColor = [UIColor colorWithRed:redSlider.value green:greenSlider.value blue:blueSlider.value alpha:1];
}