天天看點

C進階第三次作業

C進階第三次作業(1)

6-1 輸出月份英文名

C進階第三次作業
C進階第三次作業
C進階第三次作業

1.設計思路

(1)算法:

第一步:定義整型變量n,字元指針s,輸入一個數賦給n。

第二步:調用函數getmonth将值賦給s。

第三步:在函數getmonth中使用switch—case語句來實作,傳回月份的英文名。

第四步:在主函數中如果s==NULL就輸出wrong input,否則輸出s。

(2)流程圖:

2.實驗代碼:

char *getmonth( int n ){
	char *a;
	switch(n){
		case 1:strcpy(a,"January");break;
		case 2:strcpy(a,"February");break;
		case 3:strcpy(a,"March");break;
		case 4:strcpy(a,"April");break;
		case 5:strcpy(a,"May");break;
		case 6:strcpy(a,"June");break;
		case 7:strcpy(a,"July");break;
		case 8:strcpy(a,"August");break;
		case 9:strcpy(a,"September");break;
		case 10:strcpy(a,"October");break;
		case 11:strcpy(a,"November");break;
		case 12:strcpy(a,"December");break;
		default:strcpy(a,"NULL");
	}
	return(a);
}
           
C進階第三次作業

修改後的代碼

char *getmonth( int n ){
	switch(n){
		case 1:return "January";
		case 2:return "February";
		case 3:return "March";
		case 4:return "April";
		case 5:return "May";
		case 6:return "June";
		case 7:return "July";
		case 8:return "August";
		case 9:return "Septemer";
		case 10:return "October";
		case 11:return "November";
		case 12:return "December";
		default:return NULL;
	}
}
           
C進階第三次作業

3.本題調試過程碰到問題及解決辦法:

問題1:部分正确

錯誤的原因:沒有聲明string函數庫,無法調用strcpy函數。

修正方法:自己想不出來在網上搜解題思路,自己重新寫。

6-2 查找星期

C進階第三次作業
C進階第三次作業
C進階第三次作業

1.設計思路:

第一步:定義整型變量i,字元數組s[MAXS],輸入一個字元串賦給s。

第二步:調用函數getindex,n=getindex的傳回值。

第三步:在getindex函數中,定義整型變量a=-1,i=0,字元指針數組*week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},定義字元型指向指針的指針**p=week在for循環中i為循環變量,條件為i<7,如果strcmp(s,week[i])0,就執行a=i,然後跳出這個循環,執行完for循環最後傳回a。

第四步:在主函數中如果n-1,就執行wrong input!,否則就輸出n的值

2.實驗代碼

int getindex( char *s ){
	int a=-1,i;
	char *week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	char **p=week;
	for(i=0;i<7;i++){
		if(strcmp(s,*(p+i))==0){
			a=i;
			break;
		}
	}
	return a;
}
           

C進階第三次作業

6-3 計算最長的字元串長度

C進階第三次作業
C進階第三次作業
C進階第三次作業

第一步:常量MAXN 10,MAXS 20,定義整型變量i,n,字元指針數組string[MAXN] = {NULL},輸入一個數賦給n。

第二步:借助for循環i=0;條件為i<n;在for循環中動态配置設定記憶體大小為20位元組,輸入一個字元串賦給string[i]。

第三步:調用max_len函數,将函數max_len的傳回值輸出。

第四步:在函數max_len中,定義整型變量i,count=0,j,整型數組q[20]={0};借助for循環求出(*(s+i))中的字元串的長度,将值賦給q[i];

第五步:count=q[0],借助for循環找到q[i]的最大值賦給count,最後傳回count的值。

(2)流程圖

C進階第三次作業

int max_len( char *s[], int n ){
	int i,count=0,j,q[20]={0};
	for(i=0;i<n;i++){
		for(j=0;*(*(s+i)+j)!='\0';j++){
		}
		q[i]=j;
	}
	count=q[0];
	for(i=0;i<n;i++){
		if(q[i]>count){
			count=q[i];
		}
	}
	return count;
} 
           

C進階第三次作業

6-4 指定位置輸出字元串

第一步:定義字元數組str[10],字元變量ch_start, ch_end,字元型指針變量p,

第二步:獨樹一格字元串給str,讀入兩個字元分别賦給ch_start, ch_end,

第三步:調用函數match,将函數的傳回值賦給p。

第四步:在函數match中定義整型變量i,j,字元型指針q=NULL,i=0,借助for循環條件為(s+i)!='\0',在for循環中如果(s+i)==ch1,定義字元型指針a=&s[i],j=i,借助for循環條件為((s+j)!=ch2)&&((s+j)!='\0'),輸出字元(s+j),執行完第二個for循環判斷如果(s+j)!='\0',就輸出字元(s+j),并且換行,傳回a的位址,執行完第一個for循環直到條件不滿足,就換行并且傳回q的位址。

第五步:輸出字元串p。

char *match( char *s, char ch1, char ch2 ){
	int i,j;
	char *q=s;
	for(i=0;*(s+i)!='\0';i++){
		if(*(s+i)==ch1){
			for(j=i;*(s+j)!=ch2;j++){
				*(q)=*(s+j);
				q++;
			}
			*q=ch2;
		}
	}
	*q='\0';
	printf("%s\n",q);
}
           
C進階第三次作業

修改後的代碼:

char *match( char *s, char ch1, char ch2 ){
	int i,j;
	char *q=NULL;
	for(i=0;*(s+i)!='\0';i++){
		if(*(s+i)==ch1){
		char *a=  &s[i];
			for(j=i;(*(s+j)!=ch2)&&(*(s+j)!='\0');j++){
				printf("%c",*(s+j));
			}
                        if(*(s+j)!='\0')
			printf("%c",*(s+j));
			printf("\n");
			return a;
		}
	}
	printf("\n");
	return q;
}
           
C進階第三次作業

在修改後的代碼:

char *match( char *s, char ch1, char ch2 ){
	int i,j;
	char *q=NULL;
	for(i=0;*(s+i)!='\0';i++){
		if(*(s+i)==ch1){
		  char *a=  &s[i];
			for(j=i;(*(s+j)!=ch2)&&(*(s+j)!='\0');j++){
				printf("%c",*(s+j));
			}
			if(*(s+j)!='\0')
			printf("%c",*(s+j));
			printf("\n");
			return a;
		}
	}
	printf("\n");
	return s+i;
}
           
C進階第三次作業

錯誤資訊1:段錯誤

錯誤原因:自己沒有讀懂題意就寫代碼,一方面自己最後隻是輸出兩個字元之間的所有字元,另一方面當函數找不到時傳回得值不值空指針,代碼不能滿足所有的測試點。

改正方法:在一開始就定義指針為空指針,當找到的的話就重新定義一個指針,他的值為ch1的位址,輸出在兩個字元之間的所有字元,輸出完換行并且傳回指針變量,沒有找到,就換行并且傳回空指針。

錯誤資訊2:部分正确有一個測試點錯誤

錯誤原因:在最後的傳回值傳回的是空指針原來傳回的是q空指針

改正方法:将傳回值改成s+i

碼雲代碼存放:

C進階第三次作業

C進階第三次作業(2)

6-1 奇數值結點連結清單

C進階第三次作業
C進階第三次作業
C進階第三次作業
C進階第三次作業

第一步:聲明結構體類型,整型變量data,結構體指針變量next

第二步:在主函數中,定義結構體指針變量L,Odd,調用函數readlist将函數的傳回值給L,調用函數getodd,将函數的傳回值給Odd,

第三步:在readlist函數中,定義結構體指針變量head=NULL,p=NULL,q=NULL,整型數data,對p,q動态配置設定記憶體類型強制轉化為結構體指針,大小為sizeof(struct ListNode),讀入一個書給p->data,使用while語句條件為p->data!=-1,在循環語句中如果headNULL,head=p,否則q->next=p,在if語句結束之後q=p,在對p動态配置設定記憶體類型強制轉化為結構體指針,大小為sizeof(struct ListNode),讀入一個數給p->data,在執行完while語句後q->next=NULL,傳回head。

第四步:在getodd函數中,定義結構體指針變量head=NULL,q=NULL,p,w=NULL,head1=NULL,如果L!=NULL就執行for循環,for循環條件1為head=*L,條件2為head!=NULL,條件3為head=head->next,在for循環中如果head->data%2!=0執行head1=head,否則執行p->next=head,在if語句之後p=head,如果head->data%2!=0不滿足,就執行如果qNULL,q=head,否則w->next=head,在if語句之後執行w=head,執行完for循環後p->next=NULL,如果w!=NULL執行w->next=NULL。最後*L=q。在第一個if不滿足時傳回NULL,在最後傳回head1.

第五步:兩次調用函數printlist,将奇數和偶數的連結清單分别列印出來。

struct ListNode *readlist(){
	struct ListNode *head=NULL,*p=NULL,*q=NULL;
	int data;
	p=q=(struct ListNode *)malloc(sizeof(struct ListNode));
	scanf("%d",&p->data);
	while(p->data!=-1){
		if(head==NULL){
			head=p;
		}else{
			q->next=p;
		}
		q=p;
		p=(struct ListNode *)malloc(sizeof(struct ListNode));
		scanf("%d",&p->data);
	}
	return head;
}
struct ListNode *getodd( struct ListNode **L ){
	struct ListNode *head=NULL,*q=NULL,*p,*w=NULL,*head1=NULL;
		if(*L!=NULL){
			for(head=*L;head!=NULL;head=head->next){
				if(head->data%2!=0){
					if(head1==NULL){
						head1=head;p=head1;
					}else{
						e->next=p;
					}
					for(q=*L;q->data%2==0&&q->next!=NULL;q=q->next){
						w=q;
					}
					if(q->data%2!=0){
						if(q=*L){
							*L=q->next;
						}else{
							w->next=q->next;
						}
					}//
				}		
			}
			w->next=NULL;
		}else{
			return NULL;
		}
		e->next=NULL;
	return head1;
}
           
C進階第三次作業
struct ListNode *readlist(){
	struct ListNode *head=NULL,*p=NULL,*q=NULL;
	int data;
	p=q=(struct ListNode *)malloc(sizeof(struct ListNode));
	scanf("%d",&p->data);
	while(p->data!=-1){
		if(head==NULL){
			head=p;
		}else{
			q->next=p;
		}
		q=p;
		p=(struct ListNode *)malloc(sizeof(struct ListNode));
		scanf("%d",&p->data);
	}
	q->next=NULL; 
	return(head);
}
struct ListNode *getodd( struct ListNode **L ){
	struct ListNode *head=NULL,*q=NULL,*p,*w=NULL,*head1=NULL;
		if(*L!=NULL){
			for(head=*L;head!=NULL;head=head->next){
				if(head->data%2!=0){
					if(head1==NULL){
						head1=head;
					}else{
						p->next=head;
					}
					p=head;
				}else{
					if(q==NULL){
						q=head;w=q;
					}else{
						w->next=head;
					}
				}
			}
			p->next=NULL;
			if(w!=NULL)
			w->next=NULL;
			*L=q;
		}else{
			return NULL;
		}
		return(head1);
	}
           
C進階第三次作業

再次修改

struct ListNode *readlist(){
	struct ListNode *head=NULL,*p=NULL,*q=NULL;
	int data;
	p=q=(struct ListNode *)malloc(sizeof(struct ListNode));
	scanf("%d",&p->data);
	while(p->data!=-1){
		if(head==NULL){
			head=p;
		}else{
			q->next=p;
		}
		q=p;
		p=(struct ListNode *)malloc(sizeof(struct ListNode));
		scanf("%d",&p->data);
	}
	q->next=NULL; 
	return(head);
}
struct ListNode *getodd( struct ListNode **L ){
	struct ListNode *head=NULL,*q=NULL,*p,*w=NULL,*head1=NULL;
		if(*L!=NULL){
			for(head=*L;head!=NULL;head=head->next){
				if(head->data%2!=0){
					if(head1==NULL){
						head1=head;
					}else{
						p->next=head;
					}
					p=head;
				}else{
					if(q==NULL){
						q=head;
					}else{
						w->next=head;
					}
					w=head;
				}
			}
			p->next=NULL;
			if(w!=NULL)
			w->next=NULL;
			*L=q;
		}else{
			return NULL;
		}
		return(head1);
	}
           
C進階第三次作業

3.本題調試過程碰到問題及解決辦法:

錯誤資訊1:

C進階第三次作業

錯誤的原因:調試的過程中知道1在建立連結清單時沒将最後一個節點的值指派為NULL。2.p的位址一直都是head1的起始位址,沒有将p的位址變化

修改方法在建立連結清單的函數中最後加上p->next=NULL,在getodd函數中p=head。

錯誤資訊2.

C進階第三次作業

錯誤原因:同樣的問題w的位址一直都是q的起始位址沒有叫它改變,

改正方法:使w=head。

6-2 學生成績連結清單處理

C進階第三次作業
C進階第三次作業
C進階第三次作業
C進階第三次作業

(1)算法

第一步:聲明結構體類型,整型數num,字元數組name[20],整型數score結構體指針變量next,

第二步:主函數中,定義整型min_score,結構體指針變量p,head=NULL,調用函數creatlist,将creatlist的傳回值賦給head,

第三步在creatlist函數中,定義結構體指針變量head,p=NULL,q=NULL,對p和q動态配置設定記憶體類型強制轉化為結構體指針,大小為sizeof(struct stud_node),讀入一個數賦給p->num,使用while循環條件為p->num!=0,在循環體中,讀入一個字元串和整型數字給p->name和p->score,如果headNULL,head=p,否則q->next=p。if語句結束執行q=p 在對p動态配置設定記憶體類型強制轉化為結構體指針,大小為sizeof(struct stud_node),,讀入一個數賦給p->num,當while天劍不滿足時執行q->next=NULL,傳回head給主函數

第四步:在主函數中輸入一個數給min_score,調用函數deletelist,将函數的傳回值給head,

第五步:定義結構體指針變量p,q,*w,使用if語句,條件為head!=NULL,借助for循環條件1為w=head,條件2為w!=NULL條件3為w=w->next,在循環體中借助if語句條件為w->score<min_score,借助for循環條件1為p=head條件2為min_score<=p->score&&p->next!=NULL條件3為p=p->next,在循環體中執行q=p。直到結束使用if語句條件min_score>p->score,在判斷語句中使用if語句條件為phead執行head=p->next否則執行q->next=p->next,在第一個if語句不滿足時傳回NULL,最後傳回head

第六步:列印連結清單head,

(2).流程圖:

C進階第三次作業

struct stud_node *createlist(){
	struct stud_node *head=NULL,*p=NULL,*q=NULL;
	p=q=(struct stud_node *)malloc(sizeof(struct stud_node));
	int num;
	char name[20];
	int score;
	scanf("%d",&p->num);
	while(p->num!=0){
		scanf("%s %d",p->name,&p->score);
		if(head==NULL){
			head=p;
		}else{
			q->next=p;
		}
		q=p;
	p=(struct stud_node *)malloc(sizeof(struct stud_node));
	scanf("%d",&p->num);
	}
	q->next=NULL; 
	return (head);
}
struct stud_node *deletelist( struct stud_node *head, int min_score ){
	struct stud_node *p,*q,*w;
	int i;
	if(p!=NULL){
		for(w=head;w!=NULL;w=w->next){
			p=head;
			while(min_score<=p->score&&p->next!=NULL){
				q=p;p=p->next;
			}
			if(min_score>p->score){
				if(p==head){
					head=p->next;
				}else{
					q->next=p->next;
				}
			}
		}
	}
	return (head);
}
           
C進階第三次作業
struct stud_node *createlist(){
	struct stud_node *head=NULL,*p=NULL,*q=NULL;
	p=q=(struct stud_node *)malloc(sizeof(struct stud_node));
	scanf("%d",&p->num);
	while(p->num!=0){
		scanf("%s %d",p->name,&p->score);
		if(head==NULL){
			head=p;
		}else{
			q->next=p;
		}
		q=p;
	p=(struct stud_node *)malloc(sizeof(struct stud_node));
	scanf("%d",&p->num);
	}
	q->next=NULL; 
	return (head);
}
struct stud_node *deletelist( struct stud_node *head, int min_score ){
	struct stud_node *p,*q,*w; 
	if(head!=NULL){
			for(w=head;w!=NULL;w=w->next){
				if(w->score<min_score){
					for(p=head;min_score<=p->score&&p->next!=NULL;p=p->next){
						q=p;
					}
					if(min_score>p->score){
					if(p==head){
						head=p->next;
					}else{
						q->next=p->next;
					}
				}		
			}
		}
		return (head);
	}
	else{
	 return NULL;
	}
	
}
           
C進階第三次作業

錯誤資訊1

C進階第三次作業

錯誤原因:隻删除一個不滿足條件的節點。

改正方法,使用for循環每找到一個不滿足條件的就執行一次删除操作。

6-3 連結清單拼接

(1)算法:自己的思路有問題;

struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2){
	struct ListNode *head,*p,*q;
	int a[10],i=0;
	for(p=list1;p!=NULL;p=p->next){
		a[i++]=p->data;
	}
	for(p=list2;p!=NULL;p=p->next){
		a[i++]=p->data;
	}
	int j,n,k,temp;
	n=i;
	for(i=0;i<n-1;i++){
		k=i;
		for(j=i;j<n;j++){
			if(a[j]<a[k]){
				j=k;
			}
		}
		if(k!=i){
			temp=a[i];a[i]=a[k];a[k]=temp;
		}
	}
	for(i=0;i<n;i++){
		p->data=a[i];
		if(head==NULL){
			head=p;
		}else{
			q->next=p;
		}
		q=p;
	}
	q->next=NULL;
	return head;
}
           
C進階第三次作業

C進階第三次作業

錯誤原因:考慮不全面

改正方法:不會。

部落格上的題目

C進階第三次作業

實驗代碼:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long *zhishu(long *p,int a,int b);
void print(long *p,int a,int b);
int main()
{
	int a=201,b=3936,i,j;
	long n[a*b],*p;
	for(i=0;i<a;i++){
		for(j=0;j<b;j++){
			n[(i+1)*(j+1)]=(i+1)*(j+1);
		}
	}
	p=zhishu(n,a,b);
	print(p,a,b);
	return 0;
 } 
 long *zhishu(long *p,int a,int b){
 	int i,j,c=2,d=0;
 	long *q=p;
 	j=a*b;
 	for(i=0;i<j&&c<=sqrt(a*b);i++){
 		d=0;
 		if(*(q+i)!=1){
 			if(*(q+i)%c!=0){
 				q[d++]=*(q+i);
				}
			}
		j=d;
		c=q[0];
	}
	return q;
 }
 void print(long *p,int a,int b){
 	int i;
 	for(i=0;*(p+i)<=a*b;i++){
 		if((i+1)%5!=0){
 			printf("%ld ",*(p+i));
		 }else{
		 	printf("%ld\n",*(p+i));
		 }
	 }
 }
           

運作出錯,不會寫

#include <stdio.h>
#include <stdlib.h>
#define N 20
#define M 39 
void delete(int s[N][M],int x); 
int main()
{
	int i,j,a[N][M],num=1;
	for(i=0;i<N;i++){
		for(j=0;j<M;j++){
			a[i][j]=num;
			num++;
		}
	}                       //建立一個二維數組,元素從1 到N*M 
	a[0][0]=0;                //a[0][0]=1不是質數,将它指派為0 
	for(i=0;i<N;i++){
		for(j=0;j<M;j++){
			if(a[i][j]!=0){
				delete(a,a[i][j]);  //調用函數進行篩選法 ,每調用函數一次數組a,a的值都會發生變化,不是質數的元素指派為0 
			}
		}
	}
	for(i=0;i<N;i++){
		for(j=0;j<M;j++){
			if(a[i][j]!=0){
				printf("%d\t",a[i][j]);
			}
		}
	}                         //将所有的指數(數組中不為0的數)輸出 
	return 0;
 } 
 void delete(int s[N][M],int x){
 	int i,j;
 	for(i=0;i<N;i++){
 		for(j=0;j<M;j++){
 			if(i!=0||j!=0){          //跳過a[0][0], 
 				if(s[i][j]%x==0&&s[i][j]!=x){
 					s[i][j]=0;//将所有的能被想整除的數指派為0 
				 }
			 }
		 }
	 }
 }

           
C進階第三次作業

反思:

1.在做題之前好好讀題,要讀懂題的意思

2.不要固定自己的思維,一種方法不可以,可以換個方法。

3.當出現問題的時候,不要抱怨,自暴自棄,要多想一想

C進階第三次作業
C進階第三次作業

代碼存放:

git

評論我的部落格同學:

姜健https://i.cnblogs.com/EditPosts.aspx?postid=8778302

陸文奇:https://i.cnblogs.com/EditPosts.aspx?postid=8778302

黨睿:https://i.cnblogs.com/EditPosts.aspx?postid=8778302

辛靜瑤:https://i.cnblogs.com/EditPosts.aspx?postid=8778302

我所評論的同學:

姜健:http://www.cnblogs.com/jj990519/p/8763063.html

袁中:http://www.cnblogs.com/2719610441qqcom/p/8762037.html

王姝雯:http://www.cnblogs.com/phsudie/p/8759331.html

黨睿:http://www.cnblogs.com/dangrui/p/8797836.html

辛靜瑤:http://www.cnblogs.com/X-JY/p/8761685.html

知識點總結:

char *name[]={"Follow me","BASIC",......}

指針數組相當于二維數組;

name ——>二維數組的首元素的位址 =name[0]——>行指針——>第0行首元素的位址/第0個字元串的起始位址

name[1]——>行指針——>第一行首元素的位址 /第一個字元串的起始位址

*name——>列指針——>第0行第0列元素的位址/指向第0個字元串的第0個字元的位址

*(name+1)——>列指針——>第一行第0列元素的位址/指向第一個字元串的第0個字元的位址。

*(name+i)+n >第i行第n列元素的位址

((name+i)+n)>第i行第n列元素的值 例如 ((name+1)+1)=‘A’

連結清單其實就是結構遞歸定義,有一個頭指針變量,還有表尾,表尾的位址部分放一個NULL表示連結清單結束。

形式

struct student{

int num;

float score;

......

struct student *next; //最主要的

};

在連結清單中要使用動态配置設定記憶體

malloc函數,malloc函數的傳回值是一個配置設定域的起始位址

在連結清單中形式為:p=(struct student *)malloc(sizeof struct student );

對連結清單的操作:建立動态連結清單、輸出、插入、删除;

建立連結清單的函數:

struct student *creat(void){
    struct student *head=NULL,*p=NULL,*q=NULL;
    p=q=(struct student *)malloc(sizeof(struct student));
    scanf("%d",&p->num);
    while(p->num!=0){
        if(head==NULL)head=p;
            else q->next=p;
            q=p;
         p=(struct student *)malloc(sizeof(struct student));
        scanf("%d",&p->num);
    }
    q->next=NULL;
    return(head);
}
           

輸對外連結表:

void print(struct student *head){
    struct student *p;
    for(p=head;p!=NULL;p=p->next){
            printf("%d %f ...."p->num,p->score,.....);
        }
}
           

删除節點:

struct student *deleatstruct student *head,int num){
    struct student *p,*q;
    if(head!=NULL){
        for(p=head;p->next!=NULL&&p->num!=num;p=p->next){
            q=p;
        }
        if(p->num==num){
                if(p==head)head=p->next;
                    else q->next=p->next;
            }
    }else{
    printf("沒有找到");
    }
     return(head);
}
           

插入節點:

struct student *insert(struct student *head,struct student *stud){
    struct student *p,*q;
    if(head!=NULL){
        for(p=head;p->num!=stud->num&&p->next!=NULL;p=p->head){
            q=p;
        }
        if(p->num==stud->num){
            if(p==head){head=stud;stud->next=p;}
            else{q->next=stud;stud->next=p;}
        }else{p->next=stud;stud->next=NULL}
    }  
return(head);  
}