2005年04月16日
C
■課題内容
例えば、17時28分30秒と13時50分20秒の差を求めるように、
二時刻間の差を求めよ。
つまり、60進数同士の差を求めるコードを書けという事ですね。
ただし、キャストを使うらしい。
■前提
型の種類,printf,scanfしか習っていない
過去の時間と未来の時間は逆に入力される事は無いとする、らしい。
■ソース
#include <stdio.h> int main(void) { int temp; int f_h,f_m,f_s,f_temp; //future int p_h,p_m,p_s,p_temp; //past int r_h,r_m,r_s,r_temp; //result printf("時,分,秒\n"); scanf("%d,%d,%d",&f_h,&f_m,&f_s); //未来の時間を入力 scanf("%d,%d,%d",&p_h,&p_m,&p_s); //過去の時間を入力 //10進数に変換 f_temp = f_s + f_m*60 + f_h*60*60; p_temp = p_s + p_m*60 + p_h*60*60; //差を出す temp = f_temp - p_temp; //60進数に変換 r_s=temp%60; temp=temp/60; r_m=temp%60; temp=temp/60; r_h=temp; printf("%d時%d分%d秒",r_h,r_m,r_s); return 0; }
■問題点
どこでキャストを使うのかがわからない……。
投稿者 miff : 2005年04月16日 00:41