实验3
// 一元二次方程求解 //ctrl+z结束 #include <math.h> #include <stdio.h> int main() { float a, b, c, x1, x2; float delta, real, imag; printf("Enter a, b, c: "); while(scanf("%f%f%f", &a, &b, &c) != EOF){ if(a == 0) printf("not quadratic equation.\n\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\n", x1, x2); } else{ real = -b/(2*a); imag = sqrt(-delta)/(2*a); printf("x1 =%.2f + %.2fi, x2 = %.2f - %.2fi\n\n", real, imag, real, imag); } } printf("Enter a, b,c: "); } return 0; }

// 生成N个0~9之间的随机整数, 并打印输出 #include<stdio.h> #include<stdlib.h> #include<time.h> #define N 5 int main() { int x, n; srand(time(0)); n = 0; do { n++; x = rand()%10; printf("%3d", x); } while(n<N); printf("\n"); return 0 ; }

//3 #include <stdio.h> #include <math.h> int main() { int n,i,m,N=0; for(n=101;n<=200;n++) { m=sqrt(n); for(i=2;i<=m;i++) { if(n%i==0)break; if(i==m){ printf("%6d",n); N++; } } } printf("\n101~200之间共有%d个素数,\n",N); return 0 ; }

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

#include <stdio.h> int main(){ int n; printf("Enter n(1~10): "); while(scanf("%d", &n)!=EOF){ int t, f, T; f = 1; T = 1; double s = 0.0; for (t = 1;t <= n; t++){ T = t * T; s = s + f * (1.0 / T); f = f * (-1); } printf("n = %d, s = %f\n\n", n, s); printf("Enter n (1~10): "); } }

#include <stdio.h> #include <time.h> #include <stdlib.h> int main(){ int x,i,date = 6; srand((unsigned)time(NULL)); x = rand()%31 + 1; printf("猜猜2020年12月哪一天会是你的lucky day\n"); printf("开始喽,你有三次机会,猜吧(1~31): "); for(i = 1;i <= 3;i++) { scanf("%d",&x); if(x > date) {printf("\n你猜的日期晚了,lucky day悄悄溜到前面啦\n"); } else if(x < date) {printf("\n你猜的日期早了,lucky day还没到呢\n");} if(i != 3){ printf("\n再猜(1~31): ");} } printf("次数用完啦,偷偷告诉你:12月,你的lucky day是6号"); return 0; }

通过本次实验,充分认识到了理论和实践的差距,一定要好好实践啊!!!!

浙公网安备 33010602011771号