约瑟夫环-数据结构课程设计 第2页

 

int GetPersonNumber()//确定需要处理的人数
{
int personNumber;

printf("请输入需要输入人的数目:");
scanf("%d",&personNumber);
while (personNumber > MAXPERSONNUMBER || personNumber < 0)
{
printf("\n你输入的数字无效,请输入在0到%d的整数",MAXPERSONNUMBER);
scanf("%d",&personNumber);
}
printf("最终确定的人数为%d\n",personNumber);

return personNumber;
}

int GetFirstCountValue()//确定开始的上限值
{
int firstCountValue;

printf("请输入初始的上限值");
scanf("%d",&firstCountValue);
while (firstCountValue > MAXFIRSTCOUNTVALUE || firstCountValue < 0)
{
printf("\n你输入的数字无效,请输入在0到%d的整数",MAXFIRSTCOUNTVALUE);
scanf("%d",&firstCountValue);
}
printf("最终的上限值为%d",firstCountValue);

return firstCountValue;
}
//得到真确的顺序
void GetOutputOrder(LinkList *L, int personNumber, int reportValue, int array[MAXPERSONNUMBER])
{
Node *p, *q;
int count = 1, i = 0;

p = (*L);
while (personNumber)
{
while (count != reportValue)
{
q = p;
p = p->next;
count++;
}

array[i++] = p ->data;
reportValue = p->password;
q->next = p->next;
free(p);
p = q->next;
count = 1;
personNumber--;
}
}

void printResult(int array[],int personNumer)//输出结果
{
int i;
printf("\n出队的顺序为:");
for(i = 0; i < personNumer; i++)
{
printf("%-3d",array[i]);
}
printf("\n");
}

int main(void)//主函数
{
LinkList L;
int personNumber, reportValue;
int array[MAXPERSONNUMBER];

personNumber = GetPersonNumber();
reportValue = GetFirstCountValue();

CreatLinkList(&L);
InitLinkList(&L, personNumber);

GetOutputOrder(&L, personNumber, reportValue, array);
printResult(array, personNumber);

system("pause");
return 0;
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1209861

上一页  [1] [2] 

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有