close

 

7-1

  
#include 
#include 
#include 
using namespace std;
double  calculateCharges( double hour );
int main()
{
   double hour; // number of hours for current car
   double currentCharge; // parking charge for current car
   double totalCharges = 0.0; // total charges
   double totalHours = 0.0; // total number of hours
   bool first = true; // used for printing table headers

   cout << fixed; // set floating-point number format
   cout << "Enter the hours parked for 3 cars: ";

   // loop 3 times for 3 cars
   for ( int i = 1; i <= 3; i++ )
   {
      cin >> hour;
      totalHours += hour; // add current hours to total hours

      // if first time through loop, display headers
      if ( first )
      {
         cout << setw( 5 ) << "Car" << setw( 15 ) << "Hours"
            << setw( 15 ) << "Charge\n";

         first = false; // prevent from printing again
      } // end if

      // calculate current car's parking charge
      currentCharge = calculateCharges( hour );
      totalCharges += currentCharge; // update total charges

      // display row data for current car
      cout << setw( 3 ) << i << setw( 17 ) << setprecision( 1 ) << hour
         << setw( 14 ) << setprecision( 2 ) << currentCharge << "\n";
   } // end for

   // display row data for totals
   cout << setw( 7 ) << "TOTAL" << setw( 13 ) << setprecision( 1 )
      << totalHours << setw( 14 ) << setprecision( 2 )
      << totalCharges << endl;
} // end main


 double  calculateCharges( double hour )
 {
 	
 
//算到底有幾小時 
 	
 	
 	//24小時直接跳10  
 	if(hour >= 24)
 	{
 		return 10;
 	}
 	//未達3小時 
 	if(hour<=3)
 	{
 	return 2;
	}
	else
	{
	//自己寫的判斷方法(不使用數學函式庫)
	//原理:強制轉換成int 減掉原本的double有不等於0就無條件進入 
	double temp = (hour-3); //算剩餘 (扣掉3小時的)
	int sub_num = ((int)hour-3); //強制取整數 
	//算有幾個剩餘 
		if(temp-sub_num!=0) //相減判斷是否為0 
		{
				return 2+(sub_num+1)*0.5; //加1 因為有餘數 
		}
		else
		{
			return 2+sub_num*0.5;  
		}	

	}

 }

 

7-2  

 
#include 
using namespace std;

int integerPower( int , int );

int main()
{
   int exp; // integer exponent
   int base; // integer base

   cout << "Enter base and exponent: ";
   cin >> base >> exp;
   cout << base << " to the power " << exp << " is: "
      << integerPower( base, exp ) << endl;
} // end main



int integerPower( int base, int exp )
{
	
	int temp=1;
	
	if(exp<0)
	{
		exp*=-1;
	}
	
	for(int i =1;i<=exp;i++)
	{
		temp*=base;	
	}
	
	return temp;
	
}

 

7-3  

 
//不使用函式解法(因為main不能寫所以假如非直角會傳回0)
#include 
#include 
#include 
using namespace std;

double hypotenuse( double side1, double side2);
int main()
{
   double side1; // value for first side
   double side2; // value for second side
   
   cout << fixed; // set floating-point number format

   // loop 3 times
   for ( int i = 1; i <= 3; i++ ) 
   {
      cout << "\nEnter 2 sides of right triangle: ";
      cin >> side1 >> side2;

      // calculate and display hypotenuse value
      cout << "Hypotenuse:  " << setprecision( 1 )
         << hypotenuse( side1, side2 ) << endl;
   } // end for
} // end main


double hypotenuse( double side1, double side2 ) 
{
	
	double num1=1;
	double num2=1;
	 
	for(int i =1;i<=2;i++)
	{
		num1 *=side1;
	}
	
	for(int i= 1;i<=2;i++)
	{
		num2 *=side2;
	}
	
	
	int c =num1+num2;
	
	//算平方 
	
	for(int i=1;;i++)
	{
	int temp ;	
	temp = i;
	
		if(temp*temp==c)
		{
			return temp;
		} 
		if(i>c)
		{
			break;
		}
	}	
	
//	return sqrt(c);
	
	
}
 
//使用函式解法(無限制) Ps 兩個FOR多寫了 算了..
#include 
#include 
#include 
using namespace std;

double hypotenuse( double side1, double side2);
int main()
{
   double side1; // value for first side
   double side2; // value for second side
   
   cout << fixed; // set floating-point number format

   // loop 3 times
   for ( int i = 1; i <= 3; i++ ) 
   {
      cout << "\nEnter 2 sides of right triangle: ";
      cin >> side1 >> side2;

      // calculate and display hypotenuse value
      cout << "Hypotenuse:  " << setprecision( 1 )
         << hypotenuse( side1, side2 ) << endl;
   } // end for
} // end main


double hypotenuse( double side1, double side2 ) 
{
	
	double num1=1;
	double num2=1;
	 
	for(int i =1;i<=2;i++)
	{
		num1 *=side1;
	}
	
	for(int i= 1;i<=2;i++)
	{
		num2 *=side2;
	}
	
	
	int c =num1+num2;
	return sqrt(c);
	
	
}

 

4.7-4  

 
#include 
#include 
using namespace std;
int seconds( int, int, int );
int main()
{
	
   unsigned hours; // current time's hours
   unsigned minutes; // current time's minutes
   unsigned secs; // current time's seconds
   double first; // first time, in seconds
   double second; // second time, in seconds
   double difference; // difference between two times, in seconds

   cout << "Enter the first time as three integers: ";
   cin >> hours >> minutes >> secs;
   first = seconds( hours, minutes, secs ); // calculate first time

   cout << "Enter the second time as three integers: ";
   cin >> hours >> minutes >> secs;
   second = seconds( hours, minutes, secs ); // calculate second time
   difference = fabs( first - second ); // calculate difference

   // display difference
   cout << "The difference between the times is "
      << difference << " seconds" << endl;
} // end main


int seconds( int hours, int minutes, int secs )
{
	//static int temp;
	
	int temp=hours*60*60+minutes*60+secs;
	
//	int now=hours*60*60+minutes*60+secs;
	
	return temp;
	
	
}

 

7-5  

 
#include 
using namespace std;

int isPerfect( int j ); // function prototype
void printSum( int value );
int main()
{
   cout << "Perfect integers between 1 and 1000:" << endl;

   // loop from 2 to 1000
   for ( int j = 2; j <= 1000; j++ )
   {
      // if current integer is perfect 
      if ( isPerfect( j ) ) 
         printSum( j ); // print it as sum of factors
   } // end for

   cout << endl;
} // end main



// printSum displays value followed by factors in summation form
void printSum( int value )
{
   cout << value << " = 1";

   // loop through possible factor values
   for ( int i = 2; i <= value / 2; i++ )
   {
      // if i is factor
      if ( value % i == 0 )
         cout << " + " << i; // display as part of sum
   } // end for

   cout << endl;
} // end function printSum


//===================
int isPerfect( int j )
{
	//這題不是我寫的 哈哈  
	int sum=0;
	
	for(int i =1;i<j;i++)
   {
   	if(j%i==0)
   	{
   		sum+=i;
   	}
   }
   
   if(sum==j)
	return j;
	else
	return 0;
}


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 UM程式研究日誌 的頭像
    UM程式研究日誌

    UM程式研究日誌

    UM程式研究日誌 發表在 痞客邦 留言(0) 人氣()