|
|
#2
寒风中的细雨2011-09-20 06:56
程序代码:# include<stdio.h> # include<stdlib.h> # include<string.h> #define ERROR 0 #define OK 1 #define RL 100 typedef struct{ char *name[RL]; char *phonenum[RL]; int DHB_length; }dianhuabu; int creat_dianhuabu(dianhuabu *P,int Length)//创建电话簿 { int i; for(i=0;i<Length;i++){ P->phonenum[i]=(char *)malloc(20*sizeof(char)); printf("电话号码%d:",i+1); scanf("%s",P->phonenum[i]); P->name[i]=(char *)malloc(Length*20*sizeof(char)); printf("姓名%d:",i+1); scanf("%s",P->name[i]); } P->DHB_length=Length; return OK; } int Inserti(dianhuabu *P)//删除操作 { int i,j; char CR_phone[20]={'\0'},CR_name[20]={'\0'}; printf("请输入插入位置:"); scanf("%d",&i); if(i<1||i>P->DHB_length+1) return ERROR; printf("请输入插入的电话号码:"); scanf("%s",CR_phone); printf("请输入插入的姓名:"); scanf("%s",CR_name); P->phonenum[P->DHB_length]=(char *)malloc(20*sizeof(char)); P->name[P->DHB_length]=(char *)malloc(20*sizeof(char)); if(!P->phonenum[P->DHB_length]||!P->name[P->DHB_length]) return ERROR; for(j=P->DHB_length;j>=i;j--){ //P->phonenum[j]=P->phonenum[j-1]; //P->name[j]=P->name[j-1]; strcpy(P->phonenum[j], P->phonenum[j-1]); strcpy(P->name[j], P->name[j-1]); } strcpy(P->phonenum[i-1],CR_phone); strcpy(P->name[i-1],CR_name); P->DHB_length++; return OK; } int Deletei(dianhuabu *P)//插入操作 { int i,j; printf("请输入删除位置:"); scanf("%d",&i); if(i<1||i>P->DHB_length) return ERROR; for(j=i;j<=P->DHB_length;j++){ P->phonenum[j-1]=P->phonenum[j]; P->name[j-1]=P->name[j]; } P->DHB_length--; return OK; } int PrintfP(dianhuabu *P)//输出 { int i; printf("电话簿目前存储数量为:%d\n",P->DHB_length); for(i=0;i<P->DHB_length;i++){ printf("电话号码%d:%s 姓名%d:%s\n",i+1,P->phonenum[i],i+1,P->name[i]); } return OK; } int main() { int length,caozuo; dianhuabu Q; printf("创建电话簿\n\n请输入电话簿用户数量:"); scanf("%d",&length); creat_dianhuabu(&Q,length); while(1){ printf("请选择您想对电话簿进行的操作:\n1、delete(删除).\n2、insert(插入).\n3、printfP(输出电话簿).\n4、end(不进行任何操作).\n\n"); printf("请选择您要进行的操作:"); scanf("%d",&caozuo); printf("\n"); if(caozuo==4) break; if(caozuo==3){ PrintfP(&Q); } if(caozuo==2){ Inserti(&Q); } if(caozuo==1){ Deletei(&Q); } } // free(&Q); return 0; } |
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
#define ERROR 0
#define OK 1
#define RL 100
typedef struct{
char *name[RL];
char *phonenum[RL];
int DHB_length;
}dianhuabu;
int creat_dianhuabu(dianhuabu *P,int Length)//创建电话簿
{
int i;
for(i=0;i<Length;i++){
P->phonenum[i]=(char *)malloc(20*sizeof(char));
printf("电话号码%d:",i+1);
scanf("%s",P->phonenum[i]);
P->name[i]=(char *)malloc(Length*20*sizeof(char));
printf("姓名%d:",i+1);
scanf("%s",P->name[i]);
}
P->DHB_length=Length;
return OK;
}
int Inserti(dianhuabu *P)//删除操作
{
int i,j;
char CR_phone[20]={'\0'},CR_name[20]={'\0'};
printf("请输入插入位置:");
scanf("%d",&i);
if(i<1||i>P->DHB_length+1) return ERROR;
printf("请输入插入的电话号码:");
scanf("%s",CR_phone);
printf("请输入插入的姓名:");
scanf("%s",CR_name);
P->phonenum[P->DHB_length]=(char *)malloc(20*sizeof(char));
P->name[P->DHB_length]=(char *)malloc(20*sizeof(char));
if(!P->phonenum[P->DHB_length]||!P->name[P->DHB_length]) return ERROR;
for(j=P->DHB_length;j>=i;j--){
P->phonenum[j]=P->phonenum[j-1];
P->name[j]=P->name[j-1];
}
strcpy(P->phonenum[i-1],CR_phone);
strcpy(P->name[i-1],CR_name);
P->DHB_length++;
return OK;
}
int Deletei(dianhuabu *P)//插入操作
{
int i,j;
printf("请输入删除位置:");
scanf("%d",&i);
if(i<1||i>P->DHB_length) return ERROR;
for(j=i;j<=P->DHB_length;j++){
P->phonenum[i-1]=P->phonenum[i];
P->name[i-1]=P->name[i];
}
P->DHB_length--;
return OK;
}
int PrintfP(dianhuabu *P)//输出
{
int i;
printf("电话簿目前存储数量为:%d\n",P->DHB_length);
for(i=0;i<P->DHB_length;i++){
printf("电话号码%d:%s 姓名%d:%s\n",i+1,P->phonenum[i],i+1,P->name[i]);
}
return OK;
}
int main()
{
int length,caozuo;
dianhuabu Q;
printf("创建电话簿\n\n请输入电话簿用户数量:");
scanf("%d",&length);
creat_dianhuabu(&Q,length);
while(1){
printf("请选择您想对电话簿进行的操作:\n1、delete(删除).\n2、insert(插入).\n3、printfP(输出电话簿).\n4、end(不进行任何操作).\n\n");
printf("请选择您要进行的操作:");
scanf("%d",&caozuo);
printf("\n");
if(caozuo==4) break;
if(caozuo==3){
PrintfP(&Q);
}
if(caozuo==2){
Inserti(&Q);
}
if(caozuo==1){
Deletei(&Q);
}
}
free(&Q);
return 0;
}
这个是程序就是 在插入操作时,不知道什么原因,有些数据也会一样,删除操作,只要删除位置是 第一个位置,也会变成所有数据变成一样。
这个程序也是电话簿,不过时用的链表 存储的
# include<stdio.h>
# include<stdlib.h>
#define OK 1
#define ERROR 0
typedef struct dianhuabu{
char phonenum[20];
char name[20];
struct dianhuabu *next;
}dianhuabu,*DianHuaBu;
int creat_dianhuabu(DianHuaBu P,int *Len)
{
int i;
printf("请逐个输入用户的电话号码和姓名\n");
for(i=0;i<*Len;i++){
P->next=(dianhuabu *)malloc(20*sizeof(char)+20*sizeof(char)+sizeof(DianHuaBu));
P=P->next;
printf("电话号码%d:",i+1);
scanf("%s",&P->phonenum);
printf("姓名%d:",i+1);
scanf("%s",&P->name);
}
printf("\n");
return OK;
}
int Delete_i(DianHuaBu P,int *Len)
{
int j,i;
printf("请输入删除位置:");
scanf("%d",&i);
if(i<0||i>*Len) return ERROR;
for(j=0;j<i-1;j++){
P=P->next;
}
P->next=P->next->next;
(*Len)--;
return OK;
}
int Inserti(DianHuaBu P,int *Len)
{
int i,j;
DianHuaBu CR;
printf("请输入插入位置:");
scanf("%d",&i);
if(i<1||i>(*Len)+1) return ERROR;
for(j=0;j<i-1;j++){
P=P->next;
}
CR=(dianhuabu *)malloc(20*sizeof(char)+20*sizeof(char)+sizeof(DianHuaBu));
printf("请输入插入的电话号码:");
scanf("%s",&CR->phonenum);
printf("请输入插入的姓名:");
scanf("%s",&CR->name);
CR->next=P->next,P->next=CR;
(*Len)++;
free(CR);
return OK;
}
int printfP(DianHuaBu P,int *Len)
{
int i;
printf("电话薄目前的存储个数为:%d\n",*Len);
for(i=0;i<*Len;i++){
P=P->next;
printf("电话号码%d:%s 姓名%d:%s\n",i+1,P->phonenum,i+1,P->name);
}
printf("\n\n\n");
return OK;
}
int freeP(DianHuaBu P,int *Len)
{
int i;
for(i=0;i<*Len;i++){
free(P);
P=P->next;
}
return OK;
}
int main()
{
int length,*q,caozuo;
DianHuaBu Q,CR;
Q=(dianhuabu *)malloc(20*sizeof(char)+20*sizeof(char)+sizeof(DianHuaBu));
CR=(dianhuabu *)malloc(20*sizeof(char)+20*sizeof(char)+sizeof(DianHuaBu));
Q->next=NULL;
printf("创建电话簿\n\n请输入电话簿用户数量:");
scanf("%d",&length);
q=&length;
creat_dianhuabu(Q,q);
while(1){
printf("请选择您想对电话簿进行的操作:\n1、delete(删除).\n2、insert(插入).\n3、printfP(输出电话簿).\n4、end(不进行任何操作).\n\n");
printf("请选择您要进行的操作:");
scanf("%d",&caozuo);
printf("\n");
if(caozuo==4) break;
if(caozuo==3){
printfP(Q,q);
}
if(caozuo==2){
Inserti(Q,q);
}
if(caozuo==1){
Delete_i(Q,q);
}
}
freeP(Q,q);
return 0;
}
这个问题就是 只要连续插入 数据,数据就会出错。。。
求解决
[ 本帖最后由 zhuchenxi 于 2011-9-19 20:25 编辑 ]
程序代码: