永发信息网

C语言编程 找书

答案:2  悬赏:60  手机版
解决时间 2021-01-15 05:21
C语言编程 找书
最佳答案
#include
#include
int main(){    
    long L,k,i,j=-1,weizhi=0,temp,m,t;
    printf("请输入书籍数量L和要找的书的索书号k(L>0,k<100000,以逗号隔开):");
    scanf("%ld,%ld",&L,&k);
    char ai[L*k];
    printf("请输入%ld个无重复的整数作为每本书的索书号ai(0    scanf("%s",&ai);
    long ailen=strlen(ai);
    ai[ailen]=',';    
    for(i=0;i<=ailen;i++){
        if(','==ai[i]){
            temp=0;t=1;
            weizhi++;
            for(m=i-1;m>j;m--,t*=10){
                temp+=(ai[m]-'0')*t;}            
            if(temp==k){
                printf("小华要找的书%ld的书位编号是:%ld
",k,weizhi);
                break;}
            j=i;}}
    if(temp!=k){           
        printf("Error
");}
    return 0; }恩,有点复杂,不过你要是把代码搞懂,就代表你可以进入C的高级编程了。
全部回答

没有编译调试
typedef struct SHELF {
    int size;
    int count;
    int id_list[0];
} Shelf, *ShelfPtr;

int 
Shelf_create(Shelf **shelf, int size=100) {
    size_t len = sizeof(Shelf) + sizeof(int) * size;
    *shelf = (Shelf*)malloc(len);
    if (*shelf <= 0) {
        return -1;
    }
    memset(*shelf, 0x00, len);
    return 0;
}

int 
Shelf_destroy(Shelf **shelf) {
    if (*shelf > 0) {
        free(*shelf);
        *shelf = NULL;
    }
    return 0;
}

int 
Shelf_load(Shelf *shelf) {
    int count = 0;
    sscanf("input the count of books: ", &count);
    if (count <= 0 || count > shelf->size) {
        return -1;
    }
    shelf->count = count;
    char buff[1024];
    buff[0] = 0x00;
    sscanf("input IDs of all the books:", buff);
    char *from = *to = buff;
    for (int i = 0; i < count; i ++) {
        while ((*from < '0' || *from > '9') && *from != 0x00) {
            from ++;
        }
        if (*from == 0x00) {
            return -1;
        }
        while (*to >= '0' && *to <= '9') {
            to ++;
        }
        *to = 0x00;
        self->id_list[i] = atoi(from);
    }
    return 0;
}

int 
Shelf_search(Shelf* shelf, int id, int *pos) {
    *pos = search(shelf->id_list, 0, shelf->count, id);
    return 0;
}

int search(int* list, int from, int to, int value) {
    if (from == to) {
        return -1;
    }
    int mid_pos = (from + to) / 2;
    int mid_value = list[mid_pos];
    if (mid_value == value) {
        return mid_pos;
    } else if (mid_value < value) {
        return search(list, from, mid_pos, value);
    } else {
        return search(list, mid_pos, to, value);
    }
    return 0;
}
int main() {
    Shelf *shelf = NULL;
    Shelf_create(&shelf, 1024);
    Shelf_load(shelf);
    int id = -1;
    int pos = -1;
    scanf("input the id to search:", &id);
    Shelf_search(shelf, id, *pos);
    if (pos >= 0) {
        printf("the position of book[%d] is %d ", id, pos);
    } else {
        printf("the book[%d] is not found ", id);
    }
    Shelf_destroy(&shelf);
    return 0;
}追问你这个好难啊看不懂
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
普京为什么容忍车臣逐渐变成国中之国
我抓了条1斤的野生黄鳝,请问它大概活了多少年
渔家住宿什么价格,火车站怎么去渔家
单选题苏联赫鲁晓夫改革失败的最主要原因是A.
学习是基础,怎么做才是关键
算命婚姻怎么说
求典范英语7的《不走运的怪物》和《臭街》的
图为四个国家的出生率和死亡率,读图完成下列
求超次元live2d普兰资源
泰迪打疫苗可以第一针和第二针打五联第三针打
文玩手串实体商铺现好做吗
剥香蕉应该从哪边剥才正确?
暮字的意思是什么
什么地方可买花泥
如何理解“艺术化的人生”
推荐资讯
安徽木地板哪个品牌好?
单选题Lucycanwritealetter________Japanes
延津县新乡太阳神推拿按摩足疗怎么去啊,谁知
干一年光一年,年年白干,
谁能叫我如何能把心从一个地方转移到其他的地
中国营养师培训教材
关于歌颂彬县的作文
小贝壳地址在什么地方,想过去办事,
赵将括母 文言文翻译
如果是真正的热爱为什么要用坚持这个字眼
三角函数 角的集合没听懂 【a丨a=360°×k+60
英人在华的残暴行动 读后感800字
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?