The C Programming language notes

一 基础变量类型、运算符和判断循环

        char                 字符型  character               --> char c = '\n' ;   [ASCII码 0-255]

  short int long    整数形  interger             --> int num = 120;[ unsigned 无符号表示]

       float double       浮点型(单精度,双精度)--> float celsius = 12.3;

       加减乘除取余, 与或非位运算, 赋值大于小于等于, if else,switch case, for do  while continue break goto

  

二 习题代码

   1.1 摄氏转华氏,华氏转摄氏以及逆序输出代码

#include <stdio.h>

#define LOWER 0   /* 下限 */
#define UPPER 300 /* 上限 */
#define STEP 20 /* 步长 */
typedef float ElementType; void fahrToCelsius()
{
ElementType fahr, celsius; fahr = LOWER;
// %o oct-8 | %x hex-16 | %c %s | %f %e(科学) | %d %i
printf("\nfahr to celsius:\n");
while ( fahr <= UPPER ) {
celsius = (5.0 / 9.0) * (fahr- 32.0);
// %6.2f 6个字符宽
printf("%6.2f\t%6.2f\n", fahr, celsius);
fahr = fahr + STEP;
}
} void celsiusToFahr()
{ /* 1.4 摄氏温度转华氏 */
ElementType fahr, celsius;
celsius = UPPER;
printf("\ncelsius to fahr:\n");
for ( ; celsius >= LOWER ; celsius -= STEP ) {
fahr = (9.0 / 5.0)*celsius + 32.0;
printf("%6.2f\t%6.2f\n", celsius, fahr);
}
} int main(int argc, char *argv[])
{
fahrToCelsius();
celsiusToFahr();
return ;
}

1.2 标准输入输出库的单个字符获取和输出

  getchar()从STDIN中获取单个字符并返回其值,如果到达文件尾则返回EOF, putchar( cha ) 将单个字符cha输出到STDOUT

    printf("This is:%d\n", EOF);  /* EOF define as -1 */

  gets()可获取包含空格的字符串,并返回 char *指针, puts()则将字符串输出

     printf()和scanf()函数原型如下,分别以特定格式输出和获取输入

  int printf( const char *format, ... );
int scanf( const char *format, ... );

   字符获取输出程序如下: getchar()直到在WIndows上按下CTRL+Z产生EOF输入才停止

void getPutChar()
{ /* 1.5 字符输入输出 copy input to output */
int c; /* c要足够大,能存储任何getchar()的返回值 */
while ( (c = getchar())!= EOF ) {
// 赋值表达式的值为赋值后左边变量的值
putchar(c); /* EOF end of file */
}
}

1.3 标准输入输出库的单个字符获取和输出

统计字符流中的空白、换行和制表符

void cntSpaceTable()
{ /* 1-8 count space table and newline chars */
int c; /* big volume */
int nl, nt, ns;
nl = ns = nt = ;
while ( (c = getchar()) != EOF ) {
if ( c == '\n' ) {
nl++;
} else if ( c == '\t' ) {
nt++;
} else if ( c == ' ' ) {
ns++;
}
}
printf("num of space, table, newline: %d, %d, %d\n"
, ns, nt, nl);
}

1-9将字符流中的多个空格替换为一个空格,  1-10将字符进行替换

void spaceChange()
{ /* 1-9 alterante mutilple sapces into one space */
int c, cs = ; /* cs -> count space */
while ( (c = getchar()) != EOF ) {
if ( c == ' ' ) {
// print the first space, omit other space
if ( cs == ) {
putchar( c );
cs++;
}
} else {
cs = ; // 恢复计数
putchar( c );
}
}
} void charChange()
{ /* 1-10 alterante table back and slash */
int c;
while ( (c = getchar()) != EOF ) {
switch( c ) {
case '\t': printf( "\\t" );
break;
case '\b': printf( "\\b" );
break;
case '\\': printf( "\\\\ ");
break;
default: putchar(c);
break;
}
}
printf("End\n");
}

1.4 统计单词数量

/* 1.5.4 count lines, words, and chars in input */
void countWords()
{ /* 单词计数,单词不包含空格、制表、换行符 */
int c, nl, nw, nc, state; state = OUT;
nl = nw = nc = ;
/* 每当遇到单词的第一个字符,就作为一个新单词计数 */
while ( (c = getchar()) != EOF ) {
++nc; /* count char */
if ( c == '\n' ) {
++nl;
}
if ( c == ' ' ||
c == '\n' ||
c == '\t' ) {
state = OUT; /* not a word */
} else if ( state == OUT ) {
state = IN;
++nw; /* start count word */
}
} /* end of while */
printf("chars:%d, lines:%d, words:%d\n", nc, nl, nw);
} void showSingleWord()
{ /* 1-12 每个单词一行 */
int c, state; state = OUT;
printf("Print Single Word:\n");
while ( (c = getchar()) != EOF ) {
if ( c == ' ' || c == '\n' || c == '\t' ) {
if ( state == IN ) {
printf("\n"); /* end of a word */
}
state = OUT; /* not a word */
} else if ( state == OUT ) {
state = IN; /* start count word */
}
if ( state == IN ) {
putchar(c); /* print each char of word */
}
} /* end of while */
}

1.5统计字符打印次数直方图

void printGraph( int num )
{ /* Histograph subprogram*/
int i;
for ( i = ; i < num; i++ ) {
printf("|");
}
printf("\n");
} void countOtherChars()
{ /* 统计数字和字母以及空格和其他字符,打印直方图 */
int c, i, nwhite ,nother;
int ndigit[]; /* 0-9 */
int nalpha[]; /* A-Z */ nwhite = nother = ;
for ( i = ; i < ; ++i ) {
if ( i < ) {
ndigit[i] = ; /* init */
}
nalpha[i] = ;
} while ( (c = getchar()) != EOF ) {
if ( c >= '' && c <= '' ) {
++ndigit[c - ''];
} else if( c >='A' && c <= 'Z' ) {
++nalpha[c - 'A']; /* upper */
} else if( c >='a' && c <= 'z' ) {
++nalpha[c - 'a']; /* lower */
}
else if ( c == ' ' || c == '\n' || c == '\t' ) {
/* space */
++nwhite;
} else {
++nother;
}
} /* end of while */ for ( i = ; i < ; ++i ) {
printf("%d->", i); //printf(" %d", ndigit[i]);
printGraph( ndigit[i] );
}
printf("\n ");
for ( i = ; i < ; ++i ) {
printf("%c->", i+'a');//printf(" %d", nalpha[i]);
printGraph( nalpha[i] );
}
printf("\nwhite space->");
printGraph(nwhite);
printf("\nother->");
printGraph(nother);
}

参考资料: 《C程序设计语言》

转载请注明:https://www.cnblogs.com/justLittleStar/p/10487055.html

C程序设计语言笔记-第一章的更多相关文章

  1. 《JavaScript高级程序设计》笔记——第一章到第三章

    2019年,新年伊始,我打算好好重读一下<JavaScript高级程序设计>这本前端必备经典书.每天半小时. 以下内容摘自<JavaScript高级程序设计> 2019-2-1 ...

  2. JavaScript高级程序设计学习笔记第一章

    作为学习javascript的小白,为了督促自己读书,写下自己在读书时的提炼的关键点. 第一章: 1.JavaScript简史:Netscape Navigator中的JavaScript与Inter ...

  3. python程序设计语言笔记 第一部分 程序设计基础

    1.1.1中央处理器(CPU) cpu是计算机的大脑,它从内存中获取指令然后执行这些指令,CPU通常由控制单元和逻辑单元组成. 控制单元用来控制和协调除cpu之外的其他组件的动作. 算数单元用来完成数 ...

  4. JavaScript高级程序设计 读书笔记 第一章

    JavaScript是一种专门为与网页交互而设计的脚本语言 JavaScript实现 ECMAscript---核心 DOM---文档对象模型 BOM---浏览器对象模型

  5. 《linux程序设计》笔记 第一章 入门

    linux程序存放位置linux主要有一下几个存放程序的目录: /bin    系统启动程序目录 /usr/bin 用户使用的标准程序 /usr/local/bin   用于存放软件安装目录 /usr ...

  6. Android开发艺术探索笔记——第一章:Activity的生命周期和启动模式

    Android开发艺术探索笔记--第一章:Activity的生命周期和启动模式 怀着无比崇敬的心情翻开了这本书,路漫漫其修远兮,程序人生,为自己加油! 一.序 作为这本书的第一章,主席还是把Activ ...

  7. C++ Primer 笔记 第一章

    C++ Primer 学习笔记 第一章 快速入门 1.1 main函数 系统通过调用main函数来执行程序,并通过main函数的返回值确定程序是否成功执行完毕.通常返回0值表明程序成功执行完毕: ma ...

  8. Android群英传笔记——第一章:Android体系与系统架构

    Android群英传笔记--第一章:Android体系与系统架构 图片都是摘抄自网络 今天确实挺忙的,不过把第一章的笔记做一下还是可以的,嘿嘿 1.1 Google的生态圈 还是得从Android的起 ...

  9. Scala语言笔记 - 第一篇

    目录 Scala语言笔记 - 第一篇 1 基本类型和循环的使用 2 String相关 3 模式匹配相关 4 class相关 5 函数调用相关 Scala语言笔记 - 第一篇 ​ 最近研究了下scala ...

随机推荐

  1. 微软操作系统 Windows Server 2012 R2 官方原版镜像

    微软操作系统 Windows Server 2012 R2 官方原版镜像 Windows Server 2012 R2 是由微软公司(Microsoft)设计开发的新一代的服务器专属操作系统,其核心版 ...

  2. python全栈学习笔记(二)网络基础之子网划分

    阅读目录 一.ip地址基本知识 1.1 ip地址的结构和分类 1.2 特殊ip地址 1.3 子网掩码 1.4 ip地址申请 二.子网划分 2.1 子网划分概念 2.2 c类子网划分初探 2.3 子网划 ...

  3. 微软在线 VSTS/TFS 使用简介,如何删除项目,帐号,获取git地址等

    名称:微软 VSTS 全称: Visual Studio Team Services 地址:https://www.visualstudio.com/zh-hans/ 说明:注册就可以了使用了(如何使 ...

  4. 基于SAP Kyma的订单编排增强介绍

    尽管有一万个舍不得,2018年还是无可挽回地离我们远去了. 唯有SAP成都研究院的同事和我去年在网络上留下的这些痕迹,能证明2018年我们曾经很认真地去度过每一天: SAP成都研究院2018年总共87 ...

  5. 040同步条件event

    条件同步和条件变量同步差不多意思,只是少了锁功能,因为条件同步设计于不访问共享资源的条件环境,event=threading.Event():条件环境对象,初始值为False.event.isSet( ...

  6. UVa 1395 - Slim Span(最小生成树变形)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. BZOJ5293:[BJOI2018]求和(LCA,差分)

    Description master 对树上的求和非常感兴趣.他生成了一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k  次方和,而且每次的k 可能是不同的.此处节点深度的定义是这个节点 ...

  8. PHP------析构方法

    析 构 方 法 封装,有一个叫构造函数 和构造函数对应的还有一种方法叫做析构. class ren    //一个类 是 人类 { public $mingzi ://成员变量 punction__d ...

  9. jquery 写的图片左右连续滚动

    <style type="text/css"> *{ margin:0; padding:0;} body { font:12px/1.8 Arial; color:# ...

  10. Jmeter--调度器配置

    Jmeter的线程组设置里有一个调配器设置,用于设置该线程组下脚本执行的开始时间.结束时间.持续时间及启动延迟时间.当需要半夜执行性能测试时会用到这个功能. ps:设置调度器配置,需要将前面的循环次数 ...