一.结构体![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1cc3b7adc52db008826afe1a9244a1cb.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bfd13e0811506287363d242f4d1a3b57.png)
#include <stdio.h>
void main()
{
struct student
{
int num;
char name[20];
char sex;
int teg;
float score;
char addr[30];
};
}
#include <stdio.h>
#include<string.h>
void main()
{
struct student
{
int num;
char *name;//char name[20]使用boy1.name引用不了,要使用char *name;
char sex;
float score;
}boy1,boy2;
boy1.num = 7;
boy1.name ="Jane" ;//strcpy(boy1.name,"Jane")
printf("boy1.num:%d,boy1.name:%s\n", boy1.num, boy1.name);
printf("Please input sex and score:");
scanf("%c %f", &boy1.sex, &boy1.score);
printf("boy1.sex:%c,boy1.score:%.2f\n\n", boy1.sex, boy1.score);
boy2 = boy1;
printf("boy2.num:%d,boy2.name:%s\n", boy2.num, boy2.name);
printf("boy2.sex:%c,boy2.score:%.2f\n", boy2.sex, boy2.score);
}
//boy1.num:7, boy1.name : Jane
//Please input sexand score : n 32.5
//boy1.sex : n, boy1.score : 32.50
//
//boy2.num : 7, boy2.name : Jane
//boy2.sex : n, boy2.score : 32.50
#include <stdio.h>
void main()
{
struct date
{
int year;
int month;
int day;
};
struct student
{
int num;
char name[20];
char sex;
int teg;
float score;
char addr[30];
struct date birthday;
}boy1,boy2;
printf("YY:");
scanf("%d", &boy1.birthday.year);
printf("MM:");
scanf("%d", &boy1.birthday.month);
printf("DD:");
scanf("%d", &boy1.birthday.day);
printf("YY:%d MM:%d DD:%d", boy1.birthday.year, boy1.birthday.month, boy1.birthday.day);
}
//YY:2020
//MM : 7
//DD : 29
//YY : 2020 MM : 7 DD : 29
#include <stdio.h>
void main()
{
int sum;
struct student
{
int num;
char name[20];
char sex;
float score;
}student1,student2;
scanf("%d", &student1.num);
sum=student1.num + 32;
printf("%d",sum);
}
//32
//64
同一个地址
#include <stdio.h>
void main()
{
struct student
{
int num;
char *name;
char sex;
float score;
}student1;
student1.num = 7;
student1.name = "Jane";
printf("The address of struct is %o\n", &student1);
printf("The address of num is %o\n", &student1.num);
}
//The address of struct is 74774410
//The address of num is 74774410
#include <stdio.h>
void main()
{
struct student
{
int num;
char *name;
char sex;
float score;
}student1, student2 = {102,"Jane",'M',98.5};
student1 = student2;
printf("%d %s %c %.2f\n", student1.num, student1.name, student1.sex, student1.score);
printf("%d %s %c %.2f\n", student2.num, student2.name, student2.sex, student2.score);
}
//102 Jane M 98.50
//102 Jane M 98.50
#include <stdio.h>
struct student
{
int mum;
char name[20];
char sex;
int age;
float score;
char addr[30];
};
struct student stu[3] = { {10101,"Li A", 'M', 18, 87.5, "103 Beijing Road"},
{10101,"Li V", 'M', 18, 87.5, "103 Beijing Road"},
{10101,"Li B", 'M', 18, 87.5, "103 Beijing Road"}};
void main()
{
printf("%s", stu[1].name);
}
//Li V
#include <stdio.h>
struct student
{
int mum;
char name[20];
char sex;
int age;
float score;
char addr[30];
}stu[3] = { {10101,"Li A", 'M', 18, 87.5, "103 Beijing Road"},
{10102,"Li V", 'M', 18, 87.5, "103 Beijing Road"},
{10103,"Li B", 'M', 18, 87.5, "103 Beijing Road"} };
void main()
{
printf("%s", stu[1].name);
}
//Li V
#include <stdio.h>
#include<string.h>
struct people
{
char name[20];
int num;
}stu[] = {{"小米",0},{"华为",0},{"魅族",0},{"苹果",0},{"三星",0}};
void main()
{
printf("手机品牌有小米、华为、魅族、苹果、三星\n");
char str[20];
for (int i = 0; i < 10; i++)
{
printf("请输入第%d位你喜欢的手机品牌:",i+1);
scanf("%s", &str);
for (int i = 0; i < 5; i++)
{
if (strcmp(stu[i].name, str) == 0)
{
stu[i].num++;
}
}
}
for (int i = 0; i < 5; i++)
{
printf("手机品牌%s,一共有%d票\n", stu[i].name, stu[i].num);
}
}
//手机品牌有小米、华为、魅族、苹果、三星
//请输入第1位你喜欢的手机品牌 : 小米
//请输入第2位你喜欢的手机品牌 : 小米
//请输入第3位你喜欢的手机品牌 : 苹果
//请输入第4位你喜欢的手机品牌 : 华为
//请输入第5位你喜欢的手机品牌 : 华为
//请输入第6位你喜欢的手机品牌 : ViVo
//请输入第7位你喜欢的手机品牌 : OPPO
//请输入第8位你喜欢的手机品牌 : 小米
//请输入第9位你喜欢的手机品牌 : 魅族
//请输入第10位你喜欢的手机品牌 : 华为
//手机品牌小米, 一共有3票
//手机品牌华为, 一共有3票
//手机品牌魅族, 一共有1票
//手机品牌苹果, 一共有1票
//手机品牌三星, 一共有0票
#include <stdio.h>
struct stu
{
int num;
char name[20];
char sex;
float score;
}boy1= {102,"FishC",'M',78.5};
void main()
{
struct stu* pstu;
pstu = &boy1;
printf("Number:%d\nName:%s\n", boy1.num, boy1.name);
printf("Sex:%c\nScore:%.2f\n\n\n", boy1.sex, boy1.score);
printf("Number:%d\nName:%s\n", (*pstu).num, (*pstu).name);
printf("Sex:%c\nScore:%.2f\n", pstu->sex, pstu->score);
}
//Number:102
//Name : FishC
//Sex : M
//Score : 78.50
//
//
//Number : 102
//Name : FishC
//Sex : M
//Score : 78.50
第一种
#include <stdio.h>
struct student
{
int num;
char name[20];
float score[3];
};
void main()
{
void print(struct student);
struct student stu[] = { { 1,"XiaoMi",{78.5,68.5,98}},{ 2,"Huawei",{99.5,88.5,78}} };
//struct student stu = { 1,"XiaoMi",{78.5,68.5,98} };
//print(stu);
for (int i = 0; i < 2; i++)
{
print(stu[i]);
}
}
void print(struct student stu)
{
printf("num:%d\n", stu.num);
printf("name:%s\n", stu.name);
for (int i = 0; i < 3; i++)
{
printf("score%d:%.2f\n", i,stu.score[i]);
}
}
//num:1
//name : XiaoMi
//score0 : 78.50
//score1 : 68.50
//score2 : 98.00
//num : 2
//name : Huawei
//score0 : 99.50
//score1 : 88.50
//score2 : 78.00
第二种
#include <stdio.h>
struct student
{
int num;
char name[20];
float score[3];
};
void main()
{
void print(struct student *stu);
struct student stu[] = { { 1,"XiaoMi",{78.5,68.5,98}},{ 2,"Huawei",{99.5,88.5,78}} };
for (int i = 0; i < 2; i++)
{
print(&stu[i]);
}
}
void print(struct student *stu)
{
printf("num:%d\n", (*stu).num);
printf("name:%s\n", (*stu).name);
for (int i = 0; i < 3; i++)
{
printf("score%d:%.2f\n", i, (*stu).score[i]);
}
}
//num:1
//name : XiaoMi
//score0 : 78.50
//score1 : 68.50
//score2 : 98.00
//num : 2
//name : Huawei
//score0 : 99.50
//score1 : 88.50
//score2 : 78.00
#include <stdio.h>
struct student
{
int num;
float score;
struct student* next;
};
void main()
{
struct student a, b, c, * head;
a.num = 10101;
a.score = 89.5;
b.num = 10102;
b.score = 90.5;
c.num = 10103;
c.score = 91;
head = &a;
a.next = &b;
b.next = &c;
c.next = NULL;
do
{
printf("%d %.2f\n", (*head).num, (*head).score);
head = (*head).next;
} while (head!=NULL);//符号条件执行do语句,不符合退出循环
}
//10101 89.50
//10102 90.50
//10103 91.00
struct studen *creat()
{
struct student *p1,*p2,*head;
p1 = p2 = (struct student *)malloc(LEN);
printf("Please enter the num:");
scanf("%d", &p1->num);
printf("Please enter the score:");
scanf("%f", &p1->score);
n = 0;
head = NULL;
while (p1->num != 0)
{
n = n + 1;
if (n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student*)malloc(LEN);
printf("Please enter the num:");
scanf("%d", &p1->num);
printf("Please enter the score:");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
void print(struct student* head)
{
struct student* p;
p = head;
do
{
printf("%d %.2f\n",p->num,p->score);
p = p->next;
} while (p!=0);
}
主程序
#include <stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)//student结构大小
struct student* creat();//创建链表
void print(struct student* head);//打印链表
int n;//全局变量
struct student
{
int num;
float score;
struct student* next;
};
void main()
{
struct student* stu;
stu = creat();
print(stu);
}
struct student* creat()
{
struct student* p1, * p2;
struct student* head;
p1 = p2 = (struct student*)malloc(LEN);
printf("Please enter the num:");
scanf("%d", &p1->num);
printf("Please enter the score:");
scanf("%f", &p1->score);
n = 0;
head = NULL;
while (p1->num != 0)
{
n = n + 1;
if (n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student*)malloc(LEN);
printf("Please enter the num:");
scanf("%d", &p1->num);
printf("Please enter the score:");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
void print(struct student* head)
{
struct student* p;
p = head;
if (head != 0)
{
do
{
printf("%d %.2f\n", p->num, p->score);
p = p->next;
} while (p != 0);
}
}
//Please enter the num : 1
//Please enter the score : 99.5
//Please enter the num : 2
//Please enter the score : 89.5
//Please enter the num : 3
//Please enter the score : 67
//Please enter the num : 0
//Please enter the score :0
//1 99.50
//2 89.50
//3 67.00
struct student *del(struct student* head, int num)
{
struct student* p1, * p2;
if (head == NULL)//链表为空时
{
print("空链表\n");
goto end;//跳出程序
}
p1 = head;
while (p1->num != num && p1->next != NULL)//两种情况退出循环
{
p2 = p1;
p1 = p1->next;
}
if (p1->num == num)
{
if (p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
n = n - 1;//由于删除一位数据成员从链表中
}
else
{
printf("找不到\n");
}
end:return head;
}
主程序
#include <stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)//student结构大小
struct student *creat();//创建链表
struct student *del(struct student* head, int num);//删除链表
void print(struct student* head);//打印链表
int n;//全局变量
struct student
{
int num;
float score;
struct student* next;
};
void main()
{
struct student* stu;
stu = creat();
print(stu);
int num;
printf("请输入要删除的链表:");
scanf("%d", &num);
print(del(stu, num));
}
struct student* creat()
{
struct student* p1, * p2;
struct student* head;
p1 = p2 = (struct student*)malloc(LEN);
printf("Please enter the num:");
scanf("%d", &p1->num);
printf("Please enter the score:");
scanf("%f", &p1->score);
n = 0;
head = NULL;
while (p1->num != 0)
{
n = n + 1;
if (n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student*)malloc(LEN);
printf("Please enter the num:");
scanf("%d", &p1->num);
printf("Please enter the score:");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
struct student *del(struct student* head, int num)
{
struct student* p1, * p2;
if (head == NULL)
{
print("空链表\n");
goto end;
}
p1 = head;
while (p1->num != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (p1->num == num)
{
if (p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
n = n - 1;
}
else
{
printf("找不到\n");
}
end:return head;
}
void print(struct student* head)
{
struct student* p;
p = head;
if (head != 0)
{
do
{
printf("%d %.2f\n", p->num, p->score);
p = p->next;
} while (p != 0);
}
}
//Please enter the num : 1
//Please enter the score : 99
//Please enter the num : 2
//Please enter the score : 80
//Please enter the num : 3
//Please enter the score : 70
//Please enter the num : 0
//Please enter the score : 0
//1 99.00
//2 80.00
//3 70.00
//请输入要删除的链表 : 3
//1 99.00
//2 80.00
struct student* insert(struct student* head, struct student* stud)
{
struct student* p0, * p1,* p2;
p1 = head;
p0 = stud;
if (head == NULL)//链表为空时
{
p0 = head;
p0->next = NULL;
}
else
{
while ((p0->num > p1->num) &&(p1->next != NULL))//两种情况退出循环
{
p2 = p1;
p1=p1->next;
}
if (p0->num <= p1->num)
{
if (p1 == head)//p1是头结点,插入头部
{
head = p0;
p0->next = p1;
}
else//普通情况,插入中间
{
p2->next = p0;
p0->next = p1;
}
}
else//p0的值最大,插入到尾部
{
p1->next = p0;
p0->next = NULL;
}
}
n = n + 1;//由于插入一位数据成员进入链表中
return head;
}
主程序
#include <stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)//student结构大小
struct student* creat();//创建链表
struct student* del(struct student* head, int num);//删除链表
struct student* insert(struct student* head, struct student* stud);//插入链表
void print(struct student* head);//打印链表
int n;//全局变量
struct student
{
int num;
float score;
struct student* next;
};
void main()
{
struct student *stu;
struct student stud;
stu = creat();
print(stu);
int num;
printf("请输入要删除的链表:");
scanf("%d", &num);
print(del(stu, num));
printf("Please enter the num:");
scanf("%d", &stud.num);
printf("Please enter the score:");
scanf("%f", &stud.score);
print(insert(stu, &stud));
}
struct student* creat()
{
struct student* p1, * p2;
struct student* head;
p1 = p2 = (struct student*)malloc(LEN);
printf("Please enter the num:");
scanf("%d", &p1->num);
printf("Please enter the score:");
scanf("%f", &p1->score);
n = 0;
head = NULL;
while (p1->num != 0)
{
n = n + 1;
if (n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student*)malloc(LEN);
printf("Please enter the num:");
scanf("%d", &p1->num);
printf("Please enter the score:");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
struct student* del(struct student* head, int num)
{
struct student* p1, * p2;
if (head == NULL)
{
print("空链表\n");
goto end;
}
p1 = head;
while (p1->num != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if (p1->num == num)
{
if (p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
n = n - 1;
}
else
{
printf("找不到\n");
}
end:return head;
}
struct student* insert(struct student* head, struct student* stud)
{
struct student* p0, * p1,* p2;
p1 = head;
p0 = stud;
if (head == NULL)//链表为空时
{
p0 = head;
p0->next = NULL;
}
else
{
while ((p0->num > p1->num) &&(p1->next != NULL))//两种情况退出循环
{
p2 = p1;
p1=p1->next;
}
if (p0->num <= p1->num)
{
if (p1 == head)//p1是头结点,插入头部
{
head = p0;
p0->next = p1;
}
else//普通情况,插入中间
{
p2->next = p0;
p0->next = p1;
}
}
else//p0的值最大,插入到尾部
{
p1->next = p0;
p0->next = NULL;
}
}
n = n + 1;
return head;
}
void print(struct student* head)
{
struct student* p;
p = head;
if (head != 0)
{
do
{
printf("%d %.2f\n", p->num, p->score);
p = p->next;
} while (p != 0);
}
}
//Please enter the num : 1
//Please enter the score : 1
//Please enter the num : 2
//Please enter the score : 2
//Please enter the num : 3
//Please enter the score : 3
//Please enter the num : 0
//Please enter the score : 0
//1 1.00
//2 2.00
//3 3.00
//请输入要删除的链表 : 3
//1 1.00
//2 2.00
//Please enter the num : 3
//Please enter the score : 3
//1 1.00
//2 2.00
//3 3.00
二.共用体
#include<stdio.h>
union student
{
int num;
float score;
char* name;
char job[20];
}boy1;
int main()
{
boy1.num = 1;
printf("%d\n", boy1.num);//不能一起打印,要一个一个打印
boy1.score = 99.5;
printf("%.2f\n",boy1.score);
boy1.name = "小明";
printf("%s\n", boy1.name);
printf("请输入职业:");
scanf("%s", &boy1.job);
printf("%s", boy1.job);
}
//1
//99.50
//小明
//请输入职业:学生
//学生
#include<stdio.h>
struct
{
int phone;
char name[20];
char sex;
char job;
union
{
int class;
char position[20];
}category;
}person[2];
void main()
{
for (int i = 0; i < 2; i++)
{
printf("请输入号码:");
scanf(" %d", &person[i].phone);
printf("请输入姓名:");
scanf(" %s", &person[i].name);
printf("请输入性别(b/g):");
scanf(" %c", &person[i].sex);
printf("请输入职业(s/t):");
scanf(" %c", &person[i].job);
if (person[i].job == 's')
{
printf("请输入班级:");
scanf(" %d", &person[i].category.class);
}
else
{
if (person[i].job == 't')
{
printf("请输入职务:");
scanf(" %s", &person[i].category.position);
}
else
{
printf("输入错误");
break;
}
}
}
for (int i = 0; i < 2; i++)
{
if (person[i].job == 's')
{
printf("号码:%d、姓名%s、性别%c、职业%c、班级%d\n", person[i].phone, person[i].name, person[i].sex, person[i].job, person[i].category.class);
}
else if (person[i].job == 't')
{
printf("号码:%d、姓名%s、性别%c、职业%c、职务%s\n", person[i].phone, person[i].name, person[i].sex, person[i].job, person[i].category.position);
}
}
}
//请输入号码:123
//请输入姓名 : Jane
//请输入性别(b / g) : b
//请输入职业(s / t) : s
//请输入班级 : 101
//请输入号码 : 456
//请输入姓名 : Alex
//请输入性别(b / g) : g
//请输入职业(s / t) : t
//请输入职务 : teacher
//号码 : 123、姓名Jane、性别b、职业s、班级101
//号码 : 456、姓名Alex、性别g、职业t、职务teacher
#include<stdio.h>
void main()
{
enum weekday {sun,mon,tue,wed,thu,fri,sat}a,b,c;
a = sun;
b = mon;
c = tue;
printf("%d %d %d", a, b, c);
}
//0 1 2
#include<stdio.h>
void main()
{
enum boy {Tom,Danny,Gan,LiLei}month[31],j;
j = Tom;
for (int i = 0; i < 31; i++)
{
month[i] = j;//(month[0]=0)==(month[0]=Tom)
j++;
if (j > LiLei)//j=4
{
j = Tom;//month[4]=Tom
}
}
for (int i = 1; i <= 30; i++)
{
switch (month[i])//month[0]=Tom,month[1]=Danny,month[2]=Gan,month[3]=LiLei,month[4]=Tom,month[5]=Danny
{
case Tom:printf("%4d %s\t", i, "Tom");
break;
case Danny:printf("%4d %s\t", i, "Danny");
break;
case Gan:printf("%4d %s\t", i, "Gan");
break;
case LiLei:printf("%4d %s\t", i, "LiLei");
break;
default:
break;
}
}
}
#include<stdio.h>
typedef struct
{
int year;
int month;
int day;
}DATE;
void main()
{
DATE date_one;
date_one.year = 2020;
date_one.month = 12;
date_one.day = 31;
printf("year:%d month:%d day:%d", date_one.year, date_one.month, date_one.day);
}
//year:2020 month : 12 day : 31
#include<stdio.h>
typedef int NUM[100];
void main()
{
NUM num;
printf("%d", sizeof(num));//400
}
#include<stdio.h>
typedef char* STRING;
void main()
{
STRING x;
x = "I Love You";
printf("%s", x);//I Love You
}
#include<stdio.h>
typedef int (*POINTER)();
void main()
{
int max(int a, int b);
int a, b, c;
scanf("%d %d", &a, &b);
POINTER p;//int(*p)()
p = max;
c = (*p)(a,b);
printf("max:%d", c);
}
int max(int a, int b)
{
int max;
if (a > b)
{
max = a;
}
else
{
max = b;
}
return max;
}
//32 2
//max:32
#include<stdio.h>
#define PIN1 char *//char*是一个指针型的字符
typedef char* PIN2;//分号结尾
void main()
{
PIN1 x, y;//char *x,char y
PIN2 a, b;//char *a,char *b
printf("#define:%d %d\n", sizeof(x), sizeof(y));
printf("typedef:%d %d\n", sizeof(a), sizeof(b));
}