2018第三次作业

一.代码:

1.输出月份英文名:

1).设计思路:

(1).文字描述:

第一步:主函数:先输入了月份的数字n,调用函数求出所对应的月份并赋给s,判断s是否为null,如果是输出wrong input,如果不是输出月份;
第二步:在函数中,运用一个switch语句在每个语句后面返回相应的月份;

2).实验代码

` 
#include <stdio.h>

char *getmonth( int n );

int main()
{
    int n;
    char *s;

    scanf("%d", &n);
    s = getmonth(n);
    if ( s==NULL ) printf("wrong input!\n");
    else printf("%s\n", s);

    return 0;
}

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 "September";
        case 10:return "October";
        case 11:return "November";
        case 12:return "December";
        default:return NULL;
    }
    
 } 
`

3).遇到的问题:

未遇到问题。

2.查找星期:

1).设计思路:

(1).文字描述:

第一步:主函数:首先输入一个星期的字符s,调用函数求出相应数组并赋值给n,判断s是否为-1,如果是输出wrong input,如果不是输出星期所对应的数字;
第二步:将各个星期存入day数组中,用一个for循环遍历day数组,用一个if语句判断所需查找的是不是所需字符,如果是跳出循环。在for循环后用if语句判断i是否等于7,如果是令i等于-1.最后返回i的值;

2).实验代码

`
#include <stdio.h>
#include <string.h>

#define MAXS 80

int getindex( char *s );

int main()
{
    int n;
    char s[MAXS];

    scanf("%s", s);
    n = getindex(s);
    if ( n==-1 ) printf("wrong input!\n");
    else printf("%d\n", n);

    return 0;
}

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

3).遇到的问题:

未遇到问题。

3.计算最长的字符串长度:

1).设计思路:

(1).文字描述:

第一步:主函数:输入字符串个数,用for循环进行输入,最后调用函数求出最长字符串的长度并输出。
第二步:用一个for语句对字符串进行循环,用if语句判断最长长度是否小于当前所遍历字符串长度如果小于将当前长度赋值给最长长度。for语句结束后返回最长长度的值。

2).实验代码

`
#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;
}

int max_len( char *s[], int n )
{
    int i,num = 0;
    for(i = 0;i < n;i++){
        if(num < strlen(*(s + i))){
            num = strlen(*(s + i));
        }
    }
    return num;
} 
`

3).遇到的问题:

未遇到问题。

4,指定位置输出字符串:

1).设计思路:

第一步:先用一个for语句遍历字符串,并用if语句判断当前字符与所需截取的字符是否相等,如果相等记录下当前位置的下标并跳出循环;
第二步:判断当前是不是存在第一个字符并进行输出;
第三步:用for语句进行遍历其中用if语句判断当前所遍历字符是否和最后一个字符相等如果相等输出当前字符,如果相等则输出当前字符并返回主函数,再用一个if语句判断是否为最后一项如果是返回主函数。

(1).文字描述:

(2).流程图:

2).实验代码

` char *match( char *s, char ch1, char ch2 ) {
int i,j,start=-1,end;
char *p=NULL; 
int len = strlen(s); 
for(i = 0;i < len;i ++){
    if(s[i] == ch1){
        start = i;
        p=&s[i];
        break;
    }
}

if(start == -1){
    p=&s[i];
    printf("\n");
    return p;
}

for(j = start;j < len;j ++){
    if(s[j]!=ch2){  
          printf("%c", s[j]);  
              }  
      if(s[j]==ch2){  
          printf("%c\n", s[j]);  
          return p;  
              }
      if(j == len -1){
        printf("\n");
        return p;
    }
}

} `

3).遇到的问题:

在第二点测试(有第二个没有第一个)一直在错。
是因为在判断有没有第一个数的时候,在没有第一个数的前提下没有加上“p=&s[i];”这句;

5,一道编程题:

1).设计思路:

(1).文字描述:

2).实验代码

`
#include <stdio.h>

int main ()
{
int m=20,n=970;
   int *p = (int *)malloc((m*n) *sizeof(int));
int *q = (int *)malloc((m*n) *sizeof(int)); 
int i=0,j;
for(i=0;i<(m*n);i++) {
    p[i] = i+1;
}
for(i=0;i<(m*n);i++) {
    for(j = i+1;j<=(m*n);j++) {
        if(p[i] !=1&&p[j] != 1) {
            if(p[j]%p[i] ==0) {
                p[j] = 1;
            }
        }
    }
}
j=0;
for(i=0;i<(m*n);i++) {
    if(p[i] != 1) {
        printf(" %d",p[i]);
        j++;
    } 
    if(j == 5) {
        printf("\n");
        j=0;
    }
}
}
`

3).遇到的问题:

未遇到问题。

6,奇数值结点链表:

1).设计思路:

(1).文字描述:

第一步:先输入第一个data,如果data不等于-1进入while循环,在当前链表元素中使data存入,为下一个申请一下空地址,并用if判断头文件是否为空,此时将p赋给head如果不是的话令tail的下一个值等于p;
第二步:用一个while语句遍历链表,用一个if语句判断当前值是否为单数如果是将p赋给n,如果不是将p赋给m。最后返回相应的值。

2).实验代码

struct ListNode *readlist()
  {
    int data;
    struct ListNode *head=NULL,*p=NULL,*tail=NULL;
    scanf("%d",&data);
    while(data != -1){
        p = (struct ListNode *)malloc(sizeof(struct ListNode));
        p->data = data;
        p->next = NULL;
        if(head == NULL){
            head = p;
            tail = p;
        }else{
            tail->next = p;
            tail = p;
        }
        scanf("%d",&data);
    }
    return head; 
    
      } 
      struct ListNode *getodd( struct ListNode **L )
      {
     struct ListNode *p = *L,*m=NULL,*n=NULL,*head1=NULL,*head2=NULL;
     head1=(struct ListNode*)malloc(sizeof(struct ListNode));
     head2=(struct ListNode*)malloc(sizeof(struct ListNode));
    head1->next=NULL;
    head2->next=NULL;
    m=head1;
    n=head2;
       while (p) {
             if((p->data)%2 == 1){
                    n->next=p;
                n=p;
             }else{
                m->next=p;
                m=p;
                 }
             p = p->next;
       }
     m->next=NULL;
    n->next=NULL;
    *L = head1->next;   
    return head2->next;
      } 

3).遇到的问题:

最开始代码都不能运行。
借鉴了同学的代码发现自己是没有为下一个申请地址。

7,学生成绩链表处理:

1).设计思路:

(1).文字描述:

第一步:按照上一题的方式输入所需数据,不过由于最后只需要输入0所以要将其分开输入。
第二步:用一个for循环遍历链表数据,用一个if语句判断其是否大于等于最小值如果是存入链表中,最后返回链表的值。

2).实验代码

`#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct stud_node {
     int    num;
     char   name[20];
     int    score;
     struct stud_node *next;
};
struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );
int main()
{
    int min_score;
    struct stud_node *p, *head = NULL;

    head = createlist();
    scanf("%d", &min_score);
    head = deletelist(head, min_score);
    for ( p = head; p != NULL; p = p->next )
        printf("%d %s %d\n", p->num, p->name, p->score);

    return 0;
}
struct stud_node *createlist()
{
    struct stud_node *tail=NULL,*head=NULL,*p=NULL;
    int num=0,score=0;
    char name[20];
    scanf("%d",&num);
    while(num!=0)
    {
        p=(struct stud_node*)malloc(sizeof(struct stud_node));
        p->num=num; 
        scanf("%s %d",p->name,&p->score);
        if(head==NULL)
        {
            head=p;
        }else
        {
            tail->next=p;
        }
        tail=p;
        scanf("%d",&num);
        p->next=NULL;
    }
    return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
struct stud_node *ptr1=NULL,*ptr2=NULL;
for(;head!=NULL;head=head->next)
{
    if(head->score>=min_score)
    {
        if(ptr1==NULL)
        {
            ptr1=head;
        }else
        {
            ptr2->next=head;
        }
        ptr2=head;
    }
}
if(ptr1==NULL)
{
    return NULL;
}else
{
    ptr2->next=NULL;
}
return head;
}
  `

3).遇到的问题:

在输入的代码中我将三个数据用一个scanf函数输入,发现会出现问题。
之后改用先输入数,再输入其它两个数据问题便解决了。

8,链表拼接:

1).设计思路:

(1).文字描述:

第一步:用一个while将链表中的数据存入temp中;
第二步:用选择排序法对数组进行排序;
第三步:用for语句进行遍历,先将数组的值赋给data,再将其赋值给链表,最后返回链表。

2).实验代码

`
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2)
{
    int num = 0;
    int temp[100];
    struct ListNode  *p = list1;
    while(p != NULL)
    {
        temp[num] = p->data;
        num++;
        p = p->next;
    }
    p = list2;
    while(p != NULL)
    {
        temp[num] = p->data;
        num++;
        p = p->next;
    }
    int i,j;
    for(i = 0; i < num; i++)
        for(j = i + 1; j < num; j++)
        {
            if(temp[i] > temp[j])
            {
                int t;
                t = temp[i];
                temp[i] = temp[j];
                temp[j] = t;
            }
        }
      struct ListNode  *newlist = NULL;
      struct ListNode  *endlist = NULL;
      struct ListNode  *q;
      for(i = 0; i < num; i++)
      {
          q = (struct ListNode  *)malloc(sizeof(struct ListNode));
          q->data = temp[i];
          if(newlist == NULL)
          {
              newlist = q;
              newlist->next = NULL;
          }
            if(endlist != NULL)
         {
            endlist->next = q;
         }
         endlist = q;
         endlist->next = NULL;
      }
      return newlist;
}

`

3).遇到的问题:

未遇到问题。

二,学习总结和进度:

1.近期所学知识点与问题:

1).如何理解指针数组,它与指针、数组有何关系?为何可以用二级指针对指针数组进行操作?

指针数组就是将指针与数组的结合体,可以让指针更加便捷。
首先学了指向函数的指针(*函数指针名)(参数表)类型名指定函数返回值的类型,变量名是指向函数的指针变量的名称。之后,学到了链表,主要学了链表的建立,链表的遍历,插入结点,删除结点。

2.Git:

Git地址;

3.学习进度:

1).表格:

2).折线图:

4.作业互评:

1,点评同学作业:

2,邀请同学点评作业:

猜你喜欢

转载自www.cnblogs.com/DavidPark/p/8778993.html