我该怎么办? [英] How do I do this problem?
问题描述
我们的区中央图书馆需要单独申请,以维护书籍的细节。现在,他们要求您使用Structure为库编写C程序,包括四个字段的详细信息,即标题,作者,页面和价格。
[如果输入指定零或小于此,显示库中没有书籍]
使用以下结构
Our District Central Library is in need of an application separately to maintain the details of books. Now they ask you to write a C program for the Library using Structure including the details of four fields namely title, author, pages and price.
[If the input specifies zero or less than that, display "No Books in the library"]
Use the following structure
struct book
{
char title[50];
char author[50];
int pages;
float price;
};
注意:以最高价格显示图书/书籍。获取图书馆中的书籍数量,然后单独获取每本书的详细信息。
测试用例
输入1
输入图书馆中的图书数量
2
输入书籍1的详细信息
输入书名
Python
输入书籍作者
ZED A.SHAW
输入书中的页数
725
输入书籍价格
500
输入第2册的详细信息
输入书名
Selenium测试工具
输入图书作者
Unmesh
输入书中的页数
1287
输入预定价格
980
输出1
最高价格书籍的详细信息
书名:Selenium测试工具
作者:Unmesh
页数:1287
价格:980.00
输入2
输入账簿数量图书馆
0
输出2
图书馆没有书籍
我的尝试:
Note : Display the book/books with the maximum price. Get the number of books in the library and then get details of each book individually.
Test Case
Input 1
Enter number of books in the library
2
Enter details for book 1
Enter book name
Python
Enter book author
ZED A.SHAW
Enter number of pages in the book
725
Enter book price
500
Enter details for book 2
Enter book name
Selenium Testing Tools
Enter book author
Unmesh
Enter number of pages in the book
1287
Enter book price
980
Output 1
Details of the book that has maximum price
Book name:Selenium Testing Tools
Author:Unmesh
Number of pages:1287
Price:980.00
Input 2
Enter number of books in the library
0
Output 2
No Books in the library
What I have tried:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct book
{
char title[100];
char author[100];
int pages;
float price;
}b[50];
void main()
{
int n,i=0,max=0,j=0,k=0,max2=0,j2,x1,y1,x2,y2,t;
printf("Enter number of books in the library\n");
scanf("%d",&n);
t=n;
if(n>0)
{
do
{
printf("Enter details for book %d\n",i+1);
printf("Enter book name\n");
getchar();
fgets(b[i].title,50,stdin);
printf("Enter book author\n");
fgets(b[i].author,50,stdin);
printf("Enter number of pages in the book\n");
scanf("%d",&b[i].pages);
printf("Enter book price\n");
scanf("%f",&b[i].price);
if(max<b[i].price)
{
x1=strlen(b[i].author);
y1=strlen(b[i].title);
//printf("%d\n%d\n",x1,y1);
max=b[i].price;
j=i;
}
else if(max==b[i].price)
{
x2=strlen(b[i].author);
y2=strlen(b[i].title);
max2=b[i].price;
j2=i;
}
i++;
n--;
}while(n!=0);
if(max2==0)
{
printf("Details of the book that has maximum price\n");
printf("Book name:%s",b[1].title);
printf("Author:%s",b[1].author);
printf("Number of pages:%d\n",b[1].pages);
printf("Price:%0.2f\n",b[1].price);
}
}
else
{
printf("No Books in the library");
}
}
注意: - 它适用于2输入,但如何比较3输入
Note:- It working fine for 2 input but how to compare 3 inputs
推荐答案
您坐下来,将您的作业问题视为规范。仔细阅读,并设计一个基于它的系统。根据问题检查您的设计,确保一切顺利进行。然后你可以考虑开始在代码中实现设计。
跳入代码做简单的位,然后发现你不知道怎么做休息没有帮助:让你围绕整个项目并首先进行设计。这是很多,从长远来看更快,并产生更好的结果。
但这是你的功课:设计是你的任务的一部分 - 所以我们不会为你这样做!
如果您遇到特定问题,请询问相关问题,我们会尽力而为帮助。但我们不打算为你做这一切!
You sit down, and treat your homework question as a specification. Read it carefully, and design a system based on it. Check your design back against the question and make sure that everything works smoothly. You can then think about starting to implement the design in code.
Jumping into code to "do the easy bits" and then finding you have no idea how to do the rest doesn't help: get you head round the whole project and do a design first. It's a lot, lot quicker in the long run, and produces a better result.
But this is your homework: and the design is part of your task - so we will not be doing that for you!
If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct book
{
int pages;
float price;
char author[10];
char title[22];
};
struct book p[3];
float max=0;
int i=0,n=0;
int main()
{
char c;
printf("Enter number of books in the library\n");
scanf("%d",&n);
if(n<=0)
{
printf("No Books in the library");
return 0;
}
for(i=0;i<n;i++)
{
printf("\nEnter details for book %d",i+1);
printf("\nEnter book name\n");
fgets(p[i].title,sizeof(p[i].title),stdin);
scanf("%[^\n]s",p[i].title);
printf("\nEnter book author\n");
fgets(p[i].author,sizeof(p[i].author),stdin);
scanf("%[^\n]s",p[i].author);
printf("\nEnter number of pages in the book\n");
scanf("%d",&p[i].pages);
printf("\nEnter book price\n");
scanf("%f",&p[i].price);
}
printf("Details of the book that has maximum price\n");
max=0;
for(i=0;i<n;i++)
{
if(p[i].price>=max)
{
max=p[i].price;
}
}
for(i=0;i<n;i++)
{
if(p[i].price==max)
{
printf("Book name:%s\n",p[i].title);
printf("Author:%s\n",p[i].author);
printf("Number of pages:%d\n",p[i].pages);
printf("Price:%.2f\n",p[i].price);
}
}
}
这是解决您问题的方法。如果您遇到任何问题,请告诉我。
和平!
Here is a solution to your problem. Let me know if you face any issues.
Peace!
这篇关于我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!