[程設習題] C Primer Plus Ch5 #3

3.     請寫一個程式,它會問使用者要打入一個天數,然後它會轉換成幾星期又幾天。譬如,如果你要轉換18天能為2週又4天,顯示的樣子如下:


18 days are 2 weeks, 4 days.


使用while迴圈讓他能夠不斷的讓使用者要打入數字,當打入一個不是正數的值時(例如0-20)結束迴圈。


#include “stdafx.h”


#include <stdio.h>                                                         //引入stdio.h


#include <stdlib.h>                                                        //引入stdlib.h


int main(int argc, char* argv[])


{


    int day, state;                                                        //宣告2個變數daystate


    do{                                                                    //讓程式先執行,之後做while迴圈


       printf(“Please enter the number of days ( <=0 to quit):\n”);


       state=scanf(“%d”, &day);                                            //讓使用者打入天數,讓scanf()傳回有幾個資料輸入成功


       if (day > 0 && state == 1)                                          //判斷day是否大於0scanf()是否有資料


          printf(“%d days are %d weeks, %d days.\n”, day , day/7, day%7);  //印出運算結果


    }while(day > 0 && state == 1);


    printf(“\n”);                                                         


    system(“PAUSE”);                                                       //「按任意鍵繼續」的程式,讓程式暫停


    return 0;                                                              //函數結束,傳回整數並跳回原本呼叫的地方


}