CHAPTER 10 实验题 1 2


这里的代码就是让我们看到,用枚举类型表示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};

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒回顾,半缘君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值