diff --git a/Chapter3/C3-输入输出专题.md b/Chapter3/C3-输入输出专题.md index bf850d2..4e4ffa8 100644 --- a/Chapter3/C3-输入输出专题.md +++ b/Chapter3/C3-输入输出专题.md @@ -207,6 +207,8 @@ int main() ```c int scanf(const char *format, 地址表); format:抑制符* +%s非常危险,因为不知道储存空间的大小 +放在循环结构中时要注意能否接收到正常内容 ``` @@ -303,5 +305,173 @@ int main() ## 字符输入输出函数 -S +```c +int getchar(void);// 返回int型是考虑出错的情况 +int putchar(int c); + +#include +#include + +int main() +{ + int ch; + ch = getchar(); + putchar(ch); + + exit(0); +} +``` + +## 字符串输入输出函数 + +```c +char *gets(char *s); +// 从标准输入读到内容放到s指针指向的缓冲区 +// 停止条件:换行或者EOF +// 尾部会追加'\0' + +int puts(const char *s); +// 将s指针缓冲区内容往外写 +// 以换行符中止 + +#include +#include + +#define STRSIZE 32 + +int main() +{ + char str[STRSIZE]; + gets(str); + puts(str); + + exit(0); +} +``` + +`gets`非常危险,不会检查是否越界。使用的话用`fgets`来代替。 + +```c +char *fgets(char *s, int size, FILE *stream);// 最多读到'size-1'个 +``` + +`getline`是`GNU libc`的方言,可以动态内存保证读取一整行。 + + + +## 练习专题 + +### 夸脱水 + +> 一个水分子3.0e-23g,一夸脱水约950g。 +> +> 变成,输入水的夸脱数,输出其包含的水分子质量。 + +```c +static void water(void) +{ + float num; + float sum; + + printf("Please input for num:"); + scanf("%f", &num); + + sum = num * KQ / WEIGHT; + + printf("Total = %e\n", sum); + + return; +} +``` + +### 三角形面积 + +> 输入三角形的三边长,求三角形面积。 + +```c +static void area(void) +{ + float a, b, c; + float s, area; + + printf("Please input:"); + scanf("%f%f%f", &a, &b, &c); + // 对scanf做好校验是好习惯 + if (a + b <= c || b + c <= a || a + c <= b) + { + fprintf(stderr, "EINVAL!\n"); + exit(0); + } + + printf("a = %f\n", a); + printf("b = %f\n", b); + printf("c = %f\n", c); + + // s = 1 / 2 * (a + b + c); + s = 1.0 / 2 * (a + b + c); + printf("s = %f\n", s); + // s=0.000000 + // 1 / 2是int型计算,得到的是商0余1 + + area = sqrt(s * (s - a) * (s - b) * (s - c)); + printf("area = %f\n", area); +} + +``` + +编译需要`-lm`。`makefile`如下,千万要注意`makefile`要使用**制表符**缩进,而非空格。 + +```makefile +CC=cc +CFLAGS= +LDFLAGS=-lm +TARGET=test +SRC=test.c +OBJ=$(SRC:.c=.o) + +$(TARGET): $(OBJ) + $(CC) -o $@ $^ $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f $(TARGET) $(OBJ) +``` + +### 求根公式 + +> ax^2^ + bx + c = 0,假设b^2^ - 4ac >0 +> +> x~1~ = (-b + sqrt( b*b - 4ac )) / 2a +> +> x~2~ = (-b - sqrt( b*b - 4ac )) / 2a + +```C +void root(void) +{ + float a, b, c; + float disc; + float p, q; + float x1, x2; + + printf("Please input :\n"); + scanf("%f%f%f", &a, &b, &c); + + if (b * b - 4 * a * c < 0) + { + fprintf(stderr, "EINVAL\n"); + exit(1); + } + disc = b * b - 4 * a * c; + p = -b / 2 * a; + q = sqrt(disc) / 2 * a; + + x1 = p + q; + x2 = p - q; + + printf("x1 = %f\n", x1); + printf("x2 = %f\n", x2); +} +``` diff --git a/Chapter3/IO/getchar_putchar.c b/Chapter3/IO/getchar_putchar.c new file mode 100644 index 0000000..dfd8865 --- /dev/null +++ b/Chapter3/IO/getchar_putchar.c @@ -0,0 +1,11 @@ +#include +#include + +int main() +{ + int ch; + ch = getchar(); + putchar(ch); + + exit(0); +} \ No newline at end of file diff --git a/Chapter3/IO/gets_puts.c b/Chapter3/IO/gets_puts.c new file mode 100644 index 0000000..3baea8e --- /dev/null +++ b/Chapter3/IO/gets_puts.c @@ -0,0 +1,13 @@ +#include +#include + +#define STRSIZE 32 + +int main() +{ + char str[STRSIZE]; + gets(str); + puts(str); + + exit(0); +} \ No newline at end of file diff --git a/Chapter3/IO/test.c b/Chapter3/IO/test.c new file mode 100644 index 0000000..6bdcb5c --- /dev/null +++ b/Chapter3/IO/test.c @@ -0,0 +1,91 @@ +#include +#include +#include + +#define WEIGHT 3.0e-23 +#define KQ 950 + +static void water(void) +{ + float num; + float sum; + + printf("Please input for num:"); + scanf("%f", &num); + + if (num <= 0) + { + fprintf(stderr, "Input Error!\n"); + exit(1); + } + sum = num * KQ / WEIGHT; + + printf("Total = %e\n", sum); + + return; +} + +static void area(void) +{ + float a, b, c; + float s, area; + + printf("Please input:"); + scanf("%f%f%f", &a, &b, &c); + // 对scanf做好校验是好习惯 + if (a + b <= c || b + c <= a || a + c <= b) + { + fprintf(stderr, "EINVAL!\n"); + exit(0); + } + + printf("a = %f\n", a); + printf("b = %f\n", b); + printf("c = %f\n", c); + + // s = 1 / 2 * (a + b + c); + s = 1.0 / 2 * (a + b + c); + printf("s = %f\n", s); + // s=0.000000 + // 1 / 2是int型计算,得到的是商0余1 + + area = sqrt(s * (s - a) * (s - b) * (s - c)); + printf("area = %f\n", area); +} + +void root(void) +{ + float a, b, c; + float disc; + float p, q; + float x1, x2; + + printf("Please input :\n"); + scanf("%f%f%f", &a, &b, &c); + + if (b * b - 4 * a * c < 0) + { + fprintf(stderr, "EINVAL\n"); + exit(1); + } + disc = b * b - 4 * a * c; + p = -b / 2 * a; + q = sqrt(disc) / 2 * a; + + x1 = p + q; + x2 = p - q; + + printf("x1 = %f\n", x1); + printf("x2 = %f\n", x2); +} + +int main() +{ + // water(); + + // area(); + + root(); + + exit(0); +} \ No newline at end of file