当前位置:网站首页>C language: realize the simple function of address book through structure
C language: realize the simple function of address book through structure
2022-07-28 05:17:00 【@Bu Xiangwan spicy】
Catalog
explain :
Because it is a simple version , All written is browsing 、 add to 、 Query function
Now look at the code :
1. The overall code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Node
{
int id;
char* name;
char* Tel;
struct Node* pNext;
}List;
typedef struct PAGE
{
int totalInfo;
int totalpage;
int nowpage;
int onepageInfo;
}Page;
int g_MenuType;
int g_key;
List* GetNode();
void AddNode(List** pphead, List** ppend, List* pNode);
int GetId();
char* GetName();
char* GetTel();
void InitInfo(List** pphead, List** ppend, int n);
Page* GetPage(List* phead, int n);
void ShowInfo(List* phead, Page* pPage);
void ShowMenu(Page* pPage);
void TurnPage(List* phead, Page* pPage);
void Browse(List* phead);
List* GetInNode();
char* Getstring();
void Search(List* phead);
int main()
{
List* phead = NULL;
List* pend = NULL;
InitInfo(&phead, &pend, 101);
while (1)
{
printf("*************************\n");
printf("*******1. Browse information ********\n");
printf("*******2. Add information ********\n");
printf("*******3. Query information ********\n");
printf("*********q. sign out **********\n");
printf("*************************\n");
char ch;
scanf_s(" %c", &ch);
switch (ch)
{
case '1':
g_MenuType = 1;
Browse(phead);
break;
case '2':
AddNode(&phead, &pend, GetInNode());
break;
case '3':
g_MenuType = 3;
Search(phead);
break;
case 'q':
while (phead != NULL)
{
List* pDel = phead;
phead = phead->pNext;
free(pDel);
pDel = NULL;
}
return 0;
break;
default:
break;
}
}
return 0;
}
List* GetNode()
{
List* ptemp = (List*)malloc(sizeof(List));
ptemp->id = GetId();
ptemp->name = GetName();
ptemp->Tel = GetTel();
ptemp->pNext = NULL;
return ptemp;
}
void AddNode(List** pphead, List** ppend, List* pNode)
{
if (*pphead == NULL)
{
*pphead = pNode;
}
else
{
(*ppend)->pNext = pNode;
}
*ppend = pNode;
}
int GetId()
{
static int Id = 0;
Id++;
return Id;
}
char* GetName()
{
char* str = (char*)malloc(sizeof(char) * 6);
for (int i = 0; i < 5; i++)
{
str[i] = rand() % 26 + 'a';
}
str[5] = '\0';
return str;
}
char* GetTel()
{
char* str = (char*)malloc(sizeof(char) * 12);
switch (rand() % 4)
{
case 0:
strcpy_s(str, 12, "133");
break;
case 1:
strcpy_s(str, 12, "155");
break;
case 2:
strcpy_s(str, 12, "177");
break;
case 3:
strcpy_s(str, 12, "188");
break;
}
for (int i = 3; i < 11; i++)
{
str[i] = rand() % 10 + '0';
}
str[11] = '\0';
return str;
}
void InitInfo(List** pphead, List** ppend, int n)
{
srand((unsigned int)time(NULL));
for (int i = 0; i < n; i++)
{
AddNode(pphead, ppend, GetNode());
}
}
Page* GetPage(List* phead, int n)
{
Page* pPage = (Page*)malloc(sizeof(Page));
pPage->nowpage = 0;
pPage->onepageInfo = n;
pPage->totalInfo = 0;
while (phead != NULL)
{
pPage->totalInfo++;
phead = phead->pNext;
}
pPage->totalpage = (pPage->totalInfo % pPage->onepageInfo == 0) ? (pPage->totalInfo / pPage->onepageInfo) : (pPage->totalInfo / pPage->onepageInfo + 1);
return pPage;
}
void ShowInfo(List* phead, Page* pPage)
{
int begin = (pPage->nowpage - 1) * pPage->onepageInfo + 1;
int end = pPage->nowpage * pPage->onepageInfo;
int count = 0;
while (phead != NULL)
{
count++;
if (count >= begin && count <= end)
{
printf("%d %s %s\n", phead->id, phead->name, phead->Tel);
}
phead = phead->pNext;
}
}
void ShowMenu(Page* pPage)
{
switch (g_MenuType)
{
case 1:
printf(" Current %d page common %d page common %d strip w The previous page s The next page b return \n", pPage->nowpage, pPage->totalpage, pPage->totalInfo);
break;
case 3:
printf(" Current %d page common %d page common %d strip w The previous page s The next page c Reexamine b return \n", pPage->nowpage, pPage->totalpage, pPage->totalInfo);
break;
}
}
void TurnPage(List* phead, Page* pPage)
{
char ch = 's';
while (1)
{
switch (ch)
{
case 'w':
if (pPage->nowpage > 1)
{
pPage->nowpage--;
ShowInfo(phead, pPage);
ShowMenu(pPage);
}
else
{
printf(" push the button wrong \n");
}
break;
case 's':
if (pPage->nowpage < pPage->totalpage)
{
pPage->nowpage++;
ShowInfo(phead, pPage);
ShowMenu(pPage);
}
else
{
printf(" It's the last page \n");
}
break;
case 'b':
return;
break;
case'c':
return;
break;
default:
printf(" push the button wrong \n");
break;
}
scanf_s(" %c", &ch);
g_key = ch;
}
}
void Browse(List* phead)
{
Page* pPage = GetPage(phead, 10);
TurnPage(phead, pPage);
free(pPage);
pPage = NULL;
}
List* GetInNode()
{
List* ptemp = (List*)malloc(sizeof(List));
ptemp->id = GetId();
printf(" Please enter a name :\n");
ptemp->name = Getstring();
printf(" Please input the phone number :\n");
ptemp->Tel = Getstring();
ptemp->pNext = NULL;
return ptemp;
}
char* Getstring()
{
char ch;
int size = 5;
char* str = (char*)malloc(sizeof(char) * size);
int count = 0;
scanf_s(" ");
while ((ch = getchar()) != '\n')
{
str[count] = ch;
count++;
if (count == size)
{
size += 5;
str = (char*)realloc(str, size);
}
}
str[count] = '\0';
return str;
}
void Search(List* phead)
{
List* pMark = phead;
char* str = NULL;
while (1)
{
while (1)
{
char ch;
printf(" Please enter the keyword to query :\n");
str = Getstring();
printf("a confirm Re input with other keys \n");
scanf_s(" %c", &ch);
if ('a' == ch)
{
break;
}
else
{
free(str);
str = NULL;
}
}
List* pNewhead = NULL;
List* pNewend = NULL;
phead = pMark;
while (phead != NULL)
{
if (0 == strncmp(str, phead->name, strlen(str)) || 0 == strncmp(str, phead->Tel, strlen(str)))
{
List* ptemp = (List*)malloc(sizeof(List));
ptemp->id = phead->id;
ptemp->name = phead->name;
ptemp->Tel = phead->Tel;
ptemp->pNext = NULL;
AddNode(&pNewhead, &pNewend, ptemp);
}
phead = phead->pNext;
}
Browse(pNewhead);
while (pNewhead != NULL)
{
List* pDel = pNewhead;
pNewhead = pNewhead->pNext;
free(pDel);
pDel = NULL;
}
if (g_key == 'b')
{
break;
}
}
}2. Output :
1. Browse
*************************
*******1. Browse information ********
*******2. Add information ********
*******3. Query information ********
*********q. sign out **********
*************************
1
1 opmpf 17797220133
2 dvjok 13344710346
3 bxneu 13354415147
4 knjif 17790373600
5 jawdp 17744088807
6 bsmjz 17728639233
7 ztezw 13328052515
8 jvgfv 13361452775
9 rixhc 15557603951
10 cqvlh 17700113115
Current 1 page common 11 page common 101 strip w The previous page s The next page b return
s
11 rmett 15503677781
12 myeho 15587556986
13 rkxei 15587962319
14 qrxyf 13311952996
15 fhrjz 17711806034
16 drggf 13392814168
17 qecdb 15503155656
18 cosoy 15542326612
19 swkdy 17750406137
20 kajea 15556496035
Current 2 page common 11 page common 101 strip w The previous page s The next page b return
s
21 copen 13378254750
22 cvuui 18860714929
23 efptc 18825769956
24 hsmwg 18869656400
25 frfau 17716864917
26 eeomk 13328198774
27 zxqgn 18878940866
28 laemi 15577497701
29 idvgd 15587890605
30 bnkoy 13326914152
Current 3 page common 11 page common 101 strip w The previous page s The next page b return
s
31 dnhck 15591856722
32 dpyit 18802587860
33 khtsz 18897784982
34 yhovv 15557495649
35 hbmjh 18859387097
36 yfffb 15548465181
37 mnjly 18882744750
38 fgcpd 15507628678
39 yszpu 18857278249
40 mhgaf 15581874319
Current 4 page common 11 page common 101 strip w The previous page s The next page b return
s
41 jtari 18825559712
42 yjvnb 13374832949
43 xqppv 13342790374
44 orjrx 18815649518
45 beklp 15559712316
46 kptdo 15547256874
47 ifhvv 13373189979
48 qkglx 18875110965
49 afiou 15512067774
50 ounbf 15582949126
Current 5 page common 11 page common 101 strip w The previous page s The next page b return
s
51 oossp 17758127698
52 ihfjj 18828286418
53 etmih 18846803630
54 dxgxv 13375080185
55 bxsxa 13388524622
56 jajzk 13312764710
57 blxwe 13316686582
58 nmsrv 13394597321
59 zsnjm 18899786428
60 uvfto 13336775584
Current 6 page common 11 page common 101 strip w The previous page s The next page b return
s
61 caxbi 13317231826
62 kwoow 13345100893
63 dlcgk 13394706062
64 siazl 18811023518
65 qoodw 18860414224
66 inmpy 18803771441
67 xiifk 13302806218
68 nrqyi 13341863966
69 ozpus 17799557231
70 qbvmr 13353726938
Current 7 page common 11 page common 101 strip w The previous page s The next page b return
s
71 nfpjp 15528283711
72 wdnjd 18806281238
73 kubdi 13324064720
74 klapk 18847620591
75 ccvel 15531087413
76 vgsnl 17793103646
77 pmmge 13395160772
78 nctmo 18879182659
79 xckji 13363408824
80 wznpb 17738521351
Current 8 page common 11 page common 101 strip w The previous page s The next page b return
s
81 fzzhn 13345400450
82 xwmwq 13309305259
83 uqbgp 13390114230
84 mbyvs 17787166711
85 rvjav 15599245550
86 pupld 15555167818
87 hlqzn 17775458503
88 dsiee 15506746737
89 pukic 13345736580
90 tcaeo 15548805679
Current 9 page common 11 page common 101 strip w The previous page s The next page b return
s
91 uyans 17710023173
92 umvhg 18879845919
93 hyehz 15584499084
94 xetoe 18810085383
95 gazeq 15582357856
96 fdqvj 17725115365
97 wxbjj 15585080558
98 ansbz 15580073612
99 bvccs 18862798471
100 llzly 18883117266
Current 10 page common 11 page common 101 strip w The previous page s The next page b return
s
101 ngdkx 18833971326
Current 11 page common 11 page common 101 strip w The previous page s The next page b return
s
It's the last page 2. add to
2
Please enter a name :
gg
Please input the phone number :
123456789
101 ngdkx 18833971326
102 gg 123456789
Current 11 page common 11 page common 102 strip w The previous page s The next page b return
3. lookup
3
Please enter the keyword to query :
133
a confirm Re input with other keys
a
2 dvjok 13344710346
3 bxneu 13354415147
7 ztezw 13328052515
8 jvgfv 13361452775
14 qrxyf 13311952996
16 drggf 13392814168
21 copen 13378254750
26 eeomk 13328198774
30 bnkoy 13326914152
42 yjvnb 13374832949
Current 1 page common 4 page common 31 strip w The previous page s The next page c Reexamine b return
边栏推荐
- 11.< tag-动态规划和子序列, 子数组>lt.115. 不同的子序列 + lt. 583. 两个字符串的删除操作 dbc
- MySQL(5)
- From the basic concept of micro services to core components - explain and analyze through an example
- Table image extraction based on traditional intersection method and Tesseract OCR
- Interpretation of afnetworking4.0 request principle
- HDU 2874 connections between cities
- MySQL(5)
- Implementation of simple upload function in PHP development
- Microservice failure mode and building elastic system
- Mysql基本查询
猜你喜欢

Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)

Summary and review of puppeter

Struct模块到底有多实用?一个知识点立马学习

这种动态规划你见过吗——状态机动态规划之股票问题(中)
![[computer level 3 information security] overview of information security assurance](/img/f0/a72e61fda58ea93ca4e9db7274f6e3.png)
[computer level 3 information security] overview of information security assurance

RT_ Use of thread message queue

FreeRTOS startup process, coding style and debugging method

【计算机三级信息安全】信息安全保障概述
![[high CPU consumption] software_ reporter_ tool.exe](/img/3f/2c1ecff0a81ead0448e1215567ede7.png)
[high CPU consumption] software_ reporter_ tool.exe

Flink mind map
随机推荐
【ARXIV2203】Efficient Long-Range Attention Network for Image Super-resolution
数据安全逐步落地,必须紧盯泄露源头
The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies
Paper reading notes -- crop yield prediction using deep neural networks
Offline loading of wkwebview and problems encountered
Transformer -- Analysis and application of attention model
【ARXIV2205】EdgeViTs: Competing Light-weight CNNs on Mobile Devices with Vision Transformers
Configuration experiment of building virtual private network based on MPLS
【CVPR2022】Multi-Scale High-Resolution Vision Transformer for Semantic Segmentation
App test process and test points
HDU 3585 maximum shortest distance
Tips for using swiper (1)
Evolution of ape counseling technology: helping teaching and learning conceive future schools
Anaconda common instructions
Service object creation and use
Message forwarding mechanism -- save your program from crashing
Making RPM packages with nfpm
Pipe /createpipe
从微服务基本概念到核心组件-通过一个实例来讲解和分析
FreeRTOS personal notes - task notification