对不起我仍然刚接触的长代码...它只是在我显示数据时,活动数据总是再插入1个数据,这使我的程序无法响应。
#include <stdio.h>
#include <stdlib.h>
struct activity{
int reps, calBurn;
char aName[30];
struct activity *ptrnexto;
};
struct activity *newptro, *curptro, *prevptro;
struct plan {
char pName[30], focus;
int recWieght;
struct plan *ptrnext;
struct activity *act;
};
struct plan *headptr, *newptr, *curptr, *prevptr;
int menu(int num);
int insertPlan ();
int displayPlans ();
int main()
{
menu(1);
}
// menu func
int menu(int no)
{
int choice;
if (no==0){
return 0;
}
while(no==1)
{
system("cls");
printf("\n");
printf("\t\t\t\t Welcome!\n");
printf("\t\t\t Input a new fitness plan\t [1]\n"
"\t\t\t View ALL fitness plan\t [5]\n");
printf("\n\t\t\t -----------\n ");
printf("\t\t\t Choice: ");
scanf("%d",&choice);
fflush(stdin);
system("cls");
switch(choice)
{
case 1:
{
insertPlan ();
break;
}
case 5:
{
displayPlans ();
return 0;
break;
}
default:
{
printf("Invalid key entry.");
break;
}
}//end switch
printf("\n\tDo you wish to return to menu?\n"
"\tYes[1]\t\tNo[0]\n\t");
printf("--> ");
scanf("%d",&no);
return menu(no);
}
return 0;
}
// insert func
int insertPlan ()
{
int pos=0,ch;
newptr = (struct plan*)malloc(sizeof(struct plan));
printf("\n Recomended Wieght : ");
scanf(" %d",&newptr->recWieght);
printf("\n Plan Name : ");
scanf(" %s",&newptr->pName);
printf("\nUpperBody(U)--LowerBody(L)--Abdomen(A)--Stamina(S) \nPlan Focus : ");
scanf(" %c",&newptr->focus);
printf("\n Put in the plans : ");
while(ch!=0)
{
newptro= (struct activity*)malloc(sizeof(struct activity));
pos = pos +1;
printf("\nInsert in actvity %d",pos);
printf("\n Activity Name : ");
scanf("%s",&newptro->aName);
printf("\n how many reps : ");
scanf("%d",&newptro->reps);
printf("\n estimated calory burn : ");
scanf("%d",&newptro->calBurn);
if (newptr->act==NULL)
{
newptr->act=newptro;
newptro->ptrnexto=NULL;
}
else
{
newptro->ptrnexto=newptr->act;
newptr->act=newptro;
}
printf("\n\tIs there anymore plans?\n"
"\tYes[1]\t\tNo[0]\n\t");
scanf("%d",&ch);
}
if (headptr==NULL)
{
headptr=newptr;
newptr->ptrnext=NULL;
}
else
{
newptr->ptrnext=headptr;
headptr=newptr;
}
return
menu(1);
}
//显示
int displayPlans ()
{
int pos=0;
if (headptr==NULL)
printf("EMPTY LIST");
else
{
curptr=headptr;
while(curptr!=NULL)
{
pos=pos +1;
printf("\n%d -%s \n",pos, curptr->pName);
curptro=curptr->act;
while(curptro!=NULL)
{
printf(" #%s --> %d", curptro->aName, curptro->reps);
curptro=curptro->ptrnexto;
}
curptr=curptr->ptrnext;
}
}
}
fitnessCookie.c fitnessCpCookie 由Asyraf Fuyu于2017年4月23日创建。 版权所有fitnessCookie©2017。保留所有权利。
无论如何输入笑脸......
答案 0 :(得分:0)
答案由BLUEPIXY在评论中给出:
将NULL
设为newptr->act
。