PTA8

这个作业属于哪个课程 C语言程序设计2
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/software-engineering-class2-2018/homework/3074
我的课程目标 运用指针和字符函
这个作业在哪个具体方面帮助我实现目标 运用指针和字符函数解决问题
参考文献 C语言程序设计 P200-210

第一题

6-1 函数实现字符串逆序

代码

void f( char *p )
{
    int i,q,h,t;
    q=0;
    while(p[i]!='\0')
        i++;
        h=i-1;
    while(q<=h)
    {
        t=p[q];
        p[q]=p[h];
        p[h]=t;
        q++;
        h--;
    }
    return 0;
}

思路

问题

第二题

6-3 字符串的连接 (15 分)

代码

char *str_cat( char *s, char *t )
{
     strcat(s,t);
     return s;
}

思路


第三题

7-1 输出学生成绩 (20 分)

代码

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,i,*p;
    double sum=0,max,min;
      scanf("%d",&n);
    if((p=(int*)malloc(sizeof(int)))==NULL){
        printf("Not able to allocate memory.\n");
        exit(1);
    }
    for(i=0;i<n;i++)
        scanf("%d",p+i);
    max=*p; 
    min=*p;
    for(i=0;i<n;i++){

        if(max<*(p+i))
            max=*(p+i);
        if(min>*(p+i))
            min=*(p+i);
        sum+=*(p+i);
    }
    printf("average = %.2f\n",sum/n);
    printf("max = %.2f\n",max); 
    printf("min = %.2f\n",min);
    
    free(p);
    return 0;

 } 

思路



问题

我原来想直接用静态写的,但怎么都不对;
讨论了一下法先用动态就对了;

第四题

7-4 字符串排序

代码

#include<stdio.h>
#include<string.h>
int main(){
    int i,j;
    char s[5][666],a[666];
    for(i=0;i<5;i++)
        scanf("%s",s[i]);
    for(i=0;i<4;i++)
        for(j=0;j<4-i;j++)
            if(strcmp(s[j],s[j+1])>0){
                strcpy(a,s[j]);
                strcpy(s[j],s[j+1]);
                strcpy(s[j+1],a);
            }
    printf("After sorted:\n");
    for(i=0;i<5;i++)
        printf("%s\n",s[i]);
    return 0;
}

思路



问题

无;
开始是开辟的空间小了;

第五题

7-3 计算平均成绩 (15 分)

代码

#include<stdio.h>
#include<string.h>
struct student
{
    char id[6];    
    char name[11]; 
    float score;
};
int main()
{
    struct student a[10]; 
    int i, N;
    float sum = 0, average;
    scanf("%d\n", &N);
    for (i = 0; i < N; i++) 
    {
        scanf("%s%s%f", &a[i].id, &a[i].name, &a[i].score);
        sum += a[i].score;
    }
    average = sum / N;
    printf("%.2f\n", average);
    for (i = 0; i<N; i++) 
    {
        if (a[i].score < average)
            printf("%s %s\n", a[i].name, a[i].id);
    }

    return 0;
}

思路

问题

不会用动态写此题
|周/日期 |这周所花时间 |代码行数 |学到知识点 |目前比较迷惑的问题|
| -------- | :-----: | :----: |:-----:|:-----:|
|4/15-4/19| 2day |92 |指针和字符函数 |动态和静态的运用|

5学习感悟

这次的作业不是很难,通过看书和百度能学到更多的东西

六、结对编程感想

两个臭皮匠赛过诸葛亮

猜你喜欢

转载自www.cnblogs.com/huangxing123/p/10737993.html
PTA