第三次作业

一.PTA

第三次作业(1)

1.输出月份英文名

(1).设计思路

1).主要描述题目算法

第一步:定义字符数组
第二步:通过遍历找到数字对应的月份,并用if语句限制条件

2).流程图

(2).实验代码


#include <stdio.h>

char *getmonth( int n )
{
int i;
char *getmonth[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};

for(i=0;i<13;i++)
{
if(i==0)
{

continue;
}
if(n==i)
{
return *(getmonth+i-1);
}
}
if(i>=13)
{
return NULL;
}
}

(3).遇到的问题及解决方法

开始在n=i时的返回值出现了多次错误,多次试验后修改正确。

2.查找星期

(1).设计思路

主要描述题目算法

第一步:定义字符数组
第二步:通过遍历找到星期对应数字后返回对应的循环变量

(2).实验代码


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

define MAXS 80

int getindex( char *s )
{
int i;
char *day[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

for(i=0;i<7;i++)
{
if(strcmp(s,day[i])==0)
{
return i;
}
}
return -1;
}

(3).遇到的问题及解决方法

本题没有遇到问题

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

(1).设计思路

主要描述题目算法

第一步:赋给最大值,字符串长度,循环变量初值
第二步:用for循环遍历字符串数组,用if判断条件,找出最长字符串长度

(2).实验代码


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

define MAXN 10

define MAXS 20

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

(3).遇到的问题及解决方法

return max误写成了return 0,发现后改正过来

4.指定位置输出字符串

(1).设计思路

主要描述题目算法

第一步:定义指针,通过遍历查找,将与第一个传入的字符相同的数组赋给指针,再进行下一次遍历,找出与第二个字符相同的数组并输出
第二步:用if(s[i]=='\0')判断只有第二种字符而无第一种字符的情况

(2).实验代码


#include <stdio.h>

define MAXS 10

char *match( char s, char ch1, char ch2 )
{
int i=0,j=0;
char a=NULL;
for(i=0;
(s+i)!='\0';i++)
{
if(s[i]==ch1)
{
a=&s[i];
for(j=i;
(s+j)!='\0';j++)
{
if(s[j]!=ch2)
{
printf("%c",s[j]);
}
if(s[j]ch2)
{
printf("%c\n",s[j]);
return a;
}
}
printf("\n");
return a;
}
}
if(s[i]
'\0')
a=&s[i];
printf("\n");
return a;

}

(3).遇到的问题及解决方法

忘记特殊情况导致答案不完整,限制条件后答案正确

第三次作业(2)

5.奇数值结点链表

(1).设计思路

主要描述题目算法

第一步:输入链表,定义结构体变量,用whlie语句进行单向赋值
第二步:通过两个链表分离奇数偶数

(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).遇到的问题及解决方法

链表的所有问题都不能很好的理解,都是通过同学的帮助完成的

6.学生成绩链表处理

(1).设计思路

主要描述题目算法

第一步:定义结构体变量,进行赋值
第二步:定义第二个结构体变量,用遍历查找出所需元素传到变量中

(2).实验代码


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(headNULL)
{
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 ptr1;
}

(3).遇到的问题及解决方法

和上题一样,并不是独立完成,经过同学的帮助后完成

7.链表拼接

(1).设计思路

主要描述题目算法

第一步:定义数组,将链表数据储存在数组中,用数组排序
第二步:用for语句把数组的值赋给链表

(2).实验代码


struct ListNode *mergelists(struct ListNode list1, struct ListNode list2)
{
int list[100],i=0,j=0,swap=0,count=0;
while(list1!=NULL)
{
list[i]=list1->data;
i++;
list1=list1->next;
count++;
}
while(list2!=NULL)
{
list[i]=list2->data;
i++;
list2=list2->next;
count++;
}
for(i=0;i<count;i++)
{
for(j=i+1;j<count;j++)
{
if(list[i]>list[j])
{
swap=list[i];list[i]=list[j];list[j]=swap;
}
}
}
struct ListNode p=NULL,head=NULL,
tail=NULL;
for(i=0;i<count;i++)
{
p=(struct ListNode
)malloc(sizeof(struct ListNode));
p->data=list[i];
p->next=NULL;
if(head==NULL)
{
head=p;
}else
{
tail->next=p;
}
tail=p;
}
return head;
}

(3).遇到的问题及解决方法

依旧是通过同学帮助写出的

8.编程题

(1).设计思路

主要描述题目算法

第一步:定义两个变量,进行动态分配
第二步:定义一个数组,进行赋值
第三步:判断出要找的值并输出

(2).实验代码


#include<stdio.h>
int main()
{
int m=0,n=0,i=0,j=0,flag=0;
scanf("%d %d",&m,&n);
flag=m*n;
int *p = (int )malloc((mn) *sizeof(int));
int *q = (int )malloc((mn) *sizeof(int));
for(i=0;i<flag;i++)
{
p[i] = i+1;
}
for(i=0;i<flag;i++) {
for(j = i+1;j<=flag;j++) {
if(p[i] !=1&&p[j] != 1) {
if(p[j]%p[i] ==0) {
p[j] = 1;
}
}
}
}
j=0;
for(i=0;i<flag;i++) {
if(p[i] != 1) {
printf(" %d",p[i]);
j++;
}
if(j == 5) {
printf("\n");
j=0;
}
}
}

(3).遇到的问题及解决方法

动态分配开始不太理解,查找资料后了解了一些

二:学习总结和进度

1.总结

(1).指针数组里的元素都为指针,二级指针与指针数组指向的地址相同。

(2).


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

(3).节省空间。不可以,指针指向地址不确定。

2.git地址:https://coding.net/u/q807443119/p/zuoye/git/tree/master/?public=true

3.点评

4.表格和折线图

posted @ 2018-04-22 21:57  顾家玮  阅读(208)  评论(3)    收藏  举报