请使用函数编写程序 [英] writing a program using functions help please

查看:71
本文介绍了请使用函数编写程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Zeller的同余是一种计算星期几的算法。公式为:



h =(q +⌊26(m + 1)10⌋+ k +⌊k4⌋+⌊j4⌋+ 5j)%7

其中:


$ b $b⌊...⌋表示楼层划分

h是星期几(0 - 星期六,1 - 星期日) ,2 - 星期一,3 - 星期二,4 - 星期三,5 - 星期四,6 - 星期五)

q是月份的日子

m是月份(3 - 3月) ,4 - 4月,... 12 - 12月)。 1月和2月计为上一年的第13和第14个月,例如对于2013年1月25日,您将使用2012年第13个月的第25天。

j是世纪,例如20 for 2013

k是本世纪的一年,例如2013年13月。

使用提示用户输入年份(例如2008年),月份(例如1-12)和月中某天(例如1-31)的功能编写程序,然后显示星期几的名称。例如,对于2013年,第1个月和第25个月的某天,星期几是星期五;用户输入2013,1和25以响应您的提示,然后计算h为6,然后输出星期五。运行该程序五个不同的日期。测试必须包括不同的月份,包括1月和2月,不同的世纪和不同的年份。





现在对于这个问题,我使用了2个模块: 1是功能模块,1是我的主程序。



功能模块:

 h =(q +(26 * m + 1))/((  10 )+ k +(k / 4)+(j / 4)+(5 * j))%7 
if h == 1:
day = Sunday
elif h == 2:
day = Monday
elif h == 3:
day = Tuesday
elif h = = 4:
day = Wednesday
elif h == 5:
day = Thursday
elif h == 6:
day = 星期五
else:
day = 星期六
返回 d ay







主程序模块:



 import q5_function 

q = int(输入( 输入月中的某一天))
m = int(输入( 输入月份 ))
j = int(输入( 输入世纪))
k = int(输入( 输入世纪年份))
h = q5_function.func_day(q,m,j,k)
print( 日期为{0 } .format(h))







现在如果我尝试运行这个程序它一直打印星期六,没有任何els。

我怎样才能使公式起作用?

解决方案

嗨兄弟,实际上你使用的公式是错误的。使用这个公式(参考维基百科)。



  int  h1 =( q +(( 13  *(m +  1 ))/  5 )+ k +(k /  4 )+(j /  4 )+ ( 5  * j))% 7 ; 





我在c#.NET中实现了相同的功能,它完美无缺......!


这是一个似乎有用的功能(我还没有测试过)针对过去一百年中的每一个日期):

  def  dayname(日期,月份,年份):  日,月,年全年 
如果(月< 3 ):
1月和2月是上一年的第13和第14个月
月+ = 12
year - = 1 ;

将世纪和年份分开来
j = int (年/ 100
k = int(年% 100

计算星期几
dow = int((date +(( 13 *(月+ 1 ))/ 5 )+ k + int(k / 4 )+ int(j / 4 )+(< span class =code-digit> 5
* j))% 7

if dow == 1
day = 星期日
elif dow == 2
day = 星期一
elif dow = = 3
day = Tuesday
elif dow == 4
day = Wednesday
elif dow = = 5
day = Thursday
elif dow == 6
day = Friday
else
day = 星期六

RET urn day



调用函数如:

  import  dayname 
print ' 今天是',dayname.dayname( 9 2 2015 ))


Zeller's congruence is an algorithm to calculate the day of the week. The formula is:

h=(q+⌊26(m+1)10⌋+k+⌊k4⌋+⌊j4⌋+5j)%7
where:

⌊…⌋ indicates floor division
h is the day of the week (0 - Saturday, 1 - Sunday, 2 - Monday, 3 - Tuesday, 4 - Wednesday, 5 - Thursday, 6 - Friday)
q is the day of the month
m is the month (3 - March, 4 - April, ... 12 - December). January and February are counted as months 13 and 14 of the previous year, e.g. for January 25 2013, you would use the 25th day of the 13th month in 2012.
j is the century, e.g. 20 for 2013
k is the year of the century, e.g. 13 for 2013.
Write a program using functions that prompts the user to enter a year (e.g. 2008), month (e.g. 1-12), and day of the month (e.g. 1-31), and then displays the name of the day of the week. For example, for year 2013, month 1, and day of month 25, the day of the week is Friday; the user inputs 2013, 1, and 25 in response to your prompts and then you calculate h which will be 6, and then output Friday. Run the program for five different dates. Testing must include different months including January and February, different centuries, and different years.


Now for this question i used 2 modules: 1 being the function module and 1 being my main program.

Function module:

h =(q+(26*m+1))/((10)+k+(k/4)+(j/4)+(5*j))%7
   if h==1:
       day="Sunday"
   elif h==2:
       day="Monday"
   elif h==3:
       day="Tuesday"
   elif h==4:
       day="Wednesday"
   elif h==5:
       day="Thursday"
   elif h==6:
       day="Friday"
   else:
       day="Saturday"
   return day




Main program module:

import q5_function

q = int (input("enter day of the month"))
m = int (input ("enter the month"))
j = int (input("enter the century"))
k = int (input("enter the year of the century"))
h=q5_function.func_day(q, m, j, k)
print ("the day is {0}".format(h))




Now if i try and run this program it keeps printing "saturday" and nothing els.
How can i get the formula to work?

解决方案

Hi bro, actually formula you used was wrong. Use this formula (refer wikipedia).

int h1 = (q + ((13 * (m + 1)) / 5) + k + (k / 4) + (j / 4) + (5 * j)) % 7;



I implemented the same in c#.NET, It works perfects...!


Here is a function that seems to work (I have not tested against every date in the last hundred years):

def dayname(date, month, year):   # Day, Month, Year in full
    if (month < 3):
        # January and February are months 13 and 14 of the previous year
        month += 12
        year -= 1;
        
    # separate out the century and year parts
    j = int(year / 100)
    k = int(year % 100)
    
    # calculate the day of the week
    dow = int((date + ((13 * (month + 1)) / 5) + k + int(k / 4) + int(j / 4) + (5 * j)) % 7)

    if dow == 1:
        day = "Sunday"
    elif dow == 2:
        day = "Monday"
    elif dow == 3:
        day = "Tuesday"
    elif dow == 4:
        day = "Wednesday"
    elif dow == 5:
        day = "Thursday"
    elif dow == 6:
        day = "Friday"
    else:
        day = "Saturday"

    return day


Call the function like:

import dayname
print('Today is', dayname.dayname(9,2,2015))


这篇关于请使用函数编写程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆