C语言综合作业:遍一个通讯录管理程序。通讯录应包含以下内容:序号,姓名,电话,地址等内容。具体要求如
1.显示所有已录入的通讯录。
2.实现新纪录的录入。
3.实现删除任何一条记录。
4.实现修改任意一条纪录。
5.可以根据姓名,电话或地址查询等相关信息。
6.系统的推出。
追问:没有吧删除记录的时候出了点问题,再改下。解决了马上采纳其他的要求都满足,不过在删除记录的时候,就终止运行了我电脑里没装C++。今下午上机课后给你回复删除记录的时候出问题了,一删除就程序终止。PS:我要是能改就不会到百度上来找人帮忙了应该变为2哦根据序号就行了啥数据结构哦,这是我们老师布置的C语言综合作业,你看我的昵称嘛我们连数据结构学都没学,话说C语言也挺悲剧的,下一届我们学院这课程就取消了550558136@qq.com 通讯录中没有序号,可不可以改下呢
答案:对数据的存储结构有要求吗?
#include <stdio.h>
#include <string.h>
const int MAX_NAME = 99;
const int MAX_PHONE = 27;
const int MAX_ADDR = 127;
typedef struct _PersonInfo
{
char name[MAX_NAME + 1];
char phone[MAX_PHONE + 1];
char addr[MAX_ADDR + 1];
}PersonInfo ;
const char *ID = "编号:";
const char *NAME = "姓名:";
const char *PHONE = "电话:";
const char *ADDR = "地址:";
const int MAX_NUM = 512;
PersonInfo g_piArry[MAX_NUM];
static int read_line ( FILE *fp, char *line, int max_length )
{
int i;
char ch;
/* initialize index to string character */
i = 0;
/* read to end of line, filling in characters in string up to its
maximum length, and ignoring the rest, if any */
for(;;)
{
/* read next character */
ch = fgetc(fp);
/* check for end of file error */
if ( ch == EOF )
return -1;
/* check for end of line */
if ( ch == '\n' )
{
/* terminate string and return */
line[i] = '\0';
return 0;
}
/* fill character in string if it is not already full*/
if ( i < max_length )
line[i++] = ch;
}
/* the program should never reach here */
return -1;
}
void menu_add();
void menu_print();
void menu_delete();
void menu_modify();
void menu_detail();
/* codes for menu */
#define PRINT_CODE 1
#define ADD_CODE 2
#define DEL_CODE 3
#define MOD_CODE 4
#define DETAIL_CODE 5
#define EXIT_CODE 6
int main(void)
{
memset(g_piArry, 0, sizeof(g_piArry));
for(;;)
{
int choice, result;
char line[301];
// FILE *fp1=fopen("input2","r");
/* print menu to standard error */
fprintf ( stderr, "\nOptions:\n" );
fprintf ( stderr, "%d: 显示所有已录入的通讯录\n", PRINT_CODE );
fprintf ( stderr, "%d: 录入新纪录\n", ADD_CODE );
fprintf ( stderr, "%d: 删除一条记录\n", DEL_CODE );
fprintf ( stderr, "%d: 修改一条纪录\n", MOD_CODE );
fprintf ( stderr, "%d: 根据姓名,电话或地址查询相关信息\n", DETAIL_CODE );
fprintf ( stderr, "%d: 退出系统\n", EXIT_CODE );
fprintf ( stderr, "\nEnter option: " );
if ( read_line ( stdin, line, 300 ) != 0 ) continue;
// read_line (fp1 , line, 300 ) ;
//printf("\n-----------%s-----------\n",line);
result = sscanf ( line, "%d", &choice );
if ( result != 1 )
{
fprintf ( stderr, "corrupted menu choice\n" );
continue;
}
switch ( choice )
{
case ADD_CODE: /* add book to database */
menu_add();
break;
case PRINT_CODE: /* get book details from database */
menu_print();
break;
case DEL_CODE: /* delete book from database */
menu_delete();
break;
case MOD_CODE: /* print database contents to screen
(standard output) */
menu_modify();
break;
case DETAIL_CODE: /* print tree to screen (standard output) */
menu_detail();
break;
/* exit */
case EXIT_CODE:
break;
default:
fprintf ( stderr, "illegal choice %d\n", choice );
break;
}
/* check for exit menu choice */
if ( choice == EXIT_CODE )
break;
}
return 0;
}
bool isdigit(char n)
{
if (n <= '9' && n >= '0')
{
return true;
}
else
{
return false;
}
}
int IsString(char *szBuff)
{
const int nLen = strlen(szBuff);
int nDigitCnt = 0;
int nPos = 0;
for (nPos = 0; nPos < nLen; nPos++)
{
if (isdigit(szBuff[nPos]))
{
nDigitCnt++;
}
}
if (nLen == nDigitCnt)
{
fprintf(stderr, "The words(%s) which you entered is digits, enter again!\n", szBuff);
return 0;
}
return 1;
}
/* trim the space of the words */
static int trim_space(char *p)
{
char *q = NULL;
if(*p != '\0')
{
q = p + strlen(p) - 1;
}
else
{
return -1;
}
for(; *q == ' '; q--);
*(q + 1) ='\0';
for(q = p; *q == ' '; q++);
strcpy(p,q);
return 0;
}
void print_node(PersonInfo *node, int n)
{
fprintf(stderr, "%s", ID);
printf("%d\n", n);
fprintf(stderr, "%s", NAME);
printf("%s\n", node->name);
fprintf(stderr, "%s", PHONE);
printf("%s\n", node->phone);
fprintf(stderr, "%s", ADDR);
printf("%s\n", node->addr);
}
void menu_add()
{
char name[MAX_NAME + 1] = {0};
char phone[MAX_PHONE + 1] = {0};
char addr[MAX_ADDR + 1] = {0};
int nId = 0;
do
{
fprintf(stderr, "%s", ID);
scanf("%d", &nId);
getchar();
}while(nId >= MAX_NUM && nId < 1);
int nLen = 0;
do
{
fprintf(stderr, "%s", NAME);
fgets(name, MAX_NAME, stdin);
nLen = strlen(name) - 1;
name[nLen] = '\0';
trim_space(name);
}while (!IsString(name));
fprintf(stderr, "%s", PHONE);
fgets(phone, MAX_PHONE, stdin);
nLen = strlen(phone) - 1;
phone[nLen] = '\0';
fprintf(stderr, "%s", ADDR);
fgets(addr, MAX_ADDR, stdin);
nLen = strlen(addr) - 1;
addr[nLen] = '\0';
nId = nId - 1;
PersonInfo *pi = &g_piArry[nId];
if (0 != strlen(pi->name))
{
int i = 0;
for (i = MAX_NUM; i > nId; i--)
{
memcpy(&g_piArry[i], &g_piArry[i - 1], sizeof(PersonInfo));
}
}
strcpy(pi->name, name);
strcpy(pi->phone, phone);
strcpy(pi->addr, addr);
return;
}
void menu_print()
{
int n = 0;
for (n = 0; n < MAX_NUM; n++)
{
if (0 != strlen(g_piArry[n].name))
{
print_node(&g_piArry[n], n + 1);
}
}
return;
}
void menu_delete()
{
int nId = 0;
do
{
fprintf(stderr, "%s", ID);
scanf("%d", &nId);
getchar();
}while(nId >= MAX_NUM && nId < 1);
PersonInfo *pi = &g_piArry[nId];
int i = 0;
for (i = nId - 1; i < MAX_NUM - 1; i++)
{
memcpy(&g_piArry[i], &g_piArry[i + 1], sizeof(PersonInfo));
}
fprintf(stderr, "删除成功(%s)", nId);
return ;
}
void menu_modify()
{
int nId = 0;
do
{
fprintf(stderr, "%s", ID);
scanf("%d", &nId);
getchar();
}while(nId >= MAX_NUM && nId < 1);
char name[MAX_NAME + 1] = {0};
char phone[MAX_PHONE + 1] = {0};
char addr[MAX_ADDR + 1] = {0};
int nLen = 0;
do
{
fprintf(stderr, "%s", NAME);
fgets(name, MAX_NAME, stdin);
nLen = strlen(name) - 1;
name[nLen] = '\0';
trim_space(name);
}while (!IsString(name));
fprintf(stderr, "%s", PHONE);
fgets(phone, MAX_PHONE, stdin);
nLen = strlen(phone) - 1;
phone[nLen] = '\0';
fprintf(stderr, "%s", ADDR);
fgets(addr, MAX_ADDR, stdin);
nLen = strlen(addr) - 1;
addr[nLen] = '\0';
nId = nId - 1;
PersonInfo *pi = &g_piArry[nId];
strcpy(pi->name, name);
strcpy(pi->phone, phone);
strcpy(pi->addr, addr);
return ;
}
void menu_detail()
{
int nId = 0;
do
{
fprintf(stderr, "%s", ID);
scanf("%d", &nId);
getchar();
}while(nId >= MAX_NUM && nId < 1);
print_node(&g_piArry[nId - 1], nId);
return;
}
其他:比如之前有3条记录,他们的序号为1、2、3,删除第二条记录之后,第三条记录的序号是仍然为3还是变为2? 需求再明确一下:
1.比如之前有3条记录,他们的序号为1、2、3,删除第二条记录之后,第三条记录的序号是仍然为3还是变为2?
2.删除和修改记录的时候分别根据什么进行删除或修改?序号?名字?还是其它? 假设记录通讯录的表名为:TList
1. select * from TList
2. insert into TList(...) values(...)
3.delete from TList where ...
4.update TList set ... Where...
5.select * from TList Where name=? or name='' ....
界面用C++写,数据库就用SQL SERVER,简单方面。 同志是哪的?怎么中国的数据结构都一个题目? 你QQ或是邮箱多少,我把源文件发给你。
上一个:c语言循环语句
下一个:关于C语言的基础编程,一个提问。NO.7