1、思维导图
https://www.zhixi.com/view/4aa2c70f
2、作业
1、
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
char name[10];
char sex;
int age;
};
void output(struct student s[])
{
for(int i=0;i<2;i++)
{
printf("name:%s\tsex: %c\tage:%d\n",s[i].name,s[i].sex,s[i].age);
}
putchar(10);
}
int main(int argc, const char *argv[])
{
//顺序
struct student s1[2]={"张三",'m',12,"李四",'w',11};
output(s1);
//不按顺序
struct student s2[2]={[1]={"王五",'m',10},[0]={.sex='w',.name="张六",.age=9}};
output(s2);
//单个赋值
struct student s3[2];
strcpy(s3[0].name,"张一");
s3[0].sex='w';
s3[0].age=17;
strcpy(s3[1].name,"李二");
s3[1].sex='w';
s3[1].age=17;
output(s3);
//循环输入
struct student s4[2];
for(int i=0;i<2;i++)
{
puts("name=");
scanf("%s",s4[i].name);
puts("sex=");
scanf(" %c",&s4[i].sex);
puts("age=");
scanf("%d",&s4[i].age);
}
output(s4);
return 0;
}
2、
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct message
{
char pp[20];
int m;
char ys[20];
};
struct message x;
void output(struct message che[],int n)
{
for(int i=0;i<n;i++)
{
printf("品牌:%s\t价钱:%d\t颜色:%s\n",che[i].pp,che[i].m,che[i].ys);
}
}
void px(struct message che[],int n,int flag)
{
//升序
if(flag==1)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(che[n-1-i].m<che[j].m)
{
x=che[n-1-i];
che[n-1-i]=che[j];
che[j]=x;
}
}
}
}
//降序
if(flag==0)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(che[n-1-i].m>che[j].m)
{
x=che[n-1-i];
che[n-1-i]=che[j];
che[j]=x;
}
}
}
}
}
void num(struct message che[],int n)
{
int j=0;
for(int i=0;i<n;i++)
{
if(strcmp(che[i].ys,"red")==0)
{
j++;
}
}
printf("红色车有%d辆\n",j);
}
int main(int argc, const char *argv[])
{
int flag;
int n;
printf("想要记录几辆车:");
scanf("%d",&n);
struct message che[n];
for(int i=0;i<n;i++)
{
printf("name:");
scanf("%s",che[i].pp);
printf("money:");
scanf("%d",&che[i].m);
printf("color:");
scanf("%s",che[i].ys);
}
printf("flag(1为升序,0为降序)=");
scanf("%d",&flag);
px(che,n,flag);
num(che,n);
output(che,n);
return 0;
}