C语言 实验二 任务报告
📝实验二任务报告
✨实验总结
😟遇到的问题:
在实验5的时候遇到getchar()这个函数,连续出现两个在最开始我也不知道为甚么,删除了之后只能输入一个字符。加上去之后就正常了。但是写报告的时候想偷懒就加了循环语句。结果第一次运行正常,但是第二次又出现了同样的问题。这就很奇怪为什么第一次正常,第二次问题又出现了。
🧐解决方法:
如果在ans2=getchar();的下一行再加一个getchar();就好了,很神奇,但是感觉不加也可以。尝试了几次之后开始觉得getchar应该还有其他的用法。
🤔反思:
搜索了之后看到有人回答:
用于清除缓冲区。 原来输入的字符,由于某种原因留在缓冲区,没有输出出来。 用getchar()将字符提取出来,避免对下文的输入输出造成影响。
这个解释我觉得让我很恍然大悟,但是为什么书上不讲呢?第一次getchar之后输入的字符留在缓存区,导致之后在进行的时候缓存区已经有东西了,就需要再来一个getchar来清除缓存。
✨实验内容
🕐任务一
📃代码:
#include <stdio.h>
// EX1 The effect of the different order of calculation on the election results.
int main() {
int a = 5, b = 7, c = 100, d ,e ,f;
d = a / b * c;
e = a * b / c;
f = c / b * a;
printf("d=%d, e=%d, f=%d\n", d, e, f);
return 0;
}
🎨截图:
🤓反思:
我们定义的全部是整型变量,整型变量的加减乘除的结果也全部是整型,且向下取整。根据计算规则“/“与”*“同优先级,从左向右依次运算。上述代码结果因顺序而不同,就是因为计算的时候无法整除,向下取整,在取整的过程中出现了偏差。
🕑任务二
代码:
#include <stdio.h>
// EX2 The function and use of various format characters that appear oin the formatted output.
int main() {
int x=1234;
float f=123.456;
double m=123.456;
char ch='a'; // Can only be defined as one character.
char a[]="Hello, world!"; // Store "Hello, world!" in character 'a'.
int y=3, z=4;
printf("%d %d\n", y, z);
printf("y=%d, z=%d\n", y,z);
printf("%8d,%2d\n", x,x);
printf("%f, %8f, %8.1f, %0.2f, %.2e\n",f,f,f,f,f);
printf("%lf\n",m);
printf("%3c\n", ch);
printf("%s\n%15s\n%10.5s\n%2.5s\n%.3s\n",a,a,a,a,a);
return 0;
}
截图:
反思:
对于int:d前面数字代表占几个格子,不够用空格从左边补充,多余原样输出
对于float . 前面代表占几个格子;后面代表小数位数,不够用0补充,多余按原数输出%.f默认小数点0位;%.2e保留2位小数的科学计数法
对于字符.s前面代表几个格子,后面代表输出前几位。
如果出现 ‘-’ 如 %-10d 表示左对齐,即不足在右边用空格补充,另+.表示带符号输出。
🕒任务三
📃代码:
#include <stdio.h>
#include <stdlib.h>
// Ex3 Format input and output
int main()
{
double x,y;
char c1,c2,c3;
int a1,a2,a3;
scanf("%d %d %d",&a1,&a2,&a3);/*If '\n' is used here, the program will out input both of numbers and characters.
So as to output.*/
printf("%d %d %d\n",a1,a2,a3);
scanf("\n%c %c %c",&c1,&c2,&c3);//Use '\n' as separator between numbers and characters
printf("%c %c %c\n",c1,c2,c3);
scanf("%lf %lf",&x,&y);
printf("%.1lf %.1lf\n",x,y);
system("pause");
return 0;
}
🎨截图:
🤓反思:
这个最开始自己写的时候最先输入的是字符,输完按回车就好,后来试了一下源代码发现数字后面要紧跟着字符,输入的时候看起来不太美观,尝试修改了一下,在%c前加个\n就好了,但是在%d后面就会出现数字与字符一起输入输出,不知为何。
🕓任务四
📃代码:
#include <stdio.h>
#include <stdlib.h>
// Ex4 Determine the type of character.
int main()
{
char x;
x = getchar();
if( x>='1'&&x<='9')
printf("%c是数字字符\n", x);
else if( x<='z'&&x>='A')
printf("%c是英文字母\n", x);
else
printf("%c是其它字符\n", x);
system("pause");
return 0;
}
🎨截图:
🤓反思:
ASCII码要熟悉。
🕔任务五
📃代码:
#include <stdio.h>
#include <stdlib.h>
// Ex5 Fill condition statement in the blank.
int main()
{
for (int i = 0; i < 2; ++i)
{
char ans1,ans2;
printf("复习了没? (输入y或Y表示复习了,输入n或N表示没复习) :");
ans1=getchar();
getchar();
printf("\n动手敲代码了没? (输入y或Y表示敲了,输入n或N表示木有敲) :");
ans2=getchar();
getchar();
if ((ans1=='y'||ans1=='Y')&&(ans2=='y'||ans2=='Y'))
printf("\n罗马不是一天建成的:)\n");
else
printf("\n罗马不是一天毁灭的。。。\n");
}
system("pause");
return 0;
}
🎨截图:
🤓反思:
见开头。
🕕任务六
📃代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
for (int i = 0; i < 5; ++i)
{
int n;
double sum;
printf("计算2^0+2^1+2^2+……+2^n的和\n");
printf("请输入n的值:");
sum=0.;
scanf("%d",&n);
/*Formula method
* n=n+1;
* sum=(1-pow(2,n))/(1-2);
*/
for (int i= 0; i < n+1; i++)//Recursion
{
double y;
y=(double)i;
sum = sum+pow(2.,y);
}
long long int z;
z=(int)sum;
printf("当n = %d时, sum = %lld\n",n,z);
}
system("pause");
return 0;
}
🎨截图:
🤓反思:
这个指数函数不知为何要用double型变量,不过好像就和sqrt函数一样,里面输入整型也会自动转化为double。
可以研究一下math.h,熟悉一下数学公式。
🕖任务七
📃代码:
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include "time.h"
// Ex7 print a picture of characters maen.
int main()
{
int m = 1;//Control the number of "\t" to print a dynamic picture.
for (int u = 0; u < 25; ++u)
{
srand((unsigned)time(NULL));//Generate a random number.
int a = rand()%5;
int n=8;
for (int l = 0; l <5 ; ++l)//Print the picture of character men.
{
for (int i = 0; i < m; ++i)
{
printf("\t");
}
for (int i = 0; i < (8-n)/2; ++i)
{
printf("\t");
}
for (int i = 0; i < n; ++i)
{
printf(" 0\t");
}
printf(" 0\n");
for (int i = 0; i < m; ++i)
{
printf("\t");
}
for (int i = 0; i < (8-n)/2; ++i)
{
printf("\t");
}
for (int i = 0; i < n; ++i)
{
printf("<H>\t");
}
printf("<H>\n");
for (int i = 0; i < m; ++i)
{
printf("\t");
}
for (int i = 0; i < (8-n)/2; ++i)
{
printf("\t");
}
for (int i = 0; i < n; ++i)
{
printf("I I\t");
}
printf("I I\n");
n--;
n--;
}
++m;
Sleep(1500);
system("cls");//Clear the screen.
switch (a)//Use the random number to change color.
{
case 1:system("color BD");break;
case 2:system("color FC");break;
case 3:system("color 07");break;
case 4:system("color 70");break;
default:system("color B9");break;
}
}
system("pause");
return 0;
}
🎨截图:
这个小人图确实迭了很多代,最终实现每过1.5秒随机更换颜色并整体向右边移动一格。不过应该是由于窗口的限制,当输出到边界的时候小人会变大,这好像无能为力。