| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 525 人关注过本帖
标题:程序运行不了
只看楼主 加入收藏
jkbj
Rank: 2
等 级:论坛游民
威 望:1
帖 子:26
专家分:51
注 册:2013-5-24
结帖率:100%
  问题点数:0  回复次数:1   
程序运行不了
我是要用c打开的但这个程序原来是c++的程序,谁帮我改下
#include<stdlib.h>                               //库中包含system("pause")和rand()函数
#include<stdio.h>                                //c语言里的库
#include<iostream>
using namespace std;
#define N 49                                   //定义为全局变量,这是迷宫数组的上线,可以自行修改
#define M 49
int X;                              
int maze[N+2][M+2];
int head=0,tail=0;                               //队列的头尾指针,初始值设为0

struct point                                    //存放迷宫访问到点的队列结构体,包含列,行,序号
{
     int row,col,predecessor;
}queue[1200];

void hand_maze(int m,int n)                       //手动生成迷宫
{
    int i,j,tx;
    printf("\n");
     printf("请按行输入迷宫,0表示通路,1表示障碍:\n");
     for(i=0;i<m;i++)
       for(j=0;j<n;j++)
        {
            scanf("%d",&maze[i][j]);
       }
}


void automatic_maze(int m,int n)                    //自动生成迷宫
{
    int i,j;
    printf("\n迷宫生成中……\n\n");
    system("pause");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
    maze[i][j]=rand()%2;                         //随机生成0、1
    maze[0][0]=0;                               //将开始和结束位置强制为0,保证有可能出来迷宫
    maze[m-1][n-1]=0;
}

void data(int m,int n)                        
{                              //当用户输入的不是规整的m行n列的迷宫,用来生成规则的数字迷宫
    int i,j;
    printf("\n");
    printf("根据您先前设定的迷宫范围\n");
    printf("\n");
    printf("   我们将取所输入的前");printf("%d",m*n);printf("个数生成迷宫\n");
    printf("  \n数字迷宫生成结果如下:\n\n");
    printf("迷宫入口\n");
    printf("↓");
    for(i=0;i<m;i++)
      {
        printf("\n");
        for(j=0;j<n;j++)
        {
            if(maze[i][j]==0)
    printf(" 0");
            if(maze[i][j]==1)
 printf(" 1");
        }
      }
    printf("→迷宫出口\n");
}

void print_maze(int m,int n)
{                                                    //打印迷宫外壳
    int i,j,k;
    printf("\n字符迷宫生成结果如下:\n\n");
    printf("迷宫入口\n");
    printf("  ↓");
    printf("\n");
    printf("▲  ");                                     //生成上外壳
    for(k=0;k<n;k++)
    {
        printf("▲");                                  //这两个黑三角用来生成顶部外壳
    }
    for(i=0;i<m;i++)
    {
        printf("\n");                                  //生成左外壳
        printf("▲");
        for(j=0;j<n;j++)
        {
            if(maze[i][j]==0) printf("□");
            if(maze[i][j]==1) printf("■");
        }
        printf("▲");                                  //生成右外壳
    }
        printf("\n");
    for(k=0;k<n;k++)
    {
        printf("▲");
    }
    printf("  ▲\n");                                   //生成底部外壳
    for(i=0;i<n;i++)
    {    printf("  ");}
    printf("↓\n");
    for(i=0;i<n;i++)
    {    printf("  ");}
    printf("迷宫出口\n");
}

void result_maze(int m,int n)                              //这个是打印输出迷宫的星号路径
{                                      
    int i,j;
    printf("迷宫通路(用☆表示)如下所示:\n\t");
    for(i=0;i<m;i++)
    {
        printf("\n");
        for(j=0;j<n;j++)
        {
            if(maze[i][j]==0||maze[i][j]==2)               //2是队列中访问过的点
                printf("□");
            if(maze[i][j]==1)
                printf("■");
            if(maze[i][j]==3)                           //3是标记的可以走通的路径
                printf("☆");
        }
    }
}

void enqueue(struct point p)                                //迷宫中队列入队操作
{
    queue[tail]=p;
    tail++;                                             //先用再加,队列尾部加1
}

struct point dequeue()                          //迷宫中队列出队操作,不需要形参,因此用结构体定义
{
    head++;
    return queue[head-1];
}

int is_empty()                                             //判断队列是否为空
{
    return head==tail;
}

void visit(int row,int col,int maze[51][51])                       //访问迷宫矩阵中的节点
{
    struct point visit_point={row,col,head-1};   //head-1的作用是正在访问的这个点的序号为之前的点序号
    maze[row][col]=2;                                     //将访问过的点位标记为2
    enqueue(visit_point);//入队
}

int path(int maze[51][51],int m,int n)                           //路径求解
{
    X=1;                                               //初始值定为1
    struct point p={0,0,-1};                                //定义入口节点
    if(maze[p.row][p.col]==1)                             //入口为1时,迷宫不可解
      {
          printf("\n===============================================\n");
          printf("此迷宫无解\n\n");
           X=0;
           return 0;
      }

        maze[p.row][p.col]=2;                              //标记为已访问
        enqueue(p);                                      //将p入队列
    while(!is_empty())
    {
              p=dequeue();
              if((p.row==m-1)&&(p.col==n-1))              //当行和列为出口时跳出
                  break;
                                                       //定义8个走位方向
              if((((p.row-1)>=0)&&((p.row-1)<m)&&((p.col+0)<n))&&(maze[p.row-1][p.col+0]==0))
                  visit(p.row-1,p.col+0,maze);             //北
              if((((p.row-1)>=0)&&((p.row-1)<m)&&((p.col+1)<n))&&(maze[p.row-1][p.col+1]==0))
                  visit(p.row-1,p.col+1,maze);             //东北
              if((((p.row+0)<m)&&((p.col+1)<n))&&(maze[p.row+0][p.col+1]==0))
                  visit(p.row+0,p.col+1,maze);             //东
              if((((p.row+1)<m)&&((p.col+1)<n))&&(maze[p.row+1][p.col+1]==0))
                  visit(p.row+1,p.col+1,maze);             //东南
              if((((p.row+1)<m)&&((p.col+0)<n))&&(maze[p.row+1][p.col+0]==0))
                  visit(p.row+1,p.col+0,maze);             //南
              if((((p.row+1)<m)&&((p.col-1)<n)&&((p.col-1)>=0))&&(maze[p.row+1][p.col-1]==0))
                  visit(p.row+1,p.col-1,maze);             //西南
              if((((p.row+0)<m)&&((p.col-1)<n)&&((p.col-1)>=0))&&(maze[p.row+0][p.col-1]==0))
                  visit(p.row+0,p.col-1,maze);             //西
      if((((p.row-1)>=0)&&((p.row-1)<m)&&((p.col-1)<n)&&((p.col-1)>=0))&&(maze[p.row-1][p.col-1]==0))
                  visit(p.row-1,p.col-1,maze);            //西北
    }
    if(p.row==m-1&&p.col==n-1)                          //如果当前矩阵点是出口点,输出路径
    {
    printf("\n==================================================================\n");
          printf("迷宫路径为:\n");
          printf("出口"\n);
          printf(" ↑\n");
          printf("(%d,%d)\n",p.row+1,p.col+1);
          printf(" ↑\n");
          maze[p.row][p.col]=3;                           //逆序将路径标记为3
          while(p.predecessor!=-1)
          {
              p=queue[p.predecessor];
              printf("(%d,%d)\n",p.row+1,p.col+1);
              printf(" ↑\n");
              maze[p.row][p.col]=3;
          }
          printf("入口"\n);
    }
    else
    {
          printf("\n=============================================================\n");
          printf("此迷宫无解!\n\n");
          X=0;
    }
    return 0;
}

void main()
{
   int i,m,n,cycle=0;
   while(cycle!=(-1))
   {
  printf("********************************************************************************\n");
      printf("                              欢迎进入迷宫求解系统\n");
      printf("\n");
      printf("                      设计者:杨志强(计算机1班)\n");
  printf("********************************************************************************\n");
      printf("                           ☆  手动生成迷宫  请按:1\n");
      printf("                           ☆  自动生成迷宫  请按:2\n");
      printf("                           ☆  退出          请按:3\n\n");
  printf("********************************************************************************\n");
      printf("\n");
      printf("请选择你的操作:\n");
      scanf("%d",&i);
      switch(i)
      {
      case 1:
          printf("\n请输入行数:");
          scanf("%d",&m);
          printf("\n");
          printf("请输入列数:");
          scanf("%d",&n);
          while((m<0||m>49)||(n<0||n>49))
          {
              printf("\n抱歉,你输入的行列数超出预设范围(0-49,0-49),请重新输入:\n\n");
              printf("\n请输入行数:");
              scanf("%d",&m);
              printf("\n");
              printf("请输入列数:");
              scanf("%d",&n);
          }
          hand_maze(m,n);
          data(m,n);
          print_maze(m,n);
          path(maze,m,n);
          if(X!=0) result_maze(m,n);                      //当X不为0时,有解,调用输出路径函数
          printf("\n\nPress Enter Contiue!\n");
          getchar();
          while(getchar()!='\n');   //接受一个输入,当为回车时执行break跳出,否则一直执行接受数据
          break;
      case 2:
          printf("\n请输入行数:");
          scanf("%d",&m);
          printf("\n");
          printf("请输入列数:");
          scanf("%d",&n);
          while((m<0||m>49)||(n<0||n>49))
          {
              printf("\n抱歉,你输入的行列数超出预设范围(0-49,0-49),请重新输入:\n\n");
              printf(*"\n请输入行数:");
              scanf("%d",&m);
              printf("\n");
              printf("请输入列数:");
              scanf("%d",&n);
          }
          automatic_maze(m,n);
          data(m,n);
          print_maze(m,n);
          path(maze,m,n);
          if(X!=0) result_maze(m,n);
          printf("\n\nPress Enter Contiue!\n");
          getchar();
          while(getchar()!='\n');
          break;
      case 3:
          cycle=(-1);break;
      default:
          printf("\n");printf("你的输入有误!\n");
          printf("\nPress Enter Contiue!\n");
          getchar();
          while(getchar()!='\n');
          break;
      }
   }
}

[ 本帖最后由 jkbj 于 2014-6-16 23:30 编辑 ]
搜索更多相关主题的帖子: include  system  c语言  
2014-06-16 23:29
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
  得分:0 
什么问题?

编译错误、输出不符。。。


[fly]存在即是合理[/fly]
2014-06-17 16:17







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

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