SCAUoj实验10 指针与结构体

断更一年的专栏突然重启,主打的就是一个措不及防


一、堂前习题

1091 [填空]交换两数,由大到小输出

下面程序,交换两数,使两数由大到小输出,请填空

#include "stdio.h"
void swap(_______________________)
{
   int temp;
   temp=*p1;
   *p1=*p2;
   *p2=temp;
}
int main()
{ int a,b; int *pa,*pb;
   scanf("%d%d", &a, &b);
   pa=&a; pb=&b;
  if(a<b) swap(_______________________);
  printf("%d %d\n",a,b);
}

输入样例 1 2
输出样例 2 1
代码如下:

#include "stdio.h"
void swap(int* p1,int* p2)
{
   int temp;
   temp=*p1;
   *p1=*p2;
   *p2=temp;
}
int main()
{ int a,b; int *pa,*pb;
   scanf("%d%d", &a, &b);
   pa=&a; pb=&b;
  if(a<b) swap(&a,&b);
  printf("%d %d\n",a,b);
}

11128 字符串与指针

请写出下列程序的运行结果

#include<stdio.h>
int main( )
{   char   string[30]="How_are_you" ;
	char   *p=&string[0],   *p2=string+8;
    printf("%s,%s\n" , p , p2 ) ;
}

程序运行结果为:

#include <stdio.h>
int main()
{
    printf("_______________________");
}

代码如下:

#include <stdio.h>
int main()
{
    printf("How_are_you,you");
}

1125 定义结构体类型

要求定义一个名为student的结构体类型,其包含如下成员:
(1)字符数组name,最多可存放10个字符;
(2)字符变量sex,用于记录性别;
(3)整数类型变量num,用于记录学号;
(4)float类型变量score,用于记录成绩;
并使下列代码完整。

#include "stdio.h"
_______________________
int main()
{
    struct  student stu;
    gets(stu.name);
    scanf("%c",  &stu.sex);
    scanf("%d",  &stu.num);
    scanf("%f",  &stu.score);
    printf("%s\n", stu.name);
    printf("%c\n", stu.sex);
    printf("%d\n", stu.num);
    printf("%f\n", stu.score);
    return 0;
}

代码如下:

#include "stdio.h"
struct student
{
    char name[10];
    char sex;
    int num;
    float score;
};
int main()
{
    struct  student stu;
    gets(stu.name);
    scanf("%c",  &stu.sex);
    scanf("%d",  &stu.num);
    scanf("%f",  &stu.score);
    printf("%s\n", stu.name);
    printf("%c\n", stu.sex);
    printf("%d\n", stu.num);
    printf("%f\n", stu.score);
    return 0;
}

二、堂上练习

1092 [填空]函数实现求字符串长度

下面程序实现由函数实现求字符串长度,再填空完成

#include "stdio.h"

/*create function f*/
_______________________

int main()
{
    char s[80];
    int i;
    scanf("%s", s);
    i=f(s);
    printf("%d", i);
}

输入样例
Hello!

输出样例
6

代码如下:

#include "stdio.h"
int f(char* a)
{
    int count = 0;
    while(*a != '\0')//是不等于'\0',不是'\n'
    {
        count++;
        a++;
    }
    return count;
}
int main()
{
    char s[80];
    int i;
    scanf("%s", s);
    i=f(s);//传的是字符串首元素的地址
    printf("%d", i);
    return 0;
}

1065 数组中的指针

设有如下数组定义:int a[3][4]={{1,3,5,7},{9,11,13,15},{17,19,21,23}}; (设数组a的首地址为2000,
一个int类型数占四个字节)
(1)a[2][1] (2)a[1] (3)a (4)a+1 (5)*a+1
(6)*(a+1) (7)a[2]+1(8)*(a+1)+1 (9)*(*(a+2)+2)
编写一个程序直接输出你的答案,一行一个。
输出样例
19
2016
……
……
……

提示
注意:地址则输出地址,变量则输出变量值;输出格式,要求,一行一个答案,不允许多余空格
测试代码及解释如下:

#include<stdio.h>
int main()
{
    int a[3][4] = {{1,3,5,7},{9,11,13,15},{17,19,21,23}};//a是二级地址,一个*升一级
    printf("%d\n",a[2][1]);//19
    printf("%p\n",a[1]);//a[1]+0,*(a+1)+0第1行第0列的一级地址,2016
    printf("%p\n",a);//2000
    printf("%p\n",a+1);//第一行的地址,仍然是二级地址,2016
    printf("%p\n",*a+1);//第0行第1列的地址,2004
    printf("%p\n",*(a+1));//和第二个一样,第一行第0列 2016
    printf("%p\n",a[2]+1);//第2行第一列的地址,2036
    printf("%p\n",*(a+1)+1);//第一行第一列的地址,2020
    printf("%d\n",*(*(a+2)+2));//a[2][2]元素
    return 0;
}

答案如下:

#include <stdio.h>
int main()
{
    printf("19\n2016\n2000\n2016\n2004\n2016\n2036\n2020\n21");
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值