26060 programming language paradigm

本文介绍了编程中的主要范式,包括命令式编程和声明式编程,并以SQL为例说明了声明式编程的特点。接着详细阐述了C语言的基础知识,如变量、类型、数组、字符串、控制结构、循环、函数以及指针的使用,强调了C语言在声明变量、类型检查和内存管理等方面的特点。
摘要由CSDN通过智能技术生成

the main programming paradigms

  1. imperative programming 命令式编程:详细的命令机器怎么(How)去处理一件事情以达到你想要的结果(What)(java,python,c++,c)
  2. 声明式编程( 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)。

  1. imperative structured programming paradigm:the programmer uses control flow oprations

    1. loop, conditionals, procedures.
    2. compared to pure imperative, easier to describe/reason about complex/large programs.
  2. imperative object-oriented programming:

    1. well suited to represent problems with a lot of state/oprations(非常适合表示具有多种状态的问题)
    2. ease reasoning about large codebases.
    3. can sometimes be overkill for small programs(有时对于小程序来说可能是过大的杀伤力)
  3. declarative programming paradigm:

    1. high level of abstraction(非常抽象)
    2. code can easily become convoluted(代码很容易混淆)
  4. 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);
    
    1. 在函数式语言中,函数被称为一等函数(First-class function),与其他数据类型一样,作为一等公民,处于平等地位,可以在任何地方定义,在函数内或函数外;可以赋值给其他变量;可以作为参数,传入另一个函数,或者作为别的函数的返回值。
    2. loops implemented with recursion(递归实现的循环)
    3. pure function have no side-effects(纯功能没有副作用)
  5. declarative concurrent programming paradigm(并发编程):并发编程是指在一台处理器上“同时”处理多个任务。并发是在同一实体上的多个事件。多个事件在同一时间间隔发生。

    1. 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. /*  */
  1. 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). 必须要在使用前声明

  2. type :

    1. define memory space allocated to the variable(定义分配给变量的内存空间)
    2. compiler checks the validity of operation on the variable(编译器检查变量操作的有效性)
    3. 常见三种types:int 整数,floating-point numbers 小数,characters 字符。
      在这里插入图片描述
      在这里插入图片描述
  3. print:
    在这里插入图片描述
    %d代表了类型 type,后面的int_var代表名字 name,\n代表结束
    %u代表了类型 type,后面的uint_var代表名字 name,\n代表结束

  4. arrays 数组:它可以存储一个固定大小的相同类型元素的顺序集合。数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量。在这里插入图片描述

  5. character arrays(string):array of characters that ends with the special \0 end of string character marker.
    在这里插入图片描述

  6. 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
    
  7. conditionals 判断:
    C 语言把任何非零和非空的值假定为 true(everthing is ture),把零或 null 假定为 false(false is 0)

    1. if 语句: 由一个布尔表达式后跟一个或多个语句组成。
    2. if else语句: 后可跟一个可选的 else 语句,else 语句在先前布尔表达式为假时执行。
    3. 嵌套if:一个 if 或 else if 语句内使用另一个 if 或 else if 语句。
    4. 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;
    }
    

    在这里插入图片描述

  8. loops 循环:

    1. while循环:当给定条件为真时,重复语句或语句组。它会在执行循环主体之前测试条件。
    2. do while循环:先做do里面的内容,然后在去做while里面的内容。
    3. loop循环:
      公式:for (int initial ; condition 条件;增加的值{
      主体循环
      }
  9. 控制循环语句:

    1. break:终止循环或 switch 语句,程序流将继续执行紧接着循环或 switch 的下一条语句。
      在这里插入图片描述
    2. continue 语句:continue within a loop body directly goes to the next iteration.
      在这里插入图片描述
  10. function :

    定义:
    return_type 返回类型值 function_name 方程名字 ( parameter list 参数 ){
    	 body of the function 主体
    }
    

    global variable 全局变量
    local variable 本地变量

  11. 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);
    }
    

    在这里插入图片描述

  12. enumeration 举例:define type allowing to assign textual names to integer constants 定义一种类型,允许将文本名称分配给整型常量

  13. 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 代表地址里面的内容
    

    在这里插入图片描述

  14. 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的地址
    
  15. 指针:

    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;
    }
    
  16. 函数指针于回调函数:

  17. 动态内存位置 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);
    }
    

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  18. string copy: 字符串复制

    char a[20],c[]="hello,world";
    strcpy(a,c);
    
  19. string connect :
    在这里插入图片描述

  20. Fgets 函数:

    1. 原型:
      # include <stdio.h>
      char *fgets(char *s, int size, FILE *stream);
      
    2. 例:
    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 个字符。
    
  21. file open 函数:

    1. 定义函数:
      int open(const char * pathname , int flags , mode_t mode)
      
      pathname : 代表读取文件的路径
      flags:O_RDONLY 以只读方式打开文件
      O_WRONLY 以只写方式打开文件
      O_RDWR 以可读写方式打开文件
      O_CREAT 若欲打开的文件不存在则自动建立该文件.
      O_TRUNC 若文件存在并且以可写的方式打开时, 此旗标会令文件长度清为0, 而原来存于该文件的资料也会消失.
      mode:
      在这里插入图片描述
  22. read 函数:

    ssize_t read(int fd , void *buf , size_t count);
    

    fd:这个是文件指针
    void *buf:读上来的数据保存在缓冲区buf中,同时文件的当前读写位置向后移
    size_t count:是请求读取的字节数。若参数count 为0, 则read()不会有作用并返回0. 返回值为实际读取到的字节数, 如果返回0

  23. write 函数:

    ssize_t write(int fd, const void *buf, size_T count);
    
    结尾:
    int close (int fd);
    

    解释:在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  24. random number:在这里插入图片描述

  25. error management :
    在这里插入图片描述

外文文献:Java Programming Language 摘要:Java编程语言是一种广泛使用的高级编程语言,它最初由Sun Microsystems于1995年推出。Java具有简单、可移植、面向对象、安全和高效等特点,被广泛应用于客户端程序、服务器端程序、嵌入式系统、移动应用程序等领域。本文主要介绍Java编程语言的基本特点、语法结构、面向对象编程思想、异常处理机制等方面。 关键词:Java编程语言;面向对象;语法结构;异常处理机制 Introduction Java programming language is a widely used high-level programming language, which was initially introduced by Sun Microsystems in 1995. Java is characterized by simplicity, portability, object-oriented, safety, and efficiency, making it widely used in various fields, such as client-side programs, server-side programs, embedded systems, mobile applications, etc. This article mainly introduces the basic characteristics, syntax structure, object-oriented programming ideas, exception handling mechanism, etc. of Java programming language. Basic Characteristics of Java 1. Simple: Java programming language is easy to learn and use, with a concise and clear syntax structure, which greatly reduces the complexity of programming. 2. Portable: Java programming language can run on different platforms without modification, which greatly improves the compatibility and portability of programs. 3. Object-oriented: Java programming language is based on object-oriented programming ideas, which simplifies the development of complex programs and improves the maintainability of programs. 4. Safe: Java programming language has built-in security mechanisms, such as memory management, exception handling, and access control, which greatly reduces the risk of program crashes and security vulnerabilities. 5. Efficient: Java programming language uses just-in-time compilation technology, which can dynamically compile bytecode into machine code, making the program run faster and more efficient. Syntax Structure of Java 1. Package: Java programming language organizes classes into packages, which can improve program structure and scalability. 2. Class: Java programming language defines classes using the keyword "class", which is the basic unit of object-oriented programming. 3. Method: Java programming language defines methods using the keyword "public" or "private", which encapsulates the behavior of objects. 4. Variable: Java programming language supports various types of variables, such as int, double, boolean, String, etc., which can store different types of data. 5. Control statements: Java programming language supports various control statements, such as if-else, for, while, do-while, switch, etc., which can control program flow and make programs more flexible. Object-oriented Programming Ideas of Java Java programming language is based on object-oriented programming ideas, which is a programming paradigm that emphasizes objects rather than functions. Object-oriented programming has the following characteristics: 1. Encapsulation: Java programming language encapsulates data and methods into objects, which can improve program security and maintainability. 2. Inheritance: Java programming language supports inheritance, which can reuse code and simplify program design. 3. Polymorphism: Java programming language supports polymorphism, which can implement different behaviors for the same method in different objects. 4. Abstraction: Java programming language supports abstraction, which can hide the complexity of objects and provide a simple interface for users. Exception Handling Mechanism of Java Java programming language has built-in exception handling mechanism, which can handle various types of errors and exceptions in programs. The exception handling mechanism of Java programming language has the following characteristics: 1. Try-catch block: Java programming language uses the try-catch block to handle exceptions, which can catch and handle different types of exceptions. 2. Throw statement: Java programming language uses the throw statement to throw exceptions, which can generate and propagate exceptions in programs. 3. Finally block: Java programming language uses the finally block to release resources, which can ensure that the program can run correctly even if an exception occurs. Conclusion Java programming language is a widely used high-level programming language, which has the characteristics of simplicity, portability, object-oriented, safety, and efficiency. Java programming language has a clear and concise syntax structure, which is based on object-oriented programming ideas and has built-in exception handling mechanism. Java programming language is widely used in various fields, such as client-side programs, server-side programs, embedded systems, mobile applications, etc., and has become an important tool for software development.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值