- 已知结构体类型
定义该结构体类型的数组并输入4名学生的相关信息,计算并输出他们的平均年龄。
struct stud{
char name[20];
int age;
char sex;
};
定义该结构体类型的数组并输入4名学生的相关信息,计算并输出他们的平均年龄。
#include <stdio.h>
#define STU_CNT 4
struct stud{
char name[20];
int age;
char sex;
};
int main(){
// 学生个数
struct stud students[STU_CNT];
int sum=0;
int i;
for(i=0;i<STU_CNT;i++){
printf("请输入学生姓名:");
scanf("%s",students[i].name);
printf("请输入学生年龄:");
scanf("%d",&students[i].age);
sum+=students[i].age;
getchar();
printf("请输入学生性别:");
scanf("%c",&students[i].sex);
printf("\n");
}
printf("平均年龄为:%lf\n",1.0*sum/STU_CNT);
return 0;
}
2.理解教材P172的程序设计实例10.1—荷兰国旗案例并复现;
分析过程:
// i,j k
// White,Red,Red,Blue,White,Blue,Red,White
// j 是 white j++
// i j k
// White,Red,Red,Blue,White,Blue,Red,White
// j 是 red i,j中的值交换 i++,j++
// i j k
// Red,White,Red,Blue,White,Blue,Red,White
// j 是 red i,j中的值交换 i++,j++
// i j k
// Red,Red,White,Blue,White,Blue,Red,White
// j 是 blue j,k中的值交换 k--
// i j k
// Red,Red,White,White,White,Blue,Red,Blue
// j 是 white j++
// i j k
// Red,Red,White,White,White,Blue,Red,Blue
// j 是 white j++
// i j k
// Red,Red,White,White,White,Blue,Red,Blue
// j 是 blue j,k中的值交换 k--
// i j,k
// Red,Red,White,White,White,Red,Blue,Blue
// j 是 red i,j中的值交换 i++,j++
// i k j
// Red,Red,Red,White,White,White,Blue,Blue
// j > k 跳出循环
代码:
#include <stdio.h>
#define N 8
enum Color {
Red,
White,
Blue
};
/*
* 最终排序的结果应该是 : Red < White < Blue
*/
void Sort(enum Color a[],int n){
int i=0,k=n-1,j=0;
enum Color temp;
while(j<=k){
switch(a[j]){
case Red:
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j++;
break;
case Blue:
temp=a[j];
a[j]=a[k];
a[k]=temp;
k--;
break;
case White:
j++;
break;
}
}
}
/*
* 输出国旗
*/
void PrintOut(enum Color a[],int n){
int i;
for(i=0;i<n;i++){
switch(a[i]){
case Red: printf("Red ");break;
case White: printf("White ");break;
case Blue: printf("Blue ");break;
}
}
}
int main(){
enum Color a[N] = {
White,
Red,
Red,
Blue,
White,
Blue,
Red,
White
};
Sort(a,N);
printf("排序后的序列为:");
PrintOut(a,N);
return 0;
}
3.理解教材P181的程序设计实例10.2—最近对问题并复现
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define N 10
struct PointType
{
int x, y;
};
void CreatPot (struct PointType pot[], int n);
double MinPot (struct PointType pot[],int n, int *p, int *q);
void PrintPot (struct PointType pot[],int n);
int main()
{
struct PointType pot[N];
int i,minI,minJ;
double minDist;
CreatPot(pot,N);
PrintPot(pot,N);
minDist = MinPot(pot, N, &minI,&minJ);
printf("最近点(%2d, %2d)%5.2f\n",minI,minJ,minDist);
return 0;
}
void CreatPot(struct PointType pot[],int n)
{
int i;
srand(time(NULL));
for(i=0;i<n;i++)
{
pot[i].x =rand() % 100;
pot[i].y =rand() % 100;
}
}
void PrintPot(struct PointType pot[],int n)
{
int i;
printf("产生的随机点是:\n");
for(i=0;i<n;i++)
{
printf("%2d (%2d, %2d)\t",i+1,pot[i].x,pot[i].y);
if ((i+1)%5==0)
printf("\n");
}
}
double MinPot (struct PointType pot[], int n,int *p,int *q)
{
int i,j,minI,minJ,tempx,tempy;
int dist,minDist = 1000;
for(i=0;i<n;i++)
for(j=0;j<i;j++)
{
tempx=pot[i].x-pot[j].x;
tempy=pot[i].y-pot[j].y;
dist= tempx*tempx + tempy*tempy;
if(dist<minDist)
{
minDist = dist; minI=i+1;minJ=j+1;
}
}
*p = minI; *q=minJ;
return sqrt(minDist);
}