CHAPTER 8 实验题 3 1 4

实验八 变量的作用域和生存期

一、实验目的

  1. 深入理解VC中变量的作用域和生存期的概念;
  2. 练习使用局部变量,全局变量; 确定变量在程序中的生存期;
  3. 练习使用具有不同生存期的变量来构造函数,完成特定的程序。
    二、实验内容
  4. 调试本章Programming Example;
  5. 编程实现并调试本章Programming Problems 1, 3,4 题(可以选2)。
    三、 实验要求
  6. 掌握c++变量的作用域和生存期的概念;
  7. 提前写出程序源代码,设计实验测试用的输入数据,预计输出结果;
    四、注意:
  8. 具有不同生存期的变量在函数中的使用。

在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<fstream>
using namespace std;

int main()
{ 
  int i,j,k,m=0,col=5;
  double max,min,data;
  double a[10],difference[10];	
  
  //定义文件流
  ifstream inData;
  ofstream outData;
  
  inData.open("barometric.dat");//提取数据和输出结果的不是一个文件~!!!
  outData.open("differences.dat");
  
    if( !inData || !outData )
     {  
       cout<<"Can't open output file.";
       return 1;
     }

  while(col)
  { 
     for(j=0,k=0;j<6;j++,k++)
       {
        inData>>data;
        a[k]=data;
       }
       max=a[0];//先初始化再比较
       min=a[0];
       
     for(i=0;i<6;i++)
      { 
        if( a[i]>max )
          max=a[i] ;
        if(a[i]<min)
	      min=a[i];
	   }
    difference[m]=max-min;//差值
    //每计算完一行的差值,就要打印到文件里,且单独占一行
    outData<<difference[m]<<endl;
    //cout<<difference[m]<<endl;
      m++; //开始下一个
      col--;//输入的文件有几行就是从几开始控制循环次数
  }
   
 max=difference[0];
 min=difference[0];
 
  for(i=0;i<5;i++)
  {
    if(difference[i]>max)
      max=difference[i];
    if(difference[i]<min)
      min=difference[i];
  }
   
  cout<<"The greatest difference for the year:"<<max<<endl;
  cout<<"The least difference for the year:"<<min<<endl;
  
  inData.close();
  outData.close();
  
  return 0;
}

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
板英尺(BF)是北美木材记尺单位,很多时候要记千板尺(MBF),如果是板材,一千板尺木材等于2.36立方米。长度与宽度以英尺记,厚度英寸记,三个数值相乘除以12既是板材数。如需更多资料可查阅美国阔叶木分等概要中度量单位介绍。如果是原木,按照下面公式计算,其中D代表直径,L代表长度(D-4)2 x (L/16)。

借鉴一下别人的代码:

//*****************************************************************************//***** This program calculate the cost of lumber for an order.         //*****                           
//***** Lumber is priced by board feet.                                 //*****           
//***** One board foot equals one square foot,oneinch thick.            //*****           
//***** /* By Cao zaihui,20071116 , Revised 20100512  BY CUI*/          //*****           
//***************************************************************************** 
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

float calculate(int number,int wide,int high,int length,float price)
{
	float cost;
    cost = number * wide * high * length * price /12;
	return cost;
}
int main()
{	string letter,name;
	int number,	int wide,high,length;
	float priceReading,price,costPerEntry,total=0;
	
	ifstream indata;
	cout<<"Enter item:"<<endl;
	cin>>letter;
	  if(letter=="T")
	  {cout<<"Terminited !"<<endl;	return 1;}
	  cin>>number>>wide>>high>>length;
	
	while(cin)
	{
	 indata.open("board.txt");
	  if(!indata)
	  {
	   cout<<"Can't open file! "<<endl;
	   return 1;
	  }
	  
	   	indata>>name>>priceReading;
		while(indata && ( letter != name.substr(0,1) ) )//serach for the right price
		{
		 indata>>name>>priceReading;
		}
		
		if(letter!=name.substr(0,1))
		{   cout<< "invalid input! Exited! "<<endl; return 1; }
		else// get the correct price
		{   price=priceReading;
			costPerEntry=calculate(number,wide,high,length,price);
			total=total+costPerEntry;
	            
	        cout<<number<<"  "<<wide<<"*"<<high<<"*"<<length<<" "<<name; 
            cout<<" Cost:"<<fixed<<setprecision(2)<<costPerEntry<<endl;
			cout<<"Enter item:";

            indata.close();// CLOSE FILE FOR NEXT OPENING 
			cin>>letter;  //NEXT ENTRY
			if(letter=="T")
			{
			  cout<<"total cost&"<<total<<endl;
			  return 1;
			}
			cin>>number>>wide>>high>>length;
		}
	}
 return 0;
}

在这里插入图片描述
解法2:

//*****************************************************************************//***** This program calculate the cost of lumber for an order.           //*****                           
//***** Lumber is priced by board feet.                                    //*****           
//***** One board foot equals one square foot,one inch thick.               //*****           
//***** Revised 20100512                                                  //*****           
//***************************************************************************** 
#include<iostream>
#include<iomanip>
using namespace std;

double Getcost(char kind)
{
  int piecenum, width, height, length;
  double cost,price;
  cin>>piecenum>>width>>height>>length;
  cout<<endl<<piecenum<<" "<<width<<"*"<<height<<"*"<<length<<" ";
  
  if(kind=='P')
  {
  cout<< "Pine,";
  price=0.89;
  }
  else if(kind=='M')
  {
  cout<<" Maple,";
  price=4.50;
  }
  else if(kind=='F')
  {
  cout<<" Fir,";
  price=1.09;
  }
  else if(kind=='C')
  {
  cout<<" Cedar";
  price=2.26;
  }
  else if(kind=='O')
  {
  cout<<" Oak";
  price=3.10;
  }
  cost=price*piecenum*width*height*length/12.0;
  return cost;
}

int main()
{
 char kind;
 double totalcost=0;
 double cost;
 cout<<"Enter item:";
 cin>>kind;
 while(kind)
 { 
   if(kind=='P'||kind=='F'||kind=='C'||kind=='M'||kind=='O')
   { cost=Getcost(kind);
     cout<<" cost:$"<<fixed<<setprecision(2)<<cost<<endl;
     totalcost=cost+totalcost;
     cout <<"Enter item:";
    cin>>kind;
   }
   else if(kind=='T')
  { cout<<"Total cost:$"<<fixed<<setprecision(2)<<totalcost<<endl;
	return 0;
  }
}// while

return 0;
}

在这里插入图片描述

解法3:

//*******************************************************************************
//Calculate Cost Program
//This program calculate the cost of lumber for an order.Lumber is priced by board feet.
//One board foot equals one square foot,one inch thick.
// By Shang Haiyan, revised 20100512
//*******************************************************************************
#include<iostream>
#include<iomanip>
using namespace std;
const double PINE=0.89;
const double FIR=1.09;
const double CEDAR=2.26;
const double MAPLE=4.50;
const double OAK=3.10;
float Cost(int,int,int,int,float);
int main()
{
	int width,height,length;
	int number_of_pieces,number_of_kinds;
	int count;
	float cost,total=0.0;
	char kind;
	cout << "Enter item : ";
	cin >>kind;           //put in the kinds of wood
	if(kind!='T')
		cin>> number_of_pieces >> width >> height >> length ; 
    while(kind!='T')
	{
		//cout << "Enter pieces , width ,height and length: ";
		//cin >> number_of_pieces >> width >> height >> length ;
        cout <<  number_of_pieces << setw(4) << width << 'x' << height << 'x' << length;
		switch(kind)
		{
		   case 'P':cost=Cost(number_of_pieces,width,height,length,PINE); 
                  cout <<fixed<<setprecision(2)<< " Pine,cost: $" << cost << endl;
                  break;
           case 'F':cost=Cost(number_of_pieces,width,height,length,FIR);
                  cout <<fixed<<setprecision(2)<< " Fir,cost: $" << cost << endl;
                  break;
           case 'C':cost=Cost(number_of_pieces,width,height,length,CEDAR); 
                  cout <<fixed<<setprecision(2)<< " Cedar,cost: $" << cost <<endl;
                  break;
           case 'M':cost=Cost(number_of_pieces,width,height,length,MAPLE); 
                  cout <<fixed<<setprecision(2)<< " Maple,cost: $" << cost <<endl;
                  break;
           case 'O':cost=Cost(number_of_pieces,width,height,length,OAK);
  cout <<fixed<<setprecision(2)<< " Oak,cost: $" << cost <<endl;
  break;
		   default:cout << "Error" <<endl;
		}
		total=total+cost;
		cout << "Enter item : ";  //  for next entry
	    cin >>kind;             //put in the kinds of wood
	    if(kind!='T')
		cin>> number_of_pieces >> width >> height >> length ;  
	}
	//cout << "Enter item:T" << endl;
	cout << "Total cost:$" << total << endl;
    return 0;

}
//****************************************************************************
 float Cost(int number_of_pieces,int width,int height,int length,float price) 
//Cost function
//This program calculate the cost of all the wood.
//Pecondition:
//    All the argument been assigned
//Postcondition:
//    return value of cost
 {
	 float cost;
	 cost=price*number_of_pieces*(width*height*length/12.0);
	 return cost;
 }  

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

//This program can determine the day of week for a given date
#include <iostream>
using namespace std;

long JDN(int y, int m, int d);     //function prototype

int main()
{
     int year;
     int month;
     int day;
     int dayOfWeek;

     // get date
     cout << "Year: ";
     cin >> year;
     cout << "Month: ";
     cin >> month;
     cout << "Day: ";
     cin >> day;

     // calculate dayOfWeek (0--6) 0 represents Sunday
     dayOfWeek = (JDN(year, month, day) + 1) % 7;

     // output the result
     switch(dayOfWeek)
     {
     case 0:
       cout << "Sunday" ; break;
     case 1:
       cout << "Monday"; break;
     case 2:
       cout << "Tuesday"; break;
     case 3:
       cout << "Wednesday"; break;
     case 4:
       cout << "Thursday"; break;
     case 5:
       cout << "Friday"; break;
     case 6:
       cout << "Saturday"; break;
     }

     cout << endl;

     return 0;

}

//****************************************************************
long JDN(int year, int month, int day)
//Precondition:
//     year, month, day are assigned 
//     && are valid
//Postcondition:
//     return the JDN day
{
     double intRes1;
     double intRes2;
     double intRes3;
     long jdnDay;

     if(month < 3)  
     {
       month += 12;
       year--;
     }

     if((year > 1582)      //later than 1582/10/15
            || (year == 1582     && month > 10) 
            || (year == 1582 && month == 10 && day > 15))  
       intRes1 = 2 - year / 100 + year / 400;
     else
       intRes1 = 0;
     intRes2 = int(365.25 * year);
     intRes3 = int(30.6001 * (month + 1));
     jdnDay = long(intRes1 + intRes2 + intRes3 + day + 1720995);
     return jdnDay;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

懒回顾,半缘君

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

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

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

打赏作者

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

抵扣说明:

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

余额充值