4.6 KiB
4.6 KiB
title | tags |
---|---|
第三次作业(答案) |
P108-3
写出下面各逻辑表达式的值。设a = 3
,b = 4
,c = 5
。
# | 表达式 | 答案 |
---|---|---|
1 | a + b > c && b == c |
0 |
2 | `a | |
3 | `!(a > b) && !c | |
4 | !(x = a) && (y = b) && 0 |
0 |
5 | !(a + b) + c - 1 && b + c / 2 |
1 |
P108-6
#include <stdio.h>
#include <stdlib.h>
int main()
{
double x, y;
printf("This program calculates y: \n");
printf("\n");
printf(" y = x (x < 1)\n");
printf(" 2x - 1 (1 <= x < 10)\n");
printf(" 3x - 11 (x >= 10)\n");
printf("\n");
printf("Please input x:\n");
printf("x = ");
if (scanf("%lf", &x) != 1) {
fprintf(stderr, "Invalid input!\n");
exit(EXIT_FAILURE);
}
if (x < 1) {
y = x;
} else if (x < 10) {
y = 2 * x - 1;
} else {
y = 3 * x - 11;
}
printf("y = %f\n", y);
return 0;
}
P108-9
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("请输入n: \nn = ");
if (scanf("%d", &n) != 1 || n > 99999 || n < 1) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
// 求位数
int tmp = n;
int digits = 5;
for (int i = 0; i < 5; ++i) {
if (tmp == 0) {
digits = i;
break;
}
tmp /= 10;
}
printf("%d有%d位\n", n, digits);
// 输出每一位:
tmp = n;
int digit_1 = tmp % 10;
tmp /= 10;
int digit_2 = tmp % 10;
tmp /= 10;
int digit_3 = tmp % 10;
tmp /= 10;
int digit_4 = tmp % 10;
tmp /= 10;
int digit_5 = tmp;
if (digits >= 1) {
printf("它的个位是%d\n", digit_1);
}
if (digits >= 2) {
printf("它的十位是%d\n", digit_2);
}
if (digits >= 3) {
printf("它的百位是%d\n", digit_3);
}
if (digits >= 4) {
printf("它的千位是%d\n", digit_4);
}
if (digits >= 5) {
printf("它的万位是%d\n", digit_5);
}
// 逆向输出
printf("逆向输出是");
printf("%d", digit_1);
if (digits >= 2) {
printf("%d", digit_2);
}
if (digits >= 3) {
printf("%d", digit_3);
}
if (digits >= 4) {
printf("%d", digit_4);
}
if (digits >= 5) {
printf("%d", digit_5);
}
printf("\n");
return 0;
}
P108-12
#include <stdio.h>
#include <stdlib.h>
int main()
{
double r2 = 1;
double ax = 2, ay = 2,
bx = -2, by = 2,
cx = -2, cy = -2,
dx = 2, dy = -2;
double x, y;
printf("请分别输入所在位置的的x、y坐标:\nx = ");
if (scanf("%lf", &x) != 1) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
printf("y = ");
if (scanf("%lf", &y) != 1) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
double da2 = (x - ax) * (x - ax) + (y - ay) * (y - ay),
db2 = (x - bx) * (x - bx) + (y - by) * (y - by),
dc2 = (x - cx) * (x - cx) + (y - cy) * (y - cy),
dd2 = (x - dx) * (x - dx) + (y - dy) * (y - dy);
if (da2 <= r2 || db2 <= r2 || dc2 <= r2 || dd2 <= r2) {
printf("该点高度是10m\n");
} else {
printf("该点高度是0m\n");
}
return 0;
}
解一元二次方程
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <math.h>
int main()
{
double a, b, c;
printf("一元二次方程求根\n");
printf("a x^2 + b x + c = 0\n");
printf("请输入a、b、c:\na = ");
if (scanf("%lf", &a) != 1 || a == 0) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
printf("b = ");
if (scanf("%lf", &b) != 1) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
printf("c = ");
if (scanf("%lf", &c) != 1) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
double delta = b * b - 4 * a * c;
if (delta == 0) {
printf("x1 = x2 = %f\n", -b / 2 / a);
} else if (delta > 0) {
double x1 = (-b + sqrt(delta)) / 2 / a;
double x2 = (-b - sqrt(delta)) / 2 / a;
printf("x1 = %f\nx2 = %f\n", x1, x2);
} else {
double complex x1 = (-b + csqrt(delta)) / 2 / a;
double complex x2 = (-b - csqrt(delta)) / 2 / a;
printf("x1 = %f %+fi\nx2 = %f %+fi\n", creal(x1), cimag(x1), creal(x2), cimag(x2));
}
return 0;
}