C primer Plus 編程練習

C Primer Plus 編程練習(一)

第三章,問題2、5、7、8

直接上代碼了,有一部分是從Jimmy Chen 的blog上學的。

要是有什麼問題,特別希望能賜教哦,

代碼見下面:

#include <stdio.h>
#define InchPerCen 2.54             //一英寸相當於2.54釐米
#define SecondPerYear 3.156e7       //學習命名規範:秒和年的換算
#define PintPerCup 2
#define CupPerOunce 8
#define OuncePerBigSpoon 2
#define BigSpoonPerTeaSpoon 3       //Every BigSpoon equals to three TeaSpoon

void p3_2(void)
{
    int ASCII = 0;
    printf("Please enter an ASCII Number:");
    scanf("%d",&ASCII);                             //最近有的時候會忘了改變變量地址上的值從而一直使用變量初值
    printf("%d\n",ASCII);
    //printf("%d\n",(char)ASCII);                 //第一遍寫的bug,下意識的一直用%d
    printf("%c\n",(char)ASCII);                                                                //Convert to character directly

    return;
}
void p3_5(void)                 //學習模塊化的編程方法,利用在主函數外聲明函數,在主函數中調用
{
    int age = 0,SecondsNumAge = 0;
    printf("Please Enter your age:");
    scanf("%d",&age);
    SecondsNumAge = age*SecondPerYear;
    printf("%d\n",SecondsNumAge);

    return;
}

void p3_7(void)
{
    float HeightCen = 0,HeightInch = 0;

    printf("Please Enter your height in inch:");
    scanf("%f",&HeightInch);
    HeightCen = HeightInch*InchPerCen;
    printf("%f\n",HeightCen);

    return;
}

void p3_8(void)
{
    int CupNum = 0;
    float Pint = 0,Ounce = 0, BigSpoon = 0, TeaSpoon = 0;
    printf("Please Enter the Number of Cups:");
    scanf("%d",&CupNum);
    Pint = CupNum/PintPerCup;
    Ounce = CupNum*CupPerOunce;
    BigSpoon = Ounce*OuncePerBigSpoon;
    TeaSpoon = BigSpoon*BigSpoonPerTeaSpoon;
    printf("%f %f %f %f\n",Pint,Ounce,BigSpoon,TeaSpoon );

    return;
}

int main()
{
    p3_2();
    getchar();

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章