```#include <iostream> #include <iomanip> using namespace std; int main() { double price; cin >> price; double cost = 0.0; // write your code here....... if(price>=100&&price<500) cost=price*0.9; else if(price>=500&&price<2000) cost=price*0.8; else if(price>=2000&&price<5000) cost=price*0.7; else if(price>=5000) cost=price*0.6; cout << setiosflags(ios::fixed) << setprecision(1) << cost << endl; return 0; }
时间: 2024-04-22 18:24:56 浏览: 41
这段代码是一个简单的计算商品价格折扣的程序。根据输入的价格,计算出相应的折扣价格并输出。
在代码中,`price` 表示商品的价格,`cost` 表示折扣后的价格。
首先,程序会通过 `cin` 语句从用户输入中获取价格。
然后,通过一系列的 `if` 语句判断价格的范围,并根据不同的范围计算出相应的折扣价格。具体来说:
- 如果价格在 100 到 500 之间(包含100和500),折扣为原价的 90%;
- 如果价格在 500 到 2000 之间(包含500和2000),折扣为原价的 80%;
- 如果价格在 2000 到 5000 之间(包含2000和5000),折扣为原价的 70%;
- 如果价格大于等于 5000,折扣为原价的 60%。
最后,通过 `cout` 语句输出折扣后的价格,并使用 `setiosflags(ios::fixed)` 和 `setprecision(1)` 来设置输出格式,保留小数点后一位。
希望对你有帮助!如果你还有其他问题,请随时提问。
相关问题
设计书类Book,成员函数包括作者(char author[40]),书名(char title[40]),价格(double price)。根据给定的main函数设计必要的成员函数。 main函数已给定,提交时只需要提交main函数外的代码部分。 #include<iostream> #include<cstring> #include<iomanip> using namespace std; //你提交的代码在这里 int main() { cout<<fixed<<setprecision(2); Book b1; cout<<"Show object b1:"; cout<<b1.GetTitle()<<","<<b1.GetAuthor()<<","<<b1.GetPrice()<<endl; char title[40],author[40]; double price; cin>>title>>author>>price; Book b2(title,author,price); cout<<"Show object b2:"; cout<<b2.GetTitle()<<","<<b2.GetAuthor()<<","<<b2.GetPrice()<<endl; cin>>title>>author>>price; cout<<"Reset and Show object b1:"; b1.Set(title,author,price); b1.Show(); cout<<"Reset and Show object b2:"; b2.Set(); b2.Show(); return 0; } 输入 2行,每行3个数据,分别代表一本书的书名、作者以及价格。 输出 按照样例输出格式输出。 输入样例 1 C++程序设计 郑莉 45.12 c++程序设计基础 王育坚 68.5 输出样例 1 Show object b1:NULL,NONE,0.00 Show object b2:C++程序设计,郑莉,45.12 Reset and Show object b1:c++程序设计基础,王育坚,68.50 Reset and Show object b2:高等数学(第七版)上册,同济大学数学系,37.70
```
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class Book {
private:
char author[40];
char title[40];
double price;
public:
Book() {
strcpy(this->author, "NONE");
strcpy(this->title, "NULL");
this->price = 0.00;
}
Book(char title[], char author[], double price) {
strcpy(this->author, author);
strcpy(this->title, title);
this->price = price;
}
char* GetAuthor() { return this->author; }
char* GetTitle() { return this->title; }
double GetPrice() { return this->price; }
void Set(char title[], char author[], double price) {
strcpy(this->author, author);
strcpy(this->title, title);
this->price = price;
}
void Set() {
char title[40], author[40];
double price;
cin >> title >> author >> price;
Set(title, author, price);
}
void Show() {
cout << this->title << "," << this->author << "," << fixed << setprecision(2) << this->price << endl;
}
};
int main() {
cout << fixed << setprecision(2);
Book b1;
cout << "Show object b1:";
cout << b1.GetTitle() << "," << b1.GetAuthor() << "," << b1.GetPrice() << endl;
char title[40], author[40];
double price;
cin >> title >> author >> price;
Book b2(title, author, price);
cout << "Show object b2:";
cout << b2.GetTitle() << "," << b2.GetAuthor() << "," << b2.GetPrice() << endl;
cin >> title >> author >> price;
cout << "Reset and Show object b1:";
b1.Set(title, author, price);
b1.Show();
cout << "Reset and Show object b2:";
b2.Set();
b2.Show();
return 0;
}
```
#include <iostream> using namespace std; #include <iomanip> struct shuji { char name[31]; double price; }books[10]; int main() { int n; cin>>n; getchar(); int i; double a[10]; for(i=0;i<n;i++) { cin.getline(books[i].name ,32); cin>>books[i].price ; getchar(); a[i]=books[i].price; } double max=a[0],min=a[0]; int flag1=0,flag2=0; for(i=1;i<n;i++) { if(a[i]>max) { max=a[i]; flag1=i; } else if(a[i]<min) { min=a[i]; flag2=i; } } cout<<setiosflags(ios::fixed)<<setprecision(2)<<max<<", " <<books[flag1].name <<endl; cout<<setiosflags(ios::fixed)<<setprecision(2)<<min<<", " <<books[flag2].name ; return 0; }实验报告
实验目的:
通过本次实验,我们将学习如何使用结构体和数组来存储一组数据,并进行排序和查找操作。
实验要求:
编写一个程序,输入n个书籍的名称和价格,然后输出其中价格最高和价格最低的书籍名称及价格。
实验步骤:
1. 定义结构体shuji,包括书籍名称和价格两个成员变量。
2. 在main函数中,定义一个长度为10的shuji型数组books,用来存储n个书籍的信息。
3. 输入n的值,并使用循环输入每个书籍的名称和价格,并将价格存储在一个double型数组a中。
4. 使用循环遍历数组a,找出其中的最高和最低价格,并记录它们的下标。
5. 输出最高价格对应的书籍名称和价格,以及最低价格对应的书籍名称和价格。
6. 在输出时需要设置输出格式,保留两位小数。
实验代码:
```cpp
#include <iostream>
using namespace std;
#include <iomanip>
struct shuji {
char name[31];
double price;
}books[10];
int main() {
int n;
cin>>n;
getchar();
int i;
double a[10];
for(i=0;i<n;i++) {
cin.getline(books[i].name, 32);
cin>>books[i].price;
getchar();
a[i]=books[i].price;
}
double max=a[0],min=a[0];
int flag1=0,flag2=0;
for(i=1;i<n;i++) {
if(a[i]>max) {
max=a[i];
flag1=i;
}
else if(a[i]<min) {
min=a[i];
flag2=i;
}
}
cout<<setiosflags(ios::fixed)<<setprecision(2)<<max<<", " <<books[flag1].name <<endl;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<min<<", " <<books[flag2].name ;
return 0;
}
```
实验结果:
假设输入的n为4,书籍名称和价格分别为:
```
C++ Primer Plus, 99.9
Effective C++, 89.8
Thinking in Java, 120.5
Head First Design Patterns, 78.6
```
则程序输出结果为:
```
120.50, Thinking in Java
78.60, Head First Design Patterns
```
实验总结:
本次实验通过使用结构体和数组来存储一组数据,并进行排序和查找操作,加深了我们对于结构体和数组的理解和应用。同时,我们也学会了如何使用setiosflags和setprecision设置输出格式,使输出结果更加美观。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)