实验任务一
// 打印一个字符小人 #include <stdio.h> int main() { printf(" O o\n"); printf("<H> <H>\n"); printf("I I I I\n"); return 0; }
// 打印一个字符小人 #include <stdio.h> int main() { printf(" O\n"); printf("<H>\n"); printf("I I\n\n"); printf(" O\n"); printf("<H>\n"); printf("I I\n"); return 0; }
实验任务二
math1
/* 用c语言实现的一个简单算术程序 */ #include <stdio.h> int main() { int x, y; //定义两个整型变量,用来保存运算数据 int r1, r2, r3, r4;//定义四个整型变量,用来保存加减乘除运算结果 x = 1; y = 2; r1 = x + y; r2 = x - y; r3 = x * y; r4 = x / y; printf("r1 = %d\n", r1); printf("r2 = %d\n", r2); printf("r3 = %d\n", r3); printf("r4 = %d\n", r4); return 0; }
math2
/* 用c语言实现的一个简单算术程序 */ #include <stdio.h> int main() { float x, y; //定义两个整型变量,用来保存运算数据 float r1, r2, r3, r4;//定义四个整型变量,用来保存加减乘除运算结果 x = 1; y = 2; r1 = x + y; r2 = x - y; r3 = x * y; r4 = x / y; printf("r1 = %f\n", r1); printf("r2 = %f\n", r2); printf("r3 = %f\n", r3); printf("r4 = %f\n", r4); return 0; }
两次运行结果r4不同的原因:math1中,r4为整型,1/2取整型结果为0;而math2中,r4为浮点型,故r4=0.500000
实验任务三
n=100时
#include <stdio.h> int main() { int n, r1, r2, r3, r4; n = 100; r1 = n*(n+1)/2; r2 = (n+1)*n/2; r3 = n/2*(n+1); r4 = (n+1)/2*n; printf("n = %d\n", n); printf("r1 = %d\n", r1); printf("r2 = %d\n", r2); printf("r3 = %d\n", r3); printf("r4 = %d\n", r4); return 0; }
n=101时
#include <stdio.h> int main() { int n, r1, r2, r3, r4; n = 101; r1 = n*(n+1)/2; r2 = (n+1)*n/2; r3 = n/2*(n+1); r4 = (n+1)/2*n; printf("n = %d\n", n); printf("r1 = %d\n", r1); printf("r2 = %d\n", r2); printf("r3 = %d\n", r3); printf("r4 = %d\n", r4); return 0; }
line7:100*101/2 101*102/2
line8:101*100/2 102*101/2
line9:50*101 50*102
line10:50*100 51*101
n为奇数时,n/2为小数,但r3为整型,所以此时 r3=(n-1)/2*(n+1)
r4=(n+1)/2*n
n为偶数时,(n+1)/2为小数,但r4为整型,所以此时 r3=n/2*(n+1)
r4=n/2*n
实验任务四
#include<stdio.h> int main() { double x,y; char c1,c2,c3; int a1,a2,a3; scanf("%d%d%d",&a1,&a2,&a3); printf("%d%d%d\n",a1,a2,a3); scanf("%c%c%c",&c1,&c2,&c3); printf("%c%c%c\n",c1,c2,c3); scanf("%lf,%lf",&x,&y); printf("%lf,%lf\n",x,y); return 0; }
实验任务五
#include <stdio.h> int main() { int a1, a2; char c1, c2; scanf("%d%c%d%c", &a1, &c1, &a2, &c2); printf("a1 = %d, a2 = %d\n", a1, a2); printf("c1 = %c, c2 = %c\n", c1, c2); return 0; }
实验任务六
//测试你是哪一类学习者 #include <stdio.h> int main() { int answer; char words[5000]; printf("网课学习让一些人欢喜一些人忧.\n"); printf("1.作为一个宅,社恐,觉得这样很好.有网络,自主学习,很ok.\n"); printf("2.不喜,不喜,很不喜.眼睛快瞎了.脑壳有点卡...\n"); printf("3.中间派. 不知道. 说不清.\n"); printf("4.其他...\n"); printf("你的选择: "); scanf("%d",&answer); if(answer == 4) { printf("请补充你的想法: \n"); getchar(); //这行代码为什么放在这里,如果去掉了,会怎么样? gets(words); //从键盘输入你的想法,输完后,敲回车键 } printf("\n"); //换行 printf("当遇到问题时,你会选择:\n"); printf("1.遇到问题,就解决问题。不懂的,搜索式学习,逐个攻破.\n"); printf("2.遇到问题,不高兴。然后,... ,没有了.\n"); printf("3.其它...\n"); printf("你的选择: "); scanf("%d",&answer); if(answer == 1) printf(":)\n"); else if(answer == 2) printf("这样不能解决问题啊...我们选择1好不好:)\n"); else if(answer == 3) { printf("请补充说明你的想法: \n"); getchar(); //这行代码为什么放在这里,如果去掉了,会怎么样? gets(words); //从键盘输入你的想法,输完后,敲回车键 } return 0; }