C语言做一个通讯录程序(在console里面运行)

最近复习C语言的时候看到网上有个C语言通讯录的小项目,于是看了下那个程序实现的大概的功能,然后自己也跟着做了个。代码还算简洁,贴上来给有需要的人。

  1 //
  2 //  main.m
  3 //  AdressBook
  4 //
  5 //  Created by bestkayle on 15/8/8.
  6 //  Copyright (c) 2015年 bestkayle. All rights reserved.
  7 //
  8 
  9 #import <Foundation/Foundation.h>
 10 int amount = 1;//联系人数
 11 int num;//输入的指令
 12 int xuhao;//联系人编号
 13 typedef struct Contact {
 14     char name[20];
 15     int teleNumber;
 16 }Co;
 17 Co person[20] = {
 18     {"kayle",12345678},
 19 };
 20 void addContracts(){
 21 //    for (int i = 0; i < 20; i ++) {
 22 //        for (int j = i; j < amount; j++) {
 23 //            person[j+1] = person[j];
 24 //        }
 25 //    }
 26 }
 27 void deleteContracts(xuhao){
 28     for (int i = xuhao-1; i < amount; i++) {
 29         person[i] = person[i+1];
 30     }
 31 }
 32 void fixContracts(xuhao){
 33     
 34 }
 35 void displayAll(){
 36     for (int i = 0; i < amount; i ++) {
 37         printf("%d.%s %d\n",i+1,person[i].name,person[i].teleNumber);
 38     }
 39 }
 40 void searchOne(xuhao){
 41     printf("%d.%s%d\n",xuhao,person[xuhao-1].name,person[xuhao-1].teleNumber);
 42 }
 43 int main(int argc, const char * argv[]) {
 44     printf("**********************************\n");
 45     printf("****** 欢迎使用通讯录    ************\n");
 46     printf("****** 1. 添加联系人    ************\n");
 47     printf("****** 2. 删除联系人    ************\n");
 48     printf("****** 3. 修改联系人    ************\n");
 49     printf("****** 4. 查看所有联系人 ************\n");
 50     printf("****** 5. 搜索联系人    *************\n");
 51     printf("****** 6. 退出通讯录    *************\n");
 52     printf("***********************************\n");
 53     printf("请按提示进行操作:\n");
 54     while (num != 6){
 55     scanf("%d",&num);
 56     switch (num) {
 57         case 1:
 58             amount ++;
 59           //  addContracts();
 60             printf("请输入姓名和电话号码:");
 61             char name[20] = {0};
 62             int telenumber;
 63             scanf("%s%d",&*name,&telenumber);
 64             strcpy(person[amount-1].name,name);
 65             person[amount-1].teleNumber = telenumber;
 66             printf("已存入\n");
 67             break;
 68         case 2:
 69             amount --;
 70             printf("请输入序号:");
 71             scanf("%d",&xuhao);
 72             deleteContracts(xuhao);
 73             break;
 74         case 3:
 75             printf("请输入序号:");
 76             scanf("%d",&xuhao);
 77             printf("修改联系人请输入1,电话号码请输入2:");
 78             int choose;
 79             scanf("%d",&choose);
 80             if (choose == 1) {
 81                 char name[20] = {0};
 82                 printf("请输入姓名");
 83                 scanf("%s",name);
 84                 strcpy(person[xuhao-1].name, name);
 85             }
 86             else{
 87                 int telenumber;
 88                 printf("请输入电话号码");
 89                 scanf("%d",&telenumber);
 90                 person[xuhao-1].teleNumber = telenumber;
 91             }
 92             break;
 93         case 4:
 94             displayAll();
 95             break;
 96         case 5:
 97             printf("请输入序号:");
 98             scanf("%d",&xuhao);
 99             searchOne(xuhao);
100         default:
101         break;}
102         if (num == 6) {
103             printf("正在退出...\n");
104             sleep(1);
105             printf("已退出!");
106         }
107             
108     }
109 }

 

posted @ 2015-08-12 15:19  bestkayle  阅读(386)  评论(0编辑  收藏  举报