? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?C語言基礎
#define _CRT_SECURE_NO_WARNINGS //宏定義
//引入頭文件
//只有函數(shù)的聲明瑞筐,編譯時會去找到函數(shù)的實現(xiàn)
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
/*
void main(){
printf("hello world\n");
system("pause");
}
*/
//1.基本數(shù)據(jù)類型
//int short long float double char
/*
int %d
short %d
long %ld
float %f
double %lf
char %c
%x 十六進制
%o 八進制
%s 字符串
*/
/*
void main(){
int i = 1;
printf("%d\n",i);
float f = 23.3;
printf("%f\n",f);
//基本數(shù)據(jù)類型所占的字節(jié)數(shù)
printf("int占%d字節(jié)\n",sizeof(int));
printf("char占%d字節(jié)\n", sizeof(char));
printf("float占%d字節(jié)\n", sizeof(float));
//循環(huán)
int n = 0;
for (; n < 10; n++){
printf("%d\n",n);
}
//等待輸入
system("pause");
}
*/
//2.輸入輸出函數(shù)
/*
void main(){
int i;
printf("請輸入一個整數(shù):");
//賦值
scanf("%d",&i);? //控制臺輸入,&取地址符
//打印
printf("i的值為:%d\n",i);
system("pause");
}
*/
//指針
//指針存儲的是變量的內(nèi)存地址
//內(nèi)存地址肢础,系統(tǒng)給數(shù)據(jù)分配的編號(門牌號)
/*void main(){
int i = 90;
//指針變量,創(chuàng)建一個int類型的指針
int* p = &i; //p的值就是i這個變量的內(nèi)存地址
printf("%#x\n",p);
float f = 89.5f;
//創(chuàng)建一個float類型的指針
float *fp = &f;
printf("%#x\n", fp);
system("pause");
}*/
/*
void change(int* p){
*p = 300;
}
//變量名碌廓,對內(nèi)存空間上的一段數(shù)據(jù)的抽象
void main(){
int i = 90;
//i = 89;
//創(chuàng)建一個int類型的指針
int *p = &i;
//輸出地址
printf("p的地址:%#x\n",&p);
printf("i的地址:%#x\n",&i);
printf("i的值為:%d\n", i);
//間接賦值 i = 200;
//對p存的地址指向的變量進行操作
//*p = 200;
//change(p);
change(&i);? // int *p = &i;
printf("i的值為:%d\n",i);
system("pause");
}
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <math.h>
#include <time.h>
//1.指針為什么要有類型传轰?
//指針有類型,地址沒有類型
//地址只是開始的位置谷婆,類型讀取到什么位置結束
/*
void main(){
int i = 89;
//int 類型的指針
int *p = &i;
double j = 78.9;
//賦值為double類型變量的地址
p = &j;
printf("double size:%d\n", sizeof(double));
printf("%#x,%lf\n",p,*p); //想通過4字節(jié)讀取8字節(jié)變量的值路召,是不行的
getchar();
}
*/
//2.NULL空指針
/*
void main(){
int i = 9;
int *p = NULL;
//p = &i;
//空指針的默認值為0
printf("%#x\n",p);
//訪問內(nèi)存地址0x000000操作系統(tǒng)不允許
//p = 100; //操作系統(tǒng)不允許訪問
printf("%d\n",*p);
getchar();
}
*/
//3.多級指針(二級指針)
//指針保存的是變量的地址,保存的這個變量還可以是一個指針變量
//動態(tài)內(nèi)存分配給二維數(shù)組
/*
void main(){
int a = 50;
//p1上保存的a的地址
int* p1 = &a;
//p2上保存的p1的地址
int** p2 = &p1;
//int*** p3 = &p2;
printf("p1:%#x,p2:%#x\n",p1,p2);
**p2 = 90;
printf("%d\n",a);
getchar();
}
*/
//4.指針的運算
//指針的運算波材,一般在數(shù)組遍歷時才有意義,基于數(shù)組在內(nèi)存中線性排列的方式
/*
void main(){
//數(shù)組在內(nèi)存中連續(xù)存儲
int ids[] = { 78, 90, 23, 65, 19 };
//數(shù)組變量名:ids就是數(shù)組的首地址
printf("%#x\n",ids);
printf("%#x\n",&ids);
printf("%#x\n",&ids[0]);
//指針變量
int *p = ids;
printf("%d\n",*p);
//指針的加法
p++; //p++向前移動sizeof(數(shù)據(jù)類型)個字節(jié)
printf("p的值:%#x\n", p);
//p--;
printf("%d\n", *p);
getchar();
}
*/
//5.通過指針給數(shù)組賦值
/*
void main(){
int uids[5];
//高級寫法
//int i = 0;
//for (; i < 5; i++){
// uids[i] = i;
//}
//早些版本的寫法
int* p = uids;
printf("%#x\n",p);
int i = 0; //i是數(shù)組元素的值
for (; p < uids + 5; p++){
*p = i;
i++;
}
getchar();
}
*/
//6.函數(shù)指針
/*
int msg(char* msg,char* title){
MessageBox(0,msg,title,0);
return 0;
}
void main(){
//msg();
printf("%#x\n",msg);
printf("%#x\n",&msg);
//函數(shù)指針
//函數(shù)返回值類型身隐,函數(shù)指針的名稱廷区,函數(shù)的參數(shù)列表
int(*fun_p)(char* msg, char* title) = msg;
fun_p("消息內(nèi)容","標題");
getchar();
}
*/
int add(int a,int b){
return a + b;
}
int minus(int a,int b){
return a - b;
}
/*int div(int a, int b){
return a - b;
}*/
//msg函數(shù)需要傳遞一個函數(shù)指針參數(shù)
//類似于我們Java中的回調(diào)函數(shù)
/*
void msg(int(*func_p)(int a, int b), int m, int n){
printf("執(zhí)行一段代碼...\n");
printf("執(zhí)行回調(diào)函數(shù)...\n");
int r = func_p(m, n);
printf("執(zhí)行結果:%d\n",r);
}
void main(){
//加法
//int(*func_p)(int a, int b) = add;
msg(div, 10, 20);
//減法
//msg(minus,50,10);
getchar();
}
*/
/*
//案例:用隨機數(shù)生成一個數(shù)組,寫一個函數(shù)查找最小的值隙轻,并返回最小數(shù)的地址埠帕,在主函數(shù)中打印出來
int* getMinPointer(int ids[], int len){
int i = 0;
int* p = &ids[0];
for (; i < len; i++){
if (ids[i] < *p){
p = &ids[i];
}
}
return p;
}
void main(){
int ids[10];
int i = 0;
//初始化隨機數(shù)發(fā)生器,設置種子蚀瘸,種子不一樣狡蝶,隨機數(shù)才不一樣
//當前時間作為種子 有符號 int -xx - > +xx
srand((unsigned)time(NULL));
for (; i < 10; i++){
//100范圍內(nèi)
ids[i] = rand() % 100;
printf("%d\n", ids[i]);
}
int* p = getMinPointer(ids, sizeof(ids) / sizeof(int));
printf("%#x,%d\n",p,*p);
getchar();
}
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
//動態(tài)內(nèi)存分配
/*
void main(){
//40M
//stack overflow錯誤,棧溢出
//靜態(tài)內(nèi)存分配
int a[1024 * 1024 * 10];
//棧內(nèi)存
//C語言內(nèi)存分配:
//1.棧區(qū)(stack)
//windows下贮勃,棧內(nèi)存分配2M(確定的常數(shù))贪惹,超出了限制,提示stack overflow錯誤
//自動分配寂嘉,釋放
//2.堆區(qū)(heap)
//程序員手動分配釋放奏瞬,操作系統(tǒng)80%內(nèi)存
//3.全局區(qū)或靜態(tài)區(qū)
//4.字符常量區(qū)
//5.程序代碼區(qū)
getchar();
}
*/
/*
//棧內(nèi)存
void stackFun(){
int a[1024];
//棧內(nèi)存自動釋放
}
//堆內(nèi)存
void heapFun(){
//40M內(nèi)存
//字節(jié)
//void *任意類型的指針
int* p = malloc(1024 * 1024 * 10 * sizeof(int));
//釋放
free(p);
}
void main(){
//在堆內(nèi)存上,分配40M的內(nèi)存
while (1){
Sleep(1000);
stackFun();
}
getchar();
}
*/
//創(chuàng)建一個數(shù)組泉孩,動態(tài)指定數(shù)組的大小
//(在程序運行過長中硼端,可以隨意的開辟指定大小的內(nèi)存,以供使用棵譬,相當于Java中的集合)
//靜態(tài)內(nèi)存分配显蝌,分配內(nèi)存大小的是固定,問題:1.很容易超出棧內(nèi)存的最大值 2.為了防止內(nèi)存不夠用會開辟更多的內(nèi)存订咸,容易浪費內(nèi)存
//動態(tài)內(nèi)存分配曼尊,在程序運行過程中,動態(tài)指定需要使用的內(nèi)存大小脏嚷,手動釋放骆撇,釋放之后這些內(nèi)存還可以被重新使用(活水)
/*
void main(){
//靜態(tài)內(nèi)存分配創(chuàng)建數(shù)組,數(shù)組的大小是固定的
//int i = 10;
//int a[i];
int len;
printf("輸入數(shù)組的長度:");
scanf("%d",&len);
//開辟內(nèi)存父叙,大小len*4字節(jié)
int* p = malloc(len * sizeof(int));
//p是數(shù)組的首地址神郊,p就是數(shù)組的名稱
//給數(shù)組元素賦值(使用這一塊剛剛開辟出來的內(nèi)存區(qū)域)
int i = 0;
for (; i < len - 1; i++){
p[i] = rand() % 100;
printf("%d,%#x\n", p[i], &p[i]);
}
//手動釋放內(nèi)存
free(p);
getchar();
}
*/
//realloc 重新分配內(nèi)存
/*
void main(){
int len;
printf("第一次輸入數(shù)組的長度:");
scanf("%d", &len);
//int* p = malloc(len * sizeof(int));
int* p = calloc(len, sizeof(int));
int i = 0;
for (; i < len; i++){
p[i] = rand() % 100;
printf("%d,%#x\n", p[i], &p[i]);
}
int addLen;
printf("輸入數(shù)組增加的長度:");
scanf("%d", &addLen);
//內(nèi)存不夠用,擴大剛剛分配的內(nèi)存空間
//1.原來內(nèi)存的指針 2.內(nèi)存擴大之后的總大小
int* p2 = realloc(p, sizeof(int) * (len + addLen));
if (p2 == NULL){
printf("重新分配失敗趾唱,世界那么大涌乳,容不下我。甜癞。夕晓。");
}
//重新分配內(nèi)存的兩種情況:
//縮小,縮小的那一部分數(shù)據(jù)會丟失
//擴大悠咱,(連續(xù)的)
//1.如果當前內(nèi)存段后面有需要的內(nèi)存空間蒸辆,直接擴展這段內(nèi)存空間征炼,realloc返回原指針
//2.如果當前內(nèi)存段后面的空閑字節(jié)不夠,那么就使用堆中的第一個能夠滿足這一要求的內(nèi)存塊躬贡,將目前的數(shù)據(jù)復制到新的位置谆奥,并將原來的數(shù)據(jù)庫釋放掉,返回新的內(nèi)存地址
//3.如果申請失敗拂玻,返回NULL酸些,原來的指針仍然有效
//重新賦值
i = 0;
printf("--------------------------\n");
for (; i < len + addLen; i++){
p2[i] = rand() % 200;
printf("%d,%#x\n", p2[i], &p2[i]);
}
//手動釋放內(nèi)存
if (p != NULL){
free(p);
p = NULL;
}
if (p2 != NULL){
free(p2);
p2 = NULL;
}
getchar();
}
*/
//內(nèi)存分配的幾個注意細節(jié)
//1.不能多次釋放
//2.釋放完之后(指針仍然有值),給指針置NULL纺讲,標志釋放完成
//3.內(nèi)存泄露(p重新賦值之后擂仍,再free,并沒有真正釋放內(nèi)存)
/*
void main(){
int len;
printf("輸入數(shù)組的長度:");
scanf("%d", &len);
int* p = malloc(len * sizeof(int));
int i = 0;
for (; i < len; i++){
p[i] = rand() % 100;
printf("%d,%#x\n", p[i], &p[i]);
}
if (p != NULL){
free(p);
p = NULL;
}
getchar();
}
*/
void main(){
//40M
int* p1 = malloc(1024 * 1024 * 10 * sizeof(int));
free(p1);
p1 = NULL;
printf("%#x\n",p1);
//80M
p1 = malloc(1024 * 1024 * 10 * sizeof(int) * 2);
free(p1);
p1 = NULL;
getchar();
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
//使用字符數(shù)組存儲字符串
/*
void main(){
//char str[] = {'c','h','i','n','a','\0'};
//char str[6] = { 'c', 'h', 'i', 'n', 'a' };
char str[10] = "china";
//可以修改
str[0] = 's';
printf("%s\n",str);
printf("%#x\n", str);
getchar();
}
*/
//字符指針
/*
void main(){
//內(nèi)存連續(xù)排列
char *str = "how are you?";
//不可以修改
//str += 1;
//*str = 'y';
printf("%s\n", str);
printf("%#x\n", str);
//使用指針加法熬甚,截取字符串
str += 3;
while (*str){
printf("%c",*str);
str++;
}
printf("\n結束了");
getchar();
}
*/
//strcat字符串拼接函數(shù)
//在線API文檔:
//http://www.kuqin.com/clib/string/strcpy.html
/*
void main(void){
char dest[50];
char *a = "china";
char *b = " is powerful!";
strcpy(dest, a);
strcat(dest, b);
printf("%s\n", dest);
system("pause");
}
*/
//strchr在一個串中查找給定字符的第一個匹配之處
/*
void main(void){
char *str = "I want go to USA!";
printf("%#x\n", str);
//U元素的指針
//str+3
char* p = strchr(str, 'w');
if (p){
printf("索引位置:%d\n", p - str);
}
else{
printf("沒有找到");
}
system("pause");
}
*/
//strstr 從字符串haystack中尋找needle第一次出現(xiàn)的位置
void main(void){
char *haystack = "I want go to USA!";
char *needle = "to";
//U元素的指針
char* p = strstr(haystack, needle);
if (p){
printf("索引位置:%d\n", p - haystack);
}
else{
printf("沒有找到");
}
system("pause");
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <Windows.h>
//結構體是一種構造數(shù)據(jù)類型
//把不同的數(shù)據(jù)類型整合起來成為一個自定義的數(shù)據(jù)類型
/*
struct Man{
//成員
char name[20];
int age;
//int(*func)();
};
void main(){
//初始化結構體的變量
//1.
//struct Man m1 = {"Jack", 21};
//2.
struct Man m1;
m1.age = 23;
//m1.name = "Rose";
strcpy(m1.name,"rose");
sprintf(m1.name,"Jason");
//類似JavaScript字面量賦值逢渔,只能在變量聲明時賦值
//m1 = {};
struct Man m2 = m1;
printf("%s,%d\n",m1.name,m1.age);
getchar();
}
*/
//結構體的幾種寫法
/*
struct Man{
char name[20];
int age;
} m1, m2 = {"jack",20}; //m1結構體變量名
//typedef int Age;
void main(){
strcpy(m1.name,"Jack");
m1.age = 10;
printf("%s,%d\n", m2.name, m2.age);
getchar();
}
*/
//匿名結構體
//控制結構體變量的個數(shù)(限量版),相當于單例
/*
struct{
char name[20];
int age;
}m1;
*/
//結構體嵌套
/*
struct Teacher{
char name[20];
};
struct Student{
char name[20];
int age;
struct Teacher t;
};
void main(){
//字面量的方式
//struct Student s1 = { "jack", 21, {"Jason"} };
struct Student s1;
s1.age = 10;
strcpy(s1.t.name, "Jason");
system("pause");
}
*/
//結構體嵌套2
/*
struct Student{
char name[20];
int age;
struct Teacher{
char name[20];
} t;
};
void main(){
struct Student s1;
strcpy(s1.t.name, "Jason");
//struct Teacher t;
system("pause");
}
*/
//結構體與指針
/*
struct Man{
char name[20];
int age;
};
void main(){
struct Man m1 = {"Jack",30};
//結構體指針
struct Man *p = &m1;
printf("%s,%d\n", m1.name, m1.age);
printf("%s,%d\n",(*p).name,(*p).age);
//“->”(箭頭)是“(*p).”簡寫形式
printf("%s,%d\n", p->name, p->age);
//(*env)->
system("pause");
}
*/
//指針與結構體數(shù)組
/*
struct Man{
char name[20];
int age;
};
void main(){
struct Man mans[] = { {"Jack",20}, {"Rose", 19} };
//遍歷結構體數(shù)組
//1.
struct Man *p = mans;
for (; p < mans + 2; p++){
printf("%s,%d\n", p->name, p->age);
}
//2.
int i = 0;
for (; i < sizeof(mans) / sizeof(struct Man); i++){
printf("%s,%d\n", mans[i].name, mans[i].age);
}
//(*env)->
system("pause");
}
*/
//結構體的大邢缋ā(字節(jié)對齊)
/*
struct Man{
int age;
double weight;
};
void main(){
//結構體變量的大小肃廓,必須是最寬基本數(shù)據(jù)類型的整數(shù)倍
//提升讀取的效率
struct Man m1 = {20,89.0};
printf("%#x,%d\n", &m1,sizeof(m1));
getchar();
}
*/
struct Man{
char *name;
int age;
};
//結構體與動態(tài)內(nèi)存分配
/*
void main(){
struct Man *m_p = (struct Man*)malloc(sizeof(struct Man) * 10);
struct Man *p = m_p;
//賦值
p->name = "Jack";
p->age = 20;
p++;
p->name = "Rose";
p->age = 20;
struct Man *loop_p = m_p;
for (; loop_p < m_p + 2; loop_p++){
printf("%s,%d\n", loop_p->name, loop_p->age);
}
free(m_p);
getchar();
}
*/
/*
//typedef 類型取別名
//1.不同名稱代表在干不同的事情typedef int jint;?
//2.不同情況下,使用不同的別名
//#if defined(__cplusplus)
//typedef _JNIEnv JNIEnv;
//typedef _JavaVM JavaVM;
//3.書寫簡潔
struct Man{
char name[20];
int age;
};
//Age int類型的別名
typedef int Age;
//Age int類型指針的別名
typedef int* Ap;
typedef struct Man JavaMan;
typedef struct Man* JM;
//結構體取別名
//typedef struct Woman W;
//typedef struct Woman* WP;
//簡寫
typedef struct Woman{
char name[20];
int age;
} W, *WP;? //W 是woman結構體的別名, WP 是woman結構體指針的別名
void main(){
int i = 5;
Ap p = &i;
//結構體變量
W w1 = {"Rose",20};
//結構體指針
WP wp1 = &w1;
printf("%s,%d\n", w1.name, w1.age);
printf("%s,%d\n", wp1->name, wp1->age);
getchar();
}
*/
//結構體函數(shù)指針成員
/*
struct Girl{
char *name;
int age;
//函數(shù)指針
void(*sayHi)(char*);
};
//Girl結構體類似于Java中的類诲泌,name和age類似于屬性盲赊,sayHi類似于方法
void sayHi(char* text){
MessageBoxA(0, text, "title", 0);
}
void main(){
struct Girl g1;
g1.name = "Lucy";
g1.age = 18;
g1.sayHi = sayHi;
g1.sayHi("hello");
getchar();
}
*/
typedef struct Girl{
char *name;
int age;
//函數(shù)指針
void(*sayHi)(char*);
}Girl;
//Girl結構體指針取別名GirlP
typedef Girl* GirlP;
void sayHi(char* text){
MessageBoxA(0, text, "title", 0);
}
//改名
void rename(GirlP gp1){
gp1->name = "Lily";
}
void main(){
Girl g1 = { "Lucy", 18, sayHi };
GirlP gp1 = &g1;
gp1->sayHi("Byebye!");
//傳遞指針,改名
rename(gp1);
getchar();
}
#include "stdlib.h"
#include "stdio.h"
//聯(lián)合體(共用體)
//不同類型的變量共同占用一段內(nèi)存(相互覆蓋)敷扫,聯(lián)合變量任何時刻只有一個成員存在哀蘑,節(jié)省內(nèi)存
//聯(lián)合體變量的大小=最大的成員所占的字節(jié)數(shù)
//比喻:同穿一條褲子
/*
union? MyValue{
int x;
int y;
double z;
};
void main(){
union MyValue d1;
d1.x = 90;
d1.y = 100; //最后一次賦值有效
//d1.z = 23.8;
printf("%d,%d,%lf\n",d1.x,d1.y,d1.z);
system("pause");
}
*/
/*
typedef union jvalue {
jboolean? ? z;
jbyte? ? ? b;
jchar? ? ? c;
jshort? ? ? s;
jint? ? ? ? i;
jlong? ? ? j;
jfloat? ? ? f;
jdouble? ? d;
jobject? ? l;
} jvalue;
*/
//枚舉(列舉所有的情況)
//限定值,保證取值的安全性
//enumeration
//enum Day
//{
// Monday = 0,
// Tuesday = 1,
// Wednesday = 2,
// Thursday = 3,
// Friday = 4,
// Saturday = 5,
// Sunday = 6
//};
/*
enum Day
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
void main(){
//枚舉的值葵第,必須是括號中的值
enum Day d = Monday;
printf("%#x,%d\n",&d,d);
getchar();
}
*/
#define _CRT_SECURE_NO_WARNINGS
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
//讀取文本文件
/*
void main(){
char *path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\friends.txt";
//打開
FILE *fp = fopen(path,"r");
if (fp == NULL){
printf("文件打開失敗...");
return;
}
//讀取
char buff[50]; //緩沖
while (fgets(buff,50,fp)){
printf("%s",buff);
}
//關閉
fclose(fp);
system("pause");
getchar();
}
*/
//寫入文本文件
/*
void main(){
char *path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\friends_new.txt";
//打開
FILE *fp = fopen(path, "w");
char *text = "hchmily@sina.com,程華才,學清路 8\n號科技財富中心 A";
fputs(text,fp);
//關閉流
fclose(fp);
getchar();
}
*/
//計算機的文件存儲在物理上都是二進制
//文本文件和二進制之分绘迁,其實是一個邏輯之分
//C讀寫文本文件與二進制文件的差別僅僅體現(xiàn)在回車換行符
//寫文本時,每遇到一個'\n'卒密,會將其轉(zhuǎn)換成'\r\n'(回車換行)
//讀文本時缀台,每遇到一個'\r\n',會將其轉(zhuǎn)換成'\n'
//文件復制
/*
void main(){
char *read_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\liuyan.png";
char *write_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\liuyan_new.png";
//讀的文件 b字符表示操作二進制文件binary
FILE *read_fp = fopen(read_path, "rb");
//寫的文件
FILE *write_fp = fopen(write_path, "wb");
//復制
int buff[50]; //緩沖區(qū)域
int len = 0; //每次讀到的數(shù)據(jù)長度
while ((len = fread(buff, sizeof(int), 50, read_fp)) != 0){
//將讀到的內(nèi)容寫入新的文件
fwrite(buff,sizeof(int),len,write_fp);
}
//關閉流
fclose(read_fp);
fclose(write_fp);
getchar();
}
*/
//獲取文件的大小
/*
void main(){
char *read_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\liuyan.png";
FILE *fp = fopen(read_path, "r");
//重新定位文件指針
//SEEK_END文件末尾哮奇,0偏移量
fseek(fp,0,SEEK_END);
//返回當前的文件指針桩卵,相對于文件開頭的位移量
long filesize = ftell(fp);
printf("%d\n",filesize);
getchar();
}
*/
//練習:文本文件加解密
/*
//異或
//規(guī)則:1^1=0, 0^0=0, 1^0=1, 0^1=1 同為0筏餐,不同為1
//加密
void crpypt(char normal_path[],char crypt_path[]){
//打開文件
FILE *normal_fp = fopen(normal_path, "r");
FILE *crypt_fp = fopen(crypt_path, "w");
//一次讀取一個字符
int ch;
while ((ch = fgetc(normal_fp)) != EOF){ //End of File
//寫入(異或運算)
fputc(ch ^ 9,crypt_fp);
}
//關閉
fclose(crypt_fp);
fclose(normal_fp);
}
//解密
void decrpypt(char crypt_path[], char decrypt_path[]){
//打開文件
FILE *normal_fp = fopen(crypt_path, "r");
FILE *crypt_fp = fopen(decrypt_path, "w");
//一次讀取一個字符
int ch;
while ((ch = fgetc(normal_fp)) != EOF){ //End of File
//寫入(異或運算)
fputc(ch ^ 9, crypt_fp);
}
//關閉
fclose(crypt_fp);
fclose(normal_fp);
}
void main(){
char *normal_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\friends.txt";
char *crypt_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\friends_crypt.txt";
char *decrypt_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\friends_decrypt.txt";
//crpypt(normal_path, crypt_path);
//解密
decrpypt(crypt_path, decrypt_path);
getchar();
}
*/
//二進制文件加解密
//讀取二進制文件中的數(shù)據(jù)時,一個一個字符讀取
//密碼:ilovely
/*
void crpypt(char normal_path[], char crypt_path[],char password[]){
//打開文件
FILE *normal_fp = fopen(normal_path, "rb");
FILE *crypt_fp = fopen(crypt_path, "wb");
//一次讀取一個字符
int ch;
int i = 0; //循環(huán)使用密碼中的字母進行異或運算
int pwd_len = strlen(password); //密碼的長度
while ((ch = fgetc(normal_fp)) != EOF){ //End of File
//寫入(異或運算)
fputc(ch ^ password[i % pwd_len], crypt_fp);
i++;
}
//關閉
fclose(crypt_fp);
fclose(normal_fp);
}
//解密
void decrpypt(char crypt_path[], char decrypt_path[],char password[]){
//打開文件
FILE *normal_fp = fopen(crypt_path, "rb");
FILE *crypt_fp = fopen(decrypt_path, "wb");
//一次讀取一個字符
int ch;
int i = 0; //循環(huán)使用密碼中的字母進行異或運算
int pwd_len = strlen(password); //密碼的長度
while ((ch = fgetc(normal_fp)) != EOF){ //End of File
//寫入(異或運算)
fputc(ch ^ password[i % pwd_len], crypt_fp);
i++;
}
//關閉
fclose(crypt_fp);
fclose(normal_fp);
}
void main(){
char *normal_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\liuyan.png";
char *crypt_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\liuyan_crypt.png";
char *decrypt_path = "E:\\dongnao\\vip\\ndk\\08_08_C_05\\files\\liuyan_decrypt.png";
//crpypt(normal_path, crypt_path,"iloveqq");
//解密
decrpypt(crypt_path, decrypt_path,"iloveqq");
getchar();
}
*/
//作業(yè):文件的分割以及合并
#include <stdlib.h>
#include <stdio.h>
#include "A.h"
void printfA(){
printf("print A");
}
//C語言執(zhí)行的流程
//編譯:形成目標代碼(.obj)
//連接:將目標代碼與C函數(shù)庫連接合并绣檬,形成最終的可執(zhí)行文件
//執(zhí)行
//預編譯(預處理)变过,為編譯做準備工作吏够,完成代碼文本的替換工作
//C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include
//C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\crt\src
//頭文件告訴編譯器有這樣一個函數(shù)可都,連接器負責找到這個函數(shù)的實現(xiàn)
//宏定義耕姊、宏替換、預編譯指令
//define指令
//1.定義標示
//#ifdef __cplusplus 標識支持C++語法
//防止文件重復引入
//2.定義常數(shù)(便于修改與閱讀)
#define MAX 100
//int MIN = 40;
//3.定義“宏函數(shù)”
void dn_com_jni_read(){
printf("read\n");
}
void dn_com_jni_write(){
printf("write\n");
}
//NAME是參數(shù)
#define jni(NAME) dn_com_jni_##NAME();
//webrtc JNI函數(shù)名稱很長,也是JOW宏函數(shù)縮短函數(shù)名稱
//日志輸出
//__VA_ARGS__可變參數(shù)
//#define LOG(FORMAT,...) printf(##FORMAT,__VA_ARGS__);
////日志會有級別
//#define LOG_I(FORMAT,...) printf("INFO:"); printf(##FORMAT,__VA_ARGS__);
//#define LOG_E(FORMAT,...) printf("ERRO:"); printf(##FORMAT,__VA_ARGS__);
//升級版本
#define LOG(LEVEL,FORMAT,...) printf(##LEVEL); printf(##FORMAT,__VA_ARGS__);
#define LOG_I(FORMAT,...) LOG("INFO:",##FORMAT,__VA_ARGS__);
#define LOG_E(FORMAT,...) LOG("ERROR:",##FORMAT,__VA_ARGS__);
#define LOG_W(FORMAT,...) LOG("WARN:",##FORMAT,__VA_ARGS__);
//Android
//#define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"jason",FORMAT,##__VA_ARGS__);
//LOGI("%s","fix");
//替換
//__android_log_print(ANDROID_LOG_INFO, "jason", "%s", "fix");
void main(){
//#include "my.txt"
//printf("%s\n", "I am a little boy!");
//printfA();
int i = 90;
if (i < MAX){
printf("比MAX小..");
}
jni(write);//替換:dn_com_jni_write();
LOG_E("%s%d","大形蠹:",89);
//替換成:printf("INFO:"); printf("%s%d","大小:",89);
//LOG_I
getchar();
}