栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Lab0 C Programming Lab(CMU)(CSAPP深入理解计算机系统)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Lab0 C Programming Lab(CMU)(CSAPP深入理解计算机系统)

目录

实验下载地址

大致要求

大致操作

自动打分具体操作

代码和大致思路

queue.h中两个结构体

queue.c中的几个函数

Free queue的函数

两个insert函数:

Remove函数

Return Size函数:

Reverse函数

Auto Grade分数


实验下载地址

15-213/14-513/15-513: Introduction to Computer Systems / Schedule Fall 2021Introduction to Computer Systems; Schedulehttp://www.cs.cmu.edu/afs/cs/academic/class/15213-s22/www/schedule.html

大致要求

1.Linux命令行基础

2.C语言基础

3.数据结构基础(链表基本操作)

4.基本英语阅读能力

大致操作

下载.tar 文件,解压后对着README操作即可;

简单来说,允许直接修改的文件只有 queue.h和queue.c,打代码的时候打开这两个就够了,其他的鼓励查看学习;

按照要求修改后,保存;

在linux终端输入命令进行测试打分。

自动打分具体操作

(在所在路径下操作)

ccc@ccc-virtual-machine:~/桌面/CSAPP/Lab/Lab0 CProgramming Lab/cprogramminglab-h
andout$ make format
clang-format -style=file -i queue.c queue.h

make format:直接make可能会返回一个跟clang-format有关的报错

make:编译文件并产生自动测试文件

make test:测试正确性

这里的报错方式是valgrind,可以连带着大致学习一下

代码和大致思路

用中文写注释会报错(QAQ)

queue.h中两个结构体


typedef struct list_ele {
    
    char *value;

    
    struct list_ele *next;
} list_ele_t;


typedef struct {
    
    size_t size;
    list_ele_t *tail;
    list_ele_t *head;
    
} queue_t;

这里可以看到cmu给的注释还是比较详细的

queue.c中的几个函数

创建新queue:

queue_t *queue_new(void) {
    queue_t *q = malloc(sizeof(queue_t));
    if (q != NULL)
    
    {
        q->head = NULL;
        q->tail = NULL;
        q->size = 0;
    }
    return q;
}

Free queue的函数
void queue_free(queue_t *q) {
    
    if (q != NULL) {
        while (q->head != NULL) {
            
            // for (size_t i = 0; i < q->size; i++) {
            
            list_ele_t *temp;
            temp = q->head;
            q->head = temp->next;
   
            free(temp->value);
            

            free(temp);
        }
        
        free(q);
    }
}

两个insert函数:

拿到文件时已有的代码后面有注释:already there

bool queue_insert_head(queue_t *q, const char *s) {
    list_ele_t *newh; // already there.
    if (!q)
        return false;
    

    newh = (list_ele_t *)malloc(sizeof(list_ele_t)); // alreadythere
    if (!newh)
        return false;
    char *stemp;

    
    if (s) {
        stemp = (char *)malloc((strlen(s) + 1) * sizeof(char));
        if (!stemp) {
            free(newh);
            return false;
        }
        strcpy(stemp, s);
    } else
        stemp = NULL;
    

    newh->value = stemp;

    newh->next = q->head; // already there.
    q->head = newh;       // already there.
    if (q->tail == NULL)  //(q->size == 0)     // means q->size==0
        q->tail = q->head;
    q->size++;

    return true; // already there.
}


bool queue_insert_tail(queue_t *q, const char *s) {
    if (!q)
        return false;
    list_ele_t *newh;

    newh = (list_ele_t *)malloc(sizeof(list_ele_t));
    if (!newh) {
        return false;
    }

    char *newc;

    if (s) {
        newc = (char *)malloc(sizeof(char) * (strlen(s) + 1));
        if (!newc) {
            free(newh);
            return false;
        }
        strcpy(newc, s);
    } else
        newc = NULL;

    newh->value = newc;

    newh->next = NULL;
    q->tail->next = newh;
    q->tail = newh;
    if (q->size == 0) {
        q->head = q->tail;
    }

    q->size++;
    return true;

    
    
    // return false;//already there.
}

Remove函数
bool queue_remove_head(queue_t *q, char *buf, size_t bufsize) {
    
    if (!q)
        return false;

    if (q->size == 0) // which means q->size==0
        return false;
    list_ele_t *newqt;
    newqt = q->head;

    q->head = newqt->next;
    q->size--;

    if (q->size == 0) {
        q->tail = NULL;
        // q->head = NULL;
    }
    if (buf) {
        // buf = newqt->value;
        // if (strlen(newqt->value) > bufsize - 1) {
        //     buf[bufsize - 1] = '';
        //     newqt->value = &buf[bufsize];
        //     // free(newqt->value);
        // }
        
        strncpy(buf, newqt->value, bufsize - 1);
        buf[bufsize - 1] = ''; ///
    }

    //if (newqt->value)
        free(newqt->value);
    free(newqt);

    return true;
}

Return Size函数:
size_t queue_size(queue_t *q) {
    
    
    if (!q)
        return 0;
    return q->size;
}

Reverse函数

(不让用allocate和free)

void queue_reverse(queue_t *q) {
    list_ele_t *s;
    list_ele_t *temp;
    if (!q || q->size == 0 || q->size == 1)
        return;

    s = q->head;
    temp = s->next;

    while (temp) {
        s->next = temp->next;
        temp->next = q->head;
        q->head = temp;
        temp = s->next;
    }
    q->tail = s;
    
}

Auto Grade分数
ccc@ccc-virtual-machine:~/桌面/CSAPP/Lab/Lab0 CProgramming Lab/cprogramminglab-h
andout$ make test 
chmod +x driver.py
./driver.py
---	Trace		Points

+++ TESTING trace trace-01-ops:
./qtest -v 1 -f ./traces/trace-01-ops.cmd
# Test of insert_head and remove_head
---	trace-01-ops	6/6

+++ TESTING trace trace-02-ops:
./qtest -v 1 -f ./traces/trace-02-ops.cmd
# Test of insert_head, insert_tail, and remove_head
---	trace-02-ops	6/6

+++ TESTING trace trace-03-ops:
./qtest -v 1 -f ./traces/trace-03-ops.cmd
# Test of insert_head, insert_tail, reverse, and remove_head
---	trace-03-ops	6/6

+++ TESTING trace trace-04-ops:
./qtest -v 1 -f ./traces/trace-04-ops.cmd
# Test of insert_head, insert_tail, and size
---	trace-04-ops	6/6

+++ TESTING trace trace-05-ops:
./qtest -v 1 -f ./traces/trace-05-ops.cmd
# Test of insert_head, insert_tail, remove_head reverse, and size
---	trace-05-ops	6/6

+++ TESTING trace trace-06-string:
./qtest -v 1 -f ./traces/trace-06-string.cmd
# Test of truncated strings
---	trace-06-string	7/7

+++ TESTING trace trace-07-robust:
./qtest -v 1 -f ./traces/trace-07-robust.cmd
# Test operations on NULL queue
---	trace-07-robust	7/7

+++ TESTING trace trace-08-robust:
./qtest -v 1 -f ./traces/trace-08-robust.cmd
# Test operations on empty queue
---	trace-08-robust	7/7

+++ TESTING trace trace-09-robust:
./qtest -v 1 -f ./traces/trace-09-robust.cmd
# Test remove_head with NULL argument
---	trace-09-robust	7/7

+++ TESTING trace trace-10-malloc:
./qtest -v 1 -f ./traces/trace-10-malloc.cmd
# Test of malloc failure on new
---	trace-10-malloc	7/7

+++ TESTING trace trace-11-malloc:
./qtest -v 1 -f ./traces/trace-11-malloc.cmd
# Test of malloc failure on insert_head
---	trace-11-malloc	7/7

+++ TESTING trace trace-12-malloc:
./qtest -v 1 -f ./traces/trace-12-malloc.cmd
# Test of malloc failure on insert_tail
---	trace-12-malloc	7/7

+++ TESTING trace trace-13-perf:
./qtest -v 1 -f ./traces/trace-13-perf.cmd
# Test performance of insert_tail
---	trace-13-perf	7/7

+++ TESTING trace trace-14-perf:
./qtest -v 1 -f ./traces/trace-14-perf.cmd
# Test performance of size
---	trace-14-perf	7/7

+++ TESTING trace trace-15-perf:
./qtest -v 1 -f ./traces/trace-15-perf.cmd
# Test performance of insert_tail, size, and reverse
---	trace-15-perf	7/7
---	TOTAL		100/100

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/884091.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号