the main programming paradigms
- imperative programming 命令式编程:详细的命令机器怎么(How)去处理一件事情以达到你想要的结果(What)(java,python,c++,c)
- 声明式编程( Declarative):只告诉你想要的结果(What),机器自己摸索过程(How)(HTML,SQL,CSS)
以生活中打车到王府井大街作为例子
命令式编程(imperative):详细描述路径
下个路口左转
下个有红灯的路口右转
前进100米
在下个路口掉头
前进1500米
到达王府井大街出租车停车区
声明式编程(Declarative):只告诉目的地
带我到王府井大街。
例子
SELECT * from user
WHERE user_name = Ben
这是一个sql语句,只声明我想要找一个叫Ben的用户(What) , 就是不说SQL该怎么(How)去寻找怎么做。
for(var i=0;i<user.length;i++)
{
if(user.user_name=="Ben")
{
print("find");
break;
}
}
有过程的去找一个ben的用户。
总结:命令式编程更加的精细化,更严谨,程序也会一是不够的执行你的命令。然后声明式编程能在特定的更高层面代码领域是给我们带来效率的提升,程序员只需要对想要的结果(What)进行深思熟虑,程序会自动的解决过程(How)。
-
imperative structured programming paradigm:the programmer uses control flow oprations
- loop, conditionals, procedures.
- compared to pure imperative, easier to describe/reason about complex/large programs.
-
imperative object-oriented programming:
- well suited to represent problems with a lot of state/oprations(非常适合表示具有多种状态的问题)
- ease reasoning about large codebases.
- can sometimes be overkill for small programs(有时对于小程序来说可能是过大的杀伤力)
-
declarative programming paradigm:
- high level of abstraction(非常抽象)
- code can easily become convoluted(代码很容易混淆)
-
declarative functional programming(函数性编程):把运算过程尽量写成一系列嵌套的函数调用。函数式编程中的函数,这个术语不是指命令式编程中的函数,而是指数学中的函数,即自变量的映射(一种东西和另一种东西之间的对应关系)。也就是说,一个函数的值仅决定于函数参数的值,不依赖其他状态。
(1 + 2) * 3 - 4 命令式编程: var a = 1 + 2; var b = a * 3; var c = b - 4; 函数式编程: var result = subtract(multiply(add(1,2), 3), 4);
- 在函数式语言中,函数被称为一等函数(First-class function),与其他数据类型一样,作为一等公民,处于平等地位,可以在任何地方定义,在函数内或函数外;可以赋值给其他变量;可以作为参数,传入另一个函数,或者作为别的函数的返回值。
- loops implemented with recursion(递归实现的循环)
- pure function have no side-effects(纯功能没有副作用)
-
declarative concurrent programming paradigm(并发编程):并发编程是指在一台处理器上“同时”处理多个任务。并发是在同一实体上的多个事件。多个事件在同一时间间隔发生。
- less need for synchronization(较少同步性的需要)
总结:都是由text或fortran 公式翻译程式语言传到toolchain 工具链 在变化成machine code(计算机语言)。
c语言的介绍
#include <stdio.h>
int main(){
printf("hello,world \n");
return0
}
在终端:
gcc listing1.c -o listing1 (listing1.c是文件名字,listing1.c是source file 源文件 然后在通过compilier 编译器 变成 binary listing1)
c语言的注释:
1. //
2. /* */
-
variables :
#include <stdio.h> int main(){ int a; // 声明 名字a是一个int类型 a = 12; //设置a的值为12 printf("a:%d\n",a); //打印a的值 return 0; //标识用于退出程序,终止 main() 函数,并返回值 0。 }
variable: name (a) , type (int) , value (12). 必须要在使用前声明
-
type :
- define memory space allocated to the variable(定义分配给变量的内存空间)
- compiler checks the validity of operation on the variable(编译器检查变量操作的有效性)
- 常见三种types:int 整数,floating-point numbers 小数,characters 字符。
-
print:
%d代表了类型 type,后面的int_var代表名字 name,\n代表结束
%u代表了类型 type,后面的uint_var代表名字 name,\n代表结束 -
arrays 数组:它可以存储一个固定大小的相同类型元素的顺序集合。数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量。
-
character arrays(string):array of characters that ends with the special \0 end of string character marker.
-
command line arguments : C语言讲解命令行参数
gcc test.c -o program gcc是program test.c -o program是arguments
C语言的main函数通常含有参数argc和argv,写法通常如下:
int main(int argc,char *argv[]) int main(int argc,char **argv)
argc : 命令行传入参数的总个数
argv : *argv[]是一个指针数组,里面存放的指针指向所有的命令行参数,argv[0]指向程序的全局路径,argv[1]指向在DOS命令行中执行程序名后的第一个字符串,argv[2]指向第二个。#include <stdio.h> int main(int argc,char *argv []) { int count; printf("The command line has %d arguments:\n",); printf("argument 0 :%s\n", argv[0]); printf("argument 1 :%s\n", argv[1]); printf("argument 2 :%s\n", argv[2]); return 0; }
在终端运行: ./listing24 one two three 运行结果: The command line has 4 arguments argument 1 :./listing24 argument 2 :one argument 3 :two
-
conditionals 判断:
C 语言把任何非零和非空的值假定为 true(everthing is ture),把零或 null 假定为 false(false is 0)- if 语句: 由一个布尔表达式后跟一个或多个语句组成。
- if else语句: 后可跟一个可选的 else 语句,else 语句在先前布尔表达式为假时执行。
- 嵌套if:一个 if 或 else if 语句内使用另一个 if 或 else if 语句。
- switch 语句:一个 switch 语句允许测试一个变量等于多个值时的情况。
switch (x ){ case 1 : do something; break; case 2: do something; break; case 3: do something; break; case n: do something; break; default:(可选) do something; break; }
-
loops 循环:
- while循环:当给定条件为真时,重复语句或语句组。它会在执行循环主体之前测试条件。
- do while循环:先做do里面的内容,然后在去做while里面的内容。
- loop循环:
公式:for (int initial ; condition 条件;增加的值{
主体循环
}
-
控制循环语句:
- break:终止循环或 switch 语句,程序流将继续执行紧接着循环或 switch 的下一条语句。
- continue 语句:continue within a loop body directly goes to the next iteration.
- break:终止循环或 switch 语句,程序流将继续执行紧接着循环或 switch 的下一条语句。
-
function :
定义: return_type 返回类型值 function_name 方程名字 ( parameter list 参数 ){ body of the function 主体 }
global variable 全局变量
local variable 本地变量 -
custom types:use typedef to alias 定义 a type. 可以使用它来为类型取一个新的名字。也可以使用 typedef 来为用户自定义的数据类型取一个新的名字。
typedef unsigned char BYTE; // BYTE 可作为类型 unsigned char 的缩写
自定义一个数据类型: //定义一个对象 struct person{ char name[10]; float size; int weight } //打印 void print_person(struct person p){ xxx } //主方法 int main(int argc, char **argv){ //声明一个在struct中的变量p1 struct person p1 //为p1赋值 p1.size= 1.6; p1.weight= 200; strcpy(p1.name, "zz");//用另一种方法 //打印 struct person p2 = {"zzb",1.9,700}; print_person(p1); print_person(p2); }
-
enumeration 举例:define type allowing to assign textual names to integer constants 定义一种类型,允许将文本名称分配给整型常量
-
pointer:每一个变量都有一个内存位置,每一个内存位置都定义了可使用连字号(&)运算符访问的地址,它表示了在内存中的一个地址。指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址。
int glob = 12; typedef struct { int member1; float member2; } int main(int argc, char **argv){ mystruct ms; printf("ms address is :0x%lx,glob address is 0x%lx\n", &ms,&glob); //可以直接用&表示 }
int v =42; int *ptr = &v ptr 代表里面的地址 *ptr 代表地址里面的内容
-
pointer and structures:
typedef struct{ int m1; int m2; int *ptr_m; } my_struct *p=&mystruct_object; //声明 (*p).m1=1; //p代表了地址,而*p代表了1这个数字 加入p的地址是150而150这个地址中存放的数是1.简单说 *p指针里面的值是1 p->m2=2; //p->m2其实是(*p).m2的缩写 p->m = &(p->m2)//p->里面的内容是p->m2的地址
-
指针:
int main (){ int var = 20; /* 实际变量的声明 */ int *ip; /* 指针变量的声明 */ ip = &var; /* 在指针变量中存储 var 的地址 */ printf("Address of var variable: %p\n", &var ); 结果:Address of var variable: bffd8b3c /* 在指针变量中存储的地址 */ printf("Address stored in ip variable: %p\n", ip ); 结果:Address stored in ip variable: bffd8b3c /* 使用指针访问值 */ printf("Value of *ip variable: %d\n", *ip ); 结果:Value of *ip variable: 20 return 0; }
-
函数指针于回调函数:
-
动态内存位置 dynamic memory location :
编程时,如果您预先知道数组的大小,那么定义数组时就比较容易。例如,一个存储人名的数组,它最多容纳 100 个字符。char name[100]
但是,假如预先不知道需要存储的文本长度,例如您想存储有关一个主题的详细描述。则我们需要一个指针去指向未定义所需内存大小的字符。
通常用在heap中分配动态内容。
Allocating on the heap : malloc
malloc allocates a chunk of memory on the heap and returns a pointer to it. memory is contiguous and can be used as an array. and use free()double *ptr1;//声明指针 int main (int argc, char **argv){ int *ptr2;//声明指针 ptr1 = malloc(10*sizeof(double)); ptr2 = malloc(30*sizeof(int)); if (ptr1==null || ptr2==null ){} free(ptr1); free(ptr2); }
-
string copy: 字符串复制
char a[20],c[]="hello,world"; strcpy(a,c);
-
string connect :
-
Fgets 函数:
- 原型:
# include <stdio.h> char *fgets(char *s, int size, FILE *stream);
- 例:
int main(void){ char str[20]; printf("请输入一个字符串:");/*定义一个最大长度为19, 末尾是'\0'的字符数组来存储字符串*/ fgets(str, 7, stdin); /*从输入流stdin即输入缓冲区中读取7个字符到字符数组str中*/ printf("%s\n", str); return 0; }
结果: 请输入一个字符串:i love you i love//我们发现输入的是“i love you”,而输出只有“i love”。原因是 fgets() 只指定了读取 7 个字符放到字符数组 str 中。“i love”加上中间的空格和最后的 '\0' 正好是 7 个字符。
- 原型:
-
file open 函数:
- 定义函数:
pathname : 代表读取文件的路径int open(const char * pathname , int flags , mode_t mode)
flags:O_RDONLY 以只读方式打开文件
O_WRONLY 以只写方式打开文件
O_RDWR 以可读写方式打开文件
O_CREAT 若欲打开的文件不存在则自动建立该文件.
O_TRUNC 若文件存在并且以可写的方式打开时, 此旗标会令文件长度清为0, 而原来存于该文件的资料也会消失.
mode:
- 定义函数:
-
read 函数:
ssize_t read(int fd , void *buf , size_t count);
fd:这个是文件指针
void *buf:读上来的数据保存在缓冲区buf中,同时文件的当前读写位置向后移
size_t count:是请求读取的字节数。若参数count 为0, 则read()不会有作用并返回0. 返回值为实际读取到的字节数, 如果返回0 -
write 函数:
ssize_t write(int fd, const void *buf, size_T count); 结尾: int close (int fd);
解释:
-
random number:
-
error management :