数组问题求帮帮
本帖最后由 baivip 于 2019-4-1 20:29 编辑1 #include <stdio.h>
2
3 int main()
4 {
5 unsigned char Sr,Sq;
6 int Year,Time,My,Mt,Md,Dy,Dt,i,M[] = {1,31,2,28,3,31,4,30,5,31,6,30,7,31,8,31,9,30,10,31,11,30,12,31};
7
8 printf("请输入您的生日(如1988-05-20):");
9 scanf("%s",Sr);
10 printf("请输入当前日期(如2019-03-31):");
11 scanf("%s",Sq);
12
13 Year = (Sr-'0')*1000+(Sr-'0')*100+(Sr-'0')*10+Sr-'0';
14 My = (Sr-'0')*10+Sr-'0';
15 Dy = (Sr-'0')*10+Sr-'0';
16 Time = (Sq-'0')*1000+(Sq-'0')*100+(Sq-'0')*10+Sq-'0';
17 Mt = (Sq-'0')*10+Sq-'0';
18 Dt = (Sq-'0')*10+Sq-'0';
19
20 for (;Year <= Time ; Year++)
21 {
22 if ( (Year % 4 == 0 && Year % 100 != 0 ) || Year % 400 == 0 )
23 {
24 for ( i = 0 ; i < 24 ; i+=2 )
25 {
26 if ( i == 2)
27 {
28 Md = Md + M+1;
29 }
30 Md = Md + M;
31 }
32 }
33 else
34 {
35 for ( i = 0 ; i < 24 ; i+=2 )
36 {
37 Md = Md + M;
38 }
39 }
40 }
41
42 printf("你在这个世界上总共生存了%d天%d\n",Year,Time);
43 printf("Sr0=%dSq0=%c\n",Sr,Sq);
44 printf("Sr1=%cSq1=%c\n",Sr,Sq);
45 printf("Sr2=%cSq2=%c\n",Sr,Sq);
46 printf("Sr3=%cSq3=%c\n",Sr,Sq);
47 printf("Sr4=%cSq4=%c\n",Sr,Sq);
48 printf("Sr5=%cSq5=%c\n",Sr,Sq);
49 printf("Sr6=%cSq6=%c\n",Sr,Sq);
50 printf("Sr7=%cSq7=%c\n",Sr,Sq);
51 printf("Sr8=%cSq8=%c\n",Sr,Sq);
52 printf("Sr9=%cSq9=%c\n",Sr,Sq);
53 printf("Sr10=%cSq10=%c\n",Sr,Sq);
54 printf("Sr11=%cSq11=%c\n",Sr,Sq);
55 printf("Sr=%pSq=%p\n",Sr,Sq);
56 printf("Sr=%pSq=%p\n",Sr+0,Sq+0);
57 printf("Sr=%pSq=%p\n",Sr+1,Sq+1);
58 printf("Sr=%pSq=%p\n",Sr+2,Sq+2);
59 printf("Sr=%pSq=%p\n",Sr+3,Sq+3);
60
61 return 0;
62 }
以上代码,为什么Year的值不正确??高手帮忙下拉,只要修改Time = (Sq-'0')*1000+(Sq-'0')*100+(Sq-'0')*10+Sq-'0';为Time = (Sq-'0')*1000+(Sq-'0')*100+(Sq-'0')*10+Sq-'0';
Year的值就正确了,但Time又出错,输出数组中的每位数均未发现异常,数据正确。
逐一调试
#include <stdio.h>
int main()
{
signed char Sr;
int Year;
printf("请输入当前日期(如2019-03-31):");
scanf("%s",Sr);
Year = (Sr-'0')*1000+(Sr-'0')*100+(Sr-'0')*10+Sr-'0';
printf("YEAR=%d\n",Year);
return 0;
}
/*
请输入当前日期(如2019-03-31):2019-03-31
YEAR=2019
请按任意键继续. . .
*/ 你这是一个数字你弄两个看看直接复制我的代码运行一下吧 一个数组正常两个会出错的 顶一下
页:
[1]