实验三
实验任务一
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 int main() { int x, n; srand(time(0)); for(n=1; n<=N; n++) { x = rand() % 100; printf("%3d", x); } printf("\n"); return 0; }

实验任务二
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int x, n ,i; srand(time(0)); x = rand() % 31+1; printf("猜猜2021年5月哪一天会是你的lucky day\n开始咯,你有三次机会,猜吧(1^31):",x); for(i=1;i<=3;i++){ scanf("%d",&n); if(n==x){ printf("你猜对了,%d日就是你的lucky day",x); break; } else if(n<x){ if(i<=2) printf("你猜的日期早了,lucky day还没到呢\n再猜:"); else printf("次数用完了,偷偷告诉你:你的lucky day是%d日",x); } else if(n>x){ if (i<=2) printf("你猜的日期晚了,lucky day悄悄溜到前面了\n再猜:"); else printf("次数用完了,偷偷告诉你:你的lucky day是%d日",x); } } return 0; }

实验任务三
#include<stdio.h> int main (){ long s; printf("enter a number:"); while((scanf("%ld",&s))!=EOF){ long t=0,p=1; while(s!=0) { if((s%10)%2!=0) { t=t+(s%10)*p; p=p*10; } s=s/10; } printf("new number is:%ld\n",t); printf("enter a number:"); } return 0; }

实验任务四
#include<math.h>
#include<stdio.h>
void solve(double a, double b, double c); int main() { double a, b, c; printf("Enter a, b, c: "); while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) { solve(a, b, c); printf("Enter a, b, c: "); } return 0; } void solve(double a, double b, double c) { double x1, x2; double delta, real, imag; if(a == 0) printf("not quadratic equation.\n"); else { delta = b*b - 4*a*c; if(delta >= 0) { x1 = (-b + sqrt(delta)) / (2*a); x2 = (-b - sqrt(delta)) / (2*a); printf("x1 = %.2f, x2 = %.2f\n", x1, x2); } else { real = -b/(2*a); imag = sqrt(-delta) / (2*a); printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n", real, imag, real, imag); } } }

思考:否,函数值只能返回一个,无法返回x1,x2两个值。
实验任务五
#include<stdio.h> double fun(int n); int main(){ int n; double s; while(printf("Enter n(1~10):"),scanf("%d", &n) != EOF){ s = fun(n); printf("n = %d, s = %f\n\n", n, s); } return 0; } double fun (int n){ int i,z,a=1,d=1; double b,s; for(i=1,s=0;i<=n;i++,a=1) { for(z=i;z>0;--z){ a=a*z; } b=d*1.0/a; s=s+b; d=-d; } return s; }

实验任务六
#include<stdio.h> #include<math.h> int isprime(int n); int main(){ int i,n; for(i=101,n=1;i<=200;i++) { if(isprime(i)){ printf("%4d",i); n++; } if(n%6==0){ printf("\n"); n=1;} } return 0; } int isprime(int n) { int k; for(k=2;k<=sqrt(n);k++) if(n%k==0) return 0; return 1; }

实验总结:能感觉到难度明显加大,花费的时间是之前的几倍,路漫漫其修远兮,干巴爹。。。

浙公网安备 33010602011771号