#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>//猜數字遊戲
#include<stdlib.h>
#include<time.h>
void menu(){
printf("##############\n");
printf("####1.play####\n");
printf("####2.exit####\n");
printf("##############\n");
}
void game(){
int random_num = rand() % 100 + 1;
int input = 0;
while (1){
printf("請輸入你猜數字:");
scanf("%d", &input);
if (input > random_num){
printf("你猜大了");
}
else if (input < random_num){
printf("你猜小了:");
}
else{
printf("恭喜你猜對了:");
break;
}
}
}
int main(){
int input = 0;
srand((unsigned)time(NULL));
do{
menu();
printf("請輸入你的選擇:");
scanf("%d", &input);
switch (input){
case 1:
game();
break;
case 0:
break;
default:
printf("選擇錯誤,請從新選擇:");
break;
}
}
while (input);
return 0;
}
#include<stdio.h>//小寫字母變大寫
#include<stdlib.h>
void main(){
char*p,c;
p = &c;
while (*p){
printf("請輸入一個小寫字母:");
*p = getchar();
*p = *p - 32;
printf("%c\n", *p);
}
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main(){
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int left = 0;
int right = sizeof(arr) / sizeof(arr[0]) - 1;
int key;
int mid = 0;
printf("請輸入一個小於十的數:");
scanf("%d", &key);
while (left <= right){
mid = (left + right) / 2;
if (arr[mid] > key){
right = mid - 1;
}
else if (arr[mid] < key){
left = mid + 1;
}
else{
printf("%d\n", mid);
break;
}
}
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h>//編寫代碼模擬三次輸入密碼場景 #include<stdlib.h> int main(){ int i = 1; int num = 888; int j; for (i = 1; i <= 3; i++){ printf("請輸入密碼:"); scanf("%d", &j); if (j == num){ printf("登陸成功:"); break; } else { printf("密碼錯誤:"); } } if (i == 4){ printf("登錄失敗:"); } system("pause"); return 0; }