标题:求大佬帮忙看一下 总是在a[]这个位置说a[i]里的i要是指针形式
只看楼主
q1822622
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-1-3
结帖率:0
已结贴  问题点数:20 回复次数:1 
求大佬帮忙看一下 总是在a[]这个位置说a[i]里的i要是指针形式
#include<stdio.h>
#include<string.h>
struct fruit
{
    int id;
    char name[20];
    int height;
    float weight;
}a[]={{001,"Apple",67,58},{002,"Banana”",75,70.5},{003,"Peach",65,57.5},{004,"Pineapple",60,55},{005,"Grape",80,76.5},{006,"Kiwi",78,68.9}};fruit temp;
main()
{
    int i,j,a;
    struct fruit *j    ;
    for(i=0;i<6;i++)
    {
        for(j=0;j<6-i-1;j++)
        {
            if(strlen(a[j].name<strlen(a[j+1].name)))
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
            else if(strlen(a[j].name)<strlen(a[j+1].name)&&a[j].weight<a[j+1].weight)
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
    for(a=1;a<=6;a++)
    {
        printf("a[a]");
    }
}
搜索更多相关主题的帖子: temp 形式 strlen name 指针 
2020-01-03 15:41
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
#include <stdio.h>
#include <string.h>

struct fruit
{
    int id;
    char name[20];
    int height;
    float weight;
};

int main( void )
{
    struct fruit a[] = { {001,"Apple",67,58.f},{002,"Banana",75,70.5f},{003,"Peach",65,57.5f},{004,"Pineapple",60,55.f},{005,"Grape",80,76.5f},{006,"Kiwi",78,68.9f} };

    const size_t n = sizeof(a)/sizeof(*a);
    for( size_t i=0;i!=n; ++i )
    {
        for( size_t j=0; j!=n-i-1; ++j )
        {
            if( strlen(a[j].name)<strlen(a[j+1].name) || (strlen(a[j].name)==strlen(a[j+1].name) && a[j].weight<a[j+1].weight) ) // 你的源代码看不懂。竟然根据“名字长度”来排序?!!!
            {
                struct fruit temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }
    for( size_t i=0;i!=n; ++i )
    {
        printf( "%d, %s, %d, %f\n", a[i].id, a[i].name, a[i].height, a[i].weight );
    }
}
2020-01-03 17:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-498793-1-1.html




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

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