| 這個作業屬于那個課程 | C語言程序設計II |
|---|---|
| 這個作業要求在哪里 | https://edu.cnblogs.com/campus/zswxy/software-engineering-class1-2018/homework/3235 |
| 我在這個課程的目標是 | <了解指針與函數的關系> |
| 這個作業在那個具體方面幫助我實現目標 | <使我更加了解了指針> |
| 參考文獻 | <C語言程序設計> |
6-1 計算最長的字符串長度 (15 分)
本題要求實現一個函數,用于計算有n個元素的指針數組s中最長的字符串的長度。
函數接口定義:
int max_len( char *s[], int n );
其中n個字符串存儲在s[]中,函數max_len應返回其中最長字符串的長度。
裁判測試程序樣例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 10
#define MAXS 20
int 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
1.我的代碼
int max_len( char *s[], int n )
{
int m=0;
for(int i=0;i<n;i++)
{
int t=strlen(s[i]);
if(m < t)
{
m=t;
}
}
return m;
}
- 實驗結果截圖

3.流程圖

6-2 統計專業人數 (15 分)
本題要求實現一個函數,統計學生學號鏈表中專業為計算機的學生人數。鏈表結點定義如下:
struct ListNode {
char code[8];
struct ListNode *next;
};
這里學生的學號共7位數字,其中第2、3位是專業編號。計算機專業的編號為02。
函數接口定義:
int countcs( struct ListNode *head );
其中head是用戶傳入的學生學號鏈表的頭指針;函數countcs統計并返回head鏈表中專業為計算機的學生人數。
裁判測試程序樣例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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
作者: 張泳
單位: 浙江大學城市學院
時間限制: 400 ms
內存限制: 64 MB
1.我的代碼
int countcs( struct ListNode *head )
{
int num = 0;
struct ListNode *p = head;
while(p != NULL)
{
if(p->code[1] == '0' && p->code[2] == '2')
num++;
p = p->next;
}
return num;
}
2.實驗結果截圖

3.流程圖

6-3 刪除單鏈表偶數節點 (20 分)
本題要求實現兩個函數,分別將讀入的數據存儲為單鏈表、將鏈表中偶數值的結點刪除。鏈表結點定義如下:
struct ListNode {
int data;
struct ListNode *next;
};
函數接口定義:
struct ListNode *createlist();
struct ListNode *deleteeven( struct ListNode *head );
函數createlist從標準輸入讀入一系列正整數,按照讀入順序建立單鏈表。當讀到?1時表示輸入結束,函數應返回指向單鏈表頭結點的指針。
函數deleteeven將單鏈表head中偶數值的結點刪除,返回結果鏈表的頭指針。
裁判測試程序樣例:
#include <stdio.h>
#include <stdlib.h>
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 *createlist()
{
int digit;
struct ListNode *head=NULL,*ptr=NULL,*ptr1=NULL;
scanf("%d",&digit);
while(digit!=-1)
{
ptr1=(struct ListNode *)malloc(sizeof(struct ListNode));
ptr1->data=digit;
ptr1->next=NULL;
if(head==NULL) head=ptr1;
else ptr->next=ptr1;
ptr=ptr1;
scanf("%d",&digit);
}
return head;
}
struct ListNode *deleteeven( struct ListNode *head )
{
struct ListNode *ptr1=NULL,*ptr2=NULL;
for(ptr1=head;ptr1!=NULL;ptr1=ptr1->next){
if(ptr1->data%2==0){
if(ptr1==head){
head=head->next;
free(ptr1);
}
else {
ptr2->next=ptr1->next;
free(ptr1);
}
}
else ptr2=ptr1;
}
return head;
}
運行結果截圖

實驗思路
本題沒有什么思路,只知道定義變量后要對ptr1進行奇偶的判斷與→循環,參考的西林哥的
預習作業
1.所在小組想要開發的項目的名稱和目標
還在策劃中,應該會是一個小游戲
2.項目主體功能的描述
策劃中,要選優處理
3.現階段已做的準備工作
在網上查找相關資料
4.小組成員名單和進度安排
昆山龍哥 岳陽吳彥祖 涉外蔡徐坤
預計在下星期天完成
本周學習進度條

浙公網安備 33010602011771號