C語言練習題:資料型別與變數(C language exercise: data type and variables)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

練習一:變數的賦值
請分別賦值給三個變數x = 111, y = 222, z = 333; 然後輸出此三個變數的總和與乘積。
Exercise 1: Variable assignment
Assign values to the variables x = 111, y = 222, z = 333; Then, print the sum, the product of these three variables.

練習一參考解法:
Exercise 1 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x = 111, y = 222, z = 333;
    printf("%d + %d + %d = %ld\n", x, y, z, (x+y+z));
    printf("%d * %d * %d = %ld\n", x, y, z, (x*y*z));
    return 0;
}


練習二:ASCII 數值
設計一個程式可以輸出 'A'、'a'、'0'、'#' 的 ASCII的數值
Exercise 2: ASCII Values
Design a C program to to display the ASCII values of these symbols: 'A', 'a', '0', '#' 


練習二參考解法:
Exercise 2 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("%c in ASCII: %d\n", 'A', 'A');
    printf("%c in ASCII: %d\n", 'a', 'a');
    printf("%c in ASCII: %d\n", '0', '0');
    printf("%c in ASCII: %d\n", '#', '#');

    return 0;
}

練習三:倍精準浮點數
使用4個倍精準浮點數變數a, b, c, avg,其中a, b, c 請任意指定數值,avg為 a, b, c 加總後的平均。
Exercise 3: Using four double variables: a, b, c, avg. Assign values to a, b, c as you want. Now assign the average of a, b and c to avg. Finally print the average value.

練習三參考解法:
Exercise 3 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>

int main()
{
    double a = 1.1111, b = 2.2222, c = 3.3333;
    double avg = (a+b+c)/3;

    printf("a = %lf, b = %lf, c = %lf\n", a, b, c);
    printf("(%lf + %lf + %lf)/3 = %lf\n", a, b, c, avg);

    return 0;
}

練習四:ASCII 字串
請使用 ASCII 數值來輸出 Learning C is fun.
Exercise 4: ASCII String
Show "Learning C is fun" by using ASCII values.

練習四參考解法:
Exercise 4 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
           76, 101, 97, 114, 110, 105, 110, 103, 32,
           67, 32,
           105, 115, 32,
           102, 117, 110);
    return 0;
}


練習五:精準度位數
馬克在物理、數學、程式設計、英文分別獲得 76.00, 65.25, 83.50, 92.75 的分數,請用C語言算出馬克這四門課的總分與平均,並輸出到小數點後兩位。
Exercise 5:  Precision
Mark got 76.00 on physics, 65.25 on mathematics, 83.50 on C programming design and 92.75 on English. Write a C program to calculate the total and the average of his score on 4 subjects. And print it up to 2 digit after the decimal point.


練習五參考解法:
Exercise 5 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float physic = 76.00, math = 65.25,
        clang = 83.50, eng = 92.75;

    float total = physic + math + clang + eng;
    float average = total / 4;

    printf("Physic:%.2f\tmath:%.2f\tc:%.2f\tEng:%.2f\n",
           physic, math, clang, eng);
    printf("Total:%.2f\taverage:%.2f\n", total, average);
    return 0;
}


練習六:遞增與遞減運算子
下面程式碼輸出結果是什麼?
Exercise 6: Increment and decrement operators
What is the output of the following program?
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i = 0, j = 0;
    j = i++ + ++i;
    printf("%d %d\n", i, j);

    j = i-- + --i;
    printf("%d %d\n", i, j);
    return 0;
}


練習六參考解法:
Exercise 6 solution:
請參考此篇文章的解釋 Difference between Increment and Decrement Operators
Read this post Difference between Increment and Decrement Operators for detailed explanation.


沒有留言:

張貼留言