这里的代码就是让我们看到,用枚举类型表示case后的条件的好处,不用再一个个名字去赋值。
第一题
题意:
main.cpp
//*****************************************************************************//Calculate the Weight on Different Planet Program //This program asks the user to enter their weight and the //name of a planet and output weight on that planet. //"planet.h" needed, May 22, 2010 //*****************************************************************************
#include<iostream>
#include<string>
#include"planet.h"
using namespace std;
double Weight(double,Planet);
Planet Planet_Name(string);
int main()
{
double weightIn,weightOut ;
string planet_name;
Planet sphere;//声明枚举类型
bool dataNotOK;
cout << "The planet name and weight: ";
cin >> planet_name >> weightIn;
while(weightIn) //Make sure the planet name is valid.
{
dataNotOK=(planet_name!="Mercury"&&planet_name!="Venus"&&planet_name!="Earth"&& planet_name!="Moon"&&planet_name!="Mars"&&planet_name!="Jupiter"&& planet_name!="Saturn"&&planet_name!="Uranus"&&planet_name!="Neptune"&& planet_name!="Pluto");
if(dataNotOK)
{ cout << "The planet name is invalid!" << endl;
cout << "The planet name and weight: ";
cin >> planet_name >> weightIn;
}
else
break;
}
sphere=Planet_Name(planet_name); //convert the name of a planet to a string
//parameter to a value of the enumeration type.
weightOut=Weight(weightIn,sphere); //Calculat the weight
cout << "The weight on the " << planet_name << " is " << weightOut << endl;
return 0;
}
Planet Planet_Name(/*in*/string planet_name)
//This program convert the name of a planet given as a string parameter to a value of the
//enumeration type.
//Precondition:
// The variable of planet_name is assigned.
//Postconditon:
// Reurn the value of name.
{
Planet name;
if(planet_name=="Mercury") name=MERCURY;
if(planet_name=="Venus") name=VENUS;
if(planet_name=="Earth") name=EARTH;
if(planet_name=="Moon") name=MOON;
if(planet_name=="Mars") name=MARS;
if(planet_name=="Jupiter") name=JUPITER;
if( planet_name=="Saturn") name=SATURN;
if(planet_name=="Uranus") name=URANUS;
if(planet_name=="Neptune") name=NEPTUNE;
if(planet_name=="Pluto") name=PLUTO;
return name;
}
//*****************************************************************************
double Weight(double weight,Planet sphere)
//This program calculate the weight on different planet.
//Precondition:
// The variable of weight and sphere is assigned.
//Postconditon:
// Reurn the value of weight.
{
switch(sphere)
{
case MERCURY:weight=weight*0.415;break;
case VENUS:weight=weight*0.8975;break;
case EARTH:weight=weight*1.0;break;
case MOON:weight=weight*0.166;break;
case MARS:weight=weight*0.3507;break;
case JUPITER:weight=weight*2.5374;break;
case SATURN:weight=weight*1.0677;break;
case URANUS:weight=weight*0.8947;break;
case NEPTUNE:weight=weight*1.1794;break;
case PLUTO:weight=weight*0.0899;break;
default:cout << "ERROR" << endl;break;
}
return weight;
}//end of Program 10.1
planet.h
enum Planet{MERCURY,VENUS,EARTH,MOON,MARS,JUPITER,SATURN,URANUS,NEPTUNE,PLUTO};
//end of file"planet.h"
运行结果:
第二题
题意:
main.cpp
//*******************************************************************************
//Write a Program for Ten Salespeople
//This program reads in the sales file and generates a separate file for each salesperson containing just their sales.
// File "Sales.dat" and "Name.h" needed
//*******************************************************************************
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>
#include"Name.h"
using namespace std;
double Total(int,double);
void OpenData(ofstream outData[],int);
Name Names(string);
void Output(Name,int,double,double, double total[],int,ofstream outData[],int);
int main()
{
ifstream inData;
ofstream outData[10];
string lastname;
Name name;
int Item_Number,i;
double quantity,total[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},price;
inData.open("sales.dat");
if(!inData)
{
cout<<"Can not open inData."<<endl;
return 1;
}
OpenData(outData,10);
inData >> lastname >> Item_Number >> quantity;
while(inData)
{
price=Total(Item_Number,quantity);
name=Names(lastname);
Output(name,Item_Number,quantity,price,total,10,outData,10);
inData >> lastname >> Item_Number >> quantity;
}
for(i=0;i<10;i++)
cout << "Total for the " << (i+1) << " salesperson: " << total[i] << endl;
inData.close();
return 0;
}
void OpenData(/*out*/ofstream outData[],/*in*/int number)
//This program open the outData of this salesperson.
//Precondition:
// Item_Number and quantity are assigned.
//Postcondition:
// Not return value.
{ outData[0].open("Wang.dat");
outData[1].open("Chang.dat");
outData[2].open("Li.dat");
outData[3].open("Sun.dat");
outData[4].open("Dong.dat");
outData[5].open("Guo.dat");
outData[6].open("Yan.dat");
outData[7].open("Zhao.dat");
outData[8].open("Xu.dat");
outData[9].open("Ma.dat");
}
Name Names(/*in*/string lastname)
//This program genarate a value of enum type.
//Precondition:
// Lastname is assigned.
//Postcondition:
// Retrun the valve of name.
{
char ch;
Name name;
ch=lastname[0];
switch(ch)
{
case 'W':name=Wang;break;
case 'C':name=Chang;break;
case 'L':name=Li;break;
case 'S':name=Sun;break;
case 'D':name=Dong;break;
case 'G':name=Guo;break;
case 'Y':name=Yang;break;
case 'Z':name=Zhao;break;
case 'X':name=Xu;break;
case 'M':name=Ma;break;
default:cout << "The name is invalid." << endl;break;
}
return name;
}
double Total(/*in*/int Item_Number,/*in*/double quantity)
//This program calculate the price that one salesperson sells a kind of item.
//Precondition:
// Item_Number and quantity are assigned.
//Postcondition:
// Retrun the value of price.
{
double price=0.0;
switch(Item_Number)
{
case 7:price=quantity*345.00;break;
case 8:price=quantity*853.00;break;
case 9:price=quantity*471.00;break;
case 10:price=quantity*933.00;break;
case 11:price=quantity*721.00;break;
case 12:price=quantity*663.00;break;
case 13:price=quantity*507.00;break;
case 14:price=quantity*259.00;break;
}
return price;
}
void Output(/*in*/Name name,
/*in*/int Item_Number,
/*in*/double quantity,
/*in*/double price,
/*out*/double total[],
/*in*/int number1,
/*out*/ofstream outData[],
/*in*/int number2)
//This program output item number and quqntity to each of file and calculate the total of each salesperson.
{ switch(name)
{ case Wang:if(price-0.0>0.00001)
outData[0]<<Item_Number<<setw(4)<<quantity << endl;
else
outData[0]<<"Item number is invalid!" << endl;
total[0]=total[0]+price;break;
case Chang:if(price!=0.0)
outData[1]<<Item_Number<<setw(4)<<quantity << endl;
else
outData[1]<<"Item number is invalid!" << endl;
total[1]=total[1]+price;break;
case Li:if(price!=0.0)
outData[2]<<Item_Number<<setw(4)<<quantity << endl;
else
outData[2]<<"Item number is invalid!" << endl;
total[2]=total[2]+price;break;
case Sun:if(price!=0.0)
outData[3]<<Item_Number<<setw(4)<<quantity << endl;
else
outData[2]<<"Item number is invalid!" << endl;
total[3]=total[3]+price;break;
case Dong:if(price!=0.0)
outData[4]<<Item_Number<<setw(4)<<quantity << endl;
else
outData[4]<<"Item number is invalid!" << endl;
total[4]=total[4]+price;break;
case Guo:if(price!=0.0)
outData[5]<<Item_Number<<setw(4)<<quantity << endl;
else
outData[5]<<"Item number is invalid!" << endl;
total[5]=total[5]+price;break;
case Yang:if(price!=0.0)
outData[6]<<Item_Number<<setw(4)<<quantity << endl;
else
outData[5]<<"Item number is invalid!" << endl;
total[6]=total[6]+price;break;
case Zhao:if(price!=0.0)
outData[7]<<Item_Number<<setw(4)<<quantity << endl;
else
outData[7]<<"Item number is invalid!" << endl;
total[7]=total[7]+price;break;
case Xu:if(price!=0.0)
outData[8]<<Item_Number<<setw(4)<<quantity << endl;
else
outData[8]<<"Item number is invalid!" << endl;
total[8]=total[8]+price;break;
case Ma:if(price!=0.0)
outData[9]<<Item_Number<<setw(4)<<quantity << endl;
else
outData[9]<<"Item number is invalid!" << endl;
total[9]=total[9]+price;break;
default:cout << "ID number is invalid." << endl;break;
}
}
“Sales.dat”
Name.h
enum Name{Wang,Chang,Li,Sun,Dong,Guo,Yang,Zhao,Xu,Ma};
运行结果: