这个作业属于那个课程 | C语言程序设计II |
---|---|
这个作业要求在哪里 | |
我在这个课程的目标是 | 了解指针与函数的关系,掌握指针作为函数返回值 |
这个作业在哪个具体方面帮助我实现目标 | 锻炼了我的编程能力 |
参考文献 | C语言程序设计II第十一章 |
6-1 计算最长的字符串长度 (15 分)
本题要求实现一个函数,用于计算有n个元素的指针数组s中最长的字符串的长度。
函数接口定义: int max_len( char *s[], int n ); 其中n个字符串存储在s[]中,函数max_len应返回其中最长字符串的长度。 裁判测试程序样例:#include#include #include #define MAXN 10#define MAXS 20int max_len( char *s[], int n );int main(){ int i, n; char *string[MAXN] = {NULL}; scanf("%d", &n); for(i = 0; i < n; i++) { string[i] = (char *)malloc(sizeof(char)*MAXS); scanf("%s", string[i]); } printf("%d\n", max_len(string, n)); return 0;}
/* 你的代码将被嵌在这里 */
输入样例: 4 blue yellow red green 输出样例: 6实验代码
int max_len( char *s[], int n ){ int max1=0,i; for(i=0;imax1) //还可以用三元运算符( ?:),如后: max1=strlen(s[i])>max1?strlen(s[i]):max1; max1=strlen(s[i]); } return max1;}
实验思路
实验截图
6-2 统计专业人数 (15 分)
本题要求实现一个函数,统计学生学号链表中专业为计算机的学生人数。链表结点定义如下:
struct ListNode { char code[8]; struct ListNode next; }; 这里学生的学号共7位数字,其中第2、3位是专业编号。计算机专业的编号为02。 函数接口定义: int countcs( struct ListNode head ); 其中head是用户传入的学生学号链表的头指针;函数countcs统计并返回head链表中专业为计算机的学生人数。 裁判测试程序样例:#include#include #include struct ListNode { char code[8]; struct ListNode *next;};struct ListNode *createlist(); /*裁判实现,细节不表*/int countcs( struct ListNode *head );int main(){ struct ListNode *head; head = createlist(); printf("%d\n", countcs(head)); return 0;}
/* 你的代码将被嵌在这里 */
输入样例: 1021202 2022310 8102134 1030912 3110203 4021205输出样例:
3实验代码
int countcs( struct ListNode *head ){ int sum=0; while(head!=NULL){ if(head->code[1]=='0'&&head->code[2]=='2') sum++; head=head->next; } return sum;}
实验思路
实验截图
6-3 删除单链表偶数节点 (20 分)
本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下:
struct ListNode { int data; struct ListNode next; }; 函数接口定义: struct ListNode createlist(); struct ListNode deleteeven( struct ListNode head ); 函数createlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。 函数deleteeven将单链表head中偶数值的结点删除,返回结果链表的头指针。 裁判测试程序样例:#include#include struct ListNode { int data; struct ListNode *next;};struct ListNode *createlist();struct ListNode *deleteeven( struct ListNode *head );void printlist( struct ListNode *head ){ struct ListNode *p = head; while (p) { printf("%d ", p->data); p = p->next; } printf("\n");}int main(){ struct ListNode *head; head = createlist(); head = deleteeven(head); printlist(head); return 0;}
/* 你的代码将被嵌在这里 */
输入样例: 1 2 2 3 4 5 6 7 -1 输出样例: 1 3 5 7实验代码
struct ListNode *deleteeven( struct ListNode *head ){ struct ListNode *p,*q,*s; while(head!=NULL){ //判断头指针位置,即第一个奇数结点 if(head->data%2!=0) break; s=head; //记录偶数结点 head=head->next; free(s); //指向下一个结点后,释放偶数结点内存 } p=head; //p是新链表的结点 ,即全为奇数结点 if(head==NULL) //为空指针或全为偶数结点,返回head的值到主函数 return head; q=p->next; while(q!=NULL){ //判断头指针后的结点 if(q->data%2==0){ //偶数结点,s记录 ,q指向下一个结点后释放内存 s=q; q=q->next; free(s); } else{ //奇数结点,赋值p的下一个结点为奇数结点,然后p的地址改变为这个新的奇数结点 p->next=q; p=p->next; q=q->next; } if(q==NULL) //q已经是最后一个结点,p没有下一个奇数结点可以指向,赋值为NULL p->next=NULL; } return head;}
实验思路
实验截图
学习进度条
周/日期 | 这周所花的时间 | 代码行 | 学习的知识点 | 目前比较迷惑的问题 |
---|---|---|---|---|
3/2-3/8 | 两天 | 30 | 文件的处理 | 文件指针的用法 |
3/9-3/15 | 三天 | 45 | 数组的使用 | 数组的下标 |
3/16-3/22 | 两天 | 110 | 1.二维数组与矩阵 2.选择排序法 | 不是很懂二分查找法 |
3/23-3/29 | 两天 | 78 | 判断回文、一维字符数组的用法、使用字符串编程 | 使用字符串编程 |
****3/30-4/5**** | 三天 | 150 | 1、指针的含义,变量、地址、指针变量等间的关系;2、指针变量的初始化,运用指针做一些简单运算;3、指针与数组之间的关系 | 在用指针处理字符串时,应怎样定义指针变量 |
4/6-4/12 | 两天 | 200 | 学到了scanf的自定输入 | 暂无 |
4/13-4/19 | 两天 | 180 | 字符串和字符指针 | 字符数组 |
4/20-4/26 | 一天 | 100 | 定义结构,能够使用结构变量与结构数组进行熟练编程,掌握结构指针的操作 | 命令行参数怎么使用 |
4/27-5/2 | 十小时 | 0 | 1、如何有效地记忆与学习2、如何提问 | 能够使用递归函数进行编程 |
5/3-5/10 | 一天 | 22 | 递归思想 | 命令行参数怎么定义和使用 |
5/11-5/17 | 9小时 | 200 | 链表的基本使用方法 | 怎么做出游戏 |