2018 C language programming (advanced) homework - the third homework

6-1 Output the English name of the month

Design ideas

1. Algorithm

Step 1: Look at the function, look at the function declaration
Step 2: Understand the analysis

2. The flow chart is as follows

code show as below

...
char a[12][15]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char
getmonth( int n )
{
int i;
for(i=1;i<=12;i++)
{
if(i==n)
{
return a[i-1];
}
}
return NULL;

}
...

mistake

without

6-2 Find the day of the week

Design ideas:

1. Algorithm

2. Flowchart

code

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

mistake

without

6-3 Calculate the longest string length

Design ideas

algorithm

2. Flowchart

experimental code

...
int max_len( char *s[], int n )
{
int i,max=0,t=0;
max=strlen(s[0]);
for(i=1;i<n;i++)
{
t=strlen(s[i]);
if(max<t)
max=t;
}
return max;
}
...

mistake

without

Student grade list processing

Design ideas

1. Algorithm

2. Flowchart

experimental code

...

include

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

mistake

without

odd-valued node list

Design ideas

1. Algorithm

2. Flowchart

experimental code

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

mistake

without

Summarize

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324676530&siteId=291194637
Recommended