#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <CertExit.h>
#define YES 1
#define NO 0
#define LIST struct list
LIST {
char *name;
char *tel;
LIST *next;
};
LIST *root;
void node(char *nnam, char *ntel);
int inp(char *inam, char *itel);
LIST *nalloc();
char *strsv(char *sl);
void prn(LIST *pp);
void srch(char *snam);
void modi(char *mnam);
void dele(char *dnam);
void main()
{
char name[15], tel[15];
char cname[15];
int key;
root = NULL;
do {
printf("\n\n < Select the number >\n");
printf("\n input-1 display-2 search-3 modify-4 delete-5 end-6 \n");
printf("\n If you input wrong key_then program exit!\n");
printf("\n key --> ");
scanf("%d",&key);
if (key == 1) {
printf("\n ------------- input ------------- \n");
while (inp(name, tel) == NO)
node(name, tel);
}
else if (key == 2) {
printf("\n ---------- disply ---------- \n");
printf("\n name\t\t\ttel\n\n");
prn(root);
}
else if (key == 3) {
printf("\n ---------- search ---------- \n");
printf("\n ( If you search a nonexistent item then Program RESTART )\n");
printf("\n name --> ");
scanf("%s", cname);
srch(cname);
}
else if (key == 4) {
printf("\n ---------- modify ---------- \n");
printf(" name --> ");
scanf("%s", cname);
modi(cname);
}
else if (key == 5) {
printf("\n ---------- delete ---------- \n");
printf("\n ( After you delete an item_You should input new item )\n");
printf("\n name --> ");
scanf("%s", cname);
dele(cname);
}
else if (key == 6) {
printf("\n ------------ end ------------ \n");
}
else exit(0);
} while (key < 6);
getch();
}
void node(char *nnam, char *ntel)
{
LIST *rp,*Tp;
LIST *nalloc();
char *strsv(char *z);
int cmp;
rp = nalloc();
rp->name = strsv(nnam);
rp->tel = strsv(ntel);
rp->next = NULL;
if(root == NULL){
root=rp;
return;
}
Tp=root;
while(Tp->next!=NULL)
{
if ((cmp = strcmp(nnam, Tp->name)) == 0 && (cmp = strcmp(ntel, Tp->tel)) == 0)
{
printf("\n -------------------------------------------\n");
printf("\n Already Existence!\n");
printf(" NAME = %-15s TEL = %-13s \n", rp->name, rp->tel);
printf("\n -------------------------------------------\n");
free(rp);
return;
}
Tp=Tp->next;
}
Tp->next=rp;
}
int inp(char *inam, char *itel)
{
printf("\n ---------------------------------- \n");
printf("\n ---- If name is 'q' then exit ---- \n");
printf("\n ---------------------------------- \n");
printf("\n name --> ");
scanf("%s",inam);
if (*inam == 'q' || *inam == 'Q')
{
return(YES);
}
printf("\n tel --> ");
scanf("%s", itel);
return(NO);
}
LIST *nalloc()
{
return (LIST*) malloc(sizeof(LIST));
}
char *strsv(char *sl)
{
char *p;
if ((p =(char*)malloc(strlen(sl) + 1)) !=NULL)
strcpy(p, sl);
return(p);
}
void prn(LIST *pp)
{
LIST *Pp;
if(root==NULL){
printf("\n No data!\n");
return ;
}
else Pp=root;
printf(" %-13s s\n",root->name,root->tel);
while(Pp->next!=NULL){
Pp=Pp->next;
printf(" %-13s s\n", Pp->name, Pp->tel);
}
if(Pp->next==NULL){
return;
}
}
void srch(char *snam)
{
LIST *Tp;
int cmp;
if(root==NULL){
printf("\n No data!\n");
return;
}
else Tp=root;
while((cmp=strcmp(snam,Tp->name))!=0&&Tp->next!=NULL)
Tp=Tp->next;
if(cmp==0){
printf("\n name\t\t\ttel \n\n");
printf(" %-13s s\n", Tp->name, Tp->tel);
}
else{
printf("\n Fail to search\n");
}
}
void modi(char *mnam)
{
LIST *Tp;
char mtel[15];
int cmp=1;
if(root==NULL){
printf("\n No data!\n");
return;
}
else Tp=root;
while((cmp=strcmp(mnam,Tp->name))!=0&&Tp->next!=NULL)
Tp=Tp->next;
if(cmp==0){
printf(" tel --> ");
scanf("%s",mtel);
strcpy(Tp->name,mnam);
strcpy(Tp->tel,mtel);
printf("\n -- Be Modified Data --\n");
printf("\n name\t\t\ttel \n\n");
printf(" %-13s s\n",Tp->name,Tp->tel);
}
else{
printf("\n It's nonexistent\n");
}
}
void dele(char *dnam)
{
int cmp=1;
LIST *p,*dp;
if(root==NULL){
printf("\n No data!\n");
return;
}
else
dp=root;
while((cmp=strcmp(dnam,dp->name))!=0&&dp->next!=NULL){
p=dp;
dp=dp->next;
}
if(cmp==0){
if(dp!=root){
p->next=dp->next;
free(dp);
}
else if(root->next==NULL){
free(root);
root==NULL;
}
else{
root=root->next;
free(dp);
}
printf("\n ( Check the datas )\n");
printf("\n ---------- disply ---------- \n");
printf("\n name\t\t\ttel\n\n");
prn(dp);
}
else
printf("\n It's nonexistent\n");
}
형님들 실행했을 때 빨간부분에서 잘안됩니다.
목록에 입력한 것이 있으면 이미있다고 알려주는 것은 어떻게하나요?
제가한것은 연속해서 알려주지 않습니다
http://colorscripter.com/
이 사이트를 써보세요!
그런데 뭐하는 프로그램인지 볼 자신이 없습니다. 형님은 할 수 있을 겁니다. 형님!
php에선 array_push, unset, array_search 세가지 펑션만 쓰면 끝나겠습니다만 ㄷㄷ 역시 c는 어려운 프로그래밍이군요