掌握Excel除法运算技能助你提升工作效率
734
2022-05-29
编写一个计算天数的程序,用户从键盘中输入年、月、日,在屏幕中输出此日期是该年的第几天。
C代码:
/*第三天、计算某日是该年的第几天*/ #include
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
结果显示:
python代码,C代码的升级版,可以进行输入判断:
def leap(a): if (a % 4 == 0) & (a % 100 != 0) | (a % 400 == 0): return 1 else: return 0 def number(y,m,d): result = 0 average_year = (31,28,31,30,31,30,31,31,30,31,30,31) #平年的元组 leap_year = (31,29,31,30,31,30,31,31,30,31,30,31) #闰年的元组 if (1 <= y <= 5000) & (1 <= m <= 12) & (1 <= d <=31) & leap(y) & (d <= leap_year[m-1]): for i in range(0,m-1): result += leap_year[i] elif (1 <= y <= 5000) & (1 <= m <= 12) & (1 <= d <=31) & (leap(y) == 0) & (d <= average_year[m-1]): for i in range(0,m-1): result += average_year[i] else: result = 0 d = 0 result += d return result def tranform(contents): if ('年' in contents) & ('月'in contents) & ('日' in contents) & (' ' not in contents): str_len = len(contents) for i in range(1,str_len): if contents[i] == '年': year = int(contents[0:i]) #input()接收的是字符串 year_num = i + 1 if contents[i] == '月': month = int(contents[year_num:i]) #用int()强制转换成整型 month_num = i + 1 if contents[i] == '日': day = int(contents[month_num:i]) return (year,month,day) else: return 0 choose = 1 while choose: contents = input('请输入要查询的日期,查询范围公元1年-公元5000年,例如:1993年1月30日\n') t = tranform(contents) if t != 0: result = number(t[0],t[1],t[2]) if result != 0: print('第%d天' %(result)) while True: choose = input('输入‘是’继续查询,输入‘否’放弃查询\n') if ('是' in choose) | ('否' in choose) & (len(choose) == 1): if '是' in choose: choose = 1 break else: choose = 0 break else: print('输入选择错误,请重新输入\n') else: print('输入日期错误,请重新输入\n') else: print('输入格式错误,请重新输入\n')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
结果显示:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。