| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1604 人关注过本帖
标题:真的绝望,为了这道题用了5 6小时,主要两个问题,在以下我会表达清楚(字数 ...
只看楼主 加入收藏
q18371528148
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2018-6-21
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:7 
真的绝望,为了这道题用了5 6小时,主要两个问题,在以下我会表达清楚(字数限制。。。)
问题一:为什么我输出的数据后面有,?...  我在贴吧问了好像是%s的问题
问题二:怎么做对啊,具体点,学结构到现在只敲对一个
图片附件: 游客没有浏览图片的权限,请 登录注册

程序代码:
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct stud)
#define NUll 0
struct stud
{long num;
char name[20];
char sex;
int old;
struct stud *next;
};
int n;
struct stud *creat(void)
{
    struct stud *head,*p1,*p2;
    n=0;
    p2=p1=(struct stud*)malloc(LEN);
    printf("输入:");
    scanf("%ld,%s,%c,%d",&p1->num,&p1->name,&p1->sex,&p1->old);
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else 
            p2->next=p1;
        p2=p1;
        p1=(struct stud*)malloc(LEN);
        printf("Input:");
        scanf("%ld,%s,%c,%d",&p1->num,&p1->name,&p1->sex,&p1->old);
    }
    p2->next=NULL;
    free(p1);
    return(head);
}
void print_list(struct stud *head)
{
    struct stud* p;
    printf("\nNow ,These %d records are:\n",n);
    p=head;
    if(head!=NUll)
    {
        while(p!=NULL)
        {printf("%ld,%s,%c,%d\n",p->num,p->name,p->sex,p->old);
        p=p->next;
        }
        
    }

}
struct stud *del( struct stud *head,int num )
{  struct stud  *p1, *p2;
    if (head==NULL) 
    {    printf("\n list is null!\n");   return head;     }  
    p1=head;
    while (num!=p1->old&&p1->next!=NULL)
    {  p2=p1;      p1=p1->next;   }
    if  ( num==p1->old )
    {  if (p1==head) 
        head=p1->next;
        else
        p2->next=p1->next;
        printf("delete:%d\n", num);
    }
    else 
        printf("%d not been found!\n",num);

    return head;
}

void main()
{
    struct stud *head;
    int del_num=0;
    printf("input records:\n");
    head=creat();
    print_list(head);
    printf("\nInput the deleted number:");
    scanf("%d",&del_num);
    head=del(head,del_num);
    print_list(head);
}
搜索更多相关主题的帖子: struct num next head printf 
2018-06-21 18:09
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:709
专家分:2063
注 册:2010-11-11
收藏
得分:0 
先解答你的第一个问题:scanf("%ld,%s,%c,%d",&p1->num,&p1->name,&p1->sex,&p1->old);    这一句中的第3个实参你用的是&p1->name,实际上是不需要&符号的。
2018-06-21 20:08
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:709
专家分:2063
注 册:2010-11-11
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <malloc.h>

#define LEN sizeof(struct stud)
#define NUll 0

struct stud
{
    long num;
    char name[20];
    char sex;
    int old;
    struct stud *next;
};

int n;

struct stud * creat(void)
{
    struct stud * head = NULL;
    struct stud * p1 = NULL;
    struct stud * p2 = NULL;

    n = 0;
    p2 = p1 = (struct stud *)malloc(LEN);
    printf("输入:");
    scanf("%ld %s %c %d", &p1->num, p1->name, &p1->sex, &p1->old);

    while (p1->num != 0)
    {
        n++;
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        p1 = (struct stud *)malloc(LEN);
        printf("Input:");
        scanf("%ld %s %c %d", &p1->num, p1->name, &p1->sex, &p1->old);
    }
    p2->next = NULL;
    free(p1);
    return (head);
}

void print_list(struct stud * head)
{
    struct stud * p = NULL;

    printf("\nNow ,These %d records are:\n", n);
    p = head;
    if (head != NULL)
    {
        while (p != NULL)
        {
            printf("%ld,%s,%c,%d\n", p->num, p->name, p->sex, p->old);
            p = p->next;
        }
    }
}

struct stud * del(struct stud * head, int num)
{
    struct stud * p1 = NULL;
    struct stud * p2 = NULL;

    if (head == NULL)
    {
        printf("\n list is null!\n");
        return head;
    }
   

    p1 = head;
    while (num != p1->old && p1->next != NULL)
    {
        p2 = p1;
        p1 = p1->next;
    }
    if (num == p1->old)
    {
        if (p1 == head)
            head = p1->next;
        else
            p2->next = p1->next;
        printf("delete:%d\n", num);
    }
    else
        printf("%d not been found!\n", num);

    return head;
}

void main()
{
    struct stud * head = NULL;
    int del_num = 0;

    printf("input records:\n");
    head = creat();

    print_list(head);

    printf("\nInput the deleted number:");
    scanf("%d", &del_num);
    head = del(head, del_num);
    print_list(head);
}
2018-06-21 20:20
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:709
专家分:2063
注 册:2010-11-11
收藏
得分:10 
你自己对比以下代码的不同。主要是有两个地方:1、scanf函数读取字符串时,不需要再用&符号对字符数组取地址。2、在你的用法中,scanf函数不能用','符号作为输入的分割符号,应该用' '空格或空白。如果确实要用','建议用getchar函数。
2018-06-21 20:23
书生牛犊
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:星夜征程
等 级:贵宾
威 望:10
帖 子:1101
专家分:5265
注 册:2015-10-27
收藏
得分:0 
补充说明:
1.scanf(%s)的时候,用不用&符号对字符数组取地址是没有区别的。测试参考代码如下。你可以自己多写几个字符串数组试试看
    char b[8];
    printf("%d----%d",b,&b);//一般输出地址会用%p,16位十六进制的数.鉴于你是新手,我用%d输出你方便理解 

2.scanf(%s)判断某个字符串是否已经完全读完的时候,大小写字母和数字、标点符号都算是有效的字符串一部分。只有遇到回车换行制表符空格之类的空白符才会停止,并在该位上写"\0"标示字符串结尾。{这你们老师总讲过吧。。。}

φ(゜▽゜*)♪
2018-06-21 23:19
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:10 
%s,
是造孽
这个其实算是 格式控制字符串的bug
%s 后面不能加非空白字符
因为非空白字符都会被%s处理
跳出%s第一个字符必然是空白字符(空格回车等)
而空白字符又无法和逗号匹配
导致scanf停止匹配数据
导致%s, 后面的参数无法完成输入
GG

https://zh.
2018-06-22 01:01
q18371528148
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2018-6-21
收藏
得分:0 
谢谢大佬们,现在大概弄了
以下是我自己的看法(错的地方请指出一下2
这是原题:建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去
我弄的正确代码
程序代码:
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct stud)
#define NUll 0
struct stud
{long num;
char name[20];
char sex;
int old;
struct stud *next;
};
int n;
struct stud *creat(void)
{
    struct stud *head,*p1,*p2;
    char a;  //用来消除%s的缓冲区
    n=0;
    p2=p1=(struct stud*)malloc(LEN);
    printf("输入:");
    scanf("%ld%s%c%c%d",&p1->num,p1->name,&a,&p1->sex,&p1->old);
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else 
            p2->next=p1;
        p2=p1;
        p1=(struct stud*)malloc(LEN);
        printf("Input:");
        scanf("%ld%s%c%c%d",&p1->num,p1->name,&a,&p1->sex,&p1->old);
    }
    p2->next=NULL;
    free(p1);
    return(head);
}
void print_list(struct stud *head)
{
    struct stud* p;
    printf("\nNow ,These %d records are:\n",n);
    p=head;
    if(head!=NUll)
    {
        while(p!=NULL)
        {printf("%ld,%s,%c,%d\n",p->num,p->name,p->sex,p->old);
        p=p->next;
        }
        
    }

}
struct stud *del( struct stud *head,int num )
{  struct stud  *p1, *p2;
    if (head==NULL) 
    {    printf("\n list is null!\n");   return head;     }  
    p1=head;
    while (num!=p1->old&&p1->next!=NULL)
    {  p2=p1;      p1=p1->next;   }
    if  ( num==p1->old )
    {  if (p1==head) 
        head=p1->next;
        else
        p2->next=p1->next;
        printf("delete:%d\n", num);
    }
    else 
        printf("%d not been found!\n",num);

    return head;
}

void main()
{
    struct stud *head;
    int del_num=0;
    printf("input records:\n");
    head=creat();
    print_list(head);
    printf("\nInput the deleted number:");
    scanf("%d",&del_num);
    head=del(head,del_num);
    print_list(head);
}

可以看出解决%s的问题是利用了char a来消除%s的缓冲区
原理如图
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册

大佬们有其他想法%s这个问题的可以发表一下在此感谢以上大佬们帮助,也欢迎大家发表意见
2018-06-22 11:26
q18371528148
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2018-6-21
收藏
得分:0 
回复 4楼 八画小子
谢谢大佬,我试了可以
2018-06-22 11:34
快速回复:真的绝望,为了这道题用了5 6小时,主要两个问题,在以下我会表达清楚 ...
数据加载中...
 
   



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

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