我需要减去由操作员输入的度数,分钟和秒数的2个角度。例如:30度50分45秒 - 10度20分53秒= 20度29分52秒。但我的代码返回32767分钟。
这是我的代码:
#include <stdio.h>
int main(int argc, char **argv)
{
int g1,m1,s1; //timp 1
int g2,m2,s2; //timp 2
int g,m,s; //calcule
printf("Interval1:"); scanf("%d%d%d",&g1,&m1,&s1);
printf("Interval2:"); scanf("%d%d%d",&g2,&m2,&s2);
if(s1<s2) {
s=(s1+60)-s2;
m1=m1-1;
} else if(m1<m2) {
m=(m1-60)-m2;
g1=g1-1;
} else {
s=s1-s2;
m=m1-m2;
}
g=g1-g2;
printf("%d %d %d",g,m,s);
return 0;
}
回报是:
Interval1:30 50 45
Interval2:10 20 53
20 32767 52
------------------
(program exited with code: 0)
Press return to continue
答案 0 :(得分:0)
这没关系?
#include <stdio.h>
int main(int argc, char **argv)
{
int g1,m1,s1; //timp 1
int g2,m2,s2; //timp 2
int g,m,s,mt,gt; //calcule
printf("Interval1:"); scanf("%d%d%d",&g1,&m1,&s1);
printf("Interval2:"); scanf("%d%d%d",&g2,&m2,&s2);
if(s1<s2 && m1<m2) {
s=(s1+60)-s2;
mt=m1-1;
m=mt-m2;
g=g1-g2;
m=(m1+60)-m2;
gt=g1-1;
g=gt-g2;
} else if(s1<s2) {
s=(s1+60)-s2;
mt=m1-1;
m=mt-m2;
g=g1-g2;
} else if(m1<m2) {
m=(m1+60)-m2;
gt=g1-1;
g=gt-g2;
s=s1-s2;
} else {
m=m1-m2;
s=s1-s2;
g=g1-g2;
}
printf("%d %d %d",g,m,s);
return 0;
}
答案 1 :(得分:0)
不那么复杂:
secs2gms(gms2secs(g1, m1, s1) - gms2secs(g2, m2, s2), &g, &m, &s);
...
long gms2secs(int g, int m, int s) {
return g * 3600L + m * 60 + s;
}
void secs2gms(long secs, int *g, int *m, int *s) {
*s = secs % 60;
secs /= 60;
*m = secs % 60;
*g = secs / 60;
}