根据年月日判断星期几的c程序

 /*2019年1月29日*/
 /*作者:石辅寸*/
 /*根據年月日計算星期幾*/
 /* S = {x - 1 [(x-1)/4] - [(x-1)/100] + [(x-1)/400] + C}%7 */
 /* S為星期序數,x為年份,C為當年元旦至當日(包含當日)的天數 */
 #include<stdio.h>
 main()
 {
 	int a,b,c,d,s;
 	printf("清輸入年月日:");
 	scanf("%d%d%d",&a,&b,&c);
 	/*檢驗輸入是否正確*/
 	if(b <= 0 || b > 12)
 	{
 		printf("月份輸入錯誤,請重新輸入!");
 	}
 	if(c <= 0)
 	{
 		printf("日期輸入錯誤,請重新輸入!");
 	}
 	if(c >= 31)
 	{
 		switch(b)
 		{
 			case 1: printf("日期輸入錯誤,請重新輸入!");
 			break;
 			case 3: printf("日期輸入錯誤,請重新輸入!");
 			break;
 			case 5: printf("日期輸入錯誤,請重新輸入!");
 			break;
 			case 7: printf("日期輸入錯誤,請重新輸入!");
 			break;
 			case 8: printf("日期輸入錯誤,請重新輸入!");
 			break;
 			case 10:printf("日期輸入錯誤,請重新輸入!");
 			break;
 			case 12:printf("日期輸入錯誤,請重新輸入!");
 			break;
 		}
 	}
 	if(c >= 30)
 	{
 		switch(b)
 		{
 			case 4: printf("日期輸入錯誤,請重新輸入!");
 			break;
 			case 6: printf("日期輸入錯誤,請重新輸入!");
 			break;
 			case 9: printf("日期輸入錯誤,請重新輸入!");
 			break;
 			case 11: printf("日期輸入錯誤,請重新輸入!");
 			break;
 		}
 	}
 	if((a%4 == 0 && a%100 != 0) || (a%400 == 0))
 	{
 		if(b == 2)
 		{
 			if(c >= 29)
 			{
 			printf("日期輸入錯誤,請重新輸入!");
 			}
 		}
 	}
 	else
 	{
 		if(b == 2)
 		{
 			if(c >= 28)
 			{
 			printf("日期輸入錯誤,請重新輸入!");
 			}
 		}
 	}
 	/*閏年的情況*/
 	if((a%4 == 0 && a%100 != 0) || (a%400 == 0))
 	{
 		switch(b)
 		{
 			case 1: d = c;
 			break;
 			case 2: d = c + 31;
 			break;
 			case 3: d = c + 60;
 			break;
 			case 4: d = c + 91;
 			break;
 			case 5: d = c + 121;
 			break;
 			case 6: d = c + 152;
 			break;
 			case 7: d = c + 182;
 			break;
 			case 8: d = c + 213;
 			break;
 			case 9: d = c + 244;
 			break;
 			case 10:d = c + 274;
 			break;
 			case 11:d = c + 305;
 			break;
 			case 12:d = c + 335;
 			break;
 		}
 	}
 	/*平年的情況*/
 	else
 	{
 		switch(b)
 		{
 			case 1: d = c;
 			break;
 			case 2: d = c + 31 - 1;
 			break;
 			case 3: d = c + 60 - 1;
 			break;
 			case 4: d = c + 91 - 1;
 			break;
 			case 5: d = c + 121- 1;
 			break;
 			case 6: d = c + 152- 1;
 			break;
 			case 7: d = c + 182- 1;
 			break;
 			case 8: d = c + 213- 1;
 			break;
 			case 9: d = c + 244- 1;
 			break;
 			case 10:d = c + 274- 1;
 			break;
 			case 11:d = c + 305- 1;
 			break;
 			case 12:d = c + 335- 1;
 			break;
 		}
 	}
 	s = (a-1 + (a-1)/4 - (a-1)/100 + (a-1)/400 + d)%7;
 	switch(s)
 	{
 		case 0: printf("這一天是週日。\n");
         break;
 		case 1: printf("這一天為週一。\n");
 		break;
 		case 2: printf("這一天是週二。\n");
 		break;
 		case 3: printf("這一天是週三。\n");
 		break;
 		case 4: printf("這一天是週四。\n");
 		break;
 		case 5: printf("這一天是週五。\n");
 		break;
 		case 6: printf("這一天是週六。\n");
 		break;
 	}
 }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
8 242 97 74 37 948 784 998 556 346 276 848 347 686 541 943 304 43 570 563 727 7 143 987 738 686 632 927 406以下是获取网络时间的 C# 代码: ``` using System; using System.Net; public static class NetworkTime { public 408 161 792 176 465 252 100 779 563 441 657 244 620 634 static DateTime GetNetworkTime() { const string ntpServer = "pool.ntp.org"; var ntpData = new byte43 73 290 924 117 632 713 580 710 926 41 508 301 371 70[48]; ntpData[0] = 0x1B; var addresses = Dns.GetHostEntry(ntpServer 93 753 370 225 992 518 530 417 291 503 930 147 639 724 ).AddressList; var ipEndPoint = new IPEndPoint(addresses[0], 123); var socket = new System.Net.Sockets.Socket446 541 744 688 330 694 637 186 37 393 305 660 972 912 492(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.Connect(ipEndPoint); socket.Send(ntpData); socket 583 932 248 121 425 292 741 677 626 320 785 397 916 64 .Receive(ntpData); socket.Close(); ulong intPart = (ulong)ntpData[40] << 24 | (ulong)nt443 13 105 117 711 358 214 684 586 388 15 100 713 421 604pData[41] << 16 | (ulong)ntpData[42] << 8 | ntpData[43]; ulong fract 269 625 312 130 207 327 31 654 627 978 312 263 798 139 Part = (ulong)ntpData[44] << 24 | (ulong)ntpData[45] << 16 | (ulong)414 735 123 472 987 845 400 866 816 132 792 266 91 843 740ntpData[46] << 8 | ntpData[47]; var milliseconds = (intPart * 1000) + (( 166 764 783 442 560 704 277 240 622 219 715 22 720 491 fractPart * 1000) / 0x100000000L); var networkDateTime = (new DateTime(1900,858 506 822 947 744 255 328 292 43 980 205 411 377 934 460 1, 1)).AddMilliseconds((long)milliseconds); return networkDateTime; } } ``` 使用方法如下: `` 511 787 586 515 860 247 664 107 298 522 457 423 984 802 ` DateTime networkTime = NetworkTime.GetNetworkTime(); Console.WriteLine("Network Time: " + networkTime.ToString()); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值