我想计算梯形区域,我的代码有什么问题?有时它有效它会产生错误,有时它会改变结果。
import math
def f(x):
a=0
b= math.log
return math.e**x
trapizoid=(((b-a)/2)*(f(a)+f(b)))
print(trapizoid)
答案 0 :(得分:0)
我认为它很简单:
public class Hotel
{
//Class constants
private static final double Room_Rate = 79.95;
private static final double Tax_Rate = 6.5;
private static final double Telephone = 5.75;
private static final double Meal_Cost = 12.95;
private static final double Tip_Rate = 0.075;
//Instance Variables
private int noOfNights;
private int noOfGuests;
private double amountDue;
private double meal;
private double tax;
private double subtotal;
private double total;
private double tip;
private String roomNumber;
private static double TotalRoomCharges;
private static double TotalTelephoneCharges;
private static double TotalMealCharges;
private static double TotalTips;
private static double TotalTax;
private static double GrossTransaction;
public Hotel (String room)
{
roomNumber = room;
noOfGuests = 1;
noOfNights = 1;
}
public Hotel (String room, int nights)
{
this (room);
noOfNights = nights;
}
public Hotel (String room, int nights, int guest)
{
this (room, nights);
noOfGuests = guest;
}
public void addNights (int nights)
{
noOfNights = noOfNights + nights;
}
public void addGuest (int guests)
{
noOfGuests = noOfGuests + guests;
}
public void calculate ()
{
amountDue = Room_Rate * noOfNights * noOfGuests;
tax = amountDue * Tax_Rate / 100;
subtotal = amountDue + tax;
meal = Meal_Cost * noOfNights *noOfGuests;
tip = Tip_Rate * (subtotal + meal + Telephone);
total = subtotal + Telephone + meal + tip;
TotalRoomCharges = TotalRoomCharges + amountDue;
TotalTelephoneCharges = TotalTelephoneCharges + Telephone;
TotalMealCharges = TotalMealCharges + meal;
TotalTips = TotalTips + tip;
TotalTax = TotalTax + tax;
GrossTransaction = GrossTransaction + total;
}
public double getAmountDue()
{
return amountDue;
}
public double getTaxDue()
{
return tax;
}
public double getSubtotal()
{
return subtotal;
}
public double getTotal()
{
return total;
}
public double getTip()
{
return tip;
}
double getMeal()
{
return meal;
}
public String getRoomNumber()
{
return roomNumber;
}
public double getRoomRate()
{
return Room_Rate;
}
public int getNumberOfNights()
{
return noOfNights;
}
public int getNumberOfGuests ()
{
return noOfGuests;
}
public static double getPhoneCharges()
{
return Telephone;
}
public static double getTaxRate()
{
return Tax_Rate;
}
public static double getTotalRoomCharges()
{
return TotalRoomCharges;
}
public static double getTotalTelephoneCharges()
{
return TotalTelephoneCharges;
}
public static double getTotalMealCharges()
{
return TotalMealCharges;
}
public static double getTotalTips()
{
return TotalTips;
}
public static double getTotalTax()
{
return TotalTax;
}
public static double getGrossTransaction()
{
return GrossTransaction;
}
}
public class TestHotel
{
public static void main(String[] args)
{
Date d = new Date();
DateFormat df = DateFormat.getDateInstance();
NumberFormat f = NumberFormat.getCurrencyInstance();
//Define customers
Hotel customer1 = new Hotel ("10 - M", 2, 2);
customer1.calculate();
display(customer1, f);
Hotel customer2 = new Hotel ("12 - B");
Hotel customer3 = new Hotel ("12 - C", 2);
customer3.calculate();
customer2.addNights(1);
customer2.calculate();
display(customer2, f);
customer3.addGuest(1);
customer3.calculate();
display(customer3, f);
display (f);
}
static void display (Hotel h, NumberFormat f)
{
//Set up and display heading and date for each receipt
System.out.println("\tThe ABC Cheap Lodging, Inc");
Date d = new Date ();
DateFormat df = DateFormat.getDateInstance();
System.out.println("\tDate: \t" + df.format(d));
//Display expenses line by line including subtotal
System.out.println("Room # \t\t" + h.getRoomNumber());
System.out.println("Room Rate: \t" + f.format(h.getRoomRate()));
System.out.println("Length of Stay:\t" + h.getNumberOfNights() + " Night(s)");
System.out.println("No. of Guests: \t" + h.getNumberOfGuests());
System.out.println("Room Cost: \t" + f.format(h.getAmountDue()));
System.out.println("Tax:" + h.getTaxRate() + "%\t" + f.format(h.getTaxDue()));
System.out.println("\tSubtotal \t" + f.format(h.getSubtotal()));
System.out.println("Telephone \t" + f.format(h.getPhoneCharges()));
System.out.println("Meal Charges \t" + f.format(h.getMeal()));
System.out.println("Tip \t\t" + f.format(h.getTip()));
//Display to total
System.out.println("\nTOTAL AMOUNT DUE\t.........." + f.format(h.getTotal()));
//Display thank you message
System.out.println("\nThanks for staying at The ABC Cheap Lodging, Inc");
System.out.println("\tPlease come again !!!");
System.out.println("\n");
}
static void display (NumberFormat f)
{
System.out.println("\t\t Official Use Only");
System.out.println("\t\t Today's Summary");
System.out.println("\tRoom ....." + f.format(Hotel.getTotalRoomCharges()));
System.out.println("\tTelephone ....." + f.format (Hotel.getTotalTelephoneCharges()));
System.out.println("\tMeal ....." + f.format (Hotel.getTotalMealCharges()));
System.out.println("\tTips ....." + f.format (Hotel.getTotalTips()));
System.out.println("\tTax ....." + f.format (Hotel.getTotalTax()));
System.out.println("\t------------------------------\n");
System.out.println("\tGross Transaction .." + f.format (Hotel.getGrossTransaction()));
System.out.println("Process completed.");
}
}
答案:
import math
x = 10
y = 20
z = 15
def trap(a, b, h):
area = ((a+b)/2)*h
return area
print trap(x, y, z)
计算梯形的公式是((基(a)+基(b))/ 2)*高(h)。您可以使用这三个输入定义一个函数来执行此计算 - 即两个基数和一个高度。然后,当您运行该函数时,您可以插入所需的任何变量。
答案 1 :(得分:0)
让 a , b 为梯形的基础, h 高度 。然后这是计算其面积的代码
@media print
{
#volumeAvailable
{
display: none !important;
}
}
- 当然 - 您可以使用原始命令
打印它 trapezoid = (a + b) * h / 2
如果您需要将代码编写为函数,它就像
一样简单 print( trapezoid )
然后你可以使用它,例如克。
def trapezoid( a, b, h ):
return (a + b) * h / 2