#define _CRT_SECURE_NO_WARNINGS
自定义数据类型,结构体
输出学生信息
#include<stdio.h>
struct Data {
int year;
int month;
int day;
};
struct Student {//定义学生数据类型
int age;//年龄
char name[20];//名字
char sex;//性别
double score;//分数
struct Data birth;//年月日
char addr[20];//地址
//struct Student* p;//应用很多
};
int main() {
struct Student stu1 = {18,"张三",'M',90,1968,7,3,"北京"};
stu1.age = 19;
printf("%d %s %c %lf %d %d %d %s", stu1.age, stu1.name, stu1.sex, stu1.score, stu1.birth.year, stu1.birth.month, stu1.birth.day, stu1.addr);
return 0;
}
输出两个学生的学号,姓名和成绩,输出成绩较高的学生的学号姓名成绩
#include<stdio.h>
struct Student {
int num;//学号
char name[20];//姓名
double grate;//成绩
};
int main() {
struct Student stu1, stu2;
scanf("%d%s%lf", &stu1.num, &stu1.name, &stu1.grate);
scanf("%d%s%lf", &stu2.num, &stu2.name, &stu2.grate);
struct Student max;//保存最大的那个人
if (stu1.grate > stu2.grate) {
max = stu1;//所有信息直接给
}
else {
max = stu2;
}
printf("%d %s %lf", max.num, max.name, max.grate);
return 0;
}
结构体数组
有三个候选人,最后输出每个人投票结果
#include<stdio.h>
#include<string.h>
struct Person {
char name[20];
int count;
};
int main() {
char name[20];
struct Person str[3] = { "li",0,"wang",0,"zhang",0 };//三个候选人初始化,必须初使化,要不然没法比较
for (int i = 0; i < 5; i++) {//五个选民
scanf("%s", &name);
for (int j = 0; j < 3; j++) {
if (strcmp(name, str[j].name) == 0) {//字符串比较用函数,等于0则相等
str[j].count++;
}
}
}
for (int i = 0; i < 3; i++) {
printf("%s %d\n", str[i].name, str[i].count);
}
return 0;
}
结构体指针,通过指向符->
结构体变量如何通过参数传递:结构体作为参数总是传指针(节约空间)
修改学生信息和输出结构体数组
#include<stdio.h>
struct Student {
char name[20];
int age;
double score;
};
void Show( struct Student* p) {//一个结构体成员
printf("%s %d %.1lf\n", p->name, p->age, p->score);
}
void Show_2(struct Student* str, int len) {//传入数组,一组结构体成员
for (int i = 0; i < len; i++) {
printf("%s %d %.1lf\n", str[i].name, str[i].age, str[i].score);//数组输出形式,推荐这种写法
//printf("%s %d %.1lf\n", (str+i)->name, (str + i)->age, (str + i)->score);//指针输出形式1
//printf("%s %d %.1lf\n", str->name, str->age, str->score);
//str++;//指针输出形式2
}
}
int main() {
struct Student str[] = { "li",18,90,"wang",20,91.2};
struct Student* p = str;
/*p = &str[0];
p = &str[1];*/
p->age = 19;//通过指向符更改数据
Show(str);
printf("\n");
Show_2(str, sizeof(str) / sizeof(str[0]));
return 0;
}
有n个学生信息(学号姓名成绩),按成绩高低输出学生信息,想冒泡排,快速排序有一丢复杂
用函数写
#include<stdio.h>
struct Student {
int num;
char name[20];
double score;
};
//输入学生信息
void Input(struct Student* arr, int len) {
for (int i = 0; i < len; i++) {
scanf("%d%s%lf", &arr[i].num, &arr[i].name, &arr[i].score);
}
}
//按成绩高到低排序
void Qort(struct Student* arr, int len) {
struct Student temp;//中间变量设成结构体格式
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (arr[j].score < arr[j + 1].score) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//输出学生信息
void Show(struct Student* arr, int len) {
for (int i = 0; i < len; i++) {
printf("%d %s %.1lf\n", arr[i].num, arr[i].name, arr[i].score);
}
}
int main() {
struct Student arr[10];
int len;
scanf("%d", &len);
Input(arr, len);
Qort(arr, len);
Show(arr, len);
return 0;
}
有n个结构体变量,有学生(学号,姓名,三门课)要求输出平均成绩最高的学生信息
#include<stdio.h>
struct Student {
int num;
char name[20];
double score[3];//定义分数数组
double ave;//平均成绩
};
void Input(struct Student* str, int n) {
printf("输入学号,姓名,三门课成绩");
for (int i = 0; i < n; i++) {
scanf("%d%s%lf%lf%lf", &str[i].num, &str[i].name, &str[i].score[0], &str[i].score[1], &str[i].score[2]);
str[i].ave = (str[i].score[0] + str[i].score[1] + str[i].score[2]) / 3;//求平均成绩
}
}
void Showmax(struct Student* str, int n) {
int max_index=0;//最大值的下标
double max_ave = str[0].ave;//将最大的平均数初始化为第一个平均数
for (int i = 0; i < n; i++) {
if (str[i].ave > max_ave) {
max_index = i;
}
}
printf("%d %s %.1lf %.1lf %.1lf %.1lf", str[max_index].num, str[max_index].name, str[max_index].score[0], str[max_index].score[1], str[max_index].score[2], str[max_index].ave);
}
int main() {
struct Student str[10];
int n;
scanf("%d", &n);
Input(str, n);
Showmax(str, n);
return 0;
}
枚举类型:一个变量取值只有几种情况
考点和注意事项:
1.枚举默认从0开始,后面依次加一
枚举在定义时能修改,但依旧加一(他后面的都加一),以后不能改
#include<stdio.h>
int main() {
enum Weekend { mon, tue, wes, thr, fri, sat, sun };
enum Weekend d1 = mon;
if (d1 == sun) {
printf("d1==sum\n");
}
return 0;
}
有五个球红黄蓝白黑,从中取出三个,颜色不重复,输出一共有多少种结果
#include<stdio.h>
int main() {
int count = 0;
const char *str[] = { "red","yellow","blue","white","black" };//必须是const char*str[]类型的
int n=5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (i != j && i != k && j != k) {//三个颜色不相等则输出
count++;
printf("%s %s %s\n", str[i], str[j], str[k]);
}
}
}
}
printf("%d ", count);
return 0;
}
typedef:类型定义。把一个旧类型名起一个新名字,给一个复杂类型起一个简单名字(就是起个简单名)
#include<stdio.h>
typedef struct Student Std;//法一,Std就是struct Student
typedef struct Student {//法二,Std就是struct Student
}Std;
typedef unsigned long long int ulli;//unsigned long long int改叫ulli