程式設計可以改變您的未來(Programming can change your future)。
雲林SONG 全名為雲林軟體工程(SOftware eNGineering),目標致力於軟體人才的培養並推廣開源軟體落實於資訊教育。程式設計的觀念是軟體產品的基礎,程式碼就像沙子一樣,要紮實,所建立出來的高塔才會穩固。本站也提供資訊教育相關的教學資源。
YunlinSONG stands for Yunlin SOftware eNGineering, offering tutorial for computer programming and promoting open-source software.
Teaching resources in information technology education are provided here.
/*Input a string and displays it.Author: Holan*/#include <stdio.h>#include <stdlib.h>intmain()
{
constint SIZE =100;
char str[SIZE];
printf("Input a string:");
// Get any string with space method 1
scanf("%[^\n]%*c", str);
printf("Your string: %s\n", str);
printf("Input a string:");
// Get any string with space method 2
gets(str);
printf("Your string: %s\n", str);
printf("Input a string:");
// Get any string with space method 3
fgets(str, SIZE, stdin);
printf("Your string: %s\n", str);
return0;
}
練習二:字串長度 撰寫一可以算出字串長度的程式,請不要使用strlen()函數。
Exercise 2: String's Length
Write a program to find the length of a string without using strlen() function.
/*Find the length of a string without using strlen() function.Author: Holan*/#include <stdio.h>#include <stdlib.h>intmain()
{
constint SIZE =100;
char str[SIZE], cnt =0;
printf("Input a string:");
// Get any string with space method 1//scanf("%[^\n]%*c", str);// Get any string with space method 2//gets(str);// Get any string with space method 3
fgets(str, SIZE, stdin);
while(str[cnt] !='\0'&&++cnt < SIZE);
printf("Length:%d\n", cnt);
return0;
}
練習三:字元
設計可以將一字串的每個字元以空白區隔做輸出。
Exercise 3: Characters Design a program to split a string into characters.
If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.
練習一:加法函數 設計一函數可以輸入兩個參數a與b,並回傳 a 加 b的結果。
Exercise 1: Adding function
Design a function that takes two parameters and return the result of a + b.
練習一參考解法: Exercise 1 solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>#include <stdlib.h>floatadd(float a, float b)
{
return a + b;
}
intmain()
{
float x =3.4f, y =3.6f;
printf("%.2f + %.2f = %.2f\n", x, y, add(x, y));
return0;
}
練習二:整數乘法 設計一函數可以輸入兩個參數a與b,並回傳 a 乘 b的結果。
Exercise 2: Multiplying Two Integers Design a function that takes two parameters and return the result of a * b.
練習二參考解法: Exercise 2 solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>#include <stdlib.h>intmultiply(int a, int b)
{
return a * b;
}
intmain()
{
int x =3, y =4;
printf("%3d + %3d = %3d\n", x, y, multiply(x, y));
return0;
}
練習三:奇數或偶數
設計一函數可以輸入一個整數,函數回傳數值 1 表示是奇數,回傳數值 0 表示偶數。
Exercise 3: Odd or Even? Design a function that takes one integer. It will return 1 if the integer is odd, return 0 if the integer is even.
/*Find the maximum in an array.Author: Holan*/#include <stdio.h>#include <stdlib.h>doublegetMax(double numbers[], int size)
{
// the maximum numberdouble m =-1e9;
for(int i =0; i < size; i++)
if(numbers[i] > m)
m = numbers[i];
return m;
}
intmain()
{
double data[] = {1, 3.23, 333, -334.5, 363};
int size =sizeof(data) /sizeof(double);
printf("Max:%.2lf\n", getMax(data, size));
return0;
}
練習八:總和 設計可以加總陣列中所有數值的函數。
Exercise 8: Sum Design a function that find sum of all elements in an array.
/*Find sum of all elements in an arrayAuthor: Holan*/#include <stdio.h>#include <stdlib.h>doublegetSum(double numbers[], int size)
{
// The totaldouble total =0;
for(int i =0; i < size; i++)
total += numbers[i];
return total;
}
intmain()
{
double data[] = {1, 3.23, 333, -334.5, 363};
int size =sizeof(data) /sizeof(double);
printf("Max:%.2lf\n", getSum(data, size));
return0;
}
練習九:第幾象限
設計一個可以判斷(x, y)座標在第幾象限的函數。
Exercise 9: Coordinate
Design a function that prints the quadrant of the given point(x, y).
/*Prints the quadrant of the given point(x, y).Author: Holan*/#include <stdio.h>#include <stdlib.h>voidgetQuadrant(double x, double y)
{
if(x >0&& y >0)
{
printf("First");
}
elseif(x <0&& y >0)
{
printf("Second");
}
elseif(x <0&& y <0)
{
printf("Third");
}
elseif(x >0&& y <0)
{
printf("Fourth");
}
printf(" Quadrant.\n");
}
intmain()
{
double x =1.0, y =1.0;
getQuadrant(x,y);
x =-1.0;
getQuadrant(x,y);
y =-1.0;
getQuadrant(x,y);
x =1.0;
getQuadrant(x,y);
練習十:費氏數 設計一個可以算出第 N個費氏數的函數。
Exercise 10: Design a function that find the n-th Fibonacci number.