1.教会の上限が
1の場合の演習1。2 つの数値を[空白を埋める]で交換し、大から小に出力します。
次のプログラムは、2つの数値を交換して、2つの数値を大から小に出力します。空欄に入力してください。
#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
* 注:p1はアドレスp1を指す値を示し、p1はポインター変数、特にアドレスを示します
回答:
#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. [空白埋め]機能により、文字列の長さを実現します。
次のプログラムは、文字列の長さを見つけるための関数によって実装されており、空白を埋めて完了します
#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"
#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.構造タイプの定義
次のメンバーを含む、studentという名前の構造タイプを定義する必要があります:
(1)最大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;
}