第一章代码
序言
为什么抄写
1、平时看代码的时间实在是太多了,这样的现象是自己写个基本字符串复制、求字符串长度都不会了
2、对于编程,技术一直没有进步,想用死的的办法试试看看有没有效果。
3、计划是把这本书里面的代码全部抄写一遍,然后试着去做完书里面所有的练习题目。
抄写一章之后体会
1、输入多行,保存最长的一行,然后再输出,这个程序我就卡壳了,好歹我也是上班三年的人了,编程技术一直没有提高,这就可以看出来。
2、C程序设计语言,这本书还是非常的经典的。书里说学习第一章之后,就可以写很多有趣的程序了。确实是如此,知道输入输出了,还有数组,函数,这样就可以应对单片机编程了。
#include <stdio.h>
void temp_translate(void);
void temp_translate_v2(void);
void temp_translate_v3(void);
void temp_translate_v4(void);
//1.5 字符输入与输出
//1.5.1
void getchar_test_v1(void);
void getchar_test_v2(void);
//1.5.2
void char_count_test_v1(void);
void char_count_test_v2(void);
void line_count_test(void);
//1.5.4
void word_count_test(void);
//1.6 数组
void array_test(void);
//1.7 函数
void power_func_test(void);
int power_func(int base,int n);
int power_func_v1(int base,int n);
//1.8 字符数组
void output_max_line(void);
int getline(char s[],int lim);
int getline_v1(void);
void copy(char to[],char from[]); //只要函数类型不匹配,马上就会报错误。
void copy_v1(void);
void getline_test(void);
int main(void)
{
//temp_translate();
//temp_translate_v2();
//temp_translate_v3();
//temp_translate_v4();
//1.5.1
//getchar_test_v1();
//getchar_test_v2();
//1.5.2
//char_count_test_v1();
//char_count_test_v2();
//line_count_test();
//1.5.4
//word_count_test();
//1.6
//array_test();
//1.7
//power_func_test();
//1.8字符数组测试
//output_max_line();
getline_test();
return 0;
}
void temp_translate(void)
{
int fahr,celsius;
int lower,upper,step;
lower = 0;
upper = 300;
step = 20;
fahr = lower;
while (fahr <= upper){
celsius = 5*(fahr-3) /9;
printf("%d\t%d\n",fahr,celsius );
fahr = fahr + step;
}
}
void temp_translate_v2(void)
{
float fahr,celsius;
float lower,upper,step;
lower = 0;
upper = 300;
step = 20;
fahr = lower;
while (fahr <= upper){
celsius = 5.0*(fahr-32.0) /9.0;
printf("%3.0f%6.1f\n",fahr,celsius);
fahr = fahr + step;
}
}
void temp_translate_v3(void)
{
int fahr;
for (fahr = 0; fahr <=300; fahr = fahr + 20)
printf("%3d %6.1f\n",fahr,(5.0/9.0)*(fahr - 32) );
}
void temp_translate_v4(void)
{
#define LOWER 0
#define UPPER 300
#define STEP 20
int fahr;
for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
printf("%3d %6.1f\n",fahr,(5.0 /9.0) *(fahr - 32));
}
//1.5.1
void getchar_test_v1(void)
{
int c;
c = getchar();
while ( c!= EOF)
{
putchar(c);
c = getchar();
}
}
void getchar_test_v2(void)
{
int c;
while (( c = getchar()) != EOF)
putchar(c);
}
void char_count_test_v1()
{
long nc;
nc = 0;
while ( getchar() != EOF)
{
++nc;
printf("%ld\n",nc);
}
}
void char_count_test_v2(void)
{
double nc;
for (nc = 0; getchar() != '='; ++nc)
;
printf("%0.f\n", nc);
}
void line_count_test(void)
{
int c,nl;
nl = 0;
while ((c = getchar()) != '=')
if (c== '\n')
++nl;
printf("%d\n",nl );
}
void word_count_test(void)
{
#define IN 1
#define OUT 0
int c,nl,nw,nc,state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != '=')
{
++nc;
if ( c == '\n')
++nl;
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
//
else if (state == OUT)
{
state = IN;
++nw;
}
}
printf("%d %d %d\n",nl,nw,nc);
}
void array_test(void)
{
int c,i,nwhite,nother;
int ndigit[10];
nwhite = nother = 0;
for (i=0; i < 10; i++)
ndigit[i] = 0;
while ((c = getchar()) != '=')
{
if ( c >= '0' && c <='9')
++ndigit[c - '0'];
else if ( c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
nother++;
printf("digit =");
for (i = 0; i < 10; i++)
printf("%d ",ndigit[i]);
printf(",white space = %d,other = %d\n",
nwhite,nother);
}
}
int power_func(int base,int n)
{
int i, p;
p = 1;
for (i = 1; i <= n; ++i)
p = p * base;
return p;
}
int power_func_v1(int base,int n)
{
int p;
for (p = 1; n > 0; --n)
p = p * base;
}
void power_func_test(void)
{
int i;
for (i = 1; i < 10; ++i)
{
//printf("%d %d %d\n",i,power_func(2,i),power_func(-3,i));
printf("%d %d %d\n",i,power_func_v1(2,i),power_func_v1(3,i));
}
}
//===================1.9字符数组 开始=============================================
#define MAXLINE 1000
char line[MAXLINE];
char longest[MAXLINE];
void output_max_line(void)
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getline(line,MAXLINE)) > 0)
{
if (len > max)
{
max = len;
copy(longest,line);
}
}
if (max > 0)
{
printf("%s\n",longest);
}
}
int getline(char s[],int lim)
{
int c,i;
for (i = 0; i < lim -1 && ((c = getchar()) != '-') && c != '\n'; ++i)
s[i] = c;
if ( c == '\n')
{
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[],char from[])
{
int i;
i = 0;
while ((to[i] = from[i] )!= '\0') // 这里括号添加错了,直接结果不对。
++i;
}
int getline_v1(void)
{
int c, i;
extern char line[];
for (i = 0; i < MAXLINE - 1
&& (c = getchar()) != '=' && c != '\n'; ++i)
line[i] = c;
if (c == '\n')
{
line[i] = c;
++i;
}
}
void copy_v1(void)
{
int i;
extern char line[];
extern char longest[];
while ((longest[i] = line[i]) != '\0')
i++;
}
void getline_test(void)
{
int n;
n = getline(line,MAXLINE);
//printf("%s",line);
printf("n = %d\n", n);
}
//===================1.9字符数组结束=============================================