实验2
实验1
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 #define N1 397 #define N2 476 #define N3 21 int main() { int cnt; int random_major, random_no; srand(time(NULL)); cnt = 0; while(cnt < N) { random_major = rand() % 2; if(random_major){ random_no = rand() % (N2-N1+1) + N1; printf("20248329%04d\n", random_no); } else{ random_no = rand() % N3 + 1; printf("20248329%04d\n", random_no); } cnt++; } return 0; }
line21功能:生成N1-N2的整数
line25功能:生成N2-N3的整数
程序功能:随机生成学号
实验2
#include <stdio.h> #include <math.h> int main (){ double a,b,c; double delta,p1,p2; while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) { if(a == 0){ printf("a = 0, invalid input\n"); continue; } delta =b*b -4*a*c; p1 = -b/2/a; p2 = sqrt(fabs(delta))/2/a; if(delta == 0) printf("x1 = %.2g,x2 = %.2g\n",p1+p2,p1-p2); else if(delta > 0) printf("x1 = %.2g, x2 = %.2g\n",p1+p2,p1-p2); else{ printf("x1 = %.2g + %.2gi,",p1,p2); printf("x2 = %.2g - %.2gi,",p1,p2); } } return 0; }
实验3
#include<stdio.h> int main() { char color; while((color = getchar()) !=EOF){ if (color == 'r'){ printf("stop!\n");} else if (color == 'y'){ printf("wait a minute\n");} else if(color == 'g'){ printf("go go go\n");} else{ printf("something must be wrong...\n");} getchar(); } return 0; }
实验4
#include<stdio.h> int main() { double expense; double max = 0.0; double min = 20000.0; double totalexpense = 0.0; printf("输入今日开销,直到输入-1为止:\n"); while (scanf_s("%lf", &expense) != EOF && expense != -1) { if (expense > max) { max = expense; } if (expense < min) { min = expense; } totalexpense += expense; } if (max > 0 && min < 20000 && totalexpense > 0) { printf("今日累计消费:%.1lf\n", totalexpense); printf("今日最高开销:%.1f\n", max); printf("今日最低消费:%.1f\n", min); } else { printf("error\n"); } return 0; }
实验5
#include<stdio.h> int main() { int a, b, c; while (scanf_s("%d %d %d", &a, &b, &c) != EOF) { if (a + b <= c || a + c <= b || b + c <= a) { printf("不能构成三角形\n"); } else { if (a == b && b == c) { printf("等边三角形"); } else if (a == b || a == c || b == c) { if (a * a + b * b == c * c || b * b + c * c == a * a || a * a + c * c == b * b) { printf("等腰直角三角形\n"); } else { printf("等腰三角形\n"); } }else if (a * a + b * b == c * c || b * b + c * c == a * a || a * a + c * c == b * b) { printf("直角三角形\n"); } else { printf("普通三角形\n"); } } } }
实验6
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(NULL)); int luckyDay = rand() % 30 + 1; int guess; int chances = 3; printf("猜猜2024年11月哪一天会是你的lucky day\n 开始喽,你有三次机会,猜吧( 1 and 30):\n"); while (chances > 0) { scanf_s("%d", &guess); if (guess == luckyDay) { printf("You guessed it right!\n"); return 0; } else if (guess < luckyDay) { printf("你猜的日期早了,你的lucky day还没到呢\n"); } else { printf("你猜的日期晚了,你的lucky day在前面哦\n"); } chances--; } printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d.\n", luckyDay); return 0; }