#include <stdio.h>
int main() {
int a;
printf("請輸入一個整數:");
scanf("%d", &a);
if (a == 0) {
printf("您輸入的數字是0\n");
} else {
printf("您輸入的數字不是0\n");
}
}
執行結果:
請輸入一個整數:12
您輸入的數字不是0
請輸入一個整數:0
您輸入的數字是0
說明:
printf("請輸入一個整數:"); 請你在鍵盤上輸入一個整數,輸入完就按 Enter。
scanf("%d", &a); 把你輸入的數字以整數的形態 %d 放到 a 。
2017年5月26日 星期五
流程控制,使用 if ,else if, else 來判斷 a,b 的大小
#include <stdio.h>
int main() {
int a, b;
// 提示使用者輸入資料
printf("請輸入二個整數:");
scanf("%d%d", &a, &b);
if (a > b) {
printf("%d > %d\n", a, b);
} else if (a < b) {
printf("%d < %d\n", a, b);
} else {
printf("%d == %d\n", a, b);
}
}
執行結果:
請輸入二個整數:12 16
12 < 16
int main() {
int a, b;
// 提示使用者輸入資料
printf("請輸入二個整數:");
scanf("%d%d", &a, &b);
if (a > b) {
printf("%d > %d\n", a, b);
} else if (a < b) {
printf("%d < %d\n", a, b);
} else {
printf("%d == %d\n", a, b);
}
}
執行結果:
請輸入二個整數:12 16
12 < 16
2017年5月23日 星期二
使用 fgets 和 sscanf 從鍵盤輸入三個數字,再求出 a+b>c 的值
#include <stdio.h>
int main()
{
float a, b, c ;
char buffer[80];
printf("請輸入三個浮點數: ");
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%f %f %f", &a, &b, &c);
printf("%f + %f >%f = %d\n", a,b,c,a+b>c);
return 0;
}
執行結果:
請輸入三個浮點數: 12.000 13.000 15.000
12.000000 + 13.000000 >15.000000 = 1
說明:
如果a+b>c正確,得到的結果是1,錯誤則是0。
int main()
{
float a, b, c ;
char buffer[80];
printf("請輸入三個浮點數: ");
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%f %f %f", &a, &b, &c);
printf("%f + %f >%f = %d\n", a,b,c,a+b>c);
return 0;
}
執行結果:
請輸入三個浮點數: 12.000 13.000 15.000
12.000000 + 13.000000 >15.000000 = 1
說明:
如果a+b>c正確,得到的結果是1,錯誤則是0。
使用 stdlib.h 函式庫的 rand() 取得二個亂數,比較它們的大小
使用 a= rand(),b= rand() 隨機取得整數之後,比較之。若 a<b,正確則傳回值為1, 錯誤則傳回0。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b;
a= rand();
b= rand();
printf("a=%d b=%d a<b=%d\n",a,b,a<b);
return 0;
}
執行結果:
a=41 b=18467 a<b=1
說明:
這樣也可以 printf("%d < %d = %d\n",a,b,a<b);
使用上面的程式有問題,每次執行的結果都是一樣的 a=41 b=18467,可見這個程式不可以取得亂數,應該使用下面的程式才可。
#include <stdio.h>
#include <stdlib.h> /* 亂數相關函數 */
#include <time.h> /* 時間相關函數 */
int main(){
/* 設定亂數種子 */
srand( time(NULL) );
/* 產生亂數 */
int a = rand();
int b = rand();
printf("a= %d b= %d a<b= %d\n", a,b,a<b);
return 0;
}
執行結果:
a= 3117 b= 26387 a<b= 1
a= 3172 b= 12502 a<b= 1
a= 3234 b= 20114 a<b= 1
但也奇怪?每次都是 a<b 似乎也有點不對!
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b;
a= rand();
b= rand();
printf("a=%d b=%d a<b=%d\n",a,b,a<b);
return 0;
}
執行結果:
a=41 b=18467 a<b=1
說明:
這樣也可以 printf("%d < %d = %d\n",a,b,a<b);
使用上面的程式有問題,每次執行的結果都是一樣的 a=41 b=18467,可見這個程式不可以取得亂數,應該使用下面的程式才可。
#include <stdio.h>
#include <stdlib.h> /* 亂數相關函數 */
#include <time.h> /* 時間相關函數 */
int main(){
/* 設定亂數種子 */
srand( time(NULL) );
/* 產生亂數 */
int a = rand();
int b = rand();
printf("a= %d b= %d a<b= %d\n", a,b,a<b);
return 0;
}
執行結果:
a= 3117 b= 26387 a<b= 1
a= 3172 b= 12502 a<b= 1
a= 3234 b= 20114 a<b= 1
但也奇怪?每次都是 a<b 似乎也有點不對!
使用 stdlib.h 函式庫的 rand() 函式,將兩個隨機整數(亂數)相加
使用 stdlib.h 函式庫的 rand() 函式,取得兩個隨機整數a,b,然後讓它們相加。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b;
a= rand();
b= rand();
printf("a=%d b=%d a+b=%d\n",a,b,a+b);
return 0;
}
執行結果:
a=41 b=18467 a+b=18508
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b;
a= rand();
b= rand();
printf("a=%d b=%d a+b=%d\n",a,b,a+b);
return 0;
}
執行結果:
a=41 b=18467 a+b=18508
2017年5月20日 星期六
使用 fgets 和 sscanf 來計算長方形面積
#include <stdio.h>
int main()
{
int a, b, area;
char buffer[80];
printf("請輸入寬和高: ");
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%4d %4d", &a, &b);
area = a * b;
printf("寬=%d 高=%d 面積=%d\n", a,b,area);
return 0;
}
執行結果:
請輸入寬和高: 20 30
寬=20 高=30 面積=600
int main()
{
int a, b, area;
char buffer[80];
printf("請輸入寬和高: ");
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%4d %4d", &a, &b);
area = a * b;
printf("寬=%d 高=%d 面積=%d\n", a,b,area);
return 0;
}
執行結果:
請輸入寬和高: 20 30
寬=20 高=30 面積=600
2017年5月19日 星期五
從鍵盤輸入字串,使用 fgets 和 sscanf 把三個數相加
我用 fgets 輸入了三個字串至 buffer,又用 sscanf 把 buffer 的值輸入到 a,b,c 三個變數中,再將它們加起來。
#include <stdio.h>
int main()
{
int a, b, c, sum;
char buffer[80];
printf("請輸入三個字串: ");
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%4d %4d %4d", &a, &b, &c);
sum = a + b + c;
printf("a=%d b=%d c=%d sum=%d\n", a,b,c,sum);
return 0;
}
執行結果:
請輸入三個字串: 12 345 6789
a=12 b=345 c=6789 sum=7146
#include <stdio.h>
int main()
{
int a, b, c, sum;
char buffer[80];
printf("請輸入三個字串: ");
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%4d %4d %4d", &a, &b, &c);
sum = a + b + c;
printf("a=%d b=%d c=%d sum=%d\n", a,b,c,sum);
return 0;
}
執行結果:
請輸入三個字串: 12 345 6789
a=12 b=345 c=6789 sum=7146
訂閱:
文章 (Atom)