关于一个oc写的命令行版的通讯录程序(代码贴过来无缩进,作为备忘,代码完成度:90%)

//

//  main.m

//  通讯录

//

//  Created by zw on 15/8/11.

//  Copyright (c) 2015 zw. All rights reserved.

//

//功能说明:

//add:添加联系人

//del:删除联系人

//change:修改联系人

//find:查找联系人

//sequ:对联系人进行排序

//remove:删除通讯录文件

//show:显示通讯录

//quit:退出程序

//注意:我的排序是专门做的一个功能选择,如果需要排的时候请进行sequ操作

//说明1:为了调试方便,我采用的是命令行的方式来选择操作,而没有用123456选项来进行操作

//说明2:以后做进一步改进时,对代码进行封装


#import <Foundation/Foundation.h>

#import "CZWMethod.h"


int main(int argc,const char * argv[]) {

    @autoreleasepool {

        NSFileManager* f=[NSFileManagerdefaultManager];

       NSFileHandle* file=nil;

        NSString* path=@"./person.plist";

       NSString* userName=nil;

       NSString* userName1=nil;

       NSString* userCompany=nil;

       NSString* userCompany1=nil;

       NSString* userPhone=nil;

       NSString* userPhone1=nil;

       NSString* userEmail=nil;

       NSString* userEmail1=nil;

       NSString* user=nil;

       NSString* user1=nil;

       NSString* filetmp=nil;

       NSString* list=nil;

       NSArray* listItems=nil;

       NSMutableArray* listItems1=nil;

        NSMutableData* buff=[[NSMutableDataalloc] init];

       char* op=malloc(10);

       char* name=malloc(10);

       char* company=malloc(20);

       char* phone=malloc(20);

       char* email=malloc(20);

       char* name1=malloc(10);

       char* company1=malloc(20);

       char* phone1=malloc(20);

       char* email1=malloc(20);

       memset(op, 0,10);

       memset(name, 0,10);

       memset(company, 0,20);

       memset(phone, 0,20);

       memset(email, 0,20);

       memset(name1, 0,10);

       memset(company1, 0,20);

       memset(phone1, 0,20);

       memset(email1, 0,20);

       char tmp='\0';

       int i=0;

       int j=0;

       NSLog(@"使用方法说明:");

        NSLog(@"add:添加联系人");

        NSLog(@"del:删除联系人");

        NSLog(@"change:修改联系人");

        NSLog(@"find:查找联系人");

        NSLog(@"sequ:对联系人进行排序");

        NSLog(@"remove:删除通讯录文件");

        NSLog(@"show:显示通讯录");

        NSLog(@"quit:退出程序");

        NSLog(@"注意:我的排序是专门做的一个功能选择,如果需要排的时候请进行sequ操作");

        NSLog(@"说明1:为了调试方便,我采用的是命令行的方式来选择操作,而没有用123456选项来进行操作!");

        NSLog(@"说明2:以后做进一步改进时,对代码进行封装!");

       while(1)

        {

           memset(op, 0,10);

            printf("请输入操作方式(adddelchangefindsequremoveshowquit):");

           scanf("%s",op);

            //用于接收一个多余的回车字符

           scanf("%c",&tmp);

           //添加联系人

           if(0==strcmp(op,"add"))

            {

               memset(name, 0,10);

               memset(company, 0,20);

               memset(phone, 0,20);

               memset(email, 0,20);

               printf("请输入姓名:");

               scanf("%s",name);

               printf("请输入公司:");

               scanf("%s",company);

               printf("请输入电话:");

               scanf("%s",phone);

               printf("请输入mail:");

               scanf("%s",email);

                userName=[NSStringstringWithCString:name encoding:NSUTF8StringEncoding];

                buff=[[NSMutableDataalloc] init];

               if(![f fileExistsAtPath:path])

                {

                    [fcreateFileAtPath:path contents:buff attributes:nil];

                }

                list=[NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];

                listItems=[listcomponentsSeparatedByString:@"@"];

                //类似下面for代码需要注意长度边界,从文件读出的最后一个元素是建议文件时写入的buff,它才是最后一个元素

               for(i=0;i<=listItems.count-1;i=i+4){

                   while(NSOrderedSame==[userNamecompare:listItems[i]])

                    {

                       memset(name, 0,10);

                       memset(company, 0,20);

                       memset(phone, 0,20);

                       memset(email, 0,20);

                       printf("此人已经存在,请重新输入姓名:");

                       scanf("%s",name);

                       printf("请输入公司:");

                       scanf("%s",company);

                       printf("请输入电话:");

                       scanf("%s",phone);

                       printf("请输入mail:");

                       scanf("%s",email);

                        userName=[NSStringstringWithCString:name encoding:NSUTF8StringEncoding];

                    }

                }

               if(i==listItems.count+3)

                {

                    buff=[NSMutableDatadataWithBytes:name length:strlen(name)];

                    [buffappendBytes:"@"length:strlen("@")];

                    [buffappendBytes:company length:strlen(company)];

                    [buffappendBytes:"@"length:strlen("@")];

                    [buffappendBytes:phone length:strlen(phone)];

                    [buffappendBytes:"@"length:strlen("@")];

                    [buffappendBytes:email length:strlen(email)];

                    [buffappendBytes:"@"length:strlen("@")];

                    file=[NSFileHandlefileHandleForWritingAtPath:path];

                   if(false==file)

                    {

                       NSLog(@"文件打开写失败!");

                    }else{

                        [fileseekToEndOfFile];

                        [filewriteData:buff];

                        [filecloseFile];

                       NSLog(@"添加联系人成功!");

                    }

                }

            }

           //删除联系人

           else if(0==strcmp(op,"del"))

            {

               if(![f fileExistsAtPath:path])

                {

                    NSLog(@"还没有通讯录,请先添加联系人以便建立通讯录!");

                }else{

                   memset(name, 0,10);

                   memset(company, 0,20);

                   memset(phone, 0,20);

                   memset(email, 0,20);

                   printf("请输入要删除的姓名:");

                   scanf("%s",name);

                    userName=[NSStringstringWithCString:name encoding:NSUTF8StringEncoding];

                   if([f fileExistsAtPath:path])

                    {

                        list=[NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

                        listItems=[listcomponentsSeparatedByString:@"@"];

                        //查找看是否存在此人,如果存在此人才进行删除操作

                       for(i=0;i<=listItems.count-1;i=i+4)

                        {

                           if(NSOrderedSame==[userNamecompare:listItems[i]])

                            {

                                user=[listItems[i]stringByAppendingFormat:@"@%@@%@@%@@",listItems[i+1],listItems[i+2],listItems[i+3]];

                                filetmp=[NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

                               NSString* deltmp=[filetmp stringByReplacingOccurrencesOfString:user withString:@""];

                               NSData* buff1=[deltmp dataUsingEncoding:NSUTF8StringEncoding];

                                [fremoveItemAtPath:path error:nil];

                                [fcreateFileAtPath:path contents:buff1 attributes:nil];

                               NSLog(@"删除联系人成功!");

                               break;

                            }

                           if(i==listItems.count-1 &&NSOrderedSame!=[userName compare:listItems[i]])

                            {

                               NSLog(@"对不起,没有找到你要删除的联系人!");

                            }

                        }

                    }else{

                       NSLog(@"没有通讯录文件,请先添加联系人创建通讯录!");

                    }

                }

            }

           //修改联系人

           else if(0==strcmp(op,"change"))

            {

               if(![f fileExistsAtPath:path])

                {

                    NSLog(@"还没有通讯录,请先添加联系人以便建立通讯录!");

                }else{

                   memset(name1, 0,10);

                   memset(company1, 0,20);

                   memset(phone1, 0,20);

                    memset(email1,0, 20);

                   memset(name, 0,10);

                   memset(company, 0,20);

                   memset(phone, 0,20);

                   memset(email, 0,20);

                   printf("请输入要修改的姓名:");

                   scanf("%s",name);

                    userName=[NSStringstringWithCString:name encoding:NSUTF8StringEncoding];

                    list=[NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];

                    listItems=[listcomponentsSeparatedByString:@"@"];

                   //查找通讯录看是否有这样一个联系人

                   for(j=0;j<=listItems.count-1;j=j+4)

                    {

                       if(NSOrderedSame==[userNamecompare:listItems[j]])

                        {

                           memset(name1, 0,10);

                           memset(company1, 0,20);

                           memset(phone1, 0,20);

                           memset(email1, 0,20);

                           printf("请输入修改后的姓名:");

                           scanf("%s",name1);

                           printf("请输入修改后的公司:");

                           scanf("%s",company1);

                           printf("请输入修改后的电话:");

                           scanf("%s",phone1);

                           printf("请输入修改后的mail:");

                           scanf("%s",email1);

                            

                            userName1=[NSStringstringWithCString:name1 encoding:NSUTF8StringEncoding];

                           //如果修改后的人有重名,就进行处理

                           for(i=0;i<=listItems.count-1;i=i+4){

                               while(NSOrderedSame==[userName1compare:listItems[i]])

                                {

                                   printf("此人已经存在,请重新输入姓名:");

                                   scanf("%s",name1);

                                   printf("请输入公司:");

                                   scanf("%s",company1);

                                   printf("请输入电话:");

                                   scanf("%s",phone1);

                                   printf("请输入mail:");

                                   scanf("%s",email1);

                                    userName1=[NSStringstringWithCString:name encoding:NSUTF8StringEncoding];

                                }

                            }

                           if(i==listItems.count+3)

                            {

                                user=[listItems[j]stringByAppendingFormat:@"@%@@%@@%@@",listItems[j+1],listItems[j+2],listItems[j+3]];

                                userName1=[NSStringstringWithCString:name1 encoding:NSUTF8StringEncoding];

                                userCompany1=[NSStringstringWithCString:company1 encoding:NSUTF8StringEncoding];

                                userPhone1=[NSStringstringWithCString:phone1 encoding:NSUTF8StringEncoding];

                                userEmail1=[NSStringstringWithCString:email1 encoding:NSUTF8StringEncoding];

                                user1=[userName1stringByAppendingFormat:@"@%@@%@@%@@",userCompany1,userPhone1,userEmail1];

                                filetmp=[NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];

                               NSString* usertmp=[filetmp stringByReplacingOccurrencesOfString:user withString:user1];

                               NSData* buff2=[usertmp dataUsingEncoding:NSUTF8StringEncoding];

                                [fremoveItemAtPath:path error:nil];

                                [fcreateFileAtPath:path contents:buff2 attributes:nil];

                               NSLog(@"联系人信息修改成功!");

                               break;

                            }

                        }

                       if(j==listItems.count-1 &&NSOrderedSame!=[userName compare:listItems[j]])

                        {

                           NSLog(@"对不起,没有找到你要修改的联系人!");

                        }

                    }

                }

            }

           //查找联系人并显示信息

           else if(0==strcmp(op,"find"))

            {

               if(![f fileExistsAtPath:path])

                {

                    NSLog(@"还没有通讯录,请先添加联系人以便建立通讯录!");

                }else{

                   memset(name, 0,10);

                   printf("请输入要查找的姓名:");

                   scanf("%s",name);

                    userName=[NSStringstringWithCString:name encoding:NSUTF8StringEncoding];

                    list=[NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];

                    listItems=[listcomponentsSeparatedByString:@"@"];

                   for(i=0;i<=listItems.count-1;i=i+4){

                       if(NSOrderedSame==[userNamecompare:listItems[i]])

                        {

                           NSLog(@"姓名:%@公司:%@电话:%@ mail:%@",listItems[i],listItems[i+1],listItems[i+2],listItems[i+3]);

                        }

                    }

                }

            }

           //删除通讯录

           else if(0==strcmp(op,"remove"))

            {

               if([f fileExistsAtPath:path])

                {

                    [fremoveItemAtPath:path error:nil];

                   NSLog(@"成功删除通讯录!");

                }else{

                   NSLog(@"通讯录还不存在,不需要删除通讯录!");

                }

            }

           //对联系人姓名进行排序

           else if(0==strcmp(op,"sequ"))

            {

                list=[NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];

                listItems=[listcomponentsSeparatedByString:@"@"];

                listItems1=[NSMutableArrayarrayWithArray:listItems];

               //对姓名进行排序

               for(i=0;i<listItems1.count-5;i=i+4)

                {

                   for(j=i;j<listItems1.count-5;j=j+4)

                    {

                       if(NSOrderedDescending==[listItems1[j]compare:listItems1[j+4]])

                        {

                            [listItems1exchangeObjectAtIndex:j withObjectAtIndex:j+4];

                            [listItems1exchangeObjectAtIndex:j+1withObjectAtIndex:j+5];

                            [listItems1exchangeObjectAtIndex:j+2withObjectAtIndex:j+6];

                            [listItems1exchangeObjectAtIndex:j+3withObjectAtIndex:j+7];

                        }

                    }

                }

               NSLog(@"排序成功!");

                //用数组进入文件后,它是自动生成相应的xml文件

                //[listItems1 writeToFile:path atomically:YES];

                

                buff=[[NSMutableDataalloc] init];

               for(i=0;i<=listItems1.count-2;i++)

                {

                    //[listItems1[i] cStringUsingEncoding:NSUTF8StringEncoding];

                    [buff appendBytes:[listItems1[i]cStringUsingEncoding:NSUTF8StringEncoding]length:strlen([listItems1[i]cStringUsingEncoding:NSUTF8StringEncoding])];

                    [buffappendBytes:@"@"length:strlen("@")];

                }

                

                [fremoveItemAtPath:path error:nil];

                [fcreateFileAtPath:path contents:buff attributes:nil];

            }

           //显示联系人

           else if(0==strcmp(op,"show"))

            {

               printf("通讯录显示如下:\n");

                

               if(![f fileExistsAtPath:path])

                {

                    NSLog(@"还没有通讯录,请先添加联系人以便建立通讯录!");

                }else{

                    list=[NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];

                   //NSLog(@"%@",list);

                    listItems=[listcomponentsSeparatedByString:@"@"];

                   for(i=0;i<listItems.count-1;i=i+4){

                       NSLog(@"姓名:%@公司:%@电话:%@ mail:%@",listItems[i],listItems[i+1],listItems[i+2],listItems[i+3]);

                    }

                    //NSLog(@"长度:%lu",listItems.count);

                }

            }

           //退出程序

           else if(0==strcmp(op,"quit"))

            {

               NSLog(@"程序退出!再见!");

               free(op);

               free(name);

               free(company);

               free(phone);

               free(email);

               free(name1);

               free(company1);

               free(phone1);

               free(email1);

               return 0;

            }

           //操作之外的情况

           else{

                printf("请从(adddelchangefindsequremoveshowquit)中选择你需要的操作!\n");

            }

        }

    }

   return 0;

}


以下是一个简单的通讯录列表的OC代码实现: ``` // Contact.h #import <Foundation/Foundation.h> @interface Contact : NSObject @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *phone; - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone; @end // Contact.m #import "Contact.h" @implementation Contact - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone { self = [super init]; if (self) { self.name = name; self.phone = phone; } return self; } @end // ContactCell.h #import <UIKit/UIKit.h> @interface ContactCell : UITableViewCell @property (nonatomic, strong) UILabel *nameLabel; @property (nonatomic, strong) UILabel *phoneLabel; @end // ContactCell.m #import "ContactCell.h" @implementation ContactCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 200, 20)]; [self.contentView addSubview:self.nameLabel]; self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 200, 20)]; [self.contentView addSubview:self.phoneLabel]; } return self; } @end // ContactListViewController.h #import <UIKit/UIKit.h> @interface ContactListViewController : UITableViewController @end // ContactListViewController.m #import "ContactListViewController.h" #import "Contact.h" #import "ContactCell.h" @interface ContactListViewController () @property (nonatomic, strong) NSMutableArray<Contact *> *contacts; @end @implementation ContactListViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"Contacts"; UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addContact)]; self.navigationItem.rightBarButtonItem = addButton; self.contacts = [NSMutableArray array]; Contact *contact1 = [[Contact alloc] initWithName:@"John Smith" phone:@"555-1234"]; [self.contacts addObject:contact1]; Contact *contact2 = [[Contact alloc] initWithName:@"Jane Doe" phone:@"555-5678"]; [self.contacts addObject:contact2]; [self.tableView registerClass:[ContactCell class] forCellReuseIdentifier:@"ContactCell"]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.contacts.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContactCell" forIndexPath:indexPath]; Contact *contact = self.contacts[indexPath.row]; cell.nameLabel.text = contact.name; cell.phoneLabel.text = contact.phone; return cell; } #pragma mark - Actions - (void)addContact { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Add Contact" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"Name"; }]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"Phone"; }]; UIAlertAction *addAction = [UIAlertAction actionWithTitle:@"Add" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *nameField = alert.textFields[0]; UITextField *phoneField = alert.textFields[1]; Contact *contact = [[Contact alloc] initWithName:nameField.text phone:phoneField.text]; [self.contacts addObject:contact]; [self.tableView reloadData]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:addAction]; [alert addAction:cancelAction]; [self presentViewController:alert animated:YES completion:nil]; } @end ``` 在这个实现中,我们创建了一个`Contact`类来表示每个联系人,一个`ContactCell`类来自定义通讯录列表中的单元格,一个`ContactListViewController`类来管理通讯录列表。我们使用`NSMutableArray`来存储所有的联系人,并在视图加载时向其中添加了一些示例联系人。 在`ContactListViewController`中,我们实现了`UITableViewDataSource`协议和`UITableViewDelegate`协议中的方法来显示通讯录列表和处理用户添加新联系人的操作。我们还添加了一个`UIBarButtonItem`,当用户点击它时会出现一个`UIAlertController`来允许用户输入新联系人的信息。最后,我们在`viewDidLoad`方法中注册了`ContactCell`类以供表视图使用。 这只是一个简单的例子,你可以根据自己的需要对其进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Zwarwolf

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值