Author:

AlbertWechat Official Account: 小宁静致远

小宁和你聊编程之四: 语句篇

Posted by – September 26, 2017

上一篇《小宁和你聊编程之三: 类型篇》,我们主要和大家聊了聊什么是变量的类型。大家还有印象么?什么是类型,我们将它应用在什么地方?我们又是如何在编程实践中使用它的呢? 你有什么想法需要和我交流么?你遇到了什么困难么?有什么我可以帮助你的?

让我们再次回顾下类型的内容,一起来复习一下:
什么是类型:

类型就是变量的一个分类(归类)。

比如我们把红富士、黄元帅都叫做苹果, 我们把海南香蕉、进口香蕉都要叫香蕉.
我们把和田大枣、西安洋县枣都叫大枣. 或者我们把男孩子,女孩子都叫孩子。
这是对TA们的一个分类。在编程中这个分类就是其类型。
我们也举例进行了说明。比如:

int iFive = 5; //int(整型)变量iFive的值为5.
string strJ="jin"; //字符串strJ的值为"jin"

我们前三节说明了编程的本质 、编程的核心概念: 变量编程的核心概念:类型 . 那么变量和类型他们是如何组合起来发挥作用的呢?
我们今天来看编程的另一个核心概念: 语句

那么什么是语句呢?其它我们早就见过它了。
1.什么是语句?


简单来说, 语句就是表达完整意思的一句话。

这里面有两个要点:
.表达完整意思: 就像你说话得说完整,要不然大家会听不明白。
比如你说,我现在要… 如果你这么说,大家就在等着你说完,要不不知道你要做什么。编程也是一样。
生活中我们是为了交流,沟通,编写代码时,则是为了清楚地告诉计算机我们想让它做什么。

.一句话: 这句话通常以;(绝大多数情况) 或者右大括号}, 或者>, 或者以) 结束.
现实中,我们说话的时候,说完了会有一个句号,或者一个省略号,或者一到多个!, 或者?号等等。
总之大家知道这句话结束了。那么在编程中,好多程序都有语句结束标志。
比如C++中,绝大多数情况下以;或者}为语句结束的标志。

2.语句的应用?(举个例子)


那么语句在编程中是如何使用的呢?其实它和变量、类型一样,随处可见,应用及其广泛.
基本可以说是变量和类型组成了语句。比如:

int i23=23; //语句,以int开始, 以;结束.
int iSix=6; //一条语句,以int始, 以;终.

再如:

string strJ="j"; //语句:以string始,以;终
string strJor="Mi"; //语句:以string开始,以;终

又如:

char cCh='c'; //语句:以char始,以;终.
char * pHi="Hi"; //语句:以chra始,以;终.

最后来个空语句:

; //以;结束的语句。什么也不做.

3.语句的实践?


我们上面和大家举例说明了语句在程序中的应用, 那现在来实践一下.写段代码来使用它们。
请打开VS2013或者打开http://cpp.sh来写上如下代码:

#include <iostream> //一条语句, 以>结束
#include <string> //一条语句
using namespace std; //一条语句
int main()  //以)结束
{
    int i23 = 23; // 一条完整的语句
    int iSix = 6; // 

    float fFivePotFive = 5.5; // 一条完整的语句。告诉计算机保存变量的值为5.5
    float fNine = 9;        // 

    string strJin = "jin"; // 告诉计算机, 在strJin中保存"jin"这个字符串。
    string strJordan = "Michael Jordan"; //

    char cChar = 'c';  //告诉计算机在cChar中保存字符'c'
    char * pHello = "Hello"; //

    std::cout << i23 << "," << iSix << std::endl; 
    //一条语句:打印出 i23的值, 一个,号, 和iSix的值,并且换行
    std::cout << fFivePotFive << "," << fNine << std::endl;
    //一条语句:打印出strJin的值,一个逗号, strJordan的值,然后换行
    std::cout << strJin << "," << strJordan << std::endl;
    std::cout << cChar << "," << pHello << std::endl;
    return 0;
}

如果使用的是VS2013, 那么我们按F7, 如果没有错误的话, 就会编译生成我们的程序,并且会有下面的输出:
vs2013语句: 程序输出

如果使用http://cpp.sh也会产生下面的输出.
cpp.sh语句: 程序输出

这样我们就定义了很多条“语句”, 程序就是由很多条语句组成完整的意思.就像我们说了好多句话,表达了我们的想法。然后我们看到了程序运行后输出的结果.

恭喜你, 今天你和我一起学习了又一基本概念: 语句
编程核心概念之四: 语句 
语句:

语句是表达完整意思的一句话。

语句的应用
语句的实践

怎么样?有没有理解语句的概念?你是如何理解C++中语句的概念的?能不能举几个例子?

请大家多多动手,让编程也成为你的一种爱好和生产力。
戳右上角 小宁静致远 或扫码关注我, 欢迎、感激传播! ^_^
期待和你多交流、共成长。谢谢!
扫描关注小宁静致远

鼓励我的创作?
您的鼓励我的动力!

Baidu cloud disk inviting codes

Posted by – May 13, 2012

http://pan.baidu.com/netdisk/beinvited?invite_code=36dd4118cc41a7a24a90700cef520bc0
http://pan.baidu.com/netdisk/beinvited?invite_code=cffb8a32f35851d92bf406260d27c9ee
http://pan.baidu.com/netdisk/beinvited?invite_code=74cec1471c2fe4a9b080677dc20447ab
http://pan.baidu.com/netdisk/beinvited?invite_code=562d173913cf091817788cc6fbc48150
http://pan.baidu.com/netdisk/beinvited?invite_code=616a89a58f45564ac58bfc38fff1e361
http://pan.baidu.com/netdisk/beinvited?invite_code=58838bb88468da95bbb87f574ece4cb8
http://pan.baidu.com/netdisk/beinvited?invite_code=d5fcf132aad8be4ff1a55ad68a5d964d

Env for scripts interpreter searching

Posted by – February 15, 2012

#!/usr/bin/env bash
Probably the most common use of env is to find the correct interpreter
for a script, when the interpreter may be in different directories on
different systems.  The following example will find the `perl’ inter-
preter by searching through the directories specified by PATH.
1.  #!/usr/bin/env perl
One limitation of that example is that it assumes the user’s value for
PATH is set to a value which will find the interpreter you want to exe-
cute.  The -P option can be used to make sure a specific list of directo-
ries is used in the search for utility.  Note that the -S option is also
required for this example to work correctly.
2. #!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl
The above finds `perl’ only if it is in /usr/local/bin or /usr/bin.  That
could be combined with the present value of PATH, to provide more flexi-
bility.  Note that spaces are not required between the -S and -P options:
3. #!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl

#!/usr/bin/env bash
Probably the most common use of env is to find the correct interpreter     for a script, when the interpreter may be in different directories on     different systems.  The following example will find the `perl’ inter-     preter by searching through the directories specified by PATH.
1.  #!/usr/bin/env perl     One limitation of that example is that it assumes the user’s value for     PATH is set to a value which will find the interpreter you want to exe-     cute.  The -P option can be used to make sure a specific list of directo-     ries is used in the search for utility.  Note that the -S option is also     required for this example to work correctly.
2. #!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl
The above finds `perl’ only if it is in /usr/local/bin or /usr/bin.  That     could be combined with the present value of PATH, to provide more flexi-     bility.  Note that spaces are not required between the -S and -P options:
3. #!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl

Env

Posted by – February 15, 2012

#!/usr/bin/env bash
Probably the most common use of env is to find the correct interpreter
for a script, when the interpreter may be in different directories on
different systems.  The following example will find the `perl’ inter-
preter by searching through the directories specified by PATH.
1.  #!/usr/bin/env perl
One limitation of that example is that it assumes the user’s value for
PATH is set to a value which will find the interpreter you want to exe-
cute.  The -P option can be used to make sure a specific list of directo-
ries is used in the search for utility.  Note that the -S option is also
required for this example to work correctly.
2. #!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl
The above finds `perl’ only if it is in /usr/local/bin or /usr/bin.  That
could be combined with the present value of PATH, to provide more flexi-
bility.  Note that spaces are not required between the -S and -P options:
3. #!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl

#!/usr/bin/env bash
Probably the most common use of env is to find the correct interpreter     for a script, when the interpreter may be in different directories on     different systems.  The following example will find the `perl’ inter-     preter by searching through the directories specified by PATH.
1.  #!/usr/bin/env perl     One limitation of that example is that it assumes the user’s value for     PATH is set to a value which will find the interpreter you want to exe-     cute.  The -P option can be used to make sure a specific list of directo-     ries is used in the search for utility.  Note that the -S option is also     required for this example to work correctly.
2. #!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl
The above finds `perl’ only if it is in /usr/local/bin or /usr/bin.  That     could be combined with the present value of PATH, to provide more flexi-     bility.  Note that spaces are not required between the -S and -P options:
3. #!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl

地铁线路图

Posted by – January 3, 2012

地铁线路图
http://map.baidu.com/subways/index.html

MS DOS info/tip

Posted by – October 27, 2011

http://www.dostips.com/DtTipsStringOperations.php#Function.rTrim
http://doshome.com

C++ Primer the fourth Edition

Posted by – September 28, 2011

See http://jinlab.com/website/cpp/

Using tar together with gzip

Posted by – September 22, 2011

How can I extract a tar.gz or .tgz file?
Files with extension tar.gz or .tgz are tar files compressed with gzip. On Unix extract them with:

gunzip < file.tar.gz | tar xvf –
gunzip < file.tgz    | tar xvf –
Can gzip compress several files into a single archive?
Not directly. You can first create a tar file then compress it:

tar cvf –  filenames | gzip > file.tar.gz

stty 显示和修改终端行设置

Posted by – September 13, 2011

学习和转载与: http://codingstandards.iteye.com/blog/826924

用途说明

stty命令用于显示和修改终端行设置(change and print terminal line settings)。

常用参数

stty命令不带参数可以打印终端行设置,加上-a参数可以打印得更详细些。

stty size可以显示终端的大小,即行数和列数。

stty命令还可以更改终端行的设置,格式如下:

stty SETTING CHAR

其中,SETTING可以是如下

eof : 输入结束,文件结束,默认为Ctrl+D。比如:用cat >file来创建文件时,按Ctrl+D来结束输入。
erase : 向后删除字符,擦除最后一个输入字符,默认为Ctrl+?。注意默认情况下退格键Backspace不是删除字符。
intr : 中断当前程序,默认为Ctrl+C。
kill : 删除整条命令,删除整行,默认为Ctrl+U。
quit :退出当前程序,默认为Ctrl+\或Ctrl+|。
start : 启动屏幕输出,默认为Ctrl+Q。
stop :停止屏幕输出,默认为Ctrl+S。有时候终端突然僵死了,可能是不小心按了Ctrl+S的缘故,因为我们习惯性的按Ctrl+S来保存文件。
susp : terminal stop当前程序,默认为Ctrl+Z。这样当前进程就会变成后台进程了。

werase:删除最后一个单词,默认为Ctrl+W。

stty命令还有一些其他用法,如:

stty -echo 关闭回显。比如在脚本中用于输入密码时。

stty echo 打开回显。

输入密码的脚本片段:stty -echo; read var; stty echo; 或 read -s var

使用示例

示例一 打印终端行设置

[root@web ~]# stty
speed 38400 baud; line = 0;
-brkint -imaxbel
[root@web ~]# stty -a
speed 38400 baud; rows 41; columns 132; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;
susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -cdtrdsr
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

[root@web ~]#

示例二 打印当前终端的大小(行数和列数)

[root@web ~]# stty size
41 132

示例三 设置退格键Backspace的删除行为

在默认情况下,我们按退格键Backspace时,会在屏幕上回显^H,而不是把前一个字符删除。比如使用sftp/ftp/sqlplus/ij等命令时,就会碰到这种情况。我们可以使用stty命令把Backspace的行为变成删除前一个字符。

[root@web ~]# sftp 192.168.6.12
Connecting to 192.168.6.12…
root@192.168.6.12’s password:
sftp> get abc^H^H^H^H
Couldn’t stat remote file: No such file or directory
File “/root” not found.
sftp> quit
[root@web ~]#
[root@web ~]# stty erase ^H
[root@web ~]# sftp 192.168.6.12
Connecting to 192.168.6.12…
root@192.168.6.12’s password:
sftp> get abc
Couldn’t stat remote file: No such file or directory
File “/root/abc” not found.
sftp> quit
[root@web ~]#

示例四 在vi编辑文件时按Ctrl+Q来结束终端僵死的局面

[root@web ~]# vi 1.txt
1
2
3
Ctrl+S
~

注:按了Ctrl+S之后,就会禁止屏幕输出,从而出现终端僵死的情况。

这个时候,只要按Ctrl+Q就会结束这种局面,因为它会允许屏幕输出。

Ctrl+Q

示例五 在bash脚本中与tty命令配合使用

在下面的脚本中,先取得终端文件名称,以判断脚本是否运行在交互式方式还是在管道方式。

Bash代码  收藏代码
  1. #!/bin/sh
  2. CONSOLE_INPUT=$(tty)
  3. echo “CONSOLE_INPUT=$CONSOLE_INPUT”
  4. COMMAND_LINE=”java -Xmx640m -classpath .:lib/hyjc.jar:lib/log4j-1.2.15.jar:lib/commons-logging-1.1.1.jar:lib/proxool-0.9.0RC3.jar:lib/mysql-connector-java-5.1.10-bin.jar:lib/poi-3.5-ZHY-20091107.jar:lib/jdom-1.1.jar hyjc.sql.DBCLI MhrDB.properties MhrDB”
  5. if [ “$CONSOLE_INPUT” == “not a tty” ]; then
  6. $COMMAND_LINE
  7. elif [ “$(whereis rlwrap)” == “rlwrap:” ]; then
  8. stty erase ^H
  9. $COMMAND_LINE
  10. else
  11. rlwrap -f jdb.rlwrap $COMMAND_LINE
  12. fi

vim remotely edit using scp/sftp/ftp/rsync etc.

Posted by – July 22, 2011

Here:
http://vimdoc.sourceforge.net/htmldoc/pi_netrw.html#netrw

e.g.:
vim scp://jinlab.com// path/to/file