| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1063 人关注过本帖
标题:[求助]约瑟夫环程序没错误但在C++6.0下运行不了清大家指点谢谢
只看楼主 加入收藏
西牛616
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-4-6
收藏
 问题点数:0 回复次数:4 
[求助]约瑟夫环程序没错误但在C++6.0下运行不了清大家指点谢谢

#include "malloc.h"
#include "stdio.h"
#define MAX 100
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef struct LNode
{
int num;
ElemType data;
struct LNode *next;
}LNode;
LNode *head,*this,*new;
int str[MAX];
new_code(int a);
delete_code(int a,int b);

main()
{
int m,n,i;
printf("Enter the first code (m):");
scanf("%d",&m);
printf("\nEnter the people number (n):");
scanf("%d",&n);
getchar();
printf("\n");
new_code(n);
if(head!=NULL)
delete_code(n,m);
else
{
printf("list is empty\n");
exit(0);
}
for(i=0;i<n;i++)
printf("%3d",str[i]);
printf("\n");
}
new_code(int a)
{
int i=1;
char numstr[10];
new=(LNode *)malloc(sizeof(LNode));
if(new==NULL)
return ERROR;
if(head==NULL)
head=new;
this=head;
while(--a!=0)
{
this->num=i;
printf("enter the %d code(data):",i);
gets(numstr);
this->data=atoi(numstr);
new=(LNode *)malloc(sizeof(LNode));
this->next=new;
this=new;
i++;
}
this->num=i;
printf("enter the %d code(data):",i);
gets(numstr);
this->data=atoi(numstr);
this->next=head;
return OK;
}
delete_code(int a,int b)
{
int i;
int j=0;
LNode *p;
while((a--)!=1)
{
for(i=1;i<b;i++)
{
p=this;
this=this->next;
}
b=this->data;
str[j]=this->num;
p->next=this->next;
free(this);
j++;
}
str[j]=this->next->num;
return OK;
}

这个程序编译没问题,但运行不了,请教高手指点,谢谢

搜索更多相关主题的帖子: 约瑟夫 运行 
2006-04-06 17:14
dilzjj
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-3-4
收藏
得分:0 
这个程序的错误太多了,vc++6.0 主要是用来编c++的,而你程序里面很多变量都是c++的关键字,所以
还是自己改吧,要是想看源程序,可以点击这个连接 zoujunjie.blog.com.cn[URL=http://zoujunjie.blog.com.cn]http://zoujunjie.blog.com.cn[/URL]
2006-04-06 23:45
柔美
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-4-5
收藏
得分:0 
head=new;
很危险

MSN::zhengmeier@ Email::chentianqiao06@ TM::619969970
2006-04-07 00:36
kaiyanyixiao
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-4-17
收藏
得分:0 

www.tiantiansoft.com上面看看那上面是怎么写的,好象比你这个简单啊。


爱拼才会赢!
2006-04-18 18:39
sunliulin80
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2005-12-13
收藏
得分:0 

#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFESIBLE -1
#define OVERFLOW -2
typedef int status;
typedef int BOOL;
typedef int ElemType;
typedef struct Node
{
ElemType pasword;
int number;
struct Node *next;
}LNode,*linklist;

linklist Initlinklist(void)
{
linklist p;
ElemType psw;
p=(linklist)malloc(sizeof(LNode));
if(!p)
{
printf("can't init linklist!\n");
exit(0);
}
printf("please enter the first pasword:\n");
scanf("%d",&psw);
p->number=1;
p->next=NULL;
p->pasword=psw;
return p;
}

void Insertlinklist(linklist p,ElemType a[],int n)
{
int i;
linklist q,h;
h=p;
for(i=1;i<n+1;i++)
{
q=(linklist)malloc(sizeof(LNode));
if(!q)
{
printf("can't malloc room\n");
system("pause");
exit(0);
}
q->next=NULL;
q->number=i;
q->pasword=a[i-1];
h->next=q;
h=q;
}
h->next=p;
}

void Dellinklist(linklist p,int m)
{
linklist q,h;
int i;
h=p;
while(h->next!=h)
{
for(i=0;i<m-1;i++)
h=h->next;
q=h->next;
m=q->pasword;
printf("%d, ",q->pasword);
h->next=q->next;
free(q);
}
printf("%d\n",h->pasword);
free(h);
}

int main(void)
{
linklist seqlik;
ElemType a[19];
int i,m;
seqlik=Initlinklist();
for(i=0;i<19;i++)
a[i]=i+1;
Insertlinklist(seqlik,a,19);
printf("please enter the m-ths for out queue:\n");
scanf("%d",&m);
Dellinklist(seqlik,m);

return 0;
}

2006-04-18 23:08
快速回复:[求助]约瑟夫环程序没错误但在C++6.0下运行不了清大家指点谢谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014415 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved