shop.h
#ifndef SHOP_H #define SHOP_H class Shop { public: Shop(); int buy(int m,int n);//只用钱买到的巧克力数 int chang(int m, int n);//兑换的巧克力 int remain_coupons(int m,int n);//剩余的券 void input();//输入 void output();//输出 void choose();//购买选项 int getk();//获取k的值 int gett();//获取t的值 int getmoney();//获取money的值 int getnumber();//获取number的值 int getm();//获取m的值 private: int money;//拥有的钱 int number;//兑换一块巧克力需要的优惠券 int k;//购买到的巧克力 int t;//剩余的优惠券 int m;//巧克力的单价 int q;//只用钱买到的巧克力数 int p;//优惠券兑换的巧克力数 int n;//现在拥有的优惠券数 }; #endif // SHOP_H
shop.cpp
#include "shop.h" #include<iostream> #include<fstream> using namespace std; Shop::Shop() { money=0; number=0; k=0; t=0; m=1; q=0; p=0; n=0; } //求只用钱买到的巧克力数量,m代表你有的美元,n代表巧克力单价 int Shop::buy(int m, int n) { int k; k=m/n; return k; } //运用递归函数求优惠券兑换的巧克力 int Shop::chang(int m, int n) { if((m/n + m%n) < n){ return m/n; } else{ return (chang((m/n + m%n),n) + m/n); } } //运用递归函数求剩余的优惠券 int Shop::remain_coupons(int m, int n) { if((m/n+m%n)<n){ return (m/n+m%n); } else { return (remain_coupons((m/n+m%n),n)); } } void Shop::input() { cout<<" 请问你有多少钱:"; cin>>money; cout<<" ---------------------"<<endl; cout<<" 多少优惠券兑换一块巧克力:"; cin>>number; cout<<" ---------------------"<<endl; cout<<" 巧克力的单价:"; cin>>m; cout<<" ---------------------"<<endl; } void Shop::output() { q=buy(money,m);//只用钱买到的巧克力数 n=q;//最开始拥有的优惠券 p=chang(n,number); k=q+p; t=remain_coupons(n,number); cout<<" 你总共能买到的巧克力数量是"<<k<<endl; cout<<" 你还剩下的优惠券的数量是"<<t<<endl; cout<<" -----------------------"<<endl; cout<<" 继续进行购买请按a"<<endl; cout<<" 退出请按b"<<endl; cout<<" -----------------------"<<endl; cout<<" ";//设置后面输入选项的位置 } void Shop::choose() { cout<<endl; cout<<endl; cout<<" ____________________________________________"<<endl; cout<<" |------------------商店--------------------|"<<endl; cout<<" |------------------------------------------|"<<endl; cout<<" |---------本店购买巧克力,一美元一块-------|"<<endl; cout<<" |---------且可以用优惠券兑换巧克力---------|"<<endl; cout<<" | |"<<endl; cout<<" |请选择:----------------------------------|"<<endl; cout<<" |----a.购买巧克力--------------------------|"<<endl; cout<<" |----b.退出购买----------------------------|"<<endl; cout<<" | |"<<endl; cout<<" | 请输入相应的按键进行选择 |"<<endl; cout<<" |__________________________________________|"<<endl; } int Shop::getk() { int m; m=k; return m; } int Shop::gett() { int n; n=t; return n; } int Shop::getmoney() { int a; a=money; return a; } int Shop::getnumber() { int b; b=number; return b; } int Shop::getm() { int c; c=m; return c; }
main.cpp
#include <iostream> #include<fstream> #include "shop.h" using namespace std; int main() { Shop chocolate; chocolate.choose(); ofstream fout; char ch; //设计输入选项按键位置 cout<<endl; cout<<" "; while(cin>>ch){ switch (ch) { case 'a': chocolate.input();//调用输入函数 chocolate.output();//将结果打印在屏幕 //将内容写进文档 fout.open("E:\outfile.txt",ios::app); if(!fout){ cout << "无法打开文件!!!"<<endl; return 1; } fout <<ch<<endl; fout<<" 请问你有多少钱:"<<chocolate.getmoney()<<endl; fout<<" ---------------------"<<endl; fout<<" 多少优惠券兑换一块巧克力:"<<chocolate.getnumber()<<endl; fout<<" ---------------------"<<endl; fout<<" 巧克力单价:"<<chocolate.getm()<<endl; fout<<" ---------------------"<<endl; fout<<" 你总共能买到的巧克力数量是"<<chocolate.getk()<<endl; fout<<" 你还剩下的优惠券的数量是"<<chocolate.gett()<<endl; fout<<" -----------------------"<<endl; fout<<" 继续进行购买请按a"<<endl; fout<<" 退出请按b"<<endl; fout<<" -----------------------"<<endl; fout.close(); break; case 'b': cout<<" 购买结束,按回车退出"<<endl; cout<<" -----------------------"<<endl; cout<<" ";//设置后面输入选项的位置 fout.open("E:\outfile.txt",ios::app); if(!fout){ cout << "无法打开文件!!!"<<endl; return 1; } fout<<ch<<endl; fout<<" 购买结束,按回车退出"<<endl; fout<<" -----------------------"<<endl; fout.close(); exit(0); default: cout<<" 请输入a或者b"<<endl; cout<<" -----------------------"<<endl; cout<<" ";//设置后面输入选项的位置 fout.open("E:\outfile.txt",ios::app); if(!fout){ cout << "无法打开文件!!!"<<endl; return 1; } fout<<ch<<endl; fout<<" 请输入a或者b"<<endl; fout<<" -----------------------"<<endl; fout.close(); break; } } return 0; }