完结第三章
This commit is contained in:
@@ -207,6 +207,8 @@ int main()
|
|||||||
```c
|
```c
|
||||||
int scanf(const char *format, 地址表);
|
int scanf(const char *format, 地址表);
|
||||||
format:抑制符*
|
format:抑制符*
|
||||||
|
%s非常危险,因为不知道储存空间的大小
|
||||||
|
放在循环结构中时要注意能否接收到正常内容
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
@@ -303,5 +305,173 @@ int main()
|
|||||||
|
|
||||||
## 字符输入输出函数
|
## 字符输入输出函数
|
||||||
|
|
||||||
S
|
```c
|
||||||
|
int getchar(void);// 返回int型是考虑出错的情况
|
||||||
|
int putchar(int c);
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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 <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
11
Chapter3/IO/getchar_putchar.c
Normal file
11
Chapter3/IO/getchar_putchar.c
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int ch;
|
||||||
|
ch = getchar();
|
||||||
|
putchar(ch);
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
13
Chapter3/IO/gets_puts.c
Normal file
13
Chapter3/IO/gets_puts.c
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define STRSIZE 32
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char str[STRSIZE];
|
||||||
|
gets(str);
|
||||||
|
puts(str);
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
91
Chapter3/IO/test.c
Normal file
91
Chapter3/IO/test.c
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user