#C语言# #课堂笔记#
一、结构体的定义

二、银行卡
银行卡属性
银行信息
money
id
passwd
用户信息
username
sex
age
phone
idcard ------>身份证 *****20050111**** 获取身份证时间,判断16岁
bankname
address 开户行
办卡时间
卡类型
流水明细
#include <stdio.h>
typedef struct User {
char username[10];
char idcard[20];
long long phone;
}user;
typedef struct Record {
char time[30];
char optype[10];
float rmony;
}Record;
typedef struct BankCard {
float money;
long id;
int passwd;
User user;
char bankname[20];
char address[20];
char date[30]; //时间 2024-04-14 星期日 14:46:45
char type[10]; //储蓄卡 信用卡
Record records[100]; //100条流水记录
}BankCard;
int main() {
BankCard card1 = { 0.0,1,1,{"zs","111",123}, };
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
typedef struct student {
char name[10];
float scord;
}Stu;
int main() {
Stu stus[3];
for (int i = 0; i < 3; i++) {
scanf("%s %f", stus[i].name, stus[i].scord);
}
float sum = 0;
for (int i = 0; i < 3; i++) {
sum = sum + stus[i].scord;
}
return 0;
}
三、结构体数组
Student : name[20] age
Student stus[3];
四、内存初始化为0
memset(stus ,0, 3*sizeof(student));
五、 拷贝 memcpy
memcpy(stus2,stus,3*sizeof(student));
六、结构体和指针
七、枚举类型
enum BankType={ cxk , xyk };
sizeof(enum BankType); 4字节 整型
enum Chesskind = { BLACK =1 , WHITE , BLANK = 0 };
八、位段联合体
#include <stdio.h>
struct A {
unsigned char id : 4;
unsigned char level : 4;
};
int main() {
A a;
a.id = 0x8;
a.level = 0x9;
printf("%d", sizeof(a));
return 0;
}
位域可以是无名位域
#include <stdio.h>
#include <memory.h>
#include <iostream>
using namespace std;
struct A {
int a : 5;
int b : 3;
};
int main(void) {
char str[100] = "013456789afsadfsdlfjlsdjfl";
struct A d;
memcpy(&d, str, sizeof(A));
cout << d.a << endl;
cout << d.b << endl;
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <memory.h>
union Test {
int ip;
unsigned char arr[4];
};
int main() {
Test t = { 2148205375 };
char buff[50];
sprintf(buff, "%d.%d.%d.%d", t.arr[3], t.arr[2], t.arr[1], t.arr[0]);
printf("%s\n", buff);
return 0;
}