用到的头文件
#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;//TODObreak;
case 'n':goto that;//TODObreak;
default://TODObreak;
}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');
}