| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 878 人关注过本帖
标题:显示链表中每一个结点数值信息的程序,不知道哪儿出了问题,帮忙看一下
只看楼主 加入收藏
青蝶
Rank: 2
等 级:论坛游民
帖 子:160
专家分:51
注 册:2018-2-4
结帖率:92%
收藏
已结贴  问题点数:20 回复次数:4 
显示链表中每一个结点数值信息的程序,不知道哪儿出了问题,帮忙看一下
#include<stdlib.h>
#include<stdio.h>
#define N 100

typedef enum{carre, rond, triangle} forme;         //定义枚举类型forme//

void afficherForme(forme f){
  switch(f){
    case carre : printf("carré");break;
    case rond : printf("rond");break;
    case triangle : printf("triangle");break;
    default : printf("forme inconnue");
  }
}

typedef struct noeud{              //定义结点,每个结点包含一个forme类型的变量f
  forme f;                                        和下一个内存块的首地址//
  struct noeud *suivant;          //suivant用来记录下一个内存块的首地址//
} *ListeDeFormes;

ListeDeFormes lf;

int longueur(ListeDeFormes l){      //返回值为结点的个数//
  int i=1;
  while(l->suivant!=NULL){
    l=l->suivant;
    i++;
  }
  return i;
}


forme ieme(ListeDeFormes l, int r){      //返回第r个结点中,成员f的值//
  int i=1;
  while(i<=longueur(l)){
    if(i==r) return l->f;
    l=l->suivant;
    i++;
  }
}

int main(void){
  char s[N];
  lf=s;
  lf->f=rond;
  lf->suivant=lf+1;
  ListeDeFormes s1=lf->suivant;
  s1->f=triangle;
  s1->suivant=s1+1;
  ListeDeFormes s2=s1->suivant;
  s2->f=carre;
  s2->suivant=NULL;     //定义三个结点,成员f依次为 rond, triangle, carre//

  int i;
  printf("Le contenu de lf est :\n");
  for(i=1;i<=longueur(lf);i++) {
     afficherForme(ieme(lf,i));      //依次显示三个结点的数值信息f//
     printf("\n");
  }
     return 0;
}


运行结果:
Le contenu de lf est :
rond
triangle
rond

问题:定义的三个结点成员f的值依次为rond, triangle, carre,为什么显示出来成了rond,triangle,rond?
搜索更多相关主题的帖子: 显示 定义 printf int 结点 
2018-02-06 16:06
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10527
专家分:42899
注 册:2014-5-20
收藏
得分:10 
#include<stdlib.h>
#include<stdio.h>
#define N 100

typedef enum {carre, rond, triangle} forme;        //定义枚举类型forme//

void afficherForme(forme f)
{
    switch(f)
    {
        case carre :
            printf("carre");
            break;
        case rond :
            printf("rond");
            break;
        case triangle :
            printf("triangle");
            break;
        default :
            printf("forme inconnue");
    }
}

typedef struct noeud               //定义结点,每个结点包含一个forme类型的变量f
{
    forme f;
    //和下一个内存块的首地址//
    struct noeud *suivant;          //suivant用来记录下一个内存块的首地址//
} *ListeDeFormes;

ListeDeFormes lf;

int main(void)
{
    //char s[N];
    struct noeud s[N];
    lf=s;
    lf->f=rond;
    lf->suivant=lf+1;
    ListeDeFormes s1=lf->suivant;
    s1->f=triangle;
    s1->suivant=s1+1;
    ListeDeFormes s2=s1->suivant;
    s2->f=carre;
    s2->suivant=NULL;     //定义三个结点,成员f依次为 rond, triangle, carre//

       printf("Le contenu de lf est :\n");
    ListeDeFormes p=lf;
    for (; p; p=p->suivant)
    {
        afficherForme(p->f);
        printf("\n");
    }
    printf("\n");
    return 0;
}
2018-02-06 19:42
青蝶
Rank: 2
等 级:论坛游民
帖 子:160
专家分:51
注 册:2018-2-4
收藏
得分:0 
回复 2楼 吹水佬
谢谢大佬,想问下,用那两个函数写出来的程序哪儿出错了?
2018-02-06 22:45
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10527
专家分:42899
注 册:2014-5-20
收藏
得分:10 
以下是引用青蝶在2018-2-6 22:45:54的发言:

谢谢大佬,想问下,用那两个函数写出来的程序哪儿出错了?

ieme()有问题:
forme ieme(ListeDeFormes l, int r){      //返回第r个结点中,成员f的值//
  int i=1;
  while(i<=longueur(l)){  //这个l不是一个定数,在循环体内变动
    if(i==r) return l->f;
    l=l->suivant;    //l变动了
    i++;
  }
}
可以这样:
forme ieme(ListeDeFormes l, int r){      //返回第r个结点中,成员f的值//
  int i=1,count;
  count = longueur(l);
  while(i<=count){
    if(i==r) return l->f;
    l=l->suivant;
    i++;
  }
}
还有个问题:当 if(i==r) 都不会成立时函数返回什么?

2018-02-07 05:55
青蝶
Rank: 2
等 级:论坛游民
帖 子:160
专家分:51
注 册:2018-2-4
收藏
得分:0 
回复 4楼 吹水佬
我找了好久找不到错误,感谢大佬~题目里说这个函数使用的时候,有个前提条件是1<=r<=longueur(l),我忘了说明
2018-02-07 11:47
快速回复:显示链表中每一个结点数值信息的程序,不知道哪儿出了问题,帮忙看一下 ...
数据加载中...
 
   



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

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