zoukankan      html  css  js  c++  java
  • 删除重复字符

    需求:

    1、为可变字符串类添加一个检测是不是有重复字母的方法:例如:@“abccd”调用这个函数后变成@”abcd”;

    2、给学员定义一个“私有”方法:判断学员是不是有时间。定义一个协议:服务员和学员的协议,服务员给学员送餐。如果学员忙,就让服务员送餐。


    1:

    NSMutableString+RemoveRep.h

    #import <Foundation/Foundation.h>
    
    @interface NSMutableString (RemoveRep)
    -(void)removeRepeat:(NSString *)aNum;
    @end

    NSMutableString+RemoveRep.m

    #import "NSMutableString+RemoveRep.h"
    
    @implementation NSMutableString (RemoveRep)
    -(void)removeRepeat:(NSString *)aNum
    {
        NSMutableArray *mArr = [[NSMutableArray alloc]initWithCapacity:10];
        for (int i = 0;i < aNum.length;i++)
        {
            [mArr addObject:[aNum substringWithRange:NSMakeRange(i,1)]];
        }
        [self compareNum:mArr];
        NSLog(@"%@",mArr);
        
    }
    //比较是否相等
    -(NSMutableArray *)compareNum:(NSMutableArray *)mArr//abcbcbcbda
    {
        int count = mArr.count;//重新定义了,count不会减一
    /*错误,因为冒泡排序只比较两个相邻的数
        for (int j = 0; j < count - 1; j++)
        {
            for (int i = 0; i < count - 1 - j; i++)
            {
                NSString *a = [mArr objectAtIndex:i];
                NSString *b = [mArr objectAtIndex:i+1];
                
                if ([a isEqualToString:b])
                {
                    NSLog(@"repeat = %@",[mArr objectAtIndex:i]);
                   [mArr removeObjectAtIndex:i];
                    count--;//数组长度发生变化
                }
            }
        }
    */
        for (int j = 0; j < count - 1; j++)//百钱买百鸡式的冒泡遍历
        {
            for (int i = j; i < count - 1; i++)
            {
                NSString *a = [mArr objectAtIndex:j];
                NSString *b = [mArr objectAtIndex:i+1];
                if ([a isEqualToString:b])
                {
                    NSLog(@"repeat = %@",[mArr objectAtIndex:i+1]);
                    [mArr removeObjectAtIndex:i+1];
                    count--;//数组长度发生变化
                    i--;//没有这步.如果abcccd的情况就会跳过一个c
                }
            }
        }
    
        return mArr;
    }
    
    @end

    2:

    Student.h

    #import <Foundation/Foundation.h>
    @protocol OrderDelegate;
    
    @interface Student : NSObject
    
    @property(nonatomic,assign)id<OrderDelegate> delegate;
    
    -(void)orderMeal;
    @end
    
    
    
    
    @protocol OrderDelegate <NSObject>
    @required
    -(void)order:(NSString *)aName
       withMoney:(int)aMoney;
    @end

    Student.m

    #import "Student.h"
    
    @implementation Student
    @synthesize delegate = _delegate;
    
    -(void)orderMeal
    {
        if ([self.delegate conformsToProtocol:@protocol(OrderDelegate)])
        {
            [self.delegate order:@"腊味" withMoney:10];
        }
    }
    
    
    @end

     Waiter.h

    #import <Foundation/Foundation.h>
    
    #import "Student.h"
    @interface Waiter : NSObject <OrderDelegate>
    
    @end

    Waiter.m

    #import "Waiter.h"
    
    
    @implementation Waiter
    
    -(void)order:(NSString *)aName withMoney:(int)aMoney
    {
        NSLog(@"买到了%d元的%@",aMoney,aName);
    }
    
    @end

    AppDelegate.m

    Waiter *w = [[Waiter alloc]init];
        
        Student *s = [[Student alloc]init];
        
        s.delegate = w;
        [s orderMeal];
  • 相关阅读:
    菜单项向子页面传递参数
    Grid中添加链接,打开选项卡页面
    FineUI与百度地图简单示例 (转帖)
    AppBox中main树节点单击事件JS(还有叶子的节点的页面链接)
    FineUI中在一个页面中通过控件事件(JS)向父页面中添加Tab页
    如何使用button在tab中新建打开一个链接页
    系统service
    官员详解官场对领导称谓讲究:叫大不叫小
    搞笑对话
    陆琪:男人生存的意义,就是赚钱养老婆
  • 原文地址:https://www.cnblogs.com/huen/p/3544975.html
Copyright © 2011-2022 走看看