C++题目问题
- 提问者网友:我一贱你就笑
- 2021-04-28 03:38
- 五星知识达人网友:轮獄道
- 2021-04-28 04:28
帮你写了个. 你看看.
#include <iostream>
#include <string>
using namespace std;
const N=5;
struct book
{
string title;
unsigned int copies;
unsigned int unit_price;
unsigned int sum;
};
void set(book s[]);
void desc(book s[]);
void order(book s[]);
void print(book s[]);
void set(book s[])
{
for(int i=0;i<N;i++)
{
cout<<endl;
cout<<"输入书名:";
cin>>s[i].title;
cout<<"输入册数:";
cin>>s[i].copies;
cout<<"输入单价:";
cin>>s[i].unit_price;
s[i].sum = s[i].unit_price * s[i].copies;
}
}
void desc(book s[])
{
book t;
int k;
for(int i=0;i<N-1;i++)
{
for(int j=i+1;j<N;j++)
{
if(s[i].copies<s[j].copies)
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
cout<<"\n按册数降序排列"<<endl;
print(s);
}
void order(book s[])
{
book t;
for(int i=0,j=N-1;i!=j;i++,j--)
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
cout<<"\n按册数升序序排列"<<endl;
print(s);
}
void print(book s[])
{
cout<<"书名\t册数\t单价\t金额"<<endl;
for(int i=0;i<5;i++)
{
cout<<s[i].title<<'\t'<<s[i].copies<<'\t'
<<s[i].unit_price<<'\t'<<s[i].sum<<endl;
}
}
int main()
{
book a[5];
set(a);
desc(a);
order(a);
return 0;
}