C++编程练习作业
//
Computer.h
#pragma once
#include " CPU.h "
#include " Memory.h "
#include " MainBoard.h "
#include " Monitor.h "
class CComputer
{
private:
CCPU m_cpu;
CMemory m_memory;
CMainBoard m_mainBoard;
CMonitor m_monitor;
public:
CComputer(void);
public:
~CComputer(void);
void setCpuFrequency(double frequency);
void setCpuFactory(char name);
void setMemorySize(int size);
void setMemoryFactory(char name);
void setMonitorSize(int size ,Sort sort);
void setMonitorFactory(char name);
void setMainBoardFactory(char name);
void printWholePrice();
void printConfig();
void Init();
void Start();
} ;
#pragma once
#include " CPU.h "
#include " Memory.h "
#include " MainBoard.h "
#include " Monitor.h "
class CComputer
{
private:
CCPU m_cpu;
CMemory m_memory;
CMainBoard m_mainBoard;
CMonitor m_monitor;
public:
CComputer(void);
public:
~CComputer(void);
void setCpuFrequency(double frequency);
void setCpuFactory(char name);
void setMemorySize(int size);
void setMemoryFactory(char name);
void setMonitorSize(int size ,Sort sort);
void setMonitorFactory(char name);
void setMainBoardFactory(char name);
void printWholePrice();
void printConfig();
void Init();
void Start();
} ;
//
Compter.cpp
#include " Computer.h "
#include < iostream >
using namespace std;
CComputer::CComputer( void )
{
}
CComputer:: ~ CComputer( void )
{
}
void CComputer::setCpuFrequency( double frequency)
{
this->m_cpu.setFrequency(frequency);
}
void CComputer::setCpuFactory( char name)
{
this->m_cpu.setFactoryName(name);
}
void CComputer::setMemorySize( int size)
{
this->m_memory.setSize(size);
}
void CComputer::setMemoryFactory( char name)
{
this->m_memory.setFactoryName(name);
}
void CComputer::setMonitorSize( int size ,Sort sort)
{
this->m_monitor.setSize(size);
this->m_monitor.setSort(sort);
}
void CComputer::setMonitorFactory( char name)
{
this->m_monitor.setFactoryName(name);
}
void CComputer::setMainBoardFactory( char name)
{
this->m_mainBoard.setFactoryName(name);
}
void CComputer::printWholePrice()
{
double price = this->m_cpu.getPrice();
price+=this->m_mainBoard.getPrice();
price+=this->m_memory.getPrice();
price+= this->m_monitor.getPrice();
cout<<"电脑的总价是"<<price<<endl;
}
void CComputer::printConfig()
{
cout<<"电脑配置:"<<endl;
cout<<"CPU:"<<this->m_cpu.getFactoryName()<<"\t"<<this->m_cpu.getFrequency()<<"GHz"<<endl;
cout<<"Memory:"<<this->m_memory.getFactoryName()<<"\t"<<this->m_memory.getSize()<<"MB"<<endl;
cout<<"MainBoard:"<<this->m_mainBoard.getFactoryName()<<endl;
cout<<"Monitor:"<<this->m_monitor.getFactoryName()<<"\t"<<this->m_monitor.getSize();
if(this->m_monitor.getSort()== COMMON)
{
cout<<"普通屏"<<endl;
}
else if(this->m_monitor.getSort() == LIQUID)
{
cout<<"液晶屏"<<endl;
}
}
void CComputer::Init()
{
this->m_mainBoard.Plug(&this->m_cpu,&this->m_memory);
}
void CComputer::Start()
{
if(this->m_mainBoard.SelfCheck())
{
cout<<"配置匹配,顺利启动"<<endl;
}
else
{
cout<<"配置不匹配,启动失败"<<endl;
}
}
#include " Computer.h "
#include < iostream >
using namespace std;
CComputer::CComputer( void )
{
}
CComputer:: ~ CComputer( void )
{
}
void CComputer::setCpuFrequency( double frequency)
{
this->m_cpu.setFrequency(frequency);
}
void CComputer::setCpuFactory( char name)
{
this->m_cpu.setFactoryName(name);
}
void CComputer::setMemorySize( int size)
{
this->m_memory.setSize(size);
}
void CComputer::setMemoryFactory( char name)
{
this->m_memory.setFactoryName(name);
}
void CComputer::setMonitorSize( int size ,Sort sort)
{
this->m_monitor.setSize(size);
this->m_monitor.setSort(sort);
}
void CComputer::setMonitorFactory( char name)
{
this->m_monitor.setFactoryName(name);
}
void CComputer::setMainBoardFactory( char name)
{
this->m_mainBoard.setFactoryName(name);
}
void CComputer::printWholePrice()
{
double price = this->m_cpu.getPrice();
price+=this->m_mainBoard.getPrice();
price+=this->m_memory.getPrice();
price+= this->m_monitor.getPrice();
cout<<"电脑的总价是"<<price<<endl;
}
void CComputer::printConfig()
{
cout<<"电脑配置:"<<endl;
cout<<"CPU:"<<this->m_cpu.getFactoryName()<<"\t"<<this->m_cpu.getFrequency()<<"GHz"<<endl;
cout<<"Memory:"<<this->m_memory.getFactoryName()<<"\t"<<this->m_memory.getSize()<<"MB"<<endl;
cout<<"MainBoard:"<<this->m_mainBoard.getFactoryName()<<endl;
cout<<"Monitor:"<<this->m_monitor.getFactoryName()<<"\t"<<this->m_monitor.getSize();
if(this->m_monitor.getSort()== COMMON)
{
cout<<"普通屏"<<endl;
}
else if(this->m_monitor.getSort() == LIQUID)
{
cout<<"液晶屏"<<endl;
}
}
void CComputer::Init()
{
this->m_mainBoard.Plug(&this->m_cpu,&this->m_memory);
}
void CComputer::Start()
{
if(this->m_mainBoard.SelfCheck())
{
cout<<"配置匹配,顺利启动"<<endl;
}
else
{
cout<<"配置不匹配,启动失败"<<endl;
}
}
//
CPU.h
#pragma once
class CCPU
{
private:
char m_factoryName;
double m_frequency;
public:
CCPU(void);
public:
~CCPU(void);
void setFactoryName(char name);
char getFactoryName();
double getPrice();
void setFrequency(double frequency);
double getFrequency();
} ;
#pragma once
class CCPU
{
private:
char m_factoryName;
double m_frequency;
public:
CCPU(void);
public:
~CCPU(void);
void setFactoryName(char name);
char getFactoryName();
double getPrice();
void setFrequency(double frequency);
double getFrequency();
} ;
//
CPU.cpp
#include " CPU.h "
CCPU::CCPU( void )
{
this->m_factoryName = '\0';
this->m_frequency = 0;
}
CCPU:: ~ CCPU( void )
{
}
void CCPU::setFactoryName( char name)
{
this->m_factoryName = name;
}
void CCPU::setFrequency( double frequency)
{
this->m_frequency = frequency;
}
char CCPU::getFactoryName()
{
return this->m_factoryName;
}
double CCPU::getPrice()
{
if(this->m_factoryName == 'A')
return this->m_frequency*800;
if(this->m_factoryName == 'B')
return this->m_frequency*700;
if(this->m_factoryName == 'C')
return this->m_frequency*600;
}
double CCPU::getFrequency()
{
return this->m_frequency;
}
#include " CPU.h "
CCPU::CCPU( void )
{
this->m_factoryName = '\0';
this->m_frequency = 0;
}
CCPU:: ~ CCPU( void )
{
}
void CCPU::setFactoryName( char name)
{
this->m_factoryName = name;
}
void CCPU::setFrequency( double frequency)
{
this->m_frequency = frequency;
}
char CCPU::getFactoryName()
{
return this->m_factoryName;
}
double CCPU::getPrice()
{
if(this->m_factoryName == 'A')
return this->m_frequency*800;
if(this->m_factoryName == 'B')
return this->m_frequency*700;
if(this->m_factoryName == 'C')
return this->m_frequency*600;
}
double CCPU::getFrequency()
{
return this->m_frequency;
}
//
MainBoard.h
#pragma once
#include " CPU.h "
#include " Memory.h "
class CMainBoard
{
private:
char m_factoryName;
CCPU *m_pCpu;
CMemory *m_pMemory;
public:
CMainBoard(void);
public:
~CMainBoard(void);
void setFactoryName(char name);
char getFactoryName();
double getPrice();
void Plug(CCPU* pCpu,CMemory* pMemory );
bool SelfCheck();
} ;
#pragma once
#include " CPU.h "
#include " Memory.h "
class CMainBoard
{
private:
char m_factoryName;
CCPU *m_pCpu;
CMemory *m_pMemory;
public:
CMainBoard(void);
public:
~CMainBoard(void);
void setFactoryName(char name);
char getFactoryName();
double getPrice();
void Plug(CCPU* pCpu,CMemory* pMemory );
bool SelfCheck();
} ;
//
MainBoard.cpp
#include " MainBoard.h "
CMainBoard::CMainBoard( void )
{
this->m_pCpu = 0;
this->m_factoryName = '\0';
this->m_pMemory = 0;
}
CMainBoard:: ~ CMainBoard( void )
{
}
void CMainBoard::setFactoryName( char name)
{
this->m_factoryName = name;
}
char CMainBoard::getFactoryName()
{
return this->m_factoryName;
}
double CMainBoard::getPrice()
{
if(this->m_factoryName == 'A')
return 600;
if(this->m_factoryName == 'B')
return 500;
if(this->m_factoryName == 'C')
return 400;
}
void CMainBoard::Plug(CCPU * pCpu, CMemory * pMemory)
{
this->m_pCpu = pCpu;
this->m_pMemory;
}
bool CMainBoard::SelfCheck()
{
if((this->m_pCpu->getFactoryName() == 'A'||this->m_pCpu->getFactoryName()=='B'||
this->m_pCpu->getFactoryName() == 'C')&&(this->m_pMemory->getFactoryName() == 'A' ||
this->m_pMemory->getFactoryName() == 'B'||this->m_pMemory->getFactoryName()=='C'))
return true;
else
return false;
}
#include " MainBoard.h "
CMainBoard::CMainBoard( void )
{
this->m_pCpu = 0;
this->m_factoryName = '\0';
this->m_pMemory = 0;
}
CMainBoard:: ~ CMainBoard( void )
{
}
void CMainBoard::setFactoryName( char name)
{
this->m_factoryName = name;
}
char CMainBoard::getFactoryName()
{
return this->m_factoryName;
}
double CMainBoard::getPrice()
{
if(this->m_factoryName == 'A')
return 600;
if(this->m_factoryName == 'B')
return 500;
if(this->m_factoryName == 'C')
return 400;
}
void CMainBoard::Plug(CCPU * pCpu, CMemory * pMemory)
{
this->m_pCpu = pCpu;
this->m_pMemory;
}
bool CMainBoard::SelfCheck()
{
if((this->m_pCpu->getFactoryName() == 'A'||this->m_pCpu->getFactoryName()=='B'||
this->m_pCpu->getFactoryName() == 'C')&&(this->m_pMemory->getFactoryName() == 'A' ||
this->m_pMemory->getFactoryName() == 'B'||this->m_pMemory->getFactoryName()=='C'))
return true;
else
return false;
}
//
Memory.h
#pragma once
class CMemory
{
private:
char m_factoryName;
int m_size;
public:
CMemory(void);
public:
~CMemory(void);
void setFactoryName(char name);
char getFactoryName();
double getPrice();
void setSize(int size);
int getSize();
} ;
#pragma once
class CMemory
{
private:
char m_factoryName;
int m_size;
public:
CMemory(void);
public:
~CMemory(void);
void setFactoryName(char name);
char getFactoryName();
double getPrice();
void setSize(int size);
int getSize();
} ;
//
Memory.cpp
CMemory:: ~ CMemory( void )
{
this->m_factoryName = '\0';
this->m_size =0;
}
void CMemory::setFactoryName( char name)
{
this->m_factoryName = name;
}
char CMemory::getFactoryName()
{
return this->m_factoryName;
}
double CMemory::getPrice()
{
if(this->m_factoryName == 'A')
return this->m_size*0.2;
if(this->m_factoryName == 'B')
return this->m_size*0.18;
if(this->m_factoryName == 'C')
return this->m_size*0.16;
}
void CMemory::setSize( int size)
{
this->m_size = size;
}
int CMemory::getSize()
{
return this->m_size;
}
CMemory:: ~ CMemory( void )
{
this->m_factoryName = '\0';
this->m_size =0;
}
void CMemory::setFactoryName( char name)
{
this->m_factoryName = name;
}
char CMemory::getFactoryName()
{
return this->m_factoryName;
}
double CMemory::getPrice()
{
if(this->m_factoryName == 'A')
return this->m_size*0.2;
if(this->m_factoryName == 'B')
return this->m_size*0.18;
if(this->m_factoryName == 'C')
return this->m_size*0.16;
}
void CMemory::setSize( int size)
{
this->m_size = size;
}
int CMemory::getSize()
{
return this->m_size;
}
//
Monitor.h
#pragma once
enum Sort {COMMON,LIQUID} ;
class CMonitor
{
private:
char m_factoryName;
int m_size;
Sort m_sort;
public:
CMonitor(void);
public:
~CMonitor(void);
void setFactoryName(char name);
char getFactoryName();
double getPrice();
void setSize(int size);
void setSort(Sort sort);
int getSize();
Sort getSort();
} ;
#pragma once
enum Sort {COMMON,LIQUID} ;
class CMonitor
{
private:
char m_factoryName;
int m_size;
Sort m_sort;
public:
CMonitor(void);
public:
~CMonitor(void);
void setFactoryName(char name);
char getFactoryName();
double getPrice();
void setSize(int size);
void setSort(Sort sort);
int getSize();
Sort getSort();
} ;
//
Monitor.cpp
#include " Monitor.h "
CMonitor::CMonitor( void )
{
}
CMonitor:: ~ CMonitor( void )
{
}
void CMonitor::setFactoryName( char name)
{
this->m_factoryName = name;
}
char CMonitor::getFactoryName()
{
return this->m_factoryName;
}
double CMonitor::getPrice()
{
double price =0;
if(this->m_factoryName == 'A')
price =this->m_size*90;
else if(this->m_factoryName == 'B')
price =this->m_size*80;
else if(this->m_factoryName == 'C')
price =this->m_size*70;
if(this->m_sort == COMMON)
price *= 0.8;
else if(this->m_sort == LIQUID)
price *= 1.2;
return price;
}
void CMonitor::setSize( int size)
{
this->m_size = size;
}
void CMonitor::setSort(Sort sort)
{
this->m_sort = sort;
}
int CMonitor::getSize()
{
return this->m_size;
}
Sort CMonitor::getSort()
{
return this->m_sort;
}
#include " Monitor.h "
CMonitor::CMonitor( void )
{
}
CMonitor:: ~ CMonitor( void )
{
}
void CMonitor::setFactoryName( char name)
{
this->m_factoryName = name;
}
char CMonitor::getFactoryName()
{
return this->m_factoryName;
}
double CMonitor::getPrice()
{
double price =0;
if(this->m_factoryName == 'A')
price =this->m_size*90;
else if(this->m_factoryName == 'B')
price =this->m_size*80;
else if(this->m_factoryName == 'C')
price =this->m_size*70;
if(this->m_sort == COMMON)
price *= 0.8;
else if(this->m_sort == LIQUID)
price *= 1.2;
return price;
}
void CMonitor::setSize( int size)
{
this->m_size = size;
}
void CMonitor::setSort(Sort sort)
{
this->m_sort = sort;
}
int CMonitor::getSize()
{
return this->m_size;
}
Sort CMonitor::getSort()
{
return this->m_sort;
}
//
stdafx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include < stdio.h >
#include < tchar.h >
#include < stdlib.h >
// TODO: 在此处引用程序需要的其他头文件
#include " CPU.h "
#include " MainBoard.h "
#include " Memory.h "
#include " Monitor.h "
#include " Computer.h "
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include < stdio.h >
#include < tchar.h >
#include < stdlib.h >
// TODO: 在此处引用程序需要的其他头文件
#include " CPU.h "
#include " MainBoard.h "
#include " Memory.h "
#include " Monitor.h "
#include " Computer.h "
//
stdfax.cpp
// stdafx.cpp : 只包括标准包含文件的源文件
// C++3.pch 将作为预编译头
// stdafx.obj 将包含预编译类型信息
#include " stdafx.h "
// TODO: 在 STDAFX.H 中
// 引用任何所需的附加头文件,而不是在此文件中引用
// stdafx.cpp : 只包括标准包含文件的源文件
// C++3.pch 将作为预编译头
// stdafx.obj 将包含预编译类型信息
#include " stdafx.h "
// TODO: 在 STDAFX.H 中
// 引用任何所需的附加头文件,而不是在此文件中引用
//
C++3.cpp : 定义控制台应用程序的入口点。
//
#include " stdafx.h "
char name[ 3 ] = {'A','B','C'} ;
int memSize[ 3 ] = {128,256,512} ;
int monSize[ 4 ] = {14,15,17,19} ;
Sort sort[ 2 ] = {COMMON,LIQUID} ;
double frequency[ 3 ] = {1.33,1.67,2.0} ;
void FunMain();
int _tmain( int argc, _TCHAR * argv[])
{
FunMain();
return 0;
}
void FunMain()
{
CComputer *pCompter = new CComputer[10];
for(int i = 0; i<10; i++)
{
pCompter[i].setCpuFrequency(frequency[rand()%3]);
pCompter[i].setCpuFactory(name[rand()%3]);
pCompter[i].setMainBoardFactory(name[rand()%3]);
pCompter[i].setMemoryFactory(name[rand()%3]);
pCompter[i].setMonitorFactory(name[rand()%3]);
pCompter[i].setMemorySize(memSize[rand()%3]);
pCompter[i].setMonitorSize(monSize[rand()%4],sort[rand()%2]);
}
for(int i = 0; i<10; i++)
{
pCompter[i].printConfig();
pCompter[i].printWholePrice();
}
delete []pCompter;
}
//
#include " stdafx.h "
char name[ 3 ] = {'A','B','C'} ;
int memSize[ 3 ] = {128,256,512} ;
int monSize[ 4 ] = {14,15,17,19} ;
Sort sort[ 2 ] = {COMMON,LIQUID} ;
double frequency[ 3 ] = {1.33,1.67,2.0} ;
void FunMain();
int _tmain( int argc, _TCHAR * argv[])
{
FunMain();
return 0;
}
void FunMain()
{
CComputer *pCompter = new CComputer[10];
for(int i = 0; i<10; i++)
{
pCompter[i].setCpuFrequency(frequency[rand()%3]);
pCompter[i].setCpuFactory(name[rand()%3]);
pCompter[i].setMainBoardFactory(name[rand()%3]);
pCompter[i].setMemoryFactory(name[rand()%3]);
pCompter[i].setMonitorFactory(name[rand()%3]);
pCompter[i].setMemorySize(memSize[rand()%3]);
pCompter[i].setMonitorSize(monSize[rand()%4],sort[rand()%2]);
}
for(int i = 0; i<10; i++)
{
pCompter[i].printConfig();
pCompter[i].printWholePrice();
}
delete []pCompter;
}