服务器之家

服务器之家 > 正文

C语言使用顺序表实现电话本功能

时间:2021-06-20 16:23     来源/作者:WANSNIM

 简介:

用顺序表实现电话本的功能(C语言)

电话本具有如下4个功能:

1.创建一个电话本,电话本里面包含名字和电话号码
2.在指定位置插入一个名字和电话号码
3.在指定位置删除一个名字和电话号码
4.打印电话本

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//其中那个color函数是我为了美观加上去的,如果感觉不需要的话可以将代码中所有有关color的都删掉即可
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <windows.h>
using namespace std;
const int N = 1000+10;
int n;
struct Node
{
  char name[100];
  char number[20];
};
typedef struct
{
  struct Node* mylist;
  int len;
  int listsize;
}sqlist;
 
void Init(sqlist &s1);
void Creat(sqlist &s1);
void Delet(sqlist &s1);
void Add(sqlist &s1);
void Print(sqlist &s1);
void color(const unsigned short color1);
 
int main()
{
  sqlist s1;
  Init(s1);
  color(10);
  printf("\n\n\n\n    --------------- WSM's phonetxt-------------------\n\n");
  printf("       |You could chose these ops:      |\n");
  printf("       |  1.Creat the phonetxt        |\n");
  printf("       |  2.Delet the member in the phonetxt |\n");
  printf("       |  3.Add the member in the phonetxt  |\n");
  printf("       |  4.Print the phonetxt        |\n");
  color(14);
  printf("\n\n\n\n   Now,you can enter an optiton:");
 
  int op;
  while(scanf("%d",&op)!=EOF)
  {
    if(op==1) Creat(s1);
    else if(op==2) Delet(s1);
    else if(op==3) Add(s1);
    else if(op==4) Print(s1);
    else
    {
      color(4);
      printf("   You input is invalid,reinput please:)\n");
      color(14);
    }
    printf("\n   Now,you can enter an optiton:");
  }
  return 0;
}
 
void Init(sqlist &s1)
{
  s1.mylist = (Node *)malloc(100*sizeof(Node));
  s1.len = 0;
  s1.listsize = 100;
  return;
}
void Creat(sqlist &s1)
{
  s1.len = 0;
  cout<<"   how many numbers do you want to built:";
  scanf("%d",&n);
  cout<<"   please input their informations:"<<endl;
  for(int i=1;i<=n;i++)
  {
     printf("    input the %d person name:",i);
     scanf(" %s",s1.mylist[i-1].name);
     printf("    input the %d person phonenumber:",i);
     scanf(" %s",s1.mylist[i-1].number);
     s1.len++;
  }
  color(9);
  cout<<"   well done,the phonetxt has been created!!!"<<endl;
  color(14);
  return;
}
 
void Delet(sqlist &s1)
{
  cout<<"   please enter the number you want to delet:";
  heredelet:
  int x;
  scanf("%d",&x);
  if( x<1 || x>s1.len)
  {
    color(4);
    cout<<"   sorry,your input is invalid,please input again:";
    color(14);
  goto heredelet;
  }
 
  struct Node *p,*q;
  p = &(s1.mylist[x-1]);
  q = s1.mylist + s1.len -1;
  for(++p;p<=q;++p) *(p-1) = *p;
  --s1.len;
 
  color(9);
  cout<<"   well done,the member has been deleted!!!"<<endl;
  color(14);
  return;
}
void Add(sqlist &s1)
{
  cout<<"   please enter the number you want to add:";
  hereadd:
  int x;
  scanf("%d",&x);
  if( x<1 || x>s1.len+1)
  {
    color(4);
    cout<<"   sorry,your input is invalid,please input again:";
    color(14);
    goto hereadd;
  }
 
  struct Node cur;
  printf("    input the person name:");
  scanf(" %s",cur.name);
  printf("    input the person phonenumber:");
  scanf(" %s",cur.number);
 
  struct Node *p,*q;
  q = &(s1.mylist[x-1]);
  for(p=&(s1.mylist[s1.len-1]);p>=q;--p) *(p+1) = *p;
  *q = cur;
  ++s1.len;
 
  color(9);
  cout<<"   well done,the member has been added!!!"<<endl;
  color(14);
  return;
}
void Print(sqlist &s1)
{
  color(8);
  printf("    Name------phonenumber\n");
 
  struct Node *q = s1.mylist;
  for(q;q<s1.mylist+s1.len;q++)
  {
    printf("      %s    %s\n",q->name,q->number);
  }
  color(14);
  color(9);
  cout<<"   well done,the phonetxt is above!!!"<<endl;
  color(14);
  return;
}
 
void color(const unsigned short color1)
{   
  if(color1>=0&&color1<=15)
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color1);
  else
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  return;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/Codeblocksm/article/details/49031505

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部