鱼C论坛

 找回密码
 立即注册
查看: 3948|回复: 5

C程序报错哪个进来看下

[复制链接]
发表于 2012-11-18 14:20:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int *JosephWithArray(int n)
{
        int *p;
        p = new int[n];
        int i;
        for(i=0;i<n;i++)
        {
                p[i]=i+1;
        }
        return p;
}
int NextPeople(int prev,int *WorkingArray,int number_of_people,int step)
{
        int count=0;
        int travel=prev;
        int emptyplace=0;
        while(count<step)
        {
                travel=(travel+1)%number_of_people;
                if(WorkingArray[travel])
                {
                        count++;
                        emptyplace=0;
                }
                else
                {
                        emptyplace++;
                        if(emptyplace==number_of_people)
                        {
                                return -1;
                        }
                }
        }
        return travel;
}
void CreatOutput(int *WorkingArray,int number_of_people,int step)
{
        int i;
        int outnum=-1;
        for(i=0;i<number_of_people;i++)
        {
                outnum=NextPeople(outnum,WorkingArray,number_of_people,step);
                assert(outnum>-1 && outnum<number_of_people);
                printf("%d",WorkingArray[outnum]);
                WorkingArray[outnum]=0;
        }
        printf("\n");
}
void Dispose(int *WorkingArray)
{
        delete[] WorkingArray;
}

int main()
{
        int n,m;
        int *WorkingArray;

        printf("input number of people:");
        scanf("%d",&n);
        printf("input step:");
        scanf("%d",&m);

        WorkingArray = JosephWithArray(n);
        CreatOutput(WorkingArray,n,m);
        Dispose(WorkingArray);

        return 0;
}


--------------------Configuration: 约瑟夫问题 - Win32 Debug--------------------
Compiling...
约瑟夫问题.c
F:\C源代码\约瑟夫问题.c(8) : error C2065: 'new' : undeclared identifier
F:\C源代码\约瑟夫问题.c(8) : warning C4047: '=' : 'int *' differs in levels of indirection from 'int '
F:\C源代码\约瑟夫问题.c(8) : error C2143: syntax error : missing ';' before 'type'
F:\C源代码\约瑟夫问题.c(9) : error C2143: syntax error : missing ';' before 'type'
F:\C源代码\约瑟夫问题.c(10) : error C2065: 'i' : undeclared identifier
F:\C源代码\约瑟夫问题.c(55) : error C2065: 'delete' : undeclared identifier
F:\C源代码\约瑟夫问题.c(55) : error C2059: syntax error : ']'
执行 cl.exe 时出错.

约瑟夫问题.obj - 1 error(s), 0 warning(s)

点评

下次发代码的时候 在编辑框的正中间不是有个<> 这个符号吗 你点进去 把代码粘贴进去就可以了  发表于 2012-11-19 14:01
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-11-19 14:07:58 | 显示全部楼层
  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. #include <assert.h>

  4. int *JosephWithArray(int n)
  5. {
  6.         int *p;
  7.         p = new int[n];                //在C语言里面动态的开辟空间是用的函数为 malloc  它的原型为: void *malloc(unsigned int n)
  8.                                                 //在C++里面才是用new
  9.         int i;                        //还有在C语言中  定义变量必须在所有的语句之前定义
  10.         for(i=0;i<n;i++)
  11.         {
  12.                 p[i]=i+1;
  13.         }
  14.         return p;
  15. }
  16. int NextPeople(int prev,int *WorkingArray,int number_of_people,int step)
  17. {
  18.         int count=0;
  19.         int travel=prev;
  20.         int emptyplace=0;
  21.         while(count<step)
  22.         {
  23.                 travel=(travel+1)%number_of_people;
  24.                 if(WorkingArray[travel])
  25.                 {
  26.                         count++;
  27.                         emptyplace=0;
  28.                 }
  29.                 else
  30.                 {
  31.                         emptyplace++;
  32.                         if(emptyplace==number_of_people)
  33.                         {
  34.                                 return -1;
  35.                         }
  36.                 }
  37.         }
  38.         return travel;
  39. }
  40. void CreatOutput(int *WorkingArray,int number_of_people,int step)
  41. {
  42.         int i;
  43.         int outnum=-1;
  44.         for(i=0;i<number_of_people;i++)
  45.         {
  46.                 outnum=NextPeople(outnum,WorkingArray,number_of_people,step);
  47.                 assert(outnum>-1 && outnum<number_of_people);
  48.                 printf("%d",WorkingArray[outnum]);
  49.                 WorkingArray[outnum]=0;
  50.         }
  51.         printf("\n");
  52. }
  53. void Dispose(int *WorkingArray)
  54. {
  55.         delete[] WorkingArray;
  56. }

  57. int main()
  58. {
  59.         int n,m;
  60.         int *WorkingArray;
  61.        
  62.         printf("input number of people:");
  63.         scanf("%d",&n);
  64.         printf("input step:");
  65.         scanf("%d",&m);
  66.        
  67.         WorkingArray = JosephWithArray(n);
  68.         CreatOutput(WorkingArray,n,m);
  69.         Dispose(WorkingArray);
  70.        
  71.         return 0;
  72. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-11-19 14:13:20 | 显示全部楼层
  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. #include <assert.h>

  4. int *JosephWithArray(int n)
  5. {
  6.         int *p;
  7.         int i;       
  8.         p = malloc(n);                //在C语言里面动态的开辟空间是用的函数为 malloc  它的原型为: void *malloc(unsigned int n)
  9.         //在C++里面才是用new                 //还有在C语言中  定义变量必须在所有的语句之前定义  调用malloc函数 需要头文件malloc.h
  10.         for(i=0;i<n;i++)
  11.         {
  12.                 p[i]=i+1;
  13.         }
  14.         return p;
  15. }
  16. int NextPeople(int prev,int *WorkingArray,int number_of_people,int step)
  17. {
  18.         int count=0;
  19.         int travel=prev;
  20.         int emptyplace=0;
  21.         while(count<step)
  22.         {
  23.                 travel=(travel+1)%number_of_people;
  24.                 if(WorkingArray[travel])
  25.                 {
  26.                         count++;
  27.                         emptyplace=0;
  28.                 }
  29.                 else
  30.                 {
  31.                         emptyplace++;
  32.                         if(emptyplace==number_of_people)
  33.                         {
  34.                                 return -1;
  35.                         }
  36.                 }
  37.         }
  38.         return travel;
  39. }
  40. void CreatOutput(int *WorkingArray,int number_of_people,int step)
  41. {
  42.         int i;
  43.         int outnum=-1;
  44.         for(i=0;i<number_of_people;i++)
  45.         {
  46.                 outnum=NextPeople(outnum,WorkingArray,number_of_people,step);
  47.                 assert(outnum>-1 && outnum<number_of_people);
  48.                 printf("%d",WorkingArray[outnum]);
  49.                 WorkingArray[outnum]=0;
  50.         }
  51.         printf("\n");
  52. }

  53. void Dispose(int *WorkingArray)
  54. {
  55.         free(WorkingArray);  //delete[] WorkingArray;
  56. }

  57. int main()
  58. {
  59.         int n,m;
  60.         int *WorkingArray;
  61.        
  62.         printf("input number of people:");
  63.         scanf("%d",&n);
  64.         printf("input step:");
  65.         scanf("%d",&m);
  66.        
  67.         WorkingArray = JosephWithArray(n);
  68.         CreatOutput(WorkingArray,n,m);
  69.         Dispose(WorkingArray);
  70.        
  71.         return 0;
  72. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-11-19 19:08:29 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-11-19 21:46:24 | 显示全部楼层
你这个程序到底是做什么功能
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-11-21 21:54:00 | 显示全部楼层
是约瑟夫环啊,没有报错啊
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-16 08:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表