码迷,mamicode.com
首页 > 其他好文 > 详细

算价格01

时间:2016-05-30 23:28:45      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:

题目:

输入购买图书的数量,根据购买图书的数量,有不同的优惠,计算出购买这些数量的图书需要花多少钱?

设计思想:

    定义变量number为购买的图书数量,将number除以10取余数,用switch语句根据余数选择10以内的图书的价格price_part,再根据(number-yushu)/ 10 + price_part 计算总价格。

程序源代码如下:

#include<iostream>
#include <windows.h>
using namespace std;

void main()
{
    int number=0;
    double price=0;              //总的图书最低价格
    double price_part=0;     //当图书数目除以10后,余数数量的图书所需要的价格
    int yushu=0;   

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED);
//这句话是设置字体颜色红色 cout
<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl; cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN);
//这句话是设置字体颜色绿色 cout
<<"Please input the number of book:"; cin>>number; cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED); cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl; yushu=number%10; switch(yushu) { case 0: price_part=0; break; case 1: price_part=8; break; case 2: price_part=15.2; break; case 3: price_part=21.6; break; case 4: price_part=25.6; break; case 5: price_part=30; break; case 6: price_part=43.2; break; case 7: price_part=47.2; break; case 8: price_part=51.2; break; case 9: price_part=55.6; break; } cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_BLUE); cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl; price= ( number - yushu ) / 10 * 60 + price_part; cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN); cout<<""<<number<<"本书的最低价格是 :"<<price<<" !"<<endl; cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_BLUE); cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl; cout<<endl; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
//又把字体换成白色 }

运行结果:

技术分享

技术分享

 

个人总结:

今天的算法经过老师的提示实现起来比较简单,因此百度了一下怎么改变字体的颜色。做了最简单的加工。

算价格01

标签:

原文地址:http://www.cnblogs.com/WS1004/p/5544081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!