High-level language programming-experiment 10 pointers and structures (1)

1. Exercise
1 when the upper limit of the church is 1. Exchange the two numbers with [fill in the blank], and output from big to small.
The following program exchanges two numbers to make the two numbers output from large to small. Please fill in the blanks.

#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

* Note: p1 indicates the value pointing to the address p1, p1 is a pointer variable, specifically indicating the address

answer:

#include "stdio.h"

void swap(int *p1,int *p2)
{
   int temp;
   temp=*p1;//*pa,*pb表示值
   *p1=*p2;
   *p2=temp;
}

int main()
{ int a,b; int *pa,*pb;
   scanf("%d%d", &a, &b);
   pa=&a; pb=&b;//pa,pb表示地址
  if(a<b) swap(pa,pb);
  printf("%d %d\n",a,b);
}

2. The [Fill in blank] function realizes the length of the character string.
The following program is implemented by the function to find the length of the string, and then fill in the blank to complete

#include "stdio.h"

/*create function f*/
_______________________

int main()
{
    char s[80];
    int i;
    scanf("%s", s);
    i=f(s);
    printf("%d", i);
}
输入样例
Hello!
输出样例
6

answer:

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

/*create function f*/
int f(char a[])//把实参的s[]的值赋给形参a[],具体长度根据s[]来定
{
   char *p;//定义一个指针变量
   p=a;//a[0]的地址赋值给p指针
   while(*p!='\0')//*p代表p指向的值
   {
       p++;//指针P向后移动
   }
   return p-a;//p的地址数-a[0]的地址
}

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

4. Define the structure type It is
required to define a structure type named student, which contains the following members:
(1) The character array name, which can store up to 10 characters;
(2) The character variable sex, used to record gender;
(3 ) The integer type variable num is used to record the student number;
(4) The float type variable score is used to record the grade;
and the following code is complete.

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

answer:

#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;
}
Published 10 original articles · Like1 · Visits 190

Guess you like

Origin blog.csdn.net/weixin_39475542/article/details/105073176