C高級第三次作業(1)
6-1 輸出月份英文名
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);
}
修改后的代碼
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;
}
}
3.本題調試過程碰到問題及解決辦法:
問題1:部分正確
錯誤的原因:沒有聲明string函數庫,無法調用strcpy函數。
修正方法:自己想不出來在網上搜解題思路,自己重新寫。
6-2 查找星期
1.設計思路:
(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)流程圖:
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;
}
3.本題調試過程碰到問題及解決辦法:
6-3 計算最長的字符串長度
1.設計思路:
(1)算法:
第一步:常量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)流程圖
2.實驗代碼:
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;
}
3.本題調試過程碰到問題及解決辦法:
6-4 指定位置輸出字符串
1.設計思路
(1)算法:
第一步:定義字符數組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。
(2)流程圖:
2.實驗代碼:
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);
}
修改后的代碼:
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;
}
在修改后的代碼:
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;
}
3.本題調試過程碰到問題及解決辦法:
錯誤信息1:段錯誤
錯誤原因:自己沒有讀懂題意就寫代碼,一方面自己最后只是輸出兩個字符之間的所有字符,另一方面當函數找不到時返回得值不值空指針,代碼不能滿足所有的測試點。
改正方法:在一開始就定義指針為空指針,當找到的的話就重新定義一個指針,他的值為ch1的地址,輸出在兩個字符之間的所有字符,輸出完換行並且返回指針變量,沒有找到,就換行並且返回空指針。
錯誤信息2:部分正確有一個測試點錯誤
錯誤原因:在最后的返回值返回的是空指針原來返回的是q空指針
改正方法:將返回值改成s+i
碼雲代碼存放:
C高級第三次作業(2)
6-1 奇數值結點鏈表
1.設計思路
(1)算法:
第一步:聲明結構體類型,整型變量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,將奇數和偶數的鏈表分別打印出來。
(2)流程圖:
2.實驗代碼:
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;
}
修改后的代碼:
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);
}
再次修改
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);
}
3.本題調試過程碰到問題及解決辦法:
錯誤信息1:
錯誤的原因:調試的過程中知道1在建立鏈表時沒將最后一個節點的值賦值為NULL。2.p的地址一直都是head1的起始地址,沒有將p的地址變化
修改方法在建立鏈表的函數中最后加上p->next=NULL,在getodd函數中p=head。
錯誤信息2.
錯誤原因:同樣的問題w的地址一直都是q的起始地址沒有叫它改變,
改正方法:使w=head。
6-2 學生成績鏈表處理
1.設計思路
(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).流程圖:
2.實驗代碼
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);
}
修改后的代碼
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;
}
}
3.本題調試過程碰到問題及解決辦法:
錯誤信息1
錯誤原因:只刪除一個不滿足條件的節點。
改正方法,使用for循環每找到一個不滿足條件的就執行一次刪除操作。
6-3 鏈表拼接
1.設計思路:
(1)算法:自己的思路有問題;
(2)流程圖
2.實驗代碼:
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;
}
3.本題調試過程碰到問題及解決辦法:
錯誤信息1:
錯誤原因:考慮不全面
改正方法:不會。
博客上的題目
實驗代碼:
#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
}
}
}
}
}
反思:
1.在做題之前好好讀題,要讀懂題的意思
2.不要固定自己的思維,一種方法不可以,可以換個方法。
3.當出現問題的時候,不要抱怨,自暴自棄,要多想一想
代碼存放:
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);
}