A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© tr2e 中级黑马   /  2015-10-10 11:46  /  1180 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define NAMELEN 22
  4. #define TELLEN 12
  5. #define CONTECTNUM 100
  6. void writeFile();
  7. void init();
  8. void doAdd();
  9. void doShow();
  10. void doDelete();
  11. void doUpdata();
  12. void doSearch();
  13. //定义文件指针
  14. char *filePath = "telbook_tree.data";

  15. typedef struct{
  16.     char name[NAMELEN];
  17.     char telNum[TELLEN];
  18. }Person;
  19. //定义联系人结构体数组
  20. Person contacts[CONTECTNUM];
  21. //定义联系人数量
  22. int contactsnum = 0;

  23. int main(int argc, const char * argv[]) {
  24.    
  25.     //初始化通讯录
  26.     init();
  27.     printf("通讯录初始化完成!\n");
  28.     while (1) {
  29.         printf("**************************\n");
  30.         printf("****** 欢迎使用通讯录 ******\n");
  31.         printf("****** 1、添加联系人  ******\n");
  32.         printf("****** 2、删除联系人  ******\n");
  33.         printf("****** 3、修改联系人  ******\n");
  34.         printf("****** 4、查看所有联系人 ****\n");
  35.         printf("****** 5、搜索联系人  ******\n");
  36.         printf("****** 6、退出系统    ******\n");
  37.         printf("**************************\n\n");
  38.         int no = 0;
  39.         printf("请输入您要选择的操作序号:");
  40.         scanf("%d",&no);
  41.         if (no<1||no>6) {
  42.             printf("您的操作请求不合法");
  43.         }else{
  44.             switch (no) {
  45.                 case 1:
  46.                     doAdd();
  47.                     break;
  48.                 case 2:
  49.                     doDelete();
  50.                     break;
  51.                 case 3:
  52.                     doUpdata();
  53.                     break;
  54.                 case 4:
  55.                     doShow();
  56.                     break;
  57.                 case 5:
  58.                     doSearch();
  59.                     break;
  60.                 case 6:
  61.                     printf("系统正在退出...\n系统推出成功\n");
  62.                     return 0;
  63.                     break;
  64.                 default:
  65.                     break;
  66.             }
  67.         }
  68.         printf("\n");
  69.     }

  70.     return 0;
  71. }

  72. void init(){
  73.    
  74.     FILE *fp = fopen(filePath, "r");
  75.     if (fp!=NULL) {
  76.         //如果不为空 将文件中的数据更新到程序中
  77.         fread(&contactsnum, sizeof(contactsnum), 1, fp);
  78.         for (int i=0; i<contactsnum; i++) {
  79.         fread(&contacts[i], sizeof(Person), 1, fp);
  80.         }
  81.     }else{
  82.         //如果为空 新建文件 将人员个数写入
  83.         fp = fopen(filePath, "wb");
  84.         fwrite(&contactsnum, sizeof(contactsnum), 1, fp);
  85.         printf(" */文件初始化中/*\n");
  86.         printf("通讯录文件创建成功\n");
  87.     }
  88.     fclose(fp);
  89. }

  90. void writeFile(){
  91.     FILE *fp = fopen(filePath, "wb");
  92.     if (fp!=NULL) {
  93.         fwrite(&contactsnum, sizeof(contactsnum), 1, fp);
  94.         for (int i=0; i<contactsnum; i++) {
  95.             fwrite(&contacts[i], sizeof(Person), 1, fp);
  96.         }
  97.     }
  98.     fclose(fp);
  99. }

  100. void doAdd(){

  101.     printf("请输入联系人姓名(注意中间不可有空格):\n");
  102.     scanf("%s",contacts[contactsnum].name);
  103.     printf("请输入联系人电话(注意中间不可有空格):\n");
  104.     scanf("%s",contacts[contactsnum].telNum);
  105.    
  106.     printf("您确定要添加吗?1.确定 0.取消\n");
  107.     int flag;
  108.     scanf("%d",&flag);
  109.     if (flag) {
  110.         contactsnum++;
  111.         writeFile();
  112.         printf("文件更新成功\n");
  113.     }else{
  114.         printf("您的操作已经取消\n");
  115.     }
  116. }

  117. void doDelete(){
  118.     doShow();
  119.     printf("请输入删除的联系人的编号:\n");
  120.     int no;
  121.     scanf("%d",&no);
  122.     if (no == contactsnum) {
  123.         contactsnum--;
  124.     }else{
  125.         for (int i=no-1; i<contactsnum; i++) {
  126.             contacts[i]=contacts[i+1];
  127.         }
  128.         contactsnum--;
  129.     }
  130.     writeFile();
  131.     printf("联系人删除成功\n");
  132. }

  133. void doShow(){
  134.    
  135.     if (contactsnum == 0) {
  136.         printf("您的通讯录里暂时没有联系人,请自行维护。\n");
  137.     }else{
  138.         printf("序号\t 联系人\t\t 联系方式\t\n");
  139.         for (int i=0; i<contactsnum; i++) {
  140.             printf("%d\t %s\t\t %s\t\n",i+1,contacts[i].name,contacts[i].telNum);
  141.         }

  142.     }
  143.    
  144. }

  145. void doUpdata(){
  146.    
  147.     doShow();
  148.     printf("请输入您要修改的联系人序号:\n");
  149.     int no = 0;
  150.     scanf("%d",&no);
  151.     printf("请选择您要修改的项目:1.联系人 2.联系方式 3.全部修改 0.取消操作\n");
  152.     int fno = 0;
  153.     scanf("%d",&fno);
  154.     switch (fno) {
  155.         case 1:
  156.             printf("请输入新的联系人姓名:\n");
  157.             char name[NAMELEN];
  158.             scanf("%s",name);
  159.             strcpy(contacts[no-1].name,name);
  160.             break;
  161.         case 2:
  162.             printf("请输入新的联系方式:\n");
  163.             char tel[TELLEN];
  164.             scanf("%s",tel);
  165.             strcpy(contacts[no-1].telNum, tel);
  166.             break;
  167.         case 3:
  168.             printf("请输入新的联系人姓名:\n");
  169.             char name1[NAMELEN];
  170.             scanf("%s",name1);
  171.             strcpy(contacts[no-1].name,name1);
  172.             printf("请输入新的联系方式:\n");
  173.             char tel1[TELLEN];
  174.             scanf("%s",tel1);
  175.             strcpy(contacts[no-1].telNum, tel1);
  176.             break;
  177.         case 0:
  178.             printf("您的操作应经成功取消\n");
  179.             break;
  180.         default:
  181.             break;
  182.     }
  183.     writeFile();
  184.     printf("您的通讯录已经同步更新\n");
  185. }

  186. void doSearch(){
  187.    
  188.     printf("请选择您要搜索的方式:1.按姓名搜索 2.按号码搜索\n");
  189.     int flag = 0;
  190.     scanf("%d",&flag);
  191.     if (flag ==1 ) {
  192.         printf("请输入您要搜索人的姓名:\n");
  193.         char name[NAMELEN];
  194.         scanf("%s",name);
  195.         printf("您的搜索结果是:\n");
  196.         int i;
  197.         for (i=0; i<contactsnum; i++) {
  198.             if (strcmp(name,contacts[i].name)==0) {
  199.                 printf("序号\t 联系人\t\t 联系方式\t\n");
  200.                 printf("%d\t %s\t\t %s\t",i+1,contacts[i].name,contacts[i].telNum);
  201.                 break;
  202.             }
  203.             }
  204.         if (i == contactsnum ) {
  205.             printf("联系人不存在,请核对后查找\n");
  206.         }
  207.     }else if (flag == 2){
  208.         printf("请输入您要搜索的联系方式:\n");
  209.         char tel[TELLEN];
  210.         scanf("%s",tel);
  211.         printf("您的搜索结果是:\n");
  212.         int i;
  213.         for (i=0; i<contactsnum; i++) {
  214.             while (strcmp(contacts[i].telNum,tel) == 0) {
  215.                 printf("序号\t 联系人\t\t 联系方式\t\n");
  216.                 printf("%d\t %s\t\t %s\t",i+1,contacts[i].name,contacts[i].telNum);
  217.                 break;
  218.             }
  219.             }
  220.         if (i == contactsnum ) {
  221.             printf("联系人不存在,请核对后查找\n");
  222.         }
  223.         
  224.     }
  225. }
复制代码


10 个回复

倒序浏览
liyang783 来自手机 中级黑马 2015-10-10 12:38:58
沙发
看起来就很牛。。。。
回复 使用道具 举报
liyang783 发表于 2015-10-10 12:38
看起来就很牛。。。。

不牛的 就是几个简单的功能函数
回复 使用道具 举报
之前做过一个类似的学生图书管理系统。
回复 使用道具 举报
chingwei2011 发表于 2015-10-10 18:28
之前做过一个类似的学生图书管理系统。

感觉差不多 类似的管理类
回复 使用道具 举报
Car *p = [Car new]; 向计算机申请内存空间
                               给类中的每个成员初始化值
回复 使用道具 举报
tr2e 中级黑马 2015-10-11 22:35:17
7#
返回新申请的空间的首地址

其作用就是用car类实例化一个实例对象,对象的名称为p
回复 使用道具 举报
tr2e 中级黑马 2015-10-11 22:36:20
8#
类似于c语言的结构体指针
回复 使用道具 举报
tr2e 中级黑马 2015-10-12 09:00:38
9#
早安 各位
回复 使用道具 举报
tr2e 中级黑马 2015-10-14 21:54:24
10#

#import <Foundation/Foundation.h>

@interface Gun : NSObject
{
    int _bulletCount;
}
-(void)shoot;
-(void)setBulletCount:(int)bulletCount;
@end

@implementation Gun
-(void)shoot{
   
    if(_bulletCount>0){
        
        _bulletCount--;
        
        NSLog(@"\npa! 剩余子弹:%d",_bulletCount);
        
    }else{
        
        NSLog(@"没有子弹");
        
    }
}

-(void)setBulletCount:(int)bulletCount{
   
    if (bulletCount>0) {
        
        _bulletCount =  bulletCount;
        
    }else{
        
        _bulletCount = 0;
    }
   
   
}

@end


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Gun *gun= [Gun new];
  
        
        [gun setBulletCount:3];
        
        [gun shoot];
        [gun shoot];
        [gun shoot];
        [gun shoot];
        [gun shoot];
        
        
    }
    return 0;
}
回复 使用道具 举报
zanyige!wo gangkandao zhe
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马