Lab report (June 12, 2019)

c program test report

Name: Huang dry Experiment Location: 514 Classroom teaching building experimental time: April 30
Experiment items:
   1, the pointer and the pointer arithmetic base
   2, the data exchange
   3, Triangle specified number of output lines
   4, the parity array elements arrayed

1, experimental purposes and requirements

1. pointer and base pointer arithmetic

● This study was designed and love the students pointer data type of understanding, familiar with the definition of a pointer that indirect access via variable

2. Data Exchange

● When this experiment is intended to enhance understanding of student pointer passed as a parameter type, the typically argument to parameter, there are two ways, i.e. by value and passed by the address, wherein the parameter that is a pointer type by address.
Experimental requirements:
● define two functions, namely void swap1 (int a, int b ) and swawp2 void (int a, int b), for exchanging a, b values.
● two integer variables input from the main function, respectively a, b
after the above-described two calls ● switching functions separately from the main function, and print out the result of the exchange a, b of

3. Flip string and connecting the string

● 本实验旨在加强学生对字符指针以及将指针作为函数的返回类型的理解,并通过指针对字符串进行操作,通常来说,一个字符串在内存中是连续存放的,起开始地址为指向该字符串的指针值,字符串均以'\0'作为结束字符
实验要求
●定义两个字符指针,通过gets()函数输入两个字符串
●定义一个函数charreverse(char str),通过指针移动方式将字符串反转
●定义一个函数charlink(charstr1,char *str2),通过指针移动方式将两个字符串连接起来。
●从主函数中分别调用上述函数,输入字符串并打印输出结果。

4.数组元素奇偶排列

●本实验旨在加强学生对使用指针对数组进行操作的理解,通常数组的名称及整个数组的起始存储地址,可以定义一个指针指向它,然后通过指针移动来访问各个数组成员。
实验要求
●定义一个整型一堆数组,任意输入数组的元素,其中包含奇数和偶数
●定义一个函数,实现将数组元素奇数存在左,偶数在右的排列。
●在上述定义的函数中,不允许再增加新的数组
●从主函数中分别调用上述含税,打印输出结果

二、实验内容

1.实验练习:8.3.1

问题描述:

(1)定义一个整型指针变量p,使它指向一个整形变量a,定义一个浮点型指针q,使它指向一个浮点型变量b,同时定义另外一个整型变量c并赋初值3
(2)使用指针变量,调用scanf函数分别输入a和b的值。
(3)通过指针间接访问并输出a、b的值。
(4)按十六进制方式输出p、q的值以及a、b的地址
(5)将p指向c,通过p间接访问c的值并输出
(6)输出p的值及c的地址,并与上面的结果进行比较

流程图

实验代码

#include<stdio.h>
int main()
{
    int *p,a,c=3;
    float *q,b;
    p=&a;
    q=&b;
    printf("Please input the value of a,b:");
    scanf("%d,%f",p,q);
    printf("Result:\n");
    printf("%d,%f\n",a,b);
    printf("%d,%f\n",*p,*q);
    printf("The Addreses of a,b:%p,%p\n",&a,&b);
    printf("The Addreses of a,b:%p,%p\n",p,q);
    p=&c;
    printf("c=%d\n",*p);
    printf("The adress of c:%x,%x\n",&c,p);
    return 0; 
}

运行结果

问题:

2.实验练习8.3.2

流程图

实验代码:

#include<stdio.h>
void swap1(int x,int y);
void swap2(int *x,int *y);
int main()
{
    int a,b;
    printf("Please input a:");
    scanf("%d",&a);
    printf("Please inpur b:");
    scanf("%d",&b);
    swap1(a,b);
    printf("\nAfter call swap1:a=%d  b=%d\n",a,b);
    swap2(&a,&b);
    printf("\nAfter Call swap2:a=%d  b=%d\n",a,b);
    return 0;
}

void swap1(int x,int y)
{
    int temp;
    temp=x;
    x=y;
    y=temp;
}

void swap2(int *x,int *y)
{
    int temp;
    temp=*x;
    *x=*y;
    *y=temp;
}

运行结果

问题:

3.实验练习:8.3.3

问题描述:

编写程序,从键盘输入行数,输出指定行数的杨辉三角形。

流程图


实验代码

#include<stdio.h>
char *reverse(char *str);
char *link(char *str1,char *str2);
int main()
{
    char str[30],str1[30],*str2;
    printf("Input Reversing Character String:");
    gets(str);
    str2=reverse(str);
    printf("Output Reversed Character String:");
    puts(str2);
    printf("Inpur String1:");
    gets(str);
    printf("Inpur String2:");
    gets(str1);
    str2=link(str,str1);
    printf("Link String1 and String2:");
    puts(str2);
    return 0;
}

char *reverse(char *str)
{
    char *p,*q,temp;
    p=str,q=str;
    while(*p!='\0')
    p++;
    p--;
    while(q<p)
    {
        temp=*q;
        *q=*p;
        *p=temp;
        q++;
        p--;
    }
    return str;
}


char *link (char *str1,char *str2)
{
    char *p=str1,*q=str2;
    while (*p!='\0')
    p++;
    while (*q!='\0')
    {
        *p=*q;
        p++;
        q++;
    }
    *p=*q;
    return str1;
}

运行结果

问题:这个题目主要的问题在于使结束字符为空字符,在第一次完成时出现了错误,在指针同时移动的时候我算错了指针位置,使后一位为空字符,导致输出结果中会含有一个未知字符

4.实验练习:8.3.4

流程图

实验代码

#include<stdio.h>
#define N 10
void arrsort(int a[],int n);
int main()
{
    int a[N],i;
    for(i=0;i<N;i++)
    scanf("%d",&a[i]);
    arrsort(a,N);
    for(i=0;i<N;i++)
    printf("%d ",a[i]);
}
void arrsort (int a[],int n)
{
    int *p,*q,temp;
    p=a;
    q=a+n-1;
    while (p<q)
    {
        while (*p%2!=0)
        p++;
        while(*q%2==0)
        q--;
        if(p>q) break; 
        temp=*p;
        *p=*q;
        *q=temp;
        p++;
        q--;
    }
}

运行结果

问题:

三、实验小结

实验中存在的问题:
1、指针的通向移动与相向移动时会搞错指针位置
2、通过指针进行值传递或是址传递会混淆

Guess you like

Origin www.cnblogs.com/1hzq/p/11008244.html