Dragon Add - 欢迎使用代理访问本站 - C语言
https://blog.dragonadd.xyz/tag/C%E8%AF%AD%E8%A8%80/
日记其二 — 七道计算题
https://blog.dragonadd.xyz/archives/28/
2021-08-15T15:54:00+08:00前言:常言道:学以致用。不但既要学会,而且还要懂得运用。很久之前,自学C语言。在学到 putchar 函数的时候,对着 ASCII 代码对照表,写了一套“计算题”,把代码复制进编译器,然后运行编译,它会列出一道简单的计算题(加减法),把算出的结果敲进去,会得到一个字符。连续做完七道计算题后,它会把你得到的字符整理成一个句子。如果算得不对,你会得到一串很奇怪的句子。代码如下:#include <stdio.h>
int main()
{
int a, b, c, answer,
a1, a2, a3, a4, a5, a6, a7;
printf("a = 15, b = 58\n\nIf a+b\n");
a = 15; b = 58;
printf("Your answer is ");
scanf_s("%d", &answer);
printf("\n");
c = a + b;
a1 = answer;
if (a + b == answer)
{
printf("Congratulation! ");
printf("You get letter : ");
putchar(c); printf("\n\n");
}
else
{
printf("No correct! ");
printf("You get letter : ");
putchar(a1); printf("\n\n");
}
printf("a = 13, b = 63\n\nIf a+b\n");
a = 13; b = 63;
printf("Your answer is ");
scanf_s("%d", &answer);
printf("\n");
c = a + b;
a2 = answer;
if (a + b == answer)
{
printf("Congratulation! ");
printf("You get letter : ");
putchar(c); printf("\n\n");
}
else
{
printf("No correct! ");
printf("You get letter : ");
putchar(a2); printf("\n\n");
}
printf("a = 102, b = 23\n\nIf a-b\n");
a = 102; b = 23;
printf("Your answer is ");
scanf_s("%d", &answer);
printf("\n");
c = a - b;
a3 = answer;
if (a - b == answer)
{
printf("Congratulation! ");
printf("You get letter : ");
putchar(c); printf("\n\n");
}
else
{
printf("No correct! ");
printf("You get letter : ");
putchar(a3); printf("\n\n");
}
printf("a = 27, b = 59\n\nIf a+b\n");
a = 27; b = 59;
printf("Your answer is ");
scanf_s("%d", &answer);
printf("\n");
c = a + b;
a4 = answer;
if (a + b == answer)
{
printf("Congratulation! ");
printf("You get letter : ");
putchar(c); printf("\n\n");
}
else
{
printf("No correct! ");
printf("You get letter : ");
putchar(a4); printf("\n\n");
}
printf("a = 96, b = 27\n\nIf a-b\n");
a = 96; b = 27;
printf("Your answer is ");
scanf_s("%d", &answer);
printf("\n");
c = a - b;
a5 = answer;
if (a - b == answer)
{
printf("Congratulation! ");
printf("You get letter : ");
putchar(c); printf("\n\n");
}
else
{
printf("No correct! ");
printf("You get letter : ");
putchar(a5); printf("\n\n");
}
printf("a = 90, b = 1\n\nIf a-b\n");
a = 90; b = 1;
printf("Your answer is ");
scanf_s("%d", &answer);
printf("\n");
c = a - b;
a6 = answer;
if (a - b == answer)
{
printf("Congratulation! ");
printf("You get letter : ");
putchar(c); printf("\n\n");
}
else
{
printf("No correct! ");
printf("You get letter : ");
putchar(a6); printf("\n\n");
}
printf("a = 15, b = 70\n\nIf a+b\n");
a = 15; b = 70;
printf("Your answer is ");
scanf_s("%d", &answer);
printf("\n");
c = a + b;
a7 = answer;
if (a + b == answer)
{
printf("Congratulation! ");
printf("You get letter : ");
putchar(c); printf("\n\n");
}
else
{
printf("No correct! ");
printf("You get letter : ");
putchar(a7); printf("\n\n");
}
printf("Now make a sentence out of the letters you collected.\n\n");
putchar(a1);
putchar('\t');
putchar(a2); putchar(a3); putchar(a4); putchar(a5);
putchar('\t');
putchar(a6); putchar(a3); putchar(a7);
printf("\n\n");
return 0;
}反复验证计算了好几遍,应该是没有错误了。正确计算结果为:I love you