SCAU高级语言程序设计--实验10 指针与结构体(1)

SCAU高级语言程序设计--实验10 指针与结构体(1)

一、堂上限时习题

1、交换两数,由大到小输出

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

#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); 
}  

思路:*pa表示一个值,pa表示一个地址,需要一一对应。

#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(pa,pb);//注意这里没有*号
    printf("%d %d\n",a,b);
    return 0;
}

2、函数实现求字符串长度

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


#include "stdio.h" 

/*create function f*/ 
_______________________ 

int main() 

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

思路:strlen()函数输出字符长度,注意头文件string.h。f(s)中的s为数组的头地址,即s[0]的地址。

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

int f(char *l){
    return strlen(l);
}

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

 

3、定义结构体类型

题目:要求定义一个名为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 studen 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;
}

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值