“网上买菜系统”的分析
软件工程综合实践专题 第一次作业 “网上买菜系统”的分析
借用隔壁寝室的室友在小学期写的C++的网上买菜系统的代码进行阅读、分析和运行。
一、阅读并分析代码
//代码有三个文件组成,并运用到了文件的调用,该程序在实现功能方面基本实现了买菜系统应该会用到的功能。
//以下是我对代码的一些分析:
buy.h文件
#include<iostream>
#include<string>
using namespace std;
class buyer
{
protected:
string name;
int buyerID;
int kilogram;
double pay;
public:
buyer();
buyer(string n, int b, int k, double p);
string getbuyname();
double getkilogram();
double getpay();
int getid();
virtual void display() = 0;
virtual void setpay(double = 0) = 0;
};
class layfolk :public buyer
{
public:
layfolk(string n, int b, int k, double p) :buyer(n, b, k, p)
{
}
void display();
void setpay(double p);
};
class member :public buyer
{
int leaguer_grade;
public:
member(string n, int b, int l, int k, double p) :buyer(n, b, k, p)
{
leaguer_grade = l;
}
void display();
void setpay(double p);
};
buyer::buyer()
{
name = "";
buyerID = 0;
kilogram = 0;
pay = 0;
}
buyer::buyer(string n, int b, int k, double p)
{
name = n;
buyerID = b;
kilogram = k;
pay = p;
}
double buyer::getpay()
{
return pay;
}
double buyer::getkilogram()
{
return kilogram;
}
string buyer::getbuyname()
{
return name;
}
int buyer::getid()
{
return buyerID;
}
void layfolk::display() //显示购菜人的信息和订单信息
{
cout <<"购菜人姓名:" << name << "\t";
cout <<"购菜人编号:" << buyerID << "\t";
cout <<"购菜人为" << "\t\t\t";
cout <<"购菜数量各为:" << kilogram <<"公斤\n";
}
void layfolk::setpay(double p) //价格计算公式
{
pay = pay + p*kilogram;
}
vegetables.h文件
#include<iostream>
#include<string>
using namespace std;
class vegetables
{
public:
int vegetables_number;
string vegetables_name;
string vegetables_date;
double price;
public:
vegetables();
vegetables(int v_num,string v_n, string v_d, double pr);
void display();
int getvegetables_number();
string getvegetables_name();
string getvegetables_date();
double getprice();
};
vegetables::vegetables(int v_num,string v_n, string v_d, double pr)
{
vegetables_number=v_num;
vegetables_name=v_n;
vegetables_date=v_d;
price = pr;
}
vegetables::vegetables()
{
vegetables_number=0;
vegetables_name= "";
vegetables_date= "";
price = 0;
}
void vegetables::display() //显示蔬菜信息
{
cout <<"蔬菜编号:"<<vegetables_number<<"\t\t";
cout << "蔬菜名称:" << vegetables_name << "\t\t";
cout << "蔬菜到货日期:" << vegetables_date << "\t\t";
cout << "定价:" << price << "\n";
}
int vegetables::getvegetables_number()
{
return vegetables_number;
}
string vegetables::getvegetables_name()
{
return vegetables_name;
}
string vegetables::getvegetables_date()
{
return vegetables_date;
}
double vegetables::getprice()
{
return price;
}
Main函数
#include<iostream>
#include<cassert>
#include<fstream>
#include<string>
#include"buy.h"
#include"vegetables.h"
using namespace std;
int main()
{ ifstream fin("C:\\Users\\39230\\Desktop\\蔬菜\\菜单信息.txt",ios::in); //打开文件
if(fin)
cout<<"\n"<<endl;
else
cout<<"文件打开失败"<<endl;
int i;
vegetables c[12];
for(i=0;i<12;i++)
{
fin>>c[i].vegetables_number>>c[i].vegetables_name>>c[i].vegetables_date>>c[i].price;
}
fin.close(); //关闭文件
int sum=0;
for(int q=0;q<12;q++) //统计价格
{
sum=sum+c[q].price;
}
char m;
while(m!=5)
{
system("cls"); //清屏
cout<<"\n";
cout<<" ****************************"<<endl;
cout<<" * 欢迎来到网上购菜系统 *"<<endl;
cout<<" *==========================*"<<endl;
cout<<" * 1.蔬菜信息 *"<<endl;
cout<<" * 2.订单人信息 *"<<endl;
cout<<" * 3.金额统计 *"<<endl;
cout<<" * 4.退出系统 *"<<endl;
cout<<" * 请选择功能1-4 *"<<endl;
cout<<" ****************************"<<endl; //显示开始界面
char k;
cin>>k;
fflush(stdin);
if ((k<1)&&(k>4))
{
cout<<"输入编号错误!"<<endl;
fflush(stdin);
return 0;
}
system("cls");
switch(k) //主界面跳转
{
case'1': //蔬菜信息
{
system("cls");
cout<<" ==========\n";
cout<<" |蔬菜信息|\n";
cout<<" ==========\n";
for (i = 0; i < 12; i++)
c[i].display();
fflush(stdin);
;}
break;
case'2': //订单人信息
{
system("cls");
layfolk b1("张三", 1, 10, 0);
member b2("李四",2,5,10,0);
buyer*b[2] = { &b1,&b2};
cout <<" ============\n";
cout <<" |购菜人信息|\n";
cout <<" ============\n";
for (i = 0; i < 3; i++)
b[i]->display();
}break;
case'3': //金额统计
{
system("cls");
int buyerid, flag;
layfolk b1("张三", 1, 90, 0);
member b2("李四",2,5,10,0);
buyer*b[1] = { &b1 ,&b2};
cout << "\n请输入购菜人编号:";
cin >> buyerid;
flag = 0;
for(i=0;i<3;i++)
if (b[i]->getid() == buyerid)
{
flag = 1;
break;
}
if (!flag)
{
cout << "编号不存在" << endl;
}
else
{
b[i]->setpay(sum);
cout << endl << "购菜人"<<b[i]->getbuyname()<<"需要付费:" << b[i]->getpay() << "元"<<"\n";
}
}break;
case'4': //退出系统
{
system("cls");
cout <<" ===============\n";
cout<<" |谢谢使用!再见!|"<<endl;
cout <<" ===============\n";
fflush(stdin);
return 0;
}
break;}
system("pause");
}
return 0;
}
二、运行结果:
三、心得体会,遇到的问题分析
1、问题分析:
//顾客有会员和非会员之分,价格也会有所变化,添加了以下代码来实现不同顾客之间的购物价格的不同计算公式。
buy.h文件
buyer::buyer(string n, int b, int k, double p)
{
name = n;
buyerID = b;
kilogram = k;
pay = p;
}
double buyer::getpay()
{
return pay;
}
double buyer::getkilogram()
{
return kilogram;
}
string buyer::getbuyname()
{
return name;
}
int buyer::getid()
{
return buyerID;
}
void member::display()
{
cout <<"购菜人姓名:" << name << "\t";
cout <<"购菜人编号:" << buyerID << "\t";
cout <<"购菜人为会员,级别:" << leaguer_grade << "\t\t";
cout <<"购菜数量各为:" << kilogram << "公斤\n";
}
void member::setpay(double p)
{
if (leaguer_grade == 1)
pay = 0.95*p*kilogram + pay;
else if (leaguer_grade == 2)
pay = 0.90*p*kilogram + pay;
else if (leaguer_grade == 3)
pay = 0.85*p*kilogram + pay;
else if (leaguer_grade == 4)
pay = 0.80*p*kilogram + pay;
else if (leaguer_grade == 5)
pay = 0.7*p*kilogram + pay;
else
cout <<"级别错误!";
}
void honoured_guest::display()
{
cout <<"购菜人姓名:" << name << "\t";
cout <<"购菜人编号:" << buyerID << "\t";
cout <<"购菜人为贵宾,折扣率为:" << discount_rate * 100 << "%\t";
cout <<"购菜数量各为:" << kilogram << "公斤\n";
}
void honoured_guest::setpay(double p)
{
pay = pay + (1 - discount_rate)*p*kilogram;
}
心得体会:
通过阅读隔壁室友的代码,重温了一遍C++的重载功能、文件输入输出功能、函数调用功能等。非常偶尔才会阅读别人的代码,阅读隔壁室友的代码就让我非常新奇,一点一点的去剖析她的代码是一件有趣的事,遇到问题的时候还会与别人交流讨论。一直只看自己的代码会形成自己的思维定势,要经常看看别人的代码会发现自己的不足,也会学习到别人的优点。阅读代码是一条能够提高自己各种语言,拓宽自己的思路的途径。以后更是应该多多阅读别人的代码,不管代码好与坏、长或短都是一种学习语言的方法。