gpt4 book ai didi

c - 未执行排序

转载 作者:太空宇宙 更新时间:2023-11-04 01:15:09 25 4
gpt4 key购买 nike

这个程序是一个通过软件工作的图书馆。问题是当我按价格对书籍进行排序并打印它们时,它们永远不会被排序!

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
static int count;
struct book
{
int bookid;
char name[30];
char author[30];
float price;
};
struct book b[40];
void add(void);
void del(void);
void sort(void);
void price(void);
void print(void);
void main(void)
{
char choice;
while(1)
{
clrscr();
printf("Enter a choice:\n 1.Add a book.\n 2.Delete a book.\n 3.Sort books by price.\n 4.To print all books details.\n 5.To print the names of the books whose price is less than 1000.\n 6.Exit\n");
choice=getche();//doing by getch() as getche makes the program rough as it is printed
switch(choice)
{
case'1':add();break;
case'2':del();break;
case'3':sort();break;
case'4':print();break;
case'5':price();break;
case'6':exit(0);
default:printf("Enter a valid choice.");break;
}
}/*switch ends*/
}
void add(void)
{
int i;
char ch[30];
clrscr();
for(i=count;i<40;i++)
{
printf("Enter books name:\n");
gets(b[i].name);
printf("Enter author's name\n");
gets(b[i].author);
printf("Enter price:\n");
gets(ch);
b[i].price=atoi(ch);
b[i].bookid=count;
break;
} /* for ends*/
count++;
printf("Dear User,the book has succesfully been added.The book id is %d",b[i].bookid);


getch();
}
void print(void)
{
int i;
clrscr();
for(i=0;i<count;i++)
{
printf("Bookid=%d,Name=%s,Author=%s,Price=%f\n",b[i].bookid,b[i].name,b[i].author,b[i].price);

}
getch();
}

void del(void)
{
int i,j;
char ch[10];
clrscr();
printf("Enter book id:");
gets(ch); // how do i put it into the structure as i dont know that which structure it belongs to
for(i=0;i<count;i++) //searching
{
if(b[i].bookid==atoi(ch))
{
for(j=i;j<count;j++)
{

b[j]=b[j+1];
}//for j ends
} //if ends
} /* for of i ends */
count--;
// sort();
getch();
}
//void del(void)
//{

// int i;
// char ch[10];
// clrscr();
//printf("Enter book id:");
// gets(ch);
// for(i=0;i<40;i++)
// {
// b[i]=b[i+1];
//
// }
// count--;
// printf("Dear user,delete succesful");
//getch();
//}
void sort(void)
{
int in,out;
struct book temp;
for(out=0;out<count-1;out++)
{
for(in=out+1;out<count;out++)
{
if(b[out].price>b[in].price)
{
temp=b[out]
b[out]=b[in];
b[out]=temp;
}
}/*for out ends*/
}//for in ends
printf("Dear user,the books are sorted by price.\n");

getch();
}

void price(void)
{
int i;
clrscr();
for(i=0;i<count;i++)
{
if(b[i].price<1000)
{
printf("%d.%s\n",i+1,b[i].name);
}
}
getch();

最佳答案

void sort(void)
{
int in,out;
struct book temp;
for(out=0;out<count-1;out++)
{
//**(in=out+1;out<count;out++)**//look here please
for(in=out+1;in<count;in++)
{
if(b[out].price>b[in].price)
{
temp=b[out]
b[out]=b[in];
b[in]=temp;//**b[out]=temp;**//look here plz
}
}/*for out ends*/
}//for in ends
printf("Dear user,the books are sorted by price.\n");

getch();
}

关于c - 未执行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3218232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com