利用OpenAI ChatGPT进行编码尝试体验

 更新时间:2023年08月09日 15:41:29   作者:天行无忌  
这几天chatgpt可是非常火的,下面这篇文章主要给大家介绍了关于利用OpenAI ChatGPT进行编码尝试体验的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

ChatGPT 是一种以类似聊天的方式进行交互的人工智能网络,从名字上看起来像是另一个“聊天机器人”,但实际上已经远超聊天的范畴了,当要求它编写一些代码时,结果相等震撼,它可以写出一些意想不到的连贯代码,而这些代码对于开发者很有启发。

开始要求它使用 JavaScript 编写一个散列(HASH)密码的函数。具体一点就是编写一个 JavaScript 函数,它接受密码的字符串输入并输出散列(HASH)密码。

function hashPassword(password) {
    return crypto.createHash("sha256").update(password).digest("hex");
}

然后逐步提高编码的难度,实现一些更加符合项目开发的需求,于是输入:

编写一个在请求正文中包含用户名和密码的 Express 路由处理程序,对密码进行哈希处理,在 MySQL 数据库中查找现有用户并检查哈希后的密码是否匹配。

const express = require("express");
const bcrypt = require("bcrypt");
const mysql = require("mysql");

// 创建 MySQL 数据库连接
const db = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "mydb",
});

// 连接数据库
db.connect((err) => {
    if (err) {
        throw err;
    }
    console.log("MySQL connected...");
});

// 创建 express app
const app = express();

// 路由 handler
app.post("/login", (req, res) => {
    // get username and password from request body
    const { username, password } = req.body;

    // HASH 密码
    const hashedPassword = bcrypt.hashSync(password, 10);

    // 在数据库中查找用户名称
    let sql = `SELECT * FROM users WHERE username = '${username}'`;
    let query = db.query(sql, (err, result) => {
        if (err) throw err;

        // 检查密码是否匹配
        if (bcrypt.compareSync(result[0].password, hashedPassword)) {
            res.send("Login successful!");
        } else {
            res.send("Incorrect username or password");
        }
    });
});

这段 JavaScript 代码基本是没什么问题的,可以直接复制到项目中进行调试。接下来使用别的语言来试一下:

用C语言编写一个链表结构的代码,具有根据“标识”属性在链表中添加、删除和查找节点的功能。

#include <stdio.h>
#include <stdlib.h>

// Structure for a node in the linked list
typedef struct Node {
    int identity; // Property of the node
    struct Node *next; // Pointer to the next node
} Node;
// Function to add a node to the linked list
void addNode(Node **head, int identity) {
    // Create a new node
    Node *newNode = (Node*)malloc(sizeof(Node));
    // Set the identity of the new node
    newNode->identity = identity;
    // Set the next pointer of the new node to point to the head
    newNode->next = *head;
    // Set the head to point to the new node
    *head = newNode;
}
// Function to delete a node from the linked list
void deleteNode(Node **head, int identity) {
    // Create a temporary node to traverse the linked list
    Node *temp = *head;
    // Check if the head is the node to be deleted
    if (temp != NULL && temp->identity == identity) {
        // Set the head to point to the next node
        *head = temp->next;
        // Free the memory allocated for the node
        free(temp);
        return;
    }
    // Traverse the linked list to find the node to be deleted
    while (temp != NULL && temp->identity != identity) {
        // Set the previous node to the current node
        Node *prev = temp;
        // Set the current node to the next node
        temp = temp->next;
    }
    // Check if the node was found
    if (temp == NULL) return;
    // Set the previous node to point to the next node
    prev->next = temp->next;
    // Free the memory allocated for the node
    free(temp);
}
// Function to find a node in the linked list
Node* findNode(Node *head, int identity) {
    // Create a temporary node to traverse the linked list
    Node *temp = head;
    // Traverse the linked list to find the node
    while (temp != NULL) {
        // Check if the current node has the given identity
        if (temp->identity == identity) {
            return temp;
        }
        // Set the current node to the next node
        temp = temp->next;
    }
    // Return NULL if the node was not found
    return NULL;
}
int main() {
    // Create a head node for the linked list
    Node *head = NULL;
    // Add some nodes to the linked list
    addNode(&head, 1);
    addNode(&head, 2);
    addNode(&head, 3);
    // Find a node in the linked list
    Node *node = findNode(head, 2);
    // Print the identity of the node
    printf("Node with identity 2: %d\n", node->identity);
    // Delete a node from the linked list
    deleteNode(&head, 2);
    // Find the deleted node in the linked list
    node = findNode(head, 2);
    // Check if the node was deleted
    if (node == NULL) {
        printf("Node with identity 2 was deleted\n");
    }
    return 0;
}

于是似乎开始担心未来的程序员,对于AI的机器学习能力,肯定是强于人类的。一个程序员宝贵的职业生涯就算 20 年,就假设这个程序员在职业生涯中相当勤奋好学,可AI应该可以通过学习收集全世界所有程序员的经验!人脑无法与在极其庞大的数据集上训练有素的 AI 模型的计算能力相提并论。未来是不是有可能程序员通过AI把自己这个职业淘汰掉!

总结

到此这篇关于利用OpenAI ChatGPT进行编码尝试体验的文章就介绍到这了,更多相关OpenAI ChatGPT编码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • js代码实现无缝滚动(文字和图片)

    js代码实现无缝滚动(文字和图片)

    js无缝滚动,图片无缝滚动,文字无缝滚动,基于js代码如何实现,本篇文章给大家详解js代码实现无缝滚动(文字和图片),需要的朋友可以参考下
    2015-08-08
  • 微信小程序利用canvas 绘制幸运大转盘功能

    微信小程序利用canvas 绘制幸运大转盘功能

    本文通过一段简单的实例代码给大家介绍微信小程序利用canvas 绘制幸运大转盘,代码很简单,感兴趣的朋友跟随脚本之家小编一起看看吧
    2018-07-07
  • js点击事件链接的问题解决

    js点击事件链接的问题解决

    这篇文章主要介绍了js点击事件链接的问题解决方法,需要的朋友可以参考下
    2014-04-04
  • 微信小程序开发之实现食堂点餐系统

    微信小程序开发之实现食堂点餐系统

    这篇文章主要为大家详细介绍了如何通过微信小程序开发一个简单的食堂点餐系统,文中的示例代码讲解详细,感兴趣的小伙伴可以和小编一起学习一下
    2023-01-01
  • uniapp-路由uni-simple-router安装配置教程

    uniapp-路由uni-simple-router安装配置教程

    专为uniapp打造的路由器,和uniapp深度集成,uniapp用到了很多vue的api,但在路由管理的功能相对于vue-router还是比较欠缺的,比如全局导航守卫,本文给大家讲解uniapp-路由uni-simple-router相关知识,感兴趣的朋友跟随小编一起看看吧
    2022-11-11
  • javascript实现炫酷的拖动分页

    javascript实现炫酷的拖动分页

    非常酷的javascript拖动分页功能,无缝循环分页,拖动鼠标即可完成分页,鼠标向左拖动回到前一页,向右拖动则翻开第二页,还带有动画特效,着实很不错,界面黑色,非主流风格,相信很多人会喜欢的。
    2015-05-05
  • JavaScript模拟实现键盘打字效果

    JavaScript模拟实现键盘打字效果

    这篇文章主要介绍了JavaScript模拟实现键盘打字效果,本文直接给出实例代码,需要的朋友可以参考下
    2015-06-06
  • JS实现的集合去重,交集,并集,差集功能示例

    JS实现的集合去重,交集,并集,差集功能示例

    这篇文章主要介绍了JS实现的集合去重,交集,并集,差集功能,结合实例形式分析了javascript基于数组实现的集合去重、交集、并集、差集等相关实现技巧,需要的朋友可以参考下
    2018-03-03
  • JavaScript中string转换成number介绍

    JavaScript中string转换成number介绍

    这篇文章主要介绍了JavaScript中string转换成number介绍,本文讲解了3种将string值转换成number的方法,需要的朋友可以参考下
    2014-12-12
  • js返回前一页刷新本页重载页面

    js返回前一页刷新本页重载页面

    本节主要为大家介绍了js返回前一页如何刷新本页重载页面的方法,需要的朋友可以参考下
    2014-07-07

最新评论