用到的头文件
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
基本输出
int Jntm(int aAge, int aHeight)
{
printf("10年后您的年龄是:%d岁, 身高是%d厘米.\r\n", aAge + 10, aHeight + 30);
return aAge + aHeight;
}
利用两人的身高计算树的高度
/**
* @brief 利用两人的身高计算树的高度
*
* @param void
*/
void CalTreeHeight(void)
{
int chenchen_high = 0;
int laoba_high = 0;
int tree = 0;
int c_l = 0;//此为距离,表示晨晨到老爸之间的距离.
int l_t = 0;//老爸到树之间的距离.
printf("以厘米为单位.");
printf("\r\n晨晨的身高是: ");
scanf("%d", &chenchen_high);
printf("老爸的身高是: ");
scanf("%d", &laoba_high);
printf("晨晨到老爸的距离: ");
scanf("%d", &c_l);
printf("老爸到树的距离: ");
scanf("%d", &l_t);
tree = chenchen_high + (laoba_high - chenchen_high) * (c_l + l_t) / c_l;
printf("树的高度是%d厘米", tree);
}
单位换算
void ConvertCm(void)
{
float li = 0.00;
float hi = 0.00;
float fi = 0.00;
float i = 0.00;
printf("请输入树有多少厘米: ");
scanf("%e", &li);
i = li / 100;
hi = li * 10;
fi = li / 10;
printf("转化为米是%.2f,分米是%.2f,毫米是%.0f.", i, fi, hi);
}
计算器
void Calculator(void)
{
double number1 = 0.0;
double number2 = 0.0;
char operation = 0;
char i = 0;
there:
printf("\r\n请输入算式: ");
scanf("%lf %c %lf", &number1, &operation, &number2);
switch (operation)
{
case '+':
printf("= %lf\r\n",number1 + number2);
break;
case '-':
printf("= %lf\r\n", number1 - number2);
break;
case '*':
printf("= %lf\r\n", number1 * number2);
break;
case '/':
if(number2 == 0)
{
printf("\r\n你输错了,请重新输入.");
}
else
{
printf("= %lf\r\n", number1 / number2);
}
break;
case '%':
if((long)number2 == 0)
{
printf("\r\n你输错了.");
}
else
{
printf("= %ld\r\n", (long)number1 % (long)number2);
}
break;
default:
printf("\r\n你又输错了.");
break;
}
getchar();
printf("\r\n你想执行另一个计算吗,那就摁'y'或'Y',若你想跳出,就摁'n'或'N'吧. ");
scanf("%c", &i);
i = tolower(i);
switch (i)
{
case 'y':
goto there;
//TODO
break;
case 'n':
goto that;
//TODO
break;
default:
//TODO
break;
}
that:
return;
}
猜数字游戏
/**
* @brief 猜数字游戏
*
* @param void
*/
void GuessGame(void)
{
int chosen = 0;
int guess = 0;
int count = 5;
int limit = 20;
srand(time(NULL));
chosen = 1 + rand() %limit;
printf("\r\n这是一个猜数字游戏.");
printf("\r\n这个数是在1到20之间.");
for(; count > 0;--count)
{
printf("\r\n猜的次数有%d,你现在还剩%d次.", count, count);
printf("\r\n你猜得数是: ");
scanf("%d", &guess);
if(guess < 1 || guess > 20)
{
printf("\r\n你猜的和我说的不一样,请重新来.");
continue;
}
if(guess < chosen)
{
printf("\r\n你猜得小了.");
}
else if(guess > chosen)
{
printf("\r\n你猜得大了.");
}
else
{
printf("\r\n你猜对了");
return;
}
}
}
记数字游戏
/**
* @brief 记数字游戏
*
* @param void
*/
void Remember(void)
{
char another_game = 'Y';
int correct = false;
int counter = 0;
int sequence_length = 0;
time_t seed = 0;
int number = 0;
time_t now = 0;
int time_taken = 0;
printf("\r\nTo play Simple Simon, watch the screen for a sequence of digits. ");
printf("\r\nWatch carefully, as the digits are only displayed for a second!\r\n");
scanf("%c", &another_game);
do{
correct = true;
counter = 0;
sequence_length = 2;
time_taken = clock();
while(correct)
{
sequence_length += counter++%3 == 0;
seed = time(NULL);
now = clock();
srand((unsigned int)seed);
for(int i = 1; i <= sequence_length;i++)
{
printf("%d ", rand() % 10);
}
for( ;clock() - now < CLOCKS_PER_SEC * 2; );
printf("\r");
for(int i = 1; i <= sequence_length; i++)
{
printf(" ");
}
if(counter == 1)
{
printf("\r\n请输入数字,不要忘了每个数字之间加上空格.\r\n");
}
else
{
printf("\r");
}
srand((unsigned int)seed);
for(int i = 1; i <= sequence_length; i++)
{
scanf("%d", &number);
if(number != rand() % 10)
{
correct = false;
break;
}
}
printf("%s\r\n", correct? "正确!" : "错误!");
}
time_taken = (clock() - time_taken) / CLOCKS_PER_SEC;
printf("\r\n\r\n Your score is %d", --counter * 100 / time_taken);
fflush(stdin);
printf("\r\n是否继续玩(y/n)?");
scanf("%c", &another_game);
}while(toupper(another_game) == 'Y');
}
井字棋代码
int player = 0;
int winner = 0;
int row = 0;
int choice = 0;
int column = 0;
int line = 0;
char board[3][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
for(int i = 0;i < 9 && winner == 0; i++)
{
printf("\n\n");
printf("%c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("---+---+---\n");
printf("%c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("---+---+---\n");
printf("%c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
player = i%2 + 1;
do
{
printf("\n Player %d, please enter the number of the square where you want to place your %c: ", player, (player == 1)? 'X' : 'O');
scanf("%d", &choice);
row = --choice / 3;
column = choice % 3;
}while(choice < 0 || choice > 9 ||board[row][column] > '9');
board[row][column] = (player == 1)? 'X' : 'O';
if((board[0][0] == board[1][1] && board[0][0] == board[2][2])
|| (board[0][2] == board[1][1] && board[0][2] == board[2][0]))
{
winner = player;
}
else
{
for(line = 0;line <= 2;line ++)
{
if((board[line][0] == board[line][1] && board[line][0] == board[line][2])
|| (board[0][line] == board[1][line] && board[0][line] == board[2][line]))
{
winner = player;
}
}
}
}
printf("\n\n");
printf("%c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("---+---+---\n");
printf("%c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("---+---+---\n");
printf("%c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
if(winner == 0)
{
printf("\nHow boring, it is a darw\n");
}
else
{
printf("\nCongratulations, player %d,you are the winner!\n", winner);
}
本文介绍了使用C语言实现的一些基础功能,如年龄和身高计算、单位转换、计算器、猜数字游戏和井字棋,展示了编程中的基本逻辑和交互设计。
2693

被折叠的 条评论
为什么被折叠?



