实验3
以下实验都在Visual Studio 2019 Preview平台完成
———————————————————————————————————————————————————————
实验3-1
#include <math.h> #include<stdlib.h> #include <stdio.h> int main() { float a, b, c, x1, x2; float delta, real, imag; printf("Enter a, b, c: "); while (scanf_s("%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; }

运行过程: 程序在读取了输入的3个数后对第一个数进行判断,若不是0再进行下一步运算→判断b^2-4ac的值来确定结果是否有虚根→通过计算得到两根的值。
ps:在对循环输入结束时,要换到新的一行输入^Z并再重复两次,否则会无法中断导致程序无限循环。(应该是没有清除缓存区内的数据,导致输入的^Z被识别成了26(ASCII),参与了运算,因此无法结束)
—————————————————————————————————————————————————
实验3-2
#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; }

运行过程:定义N的值作为输出的个数→设定当前系统时间为rand函数的种子→多次循环运算得到N个随机数→取余数以后输出得到结果。
因为系统时间在不断变化,因此每次输出的结果都有所不同,如果将srand()中的值设为固定的数,那么无论如何运行输出的结果也都是固定的几个数。
————————————————————————————————————————————————
实验3-3
#include <stdio.h> #include <stdlib.h> int main() { int a=100, b, c=0; while (a<=200) { a++; for ( b = 2; b < a; b++) { if (a%b==0) { break; } } if (b==a) { printf("%d ", b); c++; if (c%5==0) { printf("\n"); } } } printf("\n一共有%d个素数",c); return 0; }

——————————————————————————————————————————————
实验3-4
#include <stdio.h> #include <stdlib.h> int main() { long int s; int i = 1, a, b = 0; while (printf("Enter a number:"), scanf_s("%ld", &s) != EOF) { do { i *= 10; } while (i <= s); a = i; printf("new number is:"); while (a > 1) { b = (s % a) / (a / 10); a /= 10; if (b % 2 == 1) { printf("%d", b); } } printf("\n\n"); } return 0; }

实验思路:通过do while循环来判断输入的数的位数→通过取余数来保证数字的高低位不产生变化→除以同位数(例有x位)的10......00(同样有x位)得到该位上的数字→对该数取二的余数来判断奇偶→按位输出得到结果
前方也是王道征途啊(赞赏)
——————————————————————————————————————————————
实验3-5
#include <stdio.h> #include <stdlib.h> #include<math.h> int main() { double a, b = 1, c = 0, d, e = 1, f = 1; while (printf("Enter n(1~10):"), scanf_s("%lf", &a) != 0) { printf("\n"); while (b <= a) { d = pow(-1, b - 1); while (f <= b) { e *= f; f++; } c += d * (1 / e); b++; } printf("n = %.0lf,s = %lf\n\n", a, c); } return 0; }

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

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


浙公网安备 33010602011771号