实验任务1:
task1.c源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define N 5 6 #define N1 397 7 #define N2 476 8 #define N3 21 9 10 int main() 11 { 12 int cnt; 13 int random_major,random_no; 14 15 srand(time(NULL)); 16 17 cnt=0; 18 while(cnt<N) 19 { 20 random_major=rand()%2; 21 if(random_major) 22 { 23 random_no=rand()%(N2-N1+1)+N1; 24 printf("20248329%04d\n",random_no); 25 } 26 else 27 { 28 random_no=rand()%N3+1; 29 printf("20248395%04d\n",random_no); 30 } 31 cnt++; 32 } 33 34 return 0; 35 }
运行结果:
问题1:第23行代码是为了生成从N1到N2内的随机整数
问题2:第28行代码是为了生成从1到N3内的随机整数
问题3:这个程序是为了随机生成在两个范围内的整数
实验任务2:
task2.c源代码:
1 #include <stdio.h> 2 #include <math.h> 3 4 int main() 5 { 6 double a,b,c; 7 double delta,p1,p2; 8 9 while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF) 10 { 11 if(a==0) 12 { 13 printf("a=0,invalid input"); 14 continue; 15 } 16 delta=b*b-4*a*c; 17 p1=-b/2/a; 18 p2=sqrt(fabs(delta))/2/a; 19 if(delta==0) printf("x1=x2=%.2g\n",p1); 20 else if(delta>0) printf("x1=%.2g,x2=%.2g\n",p1+p2,p1-p2); 21 else 22 { 23 printf("x1=%.2g+%.2gi,",p1,p2); 24 printf("x2=%.2g-%.2gi\n",p1,p2); 25 } 26 27 } 28 return 0; 29 }
运行结果:
实验任务3:
task3.c源代码:
1 #include <stdio.h> 2 int main() 3 { 4 char color; 5 while(scanf("%c",&color)!=EOF) 6 { 7 getchar(); 8 9 if(color=='r') 10 printf("stop!\n"); 11 else if(color=='g') 12 printf("go go go\n"); 13 else if(color=='y') 14 printf("wait a minute\n"); 15 else printf("something must be wrong"); 16 } 17 18 return 0; 19 }
运行结果:
实验任务4:
task4.c源代码:
1 #include <stdio.h> 2 int main() 3 { 4 double money,c; 5 double max=0,min=20000; 6 printf("输入今日开销,直到输入-1终止:\n"); 7 while(scanf("%lf",&money),money!=-1) 8 { 9 if(money>max) max=money; 10 else min=money; 11 getchar(); 12 c=c+money; 13 14 } 15 printf("今日累计消费总额:%.1f\n",c); 16 printf("今日最高一笔开销:%.1f\n",max); 17 printf("今日最低一笔开销:%.1f\n",min); 18 19 return 0; 20 }
运行结果:
实验任务5:
task5.c源代码:
1 #include<stdio.h> 2 int main() 3 { 4 int a,b,c; 5 while(scanf("%d%d%d",&a,&b,&c)!=EOF) 6 { 7 if(a<=0||b<=0||c<=0) 8 { 9 printf("不能构成三角形\n"); 10 11 } 12 if(a+b<=c||a+c<=b||b+c<=a) 13 { 14 printf("不能构成三角形\n"); 15 16 } 17 else if(a==b&&b==c) 18 printf("等边三角形\n"); 19 else if(a==b||a==c||b==c) 20 printf("等腰三角形\n"); 21 else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a) 22 printf("直角三角形\n"); 23 else 24 printf("普通三角形\n"); 25 } 26 27 return 0; 28 }
运行结果:
实验任务6:
task6.c源代码:
1 #include <stdio.h> 2 #include<time.h> 3 int main() 4 { 5 srand(time(NULL)); 6 int random_number=rand()%30+1; 7 int day,count=1; 8 9 printf("猜猜2024年11月哪一天是你的lucky day\n"); 10 printf("\n"); 11 printf("开始喽,你有三次机会,猜吧(1~30):"); 12 13 while(scanf("%d",&day),day!=random_number,count<3) 14 { 15 printf("\n"); 16 if(day>random_number) printf("你猜的日期晚了,你的lucky day还在前面哦\n"); 17 else printf("你猜的日期早了,你的lucky day还没到呢\n") ; 18 printf("\n"); 19 printf("再猜(1~30):"); 20 count+=1; 21 } 22 printf("\n"); 23 if(day==random_number) printf("哇,猜中了:)"); 24 25 else if(day>random_number){ 26 printf("你的日期猜晚了,你的lucky day还在前面哦\n"); 27 printf("\n"); 28 printf("次数用光了。偷偷告诉你,11月你的lucky day是%d号",random_number);} 29 30 else { 31 printf("你猜的日期早了,你的lucky day还没到呢\n"); 32 printf("\n"); 33 printf("次数用光了。偷偷告诉你,11月你的lucky day是%d号",random_number);} 34 35 return 0; 36 }
运行结果:
标签:%.,int,else,实验,printf,include,day From: https://www.cnblogs.com/zzh-247061/p/18455042