Initial commit: C language learning code
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
# 排除可执行文件
|
||||
*.exe
|
||||
*.out
|
||||
*.o
|
||||
|
||||
# 排除IDE配置文件
|
||||
.idea/
|
||||
.vscode/
|
||||
.vs/
|
||||
|
||||
# 排除Trae AI配置文件
|
||||
.trae/
|
||||
|
||||
# 排除编译输出目录
|
||||
output/
|
||||
|
||||
# 排除临时文件
|
||||
*.tmp
|
||||
*.temp
|
||||
|
||||
# 排除系统文件
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# 排除竞赛平台配置文件
|
||||
.cph/
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
int n=0;
|
||||
|
||||
scanf("%d", &x);
|
||||
|
||||
n++;
|
||||
x=x/10;
|
||||
while(x>10)
|
||||
{
|
||||
n++;
|
||||
x=x/10;
|
||||
}
|
||||
printf("%d\n",n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int number;
|
||||
int sum = 0;
|
||||
int count = 0;
|
||||
|
||||
printf("请输入数字\n");
|
||||
scanf("%d", &number);
|
||||
while(number!=-1)
|
||||
{
|
||||
sum = sum+number;
|
||||
count = count+1;
|
||||
printf("请输入数字\n");
|
||||
scanf("%d", &number);
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
printf("平均数是%.2f\n", (float)sum / count);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("未输入有效数字\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
srand(time(0));
|
||||
int number = rand()%100+1;
|
||||
int count = 0;
|
||||
int a = 0;
|
||||
printf("我已经想好了一个1到100之间的数。");
|
||||
|
||||
do
|
||||
{
|
||||
printf("请猜这个1到100之间的数:");
|
||||
scanf("%d", &a);
|
||||
count = count+1;
|
||||
|
||||
if(a>number)
|
||||
{
|
||||
printf("你猜的数大了。");
|
||||
}
|
||||
else if(a<number)
|
||||
{
|
||||
printf("你猜的数小了。");
|
||||
}
|
||||
}
|
||||
while(a != number);
|
||||
|
||||
printf("太好了,你用了%d次就猜对了。\n", count);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
scanf("%d", &x);
|
||||
|
||||
int number;
|
||||
int renum=0;
|
||||
|
||||
while(x>0)
|
||||
{
|
||||
number = x%10;
|
||||
printf("%d", number);
|
||||
renum = renum*10+number;
|
||||
printf("x=%d, number=%d,renum=%d\n", x, number, renum);
|
||||
x/=10;
|
||||
}
|
||||
printf("%d", renum);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !二进制文件
|
||||
/*
|
||||
1.二进制文件
|
||||
其实所有的文件最终都是二进制的文本文件无非是用最简单的方式可以读写的文件
|
||||
more、tail
|
||||
cat
|
||||
vi
|
||||
而二进制文件是需要专门的程序来读写的文件文本文件的输入输出是格式化,可能经过转码
|
||||
2.文本 vs 二进制
|
||||
Unix喜欢用文本文件来做数据存储和程序配置
|
||||
交互式终端的出现使得人们喜欢用文本和计算机“talk"
|
||||
Unix的shel提供了一些读写文本的小程序
|
||||
Windows喜欢用二进制文件
|
||||
DOS是草根文化,并不继承和熟悉Unix文化
|
||||
PC刚开始的时候能力有限,DOS的能力更有限,二进制更接近底层
|
||||
3.文本 vs 二进制
|
||||
文本的优势是方便人类读写,而且跨平台
|
||||
文本的缺点是程序输入输出要经过格式化,开销大
|
||||
二进制的缺点是人类读写困难,而且不跨平台
|
||||
int的大小不一致,大小端的问题.
|
||||
二进制的优点是程序读写快
|
||||
4.程序为什么要文件
|
||||
配置Unix用文本,Windows用注册表
|
||||
数据稍微有点量的数据都放数据库了
|
||||
媒体这个只能是二进制的
|
||||
现实是,程序通过第三方库来读写文件,很少直接读写二进制文件了
|
||||
5.为什么nitem?
|
||||
因为二进制文件的读写一般都是通过对一个结构变量的操作来进行的
|
||||
于是nitem就是用来说明这次读写几个结构变量!
|
||||
6.在文件中定位
|
||||
long ftell(FILE *stream);
|
||||
int fseek(FILE *stream, long offset, int whence);
|
||||
SEEK_SET:从头开始
|
||||
SEEK_CUR:从当前位置开始
|
||||
SEEK_END:从尾开始(倒过来)
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !文件输入输出
|
||||
/*
|
||||
1.FILE
|
||||
FILE* fopen(const char * restrict path, const char* restrict mode);
|
||||
int fclose(FILE *stream);
|
||||
fscanf(FILE*....)
|
||||
fprintf(FILE*....)
|
||||
2.打开文件的标准代码
|
||||
FILE*fp = fopen("file","r");
|
||||
if(fp){
|
||||
fscanf(fp....);
|
||||
fclose(fp);
|
||||
}else{
|
||||
printf("打开文件失败");
|
||||
}
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
FILE *fp = fopen("test.txt", "r");
|
||||
if (fp)
|
||||
{
|
||||
int num;
|
||||
fscanf(fp,"%d", &num);
|
||||
printf("%d\n", num);
|
||||
fclose(fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("打开文件失败\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !格式化输入输出
|
||||
/*
|
||||
1.格式化的输入输出
|
||||
printf
|
||||
%[flags][width][.prec][hlL]type
|
||||
scanf
|
||||
%[flag]type
|
||||
2.printf和scanf的返回值
|
||||
读入的项目数
|
||||
输出的字符数
|
||||
在要求严格的程序中,应该判断每次调用scanf或print的返回值,从而了解程序运行中是否存在问题
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
printf("%+9d\n", 123);
|
||||
printf("%+9d\n", -123);
|
||||
printf("%-9d\n", 123);
|
||||
|
||||
printf("%+09d\n", 123);
|
||||
printf("%+09d\n", -123);
|
||||
|
||||
int n;
|
||||
printf("%dty%n\n", 12345, &n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !位运算的例子
|
||||
/*
|
||||
1.C有这些按位运算的运算符:
|
||||
.& 按位的与
|
||||
.1 按位的或
|
||||
.~ 按位取反
|
||||
.^ 按位的异或
|
||||
.<< 左移
|
||||
.>> 右移
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !位运算的例子
|
||||
/*
|
||||
1.C有这些按位运算的运算符:
|
||||
.& 按位的与
|
||||
.1 按位的或
|
||||
.~ 按位取反
|
||||
.^ 按位的异或
|
||||
.<< 左移
|
||||
.>> 右移
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !按位运算
|
||||
/*
|
||||
1.C有这些按位运算的运算符:
|
||||
.& 按位的与
|
||||
.1 按位的或
|
||||
.~ 按位取反
|
||||
.^ 按位的异或
|
||||
.<< 左移
|
||||
.>> 右移
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !移位运算
|
||||
/*
|
||||
1.C有这些按位运算的运算符:
|
||||
.& 按位的与
|
||||
.1 按位的或
|
||||
.~ 按位取反
|
||||
.^ 按位的异或
|
||||
.<< 左移
|
||||
.>> 右移
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !可变数组
|
||||
/*
|
||||
1.可变数组
|
||||
*/
|
||||
|
||||
typedef struct{
|
||||
int *array;
|
||||
int size;
|
||||
} Array;
|
||||
|
||||
Array array_create(int init_size)
|
||||
{
|
||||
Array a;
|
||||
a.size = init_size;
|
||||
a.array = (int *)malloc(sizeof(int) * a.size);
|
||||
return a;
|
||||
}
|
||||
|
||||
void array_free(Array *a)
|
||||
{
|
||||
free(a->array);
|
||||
a->array = NULL;
|
||||
a->size = 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
Array a = array_create(100);
|
||||
array_free(&a);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !可变数组的数据访问
|
||||
/*
|
||||
1.可变数组
|
||||
*/
|
||||
|
||||
typedef struct{
|
||||
int *array;
|
||||
int size;
|
||||
} Array;
|
||||
|
||||
Array array_create(int init_size)
|
||||
{
|
||||
Array a;
|
||||
a.size = init_size;
|
||||
a.array = (int *)malloc(sizeof(int) * a.size);
|
||||
return a;
|
||||
}
|
||||
|
||||
void array_free(Array *a)
|
||||
{
|
||||
free(a->array);
|
||||
a->array = NULL;
|
||||
a->size = 0;
|
||||
}
|
||||
|
||||
// 封装
|
||||
int array_size(const Array *a)
|
||||
{
|
||||
return a->size;
|
||||
}
|
||||
|
||||
int* array_at(Array *a, int index)
|
||||
{
|
||||
if (index >= a->size)
|
||||
{
|
||||
a->size = index + 1;
|
||||
a->array = (int *)realloc(a->array, sizeof(int) * a->size);
|
||||
}
|
||||
return &a->array[index];
|
||||
}
|
||||
|
||||
int array_get(const Array *a, int index)
|
||||
{
|
||||
return a->array[index];
|
||||
}
|
||||
|
||||
void array_set(Array *a, int index, int value)
|
||||
{
|
||||
a->array[index] = value;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
Array a = array_create(100);
|
||||
printf("%d\n", array_size(&a));
|
||||
|
||||
*array_at(&a, 0) = 10;
|
||||
printf("%d\n", *array_at(&a, 0));
|
||||
array_free(&a);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !可变数组的缺陷
|
||||
/*
|
||||
1.可变数组
|
||||
*/
|
||||
|
||||
const BLOCK_SIZE = 20;
|
||||
|
||||
typedef struct{
|
||||
int *array;
|
||||
int size;
|
||||
} Array;
|
||||
|
||||
Array array_create(int init_size)
|
||||
{
|
||||
Array a;
|
||||
a.size = init_size;
|
||||
a.array = (int *)malloc(sizeof(int) * a.size);
|
||||
return a;
|
||||
}
|
||||
|
||||
void array_free(Array *a)
|
||||
{
|
||||
free(a->array);
|
||||
a->array = NULL;
|
||||
a->size = 0;
|
||||
}
|
||||
|
||||
// 封装
|
||||
int array_size(const Array *a)
|
||||
{
|
||||
return a->size;
|
||||
}
|
||||
|
||||
int* array_at(Array *a, int index)
|
||||
{
|
||||
if (index >= a->size)
|
||||
{
|
||||
array_inflate(a, (index/BLOCK_SIZE+1)*BLOCK_SIZE - a->size);
|
||||
}
|
||||
return &a->array[index];
|
||||
}
|
||||
|
||||
int array_get(const Array *a, int index)
|
||||
{
|
||||
return a->array[index];
|
||||
}
|
||||
|
||||
void array_set(Array *a, int index, int value)
|
||||
{
|
||||
a->array[index] = value;
|
||||
}
|
||||
|
||||
void array_inflate(Array *a, int more_size)
|
||||
{
|
||||
int *p = (int*)malloc(sizeof(int)*(a->size + more_size));
|
||||
int i;
|
||||
memccpy(p, a->array, sizeof(int)*a->size, a->size);
|
||||
free(a->array);
|
||||
a->array = p;
|
||||
a->size =+ more_size;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
Array a = array_create(100);
|
||||
printf("%d\n", array_size(&a));
|
||||
|
||||
*array_at(&a, 0) = 10;
|
||||
printf("%d\n", *array_at(&a, 0));
|
||||
int number;
|
||||
int cnt = 0;
|
||||
while(number!=-1)
|
||||
{
|
||||
scanf("%d", &number);
|
||||
if (number!=-1)
|
||||
{
|
||||
*array_at(&a, cnt) = number;
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
array_free(&a);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !可变数组的自动增长
|
||||
/*
|
||||
1.可变数组
|
||||
*/
|
||||
|
||||
const BLOCK_SIZE = 20;
|
||||
|
||||
typedef struct{
|
||||
int *array;
|
||||
int size;
|
||||
} Array;
|
||||
|
||||
Array array_create(int init_size)
|
||||
{
|
||||
Array a;
|
||||
a.size = init_size;
|
||||
a.array = (int *)malloc(sizeof(int) * a.size);
|
||||
return a;
|
||||
}
|
||||
|
||||
void array_free(Array *a)
|
||||
{
|
||||
free(a->array);
|
||||
a->array = NULL;
|
||||
a->size = 0;
|
||||
}
|
||||
|
||||
// 封装
|
||||
int array_size(const Array *a)
|
||||
{
|
||||
return a->size;
|
||||
}
|
||||
|
||||
int* array_at(Array *a, int index)
|
||||
{
|
||||
if (index >= a->size)
|
||||
{
|
||||
array_inflate(a, (index/BLOCK_SIZE+1)*BLOCK_SIZE - a->size);
|
||||
}
|
||||
return &a->array[index];
|
||||
}
|
||||
|
||||
int array_get(const Array *a, int index)
|
||||
{
|
||||
return a->array[index];
|
||||
}
|
||||
|
||||
void array_set(Array *a, int index, int value)
|
||||
{
|
||||
a->array[index] = value;
|
||||
}
|
||||
|
||||
void array_inflate(Array *a, int more_size)
|
||||
{
|
||||
int *p = (int*)malloc(sizeof(int)*(a->size + more_size));
|
||||
int i;
|
||||
memccpy(p, a->array, sizeof(int)*a->size, a->size);
|
||||
free(a->array);
|
||||
a->array = p;
|
||||
a->size =+ more_size;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
Array a = array_create(100);
|
||||
printf("%d\n", array_size(&a));
|
||||
|
||||
*array_at(&a, 0) = 10;
|
||||
printf("%d\n", *array_at(&a, 0));
|
||||
int number;
|
||||
int cnt = 0;
|
||||
while(number!=-1)
|
||||
{
|
||||
scanf("%d", &number);
|
||||
if (number!=-1)
|
||||
{
|
||||
*array_at(&a, cnt) = number;
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
array_free(&a);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !链表
|
||||
/*
|
||||
1.
|
||||
*/
|
||||
|
||||
typedef struct _node{
|
||||
int value;
|
||||
struct _node *next;
|
||||
} Node;
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
Node *head = NULL;
|
||||
int number;
|
||||
do {
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &number);
|
||||
if (number!=-1)
|
||||
{
|
||||
// 创建一个节点
|
||||
Node *p=(Node*)malloc(sizeof(Node));
|
||||
p->value = number;
|
||||
p->next = NULL;
|
||||
// 将节点插入链表
|
||||
Node *last = head;
|
||||
if (last!=NULL)
|
||||
{
|
||||
while (last->next!=NULL)
|
||||
{
|
||||
last = last->next;
|
||||
}
|
||||
// attach
|
||||
last->next = p;
|
||||
}
|
||||
else
|
||||
{
|
||||
head = p;
|
||||
}
|
||||
}
|
||||
} while (number!=-1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !链表
|
||||
/*
|
||||
1.
|
||||
*/
|
||||
|
||||
typedef struct _node{
|
||||
int value;
|
||||
struct _node *next;
|
||||
} Node;
|
||||
|
||||
typedef struct _list{
|
||||
Node *head;
|
||||
Node *tail;
|
||||
} List;
|
||||
|
||||
void add(List* pList, int value)
|
||||
{
|
||||
Node *p=(Node*)malloc(sizeof(Node));
|
||||
p->value = value;
|
||||
p->next = NULL;
|
||||
// 将节点插入链表
|
||||
Node *last = pList->head;
|
||||
if (last!=NULL)
|
||||
{
|
||||
while (last->next!=NULL)
|
||||
{
|
||||
last = last->next;
|
||||
}
|
||||
// attach
|
||||
last->next = p;
|
||||
}
|
||||
else
|
||||
{
|
||||
pList->head = p;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
List list;
|
||||
list.head = NULL;
|
||||
list.tail = NULL;
|
||||
int number;
|
||||
do {
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &number);
|
||||
if (number!=-1)
|
||||
{
|
||||
|
||||
add(&list, number);
|
||||
|
||||
}
|
||||
} while (number!=-1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// !链表
|
||||
/*
|
||||
1.
|
||||
*/
|
||||
|
||||
typedef struct _node{
|
||||
int value;
|
||||
struct _node *next;
|
||||
} Node;
|
||||
|
||||
typedef struct _list{
|
||||
Node *head;
|
||||
Node *tail;
|
||||
} List;
|
||||
|
||||
void add(List* pList, int value)
|
||||
{
|
||||
Node *p=(Node*)malloc(sizeof(Node));
|
||||
p->value = value;
|
||||
p->next = NULL;
|
||||
// 将节点插入链表
|
||||
Node *last = pList->head;
|
||||
if (last!=NULL)
|
||||
{
|
||||
while (last->next!=NULL)
|
||||
{
|
||||
last = last->next;
|
||||
}
|
||||
// attach
|
||||
last->next = p;
|
||||
}
|
||||
else
|
||||
{
|
||||
pList->head = p;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
#endif
|
||||
|
||||
List list;
|
||||
list.head = NULL;
|
||||
list.tail = NULL;
|
||||
int number;
|
||||
do {
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &number);
|
||||
if (number!=-1)
|
||||
{
|
||||
|
||||
add(&list, number);
|
||||
|
||||
}
|
||||
} while (number!=-1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
int n=0;
|
||||
int one,two,five;
|
||||
|
||||
printf("请输入钱的数额:");
|
||||
scanf("%d", &x);
|
||||
|
||||
for(one=0;one<=x*10;one++)
|
||||
{
|
||||
for(two=0;two<=x*5;two++)
|
||||
{
|
||||
for(five=0;five<=x*2;five++)
|
||||
{
|
||||
if(one*1 + two*2 + five*5 == x*10)
|
||||
{
|
||||
n +=1;
|
||||
printf("%d元的钱可以用%d个1角、%d个2角和%d个5角换\n", x, one, two, five);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("一共有%d的可能性", n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
int one,two,five;
|
||||
|
||||
printf("请输入钱的数额:");
|
||||
scanf("%d", &x);
|
||||
|
||||
for(one=0;one<=x*10;one++)
|
||||
{
|
||||
for(two=0;two<=x*5;two++)
|
||||
{
|
||||
for(five=0;five<=x*2;five++)
|
||||
{
|
||||
if(one*1 + two*2 + five*5 == x*10)
|
||||
{
|
||||
printf("%d元的钱可以用%d个1角、%d个2角和%d个5角换\n", x, one, two, five);
|
||||
goto out; //多层嵌套的循环的内层直接结束跳到最外层的out:
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out:
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
|
||||
int a=1;
|
||||
int b=1;
|
||||
|
||||
while(b<=n)
|
||||
{
|
||||
a=a*b;
|
||||
b=b+1;
|
||||
}
|
||||
printf("n!=%d\n", a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
|
||||
int a=1;
|
||||
int b=n;
|
||||
|
||||
for(n=n;n>1;n-- )
|
||||
{
|
||||
a=a*n;
|
||||
}
|
||||
printf("%d!=%d\n", b, a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
|
||||
int a=1;
|
||||
int b=n;
|
||||
|
||||
for(n=n;n>1;n-- )
|
||||
{
|
||||
a=a*n;
|
||||
}
|
||||
printf("%d!=%d\n", b, a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int x;
|
||||
double f=0.0; //f不是整数,是小数,用double表示
|
||||
|
||||
printf("请输入数字:");
|
||||
scanf("%d", &n);
|
||||
|
||||
for (x=1 ; x<=n; x++)
|
||||
{
|
||||
f = f+(1.0/x); //1是整数,不是小数,用1.0表示小数的运算
|
||||
}
|
||||
|
||||
printf("f(%d)=%f\n", n, f); //f不是整数,是小数,用%f表示浮点数
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int m;
|
||||
int x;
|
||||
double f=0.0; //f不是整数,是小数,用double表示
|
||||
|
||||
printf("请输入数字:");
|
||||
scanf("%d", &n);
|
||||
|
||||
for (x=1 ; x<=n; x++)
|
||||
{
|
||||
m = 1;
|
||||
if (x%2==0)
|
||||
{
|
||||
m = -1;
|
||||
}
|
||||
f = f+(1.0/x)*m;
|
||||
}
|
||||
|
||||
printf("f(%d)=%f\n", n, f); //f不是整数,是小数,用%f表示浮点数
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int m=1;
|
||||
int x;
|
||||
double f=0.0; //f不是整数,是小数,用double表示
|
||||
|
||||
printf("请输入数字:");
|
||||
scanf("%d", &n);
|
||||
|
||||
for (x=1 ; x<=n; x++)
|
||||
{
|
||||
f = f+(1.0/x)*m;
|
||||
m = -1;
|
||||
}
|
||||
|
||||
printf("f(%d)=%f\n", n, f); //f不是整数,是小数,用%f表示浮点数
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int gcd(int a,int b)
|
||||
{
|
||||
int c;
|
||||
while(c>0)
|
||||
{
|
||||
c = a % b;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
int lcm(int a, int b)
|
||||
{
|
||||
return (a / gcd(a, b)) * b;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int a;
|
||||
int b;
|
||||
printf("请输入两个整数:");
|
||||
scanf("%d %d", &a, &b);
|
||||
|
||||
if (a<0 || b<0)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d和%d的最大公约数是:%d\n", a, b, gcd(a, b));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int num_small(int a,int b)
|
||||
{
|
||||
int n = a;
|
||||
if (a>b)
|
||||
{
|
||||
n = b;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int gcd=0;
|
||||
int a;
|
||||
int b;
|
||||
int n;
|
||||
printf("请输入两个整数:");
|
||||
scanf("%d %d", &a, &b);
|
||||
|
||||
if (a<0 || b<0)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = num_small(a,b);
|
||||
|
||||
for (gcd=n;gcd>=1;gcd--)
|
||||
{
|
||||
if (a%gcd==0 && b%gcd==0)
|
||||
{
|
||||
printf("最大公约数是:%d\n", gcd);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int gcd(int a,int b) //最大公约数
|
||||
{
|
||||
int gcd = a;
|
||||
if (a>b)
|
||||
{
|
||||
gcd = b;
|
||||
}
|
||||
for (;gcd>=1;gcd--)
|
||||
{
|
||||
if (a%gcd==0 && b%gcd==0)
|
||||
{
|
||||
return gcd;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int a;
|
||||
int b;
|
||||
printf("请输入两个整数:");
|
||||
scanf("%d %d", &a, &b);
|
||||
|
||||
if (a<0 || b<0)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d和%d的最大公约数是:%d\n", a, b, gcd(a, b));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
int a=0;
|
||||
int n=0;
|
||||
int m=0;
|
||||
printf("请输入数字:");
|
||||
scanf("%d", &x);
|
||||
|
||||
do{
|
||||
a = x%10;
|
||||
n = n*10+a;
|
||||
x = x/10;
|
||||
} while (x>0);
|
||||
|
||||
do{
|
||||
m = n%10;
|
||||
printf("%d", m);
|
||||
if (n>=10)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
n = n/10;
|
||||
} while (n>0);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int num_weishu(int num) //判断数字位数
|
||||
{
|
||||
int n = 0;
|
||||
num = abs(num);
|
||||
if (num==0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
while (num>0)
|
||||
{
|
||||
num /= 10;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
int m=0;
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &x);
|
||||
int a = num_weishu(x);
|
||||
|
||||
if(a>9)
|
||||
{
|
||||
printf("超过最大支持位数!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (a>0)
|
||||
{
|
||||
m = x/a;
|
||||
printf("%d", m);
|
||||
x = x%a;
|
||||
a = a/10;
|
||||
if (a>1)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int num_weishu(int num) {
|
||||
int n = 0;
|
||||
num = abs(num);
|
||||
if (num == 0) return 1;
|
||||
while (num > 0) {
|
||||
num /= 10;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x, sign = 1;
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &x);
|
||||
|
||||
if (x < 0) // 处理负数符号
|
||||
{
|
||||
sign = -1;
|
||||
x = -x; // 转换为正数处理
|
||||
}
|
||||
|
||||
int a = num_weishu(x);
|
||||
if (a > 9)
|
||||
{
|
||||
printf("超过最大支持位数!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int b=1;
|
||||
while (a>0)
|
||||
{
|
||||
int m = x/a; // 第一个数字应用符号
|
||||
if (b)
|
||||
{
|
||||
printf("%d", sign*m);
|
||||
b = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d", m);
|
||||
}
|
||||
x %= a;
|
||||
a /= 10;
|
||||
if (a > 0) printf(" "); //后续数字后加空格
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int num_weishu(int num) //判断数字位数
|
||||
{
|
||||
int n = 0;
|
||||
num = abs(num);
|
||||
if (num == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
while (num > 0)
|
||||
{
|
||||
num /= 10;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
int a=0;
|
||||
int b=0;
|
||||
int m=0;
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &x);
|
||||
|
||||
a = num_weishu(x);
|
||||
b = (int)(pow(10,a-1)+0.1); //pow函数是次方运算的函数名,+0.1是为了避免浮点数误差
|
||||
printf("这个数字的位数是%d。\n", a);
|
||||
|
||||
if(a>9)
|
||||
{
|
||||
printf("超过最大支持位数!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if (x<0) // 处理负号
|
||||
{
|
||||
printf("-");
|
||||
}
|
||||
while (b>0)
|
||||
{
|
||||
m = x/b;
|
||||
printf("%d", m);
|
||||
if (b>9)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
x = x%b;
|
||||
b = b/10;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
printf("输入数字:");
|
||||
scanf("%d", &n);
|
||||
|
||||
int a;
|
||||
int b=1;
|
||||
|
||||
for(a=2;a<n;a++)
|
||||
{
|
||||
if(n%a==0)
|
||||
{
|
||||
b=0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(b==1)
|
||||
{
|
||||
printf("%d是素数", n);
|
||||
}
|
||||
else if (b==0)
|
||||
{
|
||||
printf("%d不是素数", n);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int x=0;
|
||||
printf("输入数字:");
|
||||
scanf("%d", &x);
|
||||
|
||||
for(n=2;n<=x;n++)
|
||||
{
|
||||
int a;
|
||||
int b=1;
|
||||
|
||||
for(a=2;a<n;a++)
|
||||
{
|
||||
if(n%a==0)
|
||||
{
|
||||
b=0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(b==1)
|
||||
{
|
||||
printf("%d\n", n);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
// !素数判断的简化版本
|
||||
int sushu(int x)
|
||||
{
|
||||
int ret = 1;
|
||||
|
||||
if (x <= 1)
|
||||
{
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
int i;
|
||||
if (x == 2 || (x % 2 == 0 && x != 2)) // 排除偶数
|
||||
{
|
||||
ret = 0;
|
||||
}
|
||||
for (i = 3; i < (int)sqrt(x)+1; i += 2)
|
||||
{
|
||||
if (x % i == 0)
|
||||
{
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
printf("输入数字:");
|
||||
scanf("%d", &x);
|
||||
|
||||
if (sushu(x))
|
||||
{
|
||||
printf("%d是素数\n", x);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d不是素数\n", x);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// !定义并初始化变量
|
||||
int a = 0; // 用户输入的第一个整数,作为数列的基础数字
|
||||
int n = 0; // 用户输入的第二个整数,表示数列的项数
|
||||
|
||||
// !获取用户输入
|
||||
printf("请输入两个整数a和n:");
|
||||
scanf("%d %d", &a, &n); // 读取用户输入的两个整数
|
||||
|
||||
int sum = 0; // 用于存储数列的和
|
||||
int b = a; // 保存a的初始值,用于后续计算
|
||||
int count = 1; // 循环计数器,从1开始到n结束
|
||||
|
||||
// !计算数列的和
|
||||
while (count <= n) // 循环n次
|
||||
{
|
||||
sum = sum + a; // 将当前项a加到总和sum中
|
||||
a = a * 10 + b; // 生成下一项:将当前项乘以10再加上初始值b
|
||||
count++; // 计数器递增
|
||||
}
|
||||
|
||||
// !输出结果
|
||||
printf("数列之和为%d\n", sum);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int N;
|
||||
printf("请输入项数N: ");
|
||||
scanf("%d", &N);
|
||||
double a = 2.0, b = 1.0, sum = 0.0;
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
sum += a / b;
|
||||
double next_a = a + b; // 计算下一项的分子
|
||||
double next_b = a; // 计算下一项的分母
|
||||
a = next_a; // 更新分子
|
||||
b = next_b; // 更新分母
|
||||
}
|
||||
printf("%.2f\n", sum);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n=9;
|
||||
printf("请输入一个正整数n:");
|
||||
scanf("%d", &n);
|
||||
|
||||
if (n < 1 || n > 9)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
for (int j = 1; j <= i; j++)
|
||||
{
|
||||
printf("%d*%d=%d ", i, j, i * j);
|
||||
if (i == j)
|
||||
{
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char c = '1'; // '1'是字符常量,不是整数,注意:" "也是字符常量
|
||||
char d = 1; // 1是整数常量,不是字符常量
|
||||
printf("%c\n", c); // %c是字符输出
|
||||
printf("%d\n", d); // %d是整数输出
|
||||
|
||||
char ch;
|
||||
scanf("%c", &ch); // scanf("%c", &c)是字符输入
|
||||
printf("ch=%d\n", ch); // %d是整数输出
|
||||
printf("ch='%c'\n", ch);// %c是字符输出
|
||||
|
||||
// !char是一种数据类型,它是用数字编码表示的字符集,比如ASCII码,Unicode码等,也就是说,char类型可以表示一个字符,也可以表示一个整数,具体表示什么,取决于它的编码方式(即输出printf的是%d还是%c)
|
||||
|
||||
// 例如
|
||||
char ch1 = 'a';
|
||||
ch1++;
|
||||
printf("'%c'\n", ch1); //输出'b'
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int num_weishu(int num) // 判断数字位数
|
||||
{
|
||||
int n = 0;
|
||||
num = abs(num);
|
||||
if (num == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
while (num > 0)
|
||||
{
|
||||
num /= 10;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
int m = 0;
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &x);
|
||||
|
||||
a = num_weishu(x);
|
||||
b = (int)(pow(10, a - 1) + 0.1); // pow函数是次方运算的函数名,+0.1是为了避免浮点数误差
|
||||
printf("这个数字的位数是%d。\n", a);
|
||||
|
||||
if (a > 9)
|
||||
{
|
||||
printf("超过最大支持位数!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if (x < 0) // 处理负号
|
||||
{
|
||||
printf("-");
|
||||
}
|
||||
while (b > 0)
|
||||
{
|
||||
m = x / b;
|
||||
switch (m)
|
||||
{
|
||||
case 0:
|
||||
printf("ling ");
|
||||
break;
|
||||
case 1:
|
||||
printf("yi ");
|
||||
break;
|
||||
case 2:
|
||||
printf("er ");
|
||||
break;
|
||||
case 3:
|
||||
printf("san ");
|
||||
break;
|
||||
case 4:
|
||||
printf("si ");
|
||||
break;
|
||||
case 5:
|
||||
printf("wu ");
|
||||
break;
|
||||
case 6:
|
||||
printf("liu ");
|
||||
break;
|
||||
case 7:
|
||||
printf("qi ");
|
||||
break;
|
||||
case 8:
|
||||
printf("ba ");
|
||||
break;
|
||||
case 9:
|
||||
printf("jiu ");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (b > 9)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
x = x % b;
|
||||
b = b / 10;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
unsigned char c = 127;
|
||||
int i = 255;
|
||||
|
||||
c = c + 1;
|
||||
|
||||
printf("c=%d,i=%d", c ,i);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char c = 127;
|
||||
int i = 255;
|
||||
|
||||
c = c + 1;
|
||||
|
||||
printf("c=%d,i=%d", c ,i);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a = 0;
|
||||
int b = 1;
|
||||
|
||||
while (++a>0)
|
||||
{
|
||||
;
|
||||
}
|
||||
printf("int的最大值是:%d\n", a-1);
|
||||
|
||||
while ((a=a/10)!=0)
|
||||
{
|
||||
b++;
|
||||
}
|
||||
printf("int的位数是:%d\n", b);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a = 0;
|
||||
int b = 1;
|
||||
|
||||
while (++a>0)
|
||||
{
|
||||
;
|
||||
}
|
||||
printf("int的最大值是:%d\n", a-1);
|
||||
|
||||
while ((a=a/10)!=0)
|
||||
{
|
||||
b++;
|
||||
}
|
||||
printf("int的位数是:%d\n", b);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
/*
|
||||
条件运算符:?:
|
||||
条件运算符的优先级低于关系运算符,高于算术运算符
|
||||
条件运算符的运算对象和求值结果都是int类型
|
||||
条件运算符的运算对象可以是任意能转换成int类型的值
|
||||
*/
|
||||
int count = 0;
|
||||
scanf("%d",&count);
|
||||
count=(count>20)?count-10:-count+10;
|
||||
/*
|
||||
等价于
|
||||
if (count>20)
|
||||
count=count-10;
|
||||
else
|
||||
count=-count+10;
|
||||
*/
|
||||
|
||||
/*
|
||||
逗号运算符:,:
|
||||
逗号用来连接两个表达式,并以其右边的表达式的值做为他的结果
|
||||
逗号运算符的优先级在所有运算符中是最低的,所以他的两边的运算对象会先求值
|
||||
逗号的组合关系是从左向右的,所以左边的运算对象会先求值,而右边的表达式的值就会留下来作为整个逗号表达式的值
|
||||
*/
|
||||
int i;
|
||||
int j;
|
||||
i = 3 + 4, 5 + 6;
|
||||
printf("%d\n", i);
|
||||
// 结果是7,因为逗号运算符的优先级最低,所以i=3+4,5+6;这个表达式等价于i=(3+4),(5+6);
|
||||
|
||||
// 例如
|
||||
for (i = 0, j = 10; i < j;i++,j--)
|
||||
{
|
||||
printf("%d %d\n", i, j);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int power(int a, int x) //a的x次方运算
|
||||
{
|
||||
if (x < 0)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int sum = 1;
|
||||
for (int i = 0; i < x; i++)
|
||||
{
|
||||
sum *= a;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
printf("请输入一个正整数n(3 ≤ n ≤ 7):");
|
||||
scanf("%d", &n);
|
||||
|
||||
if (n < 3 || n > 7)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
int lower = power(10, n - 1); //n位数的最小值
|
||||
int upper = power(10, n) - 1; //n位数的最大值
|
||||
|
||||
for (int num = lower; num <= upper;num++) //缩小范围,减少运算量
|
||||
{
|
||||
int sum=0;
|
||||
int m = num;
|
||||
while(m>0)
|
||||
{
|
||||
int a = m % 10;
|
||||
sum += power(a,n);
|
||||
m /= 10;
|
||||
}
|
||||
if (num==sum)
|
||||
{
|
||||
printf("%d\n", sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// !浮点数的范围
|
||||
printf("%f\n", 12.0/0.0); //12/0是没有定义的,即只有浮点数有无穷大和无穷小
|
||||
printf("%f\n", -12.0/0.0);
|
||||
printf("%f\n", 0.0/0.0);
|
||||
//inf是无穷大,nan是无穷小
|
||||
|
||||
// !浮点数的精度
|
||||
float a, b, c;
|
||||
a = 1.345f; //带小数点的默认是double类型,所以要加f来指定是float类型
|
||||
b = 1.123f;
|
||||
c = a + b;
|
||||
if (c== 2.468)
|
||||
{
|
||||
printf("c = 2.468\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("不相等! c=%.10f,或%f\n", c, c);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
double ff = 1234.56789;
|
||||
printf("科学计数法的输出:ff = %e\n", ff); //%e是科学计数法的输出
|
||||
printf("科学计数法的输出:ff = %E\n", ff); //%E是科学计数法的输出(底数是大写的)
|
||||
printf("普通格式的输出:ff = %f\n", ff); //%f是普通格式的输出
|
||||
|
||||
double fff = 1E-10;
|
||||
printf("科学计数法的输出:ff = %e\n", fff); //%e是科学计数法的输出
|
||||
printf("科学计数法的输出:ff = %E\n", fff); //%E是科学计数法的输出(底数是大写的)
|
||||
printf("普通格式的输出:ff = %f\n", fff); //%f是普通格式的输出
|
||||
printf("普通格式的输出:ff = %.16f\n", fff); //%.16f是普通格式的保留16位小数的输出
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main() //自己输入数字,自己猜
|
||||
{
|
||||
int a, b, c, d;
|
||||
printf("请输入一个设计的整数和设计轮数:");
|
||||
scanf("%d %d", &a, &b);
|
||||
while(c!=a)
|
||||
{
|
||||
c = 0;
|
||||
d = 1;
|
||||
d++;
|
||||
printf("请输入一个的整数:");
|
||||
scanf("%d", &c);
|
||||
if(c>a)
|
||||
{
|
||||
printf("大了!\n");
|
||||
}
|
||||
else if(c<a)
|
||||
{
|
||||
printf("小了!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
d++;
|
||||
if (d <= b)
|
||||
{
|
||||
printf("你只猜了%d次,就猜对了!\n", d);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("你一共猜了%d次,才猜对!\n", d);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// !自类型转换
|
||||
/* 对于printf函数,任何小于int的整型值都会自动转换成int类型
|
||||
float会自动转换成double类型
|
||||
但是scanf函数不会自动转换类型,要输入short类型,必须输入%hd
|
||||
*/
|
||||
|
||||
// !强制类型转换
|
||||
/* 要将一个量强制转换成另一个类型,需要使用强制类型转换运算符(通常是较小的类型)
|
||||
需要:(type)expression,大的变量赋值给小的变量时,旧的值不会改变
|
||||
例如:(int)10.2
|
||||
(short)32
|
||||
强制类型转换的优先级比四则运算高
|
||||
*/
|
||||
printf("%d\n", (short)32768); //结果为-32768
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int gcd(int x, int y) // 求最大公约数
|
||||
{
|
||||
int c;
|
||||
while (y > 0)
|
||||
{
|
||||
c = x % y;
|
||||
x = y;
|
||||
y = c;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int m;
|
||||
int n;
|
||||
printf("请输入分式m/n:");
|
||||
scanf("%d/%d", &m, &n);
|
||||
|
||||
int a, b;
|
||||
int num_gcd = gcd(m, n);
|
||||
a = m / num_gcd;
|
||||
b = n / num_gcd;
|
||||
printf("%d/%d的化简分式是%d/%d\n", m, n, a, b);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int l;
|
||||
printf("请输入一个正整数:");
|
||||
scanf("%d", &n);
|
||||
printf("一行有几个整数:");
|
||||
scanf("%d", &l);
|
||||
|
||||
if (n<=0)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
int i, j, k;
|
||||
int m = 0;
|
||||
i = n;
|
||||
while(i<=n+3)
|
||||
{
|
||||
j = n;
|
||||
while(j<=n+3)
|
||||
{
|
||||
k = n;
|
||||
while(k<=n+3)
|
||||
{
|
||||
if (i!=j && j!=k && i!=k)
|
||||
{
|
||||
m++;
|
||||
printf("%d%d%d", i, j, k);
|
||||
if (m==l)
|
||||
{
|
||||
printf("\n");
|
||||
m = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
}
|
||||
k++;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int is_sushu(int x) //判断一个数是否是素数
|
||||
{
|
||||
if (x <= 1)
|
||||
return 0;
|
||||
for (int a = 2; a <= sqrt(x); a++)
|
||||
{
|
||||
if (x % a == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int num_sushuhe(int x, int y) //计算区间[x,y]内素数的和
|
||||
{
|
||||
int sum = 0;
|
||||
for (int e = x; e <= y; e++)
|
||||
{
|
||||
if (is_sushu(e))
|
||||
{
|
||||
sum += e;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int m, n;
|
||||
printf("请输入两个正整数m和n:");
|
||||
scanf("%d %d", &m, &n);
|
||||
|
||||
if (n > m) // 确保m <= n
|
||||
{
|
||||
int temp = m;
|
||||
m = n;
|
||||
n = temp;
|
||||
}
|
||||
|
||||
if (n < 1 || m < 1)
|
||||
{
|
||||
printf("输入错误,请重新输入!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("素数列表: ");
|
||||
for (int x = n; x <= m; x++)
|
||||
{
|
||||
if (is_sushu(x))
|
||||
{
|
||||
printf("%d ", x);
|
||||
}
|
||||
}
|
||||
printf("\n素数和: %d\n", num_sushuhe(n, m));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// 逃逸字符:表示无法直接输入的字符(如换行、制表符)或特殊符号(如双引号、反斜杠本身)
|
||||
// 避免语法冲突,例如在字符串中嵌套双引号
|
||||
printf("输入\"5 7\"表示5英尺7英寸");
|
||||
|
||||
// \b回退一格
|
||||
printf("123\b\n456\n");
|
||||
|
||||
// \t制表符,到下一个表格位(t是tab的缩写,相当于键盘上的tab键)
|
||||
printf("12\t456\n");
|
||||
|
||||
// \n换行
|
||||
printf("123\n456\n");
|
||||
|
||||
// \r回车
|
||||
printf("123\r456\n");
|
||||
|
||||
// \"双引号(")
|
||||
printf("123\"456\n");
|
||||
|
||||
// \'单引号(')
|
||||
printf("123\'456\n");
|
||||
|
||||
// \\反斜杠(\)
|
||||
printf("123\\456\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// !#include <stdbool.h>之后,bool类型就可用,即true和false
|
||||
// 逻辑运算符:&&(与)、||(或)、!(非)
|
||||
// 逻辑运算符的优先级低于关系运算符,高于算术运算符
|
||||
// 逻辑运算符的运算对象和求值结果都是bool类型
|
||||
// 逻辑运算符的运算对象可以是任意能转换成bool类型的值
|
||||
|
||||
bool b = 6 > 5; // b为true
|
||||
bool t = true;
|
||||
t = 2;
|
||||
printf("%d\n", t); // bool类型只能以%d输出,即结果为1
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/*
|
||||
函数定义:函数是一段代码的封装,可以重复使用
|
||||
函数的声明:告诉编译器函数的名字,函数的参数,返回值类型
|
||||
函数的调用:使用函数名调用函数,并传入参数
|
||||
*/
|
||||
|
||||
以下是该函数的文档注释:
|
||||
|
||||
/**
|
||||
* @brief 计算从begin到end之间所有整数的和
|
||||
* @param begin 起始数(包含)
|
||||
* @param end 结束数(包含)
|
||||
* @return 返回区间内所有整数的累加和
|
||||
* @note begin必须小于等于end
|
||||
*/
|
||||
int num_sum(int begin, int end)
|
||||
/*
|
||||
函数的头void num_sum(int begin, int end)
|
||||
int是返回值类型,int需要返回一个值,但void不需要返回值
|
||||
num_sum是函数名
|
||||
int是参数类型
|
||||
begin,end是参数名
|
||||
*/
|
||||
{
|
||||
// 函数体,即函数要执行的代码
|
||||
int sum = 0;
|
||||
for (int i = begin; i <= end; i++)
|
||||
{
|
||||
sum += i;
|
||||
}
|
||||
return sum; // 函数的返回值用return返回
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("%d\n", num_sum(1, 10));
|
||||
printf("%d\n", num_sum(20, 30));
|
||||
printf("%d\n", num_sum(35, 45));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !函数原型声明
|
||||
int num_sum(int begin, int end);
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("%d\n", num_sum(1, 10));
|
||||
printf("%d\n", num_sum(20, 30));
|
||||
printf("%d\n", num_sum(35, 45));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// !函数定义
|
||||
int num_sum(int begin, int end)
|
||||
{
|
||||
int sum = 0;
|
||||
for (int i = begin; i <= end; i++)
|
||||
{
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int jiecheng(int n);
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &n);
|
||||
printf("%d\n", jiecheng(n));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int jiecheng(int n)
|
||||
{
|
||||
int sum = 1;
|
||||
if (n == 1)
|
||||
return sum;
|
||||
else
|
||||
sum = n * jiecheng(n-1);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int num_sum(int a, int b) // 函数定义
|
||||
{
|
||||
int sum = 0;
|
||||
for (int i = a; i <= b;i++)
|
||||
{
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int a, b;
|
||||
printf("请输入两个整数:");
|
||||
scanf("%d %d", &a, &b);
|
||||
printf("%d和%d之间的所有数的和:sum = %d\n", a, b, num_sum(a, b));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/*
|
||||
void main(void);明确函数无返回值,即()内为空,没有参数
|
||||
void main();明确函数无返回值,但()内可以有参数
|
||||
*/
|
||||
|
||||
/*
|
||||
C语言不允许函数的嵌套定义
|
||||
*/
|
||||
|
||||
/*
|
||||
int main(void)是一个函数,所以会有return 0;作为函数的返回值
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/*
|
||||
函数定义:函数是一段代码的封装,可以重复使用
|
||||
函数的声明:告诉编译器函数的名字,函数的参数,返回值类型
|
||||
函数的调用:使用函数名调用函数,并传入参数
|
||||
*/
|
||||
|
||||
void num_sum(int begin, int end)
|
||||
/*
|
||||
函数的头void num_sum(int begin, int end)
|
||||
void是返回值类型
|
||||
num_sum是函数名
|
||||
int是参数类型
|
||||
begin,end是参数名
|
||||
*/
|
||||
{
|
||||
// 函数体,即函数要执行的代码
|
||||
int sum = 0;
|
||||
for (int i = begin; i <= end; i++)
|
||||
{
|
||||
sum += i;
|
||||
}
|
||||
printf("%d到%d的和为%d\n", begin, end, sum);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
num_sum(1, 10);
|
||||
num_sum(20, 30);
|
||||
num_sum(35, 45);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void swap(int a, int b); // !形参
|
||||
|
||||
int main()
|
||||
{
|
||||
// !参数传递
|
||||
/*
|
||||
* 1.值传递:可以理解为拷贝,C语言中所有的参数传递都是值传递,即把实参的值拷贝一份传递给形参
|
||||
* 2.地址传递:
|
||||
* 3.引用传递
|
||||
* 4.每个函数有自己变量空间,参数也位于这个独立的空间中,和其它函数没有关系(函数调用时,形参和实参是不同的变量,形参变量在栈中分配内存,实参变量在栈中分配内存,形参和实参是不同的变量,它们之间是相互独立的,没有关系,改变形参的值并不会影响实参的值,改变实参的值也不会影响形参的值)
|
||||
* 5.过去,对于函数参数表中的参数,叫做“形式参数”,调用函数时给的值,叫做“实际参数”。现在,把“形式参数”称为“参数”,“实际参数”称为“值”
|
||||
*/
|
||||
|
||||
int a = 5;
|
||||
int b = 6;
|
||||
|
||||
swap(a, b); // !实参
|
||||
printf("%d %d\n", a, b);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void swap(int a, int b) // !形参
|
||||
{
|
||||
int temp = a;
|
||||
a = b;
|
||||
b = temp;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !本地变量(也叫局部变量)
|
||||
/*
|
||||
定义:所有在函数内定义的变量都是本地变量
|
||||
生存期:变量从定义开始,到函数结束
|
||||
作用域:在什么范围内可以访问这个变量(这个变量可以起作用)
|
||||
对于本地变量,范围是函数大括号{}内
|
||||
特点:
|
||||
1.不同函数中的同名变量互不干扰
|
||||
2.外层变量会被内层同名变量暂时隐藏(变量覆盖)
|
||||
3.未初始化时值为随机数(栈内存特性)
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
// 例子
|
||||
int a = 10;
|
||||
{
|
||||
int a = 20;
|
||||
printf("a = %d\n", a); // 输出20,内层变量a覆盖了外层变量a
|
||||
}
|
||||
printf("a = %d\n", a); // 输出10,内层变量a被隐藏了
|
||||
|
||||
return 0;
|
||||
}
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !二维数组
|
||||
/*
|
||||
int a[3][5];
|
||||
通常理解为a是一个3行5列的矩阵
|
||||
*/
|
||||
|
||||
// !二维数组的遍历
|
||||
/*
|
||||
for ( i=0; i<3; i++)
|
||||
{
|
||||
for ( j=0; j<5; j++ )
|
||||
{
|
||||
a[i][j] = i*j;
|
||||
}
|
||||
}
|
||||
a[i][i]是一个int
|
||||
表示第i行第j列上的单元
|
||||
但a[i,j]是数学上表示a是一个i行j列的矩阵,在C语言是a[i][j]
|
||||
*/
|
||||
|
||||
// !二维数组的初始化
|
||||
/*
|
||||
nt a[] [5] =
|
||||
{
|
||||
{0,1,2,3,4},
|
||||
{2,3,4,5,6},
|
||||
};
|
||||
列数是必须给出的,行数可以由编译器来数每行一个,逗号分隔
|
||||
最后的逗号可以存在,有古老的传统
|
||||
如果省略,表示补零
|
||||
也可以用定位(*C99 标准)
|
||||
*/
|
||||
|
||||
// !tic-tac-toe游戏
|
||||
int main()
|
||||
{
|
||||
const int size = 3;
|
||||
int board[size][size];
|
||||
int i, j;
|
||||
int numfx, numfo;
|
||||
int result = -1;
|
||||
|
||||
// 写入棋盘矩阵
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
for (j = 0; j < size; j++)
|
||||
{
|
||||
printf("请输入第%d行第%d列的棋子(1代表X,0代表O):", i + 1, j + 1);
|
||||
scanf("%d", &board[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查行
|
||||
for (i = 0; i < size && result == -1; i++)
|
||||
{
|
||||
numfx = numfo = 0;
|
||||
for (j = 0; j < size;j++)
|
||||
{
|
||||
if (board[i][j]==1)
|
||||
{
|
||||
numfx++;
|
||||
}
|
||||
else
|
||||
{
|
||||
numfo++;
|
||||
}
|
||||
}
|
||||
|
||||
if (numfo == size)
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
else if (numfx == size)
|
||||
{
|
||||
result = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查列
|
||||
if (result == -1)
|
||||
{
|
||||
for (j = 0; j < size && result == -1; j++)
|
||||
{
|
||||
numfx = numfo = 0;
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
if (board[i][j] == 1)
|
||||
{
|
||||
numfx++;
|
||||
}
|
||||
else
|
||||
{
|
||||
numfo++;
|
||||
}
|
||||
}
|
||||
|
||||
if (numfo == size)
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
else if (numfx == size)
|
||||
{
|
||||
result = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查主对角线
|
||||
if (result == -1)
|
||||
{
|
||||
numfx = numfo = 0;
|
||||
for (i = 0; i < size;i++)
|
||||
{
|
||||
if (board[i][i]==1)
|
||||
{
|
||||
numfx++;
|
||||
}
|
||||
else
|
||||
{
|
||||
numfo++;
|
||||
}
|
||||
}
|
||||
|
||||
if (numfo ==size)
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
else if (numfx == size)
|
||||
{
|
||||
result = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查次对角线
|
||||
if (result == -1)
|
||||
{
|
||||
numfx = numfo = 0;
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
if (board[i][size - i - 1] == 1)
|
||||
{
|
||||
numfx++;
|
||||
}
|
||||
else
|
||||
{
|
||||
numfo++;
|
||||
}
|
||||
}
|
||||
|
||||
if (numfo == size)
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
else if (numfx == size)
|
||||
{
|
||||
result = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 输出结果
|
||||
if (result == 1)
|
||||
{
|
||||
printf("X获胜!\n");
|
||||
}
|
||||
else if (result == 0)
|
||||
{
|
||||
printf("O获胜!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("平局!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !二维数组
|
||||
/*
|
||||
int a[3][5];
|
||||
通常理解为a是一个3行5列的矩阵
|
||||
*/
|
||||
|
||||
// !二维数组的遍历
|
||||
/*
|
||||
for ( i=0; i<3; i++)
|
||||
{
|
||||
for ( j=0; j<5; j++ )
|
||||
{
|
||||
a[i][j] = i*j;
|
||||
}
|
||||
}
|
||||
a[i][i]是一个int
|
||||
表示第i行第j列上的单元
|
||||
但a[i,j]是数学上表示a是一个i行j列的矩阵,在C语言是a[i][j]
|
||||
*/
|
||||
|
||||
// !二维数组的初始化
|
||||
/*
|
||||
nt a[][5] =
|
||||
{
|
||||
{0,1,2,3,4},
|
||||
{2,3,4,5,6
|
||||
};
|
||||
列数是必须给出的,行数可以由编译器来数每行一个,逗号分隔
|
||||
最后的逗号可以存在,有古老的传统
|
||||
如果省略,表示补零
|
||||
也可以用定位(*C99 标准)
|
||||
*/
|
||||
|
||||
// !输入l行m列的矩阵()
|
||||
void matrix_produce(int l, int m, int a[l][m]);
|
||||
|
||||
// !找出二维数组中的鞍点,即该位置上的元素在该行上最大、在该列上最小,也可能没有鞍点
|
||||
void matrix_find_saddle_point(int rows, int cols, int matrix[rows][cols]);
|
||||
|
||||
// !打印l行n列的矩阵()
|
||||
void matrix_print(int l, int n, int c[l][n]);
|
||||
|
||||
// !释放二维数组内存
|
||||
void matrix_free(int **matrix, int rows);
|
||||
|
||||
int main()
|
||||
{
|
||||
int l, m;
|
||||
printf("输入矩阵的行数和列数(用空格分隔): ");
|
||||
scanf("%d %d", &l, &m);
|
||||
|
||||
int matrix[l][m];
|
||||
printf("按行输入矩阵元素(每行%d个,空格分隔):\n", m);
|
||||
matrix_produce(l, m, matrix);
|
||||
|
||||
printf("输入的矩阵为:\n");
|
||||
matrix_print(l, m, matrix);
|
||||
|
||||
matrix_find_saddle_point(l, m, matrix);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ! 输入l行m列的矩阵
|
||||
void matrix_produce(int l, int m, int a[l][m])
|
||||
{
|
||||
for (int i = 0; i < l; i++)
|
||||
{
|
||||
for (int j = 0; j < m; j++)
|
||||
{
|
||||
scanf("%d", &a[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// !找出二维数组中的鞍点(行最大且列最小)
|
||||
void matrix_find_saddle_point(int rows, int cols, int matrix[rows][cols])
|
||||
{
|
||||
// 标记是否找到鞍点
|
||||
int has_saddle = 0;
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
// !步骤1:找到当前行的最大值
|
||||
int row_max = matrix[i][0];
|
||||
int col_max = 0;
|
||||
|
||||
for (int j = 0; j < cols;j++)
|
||||
{
|
||||
if (matrix[i][j]>row_max)
|
||||
{
|
||||
row_max = matrix[i][j];
|
||||
col_max = j;
|
||||
}
|
||||
}
|
||||
|
||||
// !步骤2:检查该行最大值是否是所在列的最小值
|
||||
int col_min = matrix[0][col_max]; // 初始化列最小值为当前列第一行
|
||||
for (int k = 1; k < rows; k++)
|
||||
{
|
||||
// 遍历当前列的所有行
|
||||
if (matrix[k][col_max] < col_min)
|
||||
{
|
||||
col_min = matrix[k][col_max]; // 更新列最小值
|
||||
}
|
||||
}
|
||||
|
||||
// !步骤3:判断是否为鞍点(行最大且列最小)
|
||||
if (row_max == col_min)
|
||||
{
|
||||
printf("鞍点坐标为(%d, %d),值为:%d\n", i, col_max, row_max);
|
||||
has_saddle = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 未找到鞍点时提示
|
||||
if (has_saddle==0)
|
||||
{
|
||||
printf("该矩阵没有鞍点!\n");
|
||||
}
|
||||
}
|
||||
|
||||
// !打印l行n列的矩阵(适配 int ** 类型)
|
||||
void matrix_print(int l, int n, int c[l][n])
|
||||
{
|
||||
for (int i = 0; i < l; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
printf("%d ", c[i][j]); // 通过指针访问动态分配的矩阵元素
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// !释放二维数组内存
|
||||
void matrix_free(int **matrix, int rows)
|
||||
{
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
free(matrix[i]); // 释放每行的内存
|
||||
}
|
||||
free(matrix); // 释放行指针数组
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !二维数组
|
||||
/*
|
||||
int a[3][5];
|
||||
通常理解为a是一个3行5列的矩阵
|
||||
*/
|
||||
|
||||
// !二维数组的遍历
|
||||
/*
|
||||
for ( i=0; i<3; i++)
|
||||
{
|
||||
for ( j=0; j<5; j++ )
|
||||
{
|
||||
a[i][j] = i*j;
|
||||
}
|
||||
}
|
||||
a[i][i]是一个int
|
||||
表示第i行第j列上的单元
|
||||
但a[i,j]是数学上表示a是一个i行j列的矩阵,在C语言是a[i][j]
|
||||
*/
|
||||
|
||||
// !二维数组的初始化
|
||||
/*
|
||||
nt a[] [5] =
|
||||
{
|
||||
{0,1,2,3,4},
|
||||
{2,3,4,5,6},
|
||||
};
|
||||
列数是必须给出的,行数可以由编译器来数每行一个,逗号分隔
|
||||
最后的逗号可以存在,有古老的传统
|
||||
如果省略,表示补零
|
||||
也可以用定位(*C99 标准)
|
||||
*/
|
||||
|
||||
// !矩阵与矩阵的乘法运算
|
||||
int main()
|
||||
{
|
||||
int l, m, n;
|
||||
int i, j, k;
|
||||
|
||||
printf("请输入矩阵的行数、列数:\n");
|
||||
scanf("%d %d %d", &l, &m, &n);
|
||||
int a[l][m];
|
||||
int b[m][n];
|
||||
int c[l][n];
|
||||
|
||||
// !矩阵的输入
|
||||
printf("请输入矩阵a的元素:\n");
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < m; j++)
|
||||
{
|
||||
scanf("%d", &a[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
printf("请输入矩阵b的元素:\n");
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
scanf("%d", &b[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
// !矩阵的乘法运算
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
c[i][j] = 0;
|
||||
for (k = 0; k < m; k++)
|
||||
{
|
||||
c[i][j] += a[i][k] * b[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// !矩阵的打印
|
||||
printf("矩阵a和矩阵b的乘法结果矩阵c为:\n");
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
printf("%d ", c[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !二维数组
|
||||
/*
|
||||
int a[3][5];
|
||||
通常理解为a是一个3行5列的矩阵
|
||||
*/
|
||||
|
||||
// !二维数组的遍历
|
||||
/*
|
||||
for ( i=0; i<3; i++)
|
||||
{
|
||||
for ( j=0; j<5; j++ )
|
||||
{
|
||||
a[i][j] = i*j;
|
||||
}
|
||||
}
|
||||
a[i][i]是一个int
|
||||
表示第i行第j列上的单元
|
||||
但a[i,j]是数学上表示a是一个i行j列的矩阵,在C语言是a[i][j]
|
||||
*/
|
||||
|
||||
// !二维数组的初始化
|
||||
/*
|
||||
nt a[] [5] =
|
||||
{
|
||||
{0,1,2,3,4},
|
||||
{2,3,4,5,6
|
||||
};
|
||||
列数是必须给出的,行数可以由编译器来数每行一个,逗号分隔
|
||||
最后的逗号可以存在,有古老的传统
|
||||
如果省略,表示补零
|
||||
也可以用定位(*C99 标准)
|
||||
*/
|
||||
|
||||
void matrix_produce(int l,int m, int a[l][m]);
|
||||
void matrix_multiply( int l, int m, int n, int a[l][m], int b[m][n], int c[l][n]);
|
||||
void matrix_print(int l, int n, int c[l][n]);
|
||||
|
||||
// !矩阵与矩阵的乘法运算
|
||||
int main()
|
||||
{
|
||||
int l, m, q, n;
|
||||
|
||||
// !矩阵的定义
|
||||
printf("请输入矩阵a的行数、列数:\n");
|
||||
scanf("%d %d", &l, &m);
|
||||
printf("请输入矩阵b的行数、列数:\n");
|
||||
scanf("%d %d", &q, &n);
|
||||
while (m!=q)
|
||||
{
|
||||
printf("矩阵a的列数与矩阵b的行数不相等,无法进行矩阵乘法运算!\n");
|
||||
printf("请重新输入矩阵a的行数、列数:\n");
|
||||
scanf("%d %d", &l, &m);
|
||||
printf("请重新输入矩阵b的行数、列数:\n");
|
||||
scanf("%d %d", &q, &n);
|
||||
}
|
||||
|
||||
int a[l][m];
|
||||
int b[m][n];
|
||||
int c[l][n];
|
||||
|
||||
// !矩阵的输入
|
||||
printf("请输入矩阵a的元素:\n");
|
||||
matrix_produce(l, m, a);
|
||||
|
||||
printf("请输入矩阵b的元素:\n");
|
||||
matrix_produce(m, n, b);
|
||||
|
||||
// !矩阵的乘法运算
|
||||
matrix_multiply(l, m, n, a, b, c);
|
||||
|
||||
// !矩阵的打印
|
||||
printf("矩阵a和矩阵b的乘法结果矩阵c为:\n");
|
||||
matrix_print(l, n, c);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void matrix_produce(int l,int m, int a[l][m])
|
||||
{
|
||||
// !矩阵的输入
|
||||
int i, j;
|
||||
for ( i = 0; i < l; i++)
|
||||
{
|
||||
for ( j = 0; j < m; j++)
|
||||
{
|
||||
scanf("%d", &a[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void matrix_multiply (int l, int m, int n, int a[l][m], int b[m][n], int c[l][n])
|
||||
{
|
||||
// !矩阵的乘法运算
|
||||
int i, j, k;
|
||||
for ( i = 0; i < l; i++)
|
||||
{
|
||||
for ( j = 0; j < n; j++)
|
||||
{
|
||||
c[i][j] = 0;
|
||||
for ( k = 0; k < m; k++)
|
||||
{
|
||||
c[i][j] += a[i][k] * b[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void matrix_print(int l, int n, int c[l][n])
|
||||
{
|
||||
// !矩阵的打印
|
||||
int i, j;
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
printf("%d ", c[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !数组
|
||||
/*
|
||||
[i]是一个数组,例如本代码中数组名是number
|
||||
number[0]是数组的第一个元素,number[1]是数组的第二个元素,以此类推
|
||||
即从左到右是从0开始
|
||||
|
||||
数组是一种容器,其中所有的元素都具有相同的数据类型,且在内存中连续存储,依次排列
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
double sum = 0.0;
|
||||
int count = 0;
|
||||
int number[100]; // 定义一个数组,数组名为number,数组大小为100
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &x);
|
||||
|
||||
while (x != -1)
|
||||
{
|
||||
if (x < 0 || x != (int)x) // 判断输入的数字是否为正整数
|
||||
{
|
||||
printf("输入的数字不是正整数,请重新输入!\n");
|
||||
continue;
|
||||
}
|
||||
number[count] = x; // 对数组中的元素进行赋值
|
||||
count++;
|
||||
sum += x;
|
||||
printf("请输入一个整数:");
|
||||
scanf("%d", &x);
|
||||
}
|
||||
|
||||
// 防止数组越界
|
||||
if (count < 100)
|
||||
{
|
||||
if (count > 0)
|
||||
{
|
||||
int i;
|
||||
double average = sum * 1.0 / count;
|
||||
printf("数组的总和是%.0f,平均数是%.2f(保留两位小数),大于平均数有\n", sum, average);
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
if (number[i] >= average) // s使用数组中的元素,将数组中的元素与平均数进行比较
|
||||
{
|
||||
printf("%d ", number[i]); // 输出数组中大于平均数的元素
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("数组为空,请重新输入!\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("数组的规定大小是100,现在已经超过了!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !定义数组
|
||||
/*
|
||||
<类型> 变量名字[元素数量],例如int number[100],double number[100]
|
||||
元素数量必须是整数
|
||||
例如:a[10]
|
||||
每个单元都是一个变量,每个变量都是一个int类型的变量,可以出现在赋值语句的左边或右边
|
||||
如a[2] = a[1]+6
|
||||
|
||||
*/
|
||||
|
||||
void f();
|
||||
|
||||
int main()
|
||||
{
|
||||
f();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void f()
|
||||
{
|
||||
int a[10];
|
||||
a[10] = 0; // !数组越界,因为数组大小为10,从a[0]到a[9],所以a[10]是越界的
|
||||
}
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !二维数组
|
||||
/*
|
||||
int a[3][5];
|
||||
通常理解为a是一个3行5列的矩阵
|
||||
*/
|
||||
|
||||
// !二维数组的遍历
|
||||
/*
|
||||
for ( i=0; i<3; i++)
|
||||
{
|
||||
for ( j=0; j<5; j++ )
|
||||
{
|
||||
a[i][j] = i*j;
|
||||
}
|
||||
}
|
||||
a[i][i]是一个int
|
||||
表示第i行第j列上的单元
|
||||
但a[i,j]是数学上表示a是一个i行j列的矩阵,在C语言是a[i][j]
|
||||
*/
|
||||
|
||||
// !二维数组的初始化
|
||||
/*
|
||||
nt a[] [5] =
|
||||
{
|
||||
{0,1,2,3,4},
|
||||
{2,3,4,5,6
|
||||
};
|
||||
列数是必须给出的,行数可以由编译器来数每行一个,逗号分隔
|
||||
最后的逗号可以存在,有古老的传统
|
||||
如果省略,表示补零
|
||||
也可以用定位(*C99 标准)
|
||||
*/
|
||||
|
||||
int check_symmetry(int matrix[][200], int size);
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
int matrix1[100][100], matrix2[100][100], matrix3[100][100], matrix4[100][100];
|
||||
|
||||
// 读取四个n×n的矩阵
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
scanf("%d", &matrix1[i][j]);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
scanf("%d", &matrix2[i][j]);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
scanf("%d", &matrix3[i][j]);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
scanf("%d", &matrix4[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
int big_matrix[200][200];
|
||||
// 方案一:常规左上、右上、左下、右下拼接
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
big_matrix[i][j] = matrix1[i][j];
|
||||
big_matrix[i][j + n] = matrix2[i][j];
|
||||
big_matrix[i + n][j] = matrix3[i][j];
|
||||
big_matrix[i + n][j + n] = matrix4[i][j];
|
||||
}
|
||||
}
|
||||
if (check_symmetry(big_matrix, 2 * n))
|
||||
{
|
||||
printf("YES\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 方案二:交换右上和左下
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
big_matrix[i][j] = matrix1[i][j];
|
||||
big_matrix[i][j + n] = matrix3[i][j];
|
||||
big_matrix[i + n][j] = matrix2[i][j];
|
||||
big_matrix[i + n][j + n] = matrix4[i][j];
|
||||
}
|
||||
}
|
||||
if (check_symmetry(big_matrix, 2 * n))
|
||||
{
|
||||
printf("YES\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 方案三:交换左上和右下
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
big_matrix[i][j] = matrix4[i][j];
|
||||
big_matrix[i][j + n] = matrix2[i][j];
|
||||
big_matrix[i + n][j] = matrix3[i][j];
|
||||
big_matrix[i + n][j + n] = matrix1[i][j];
|
||||
}
|
||||
}
|
||||
if (check_symmetry(big_matrix, 2 * n))
|
||||
{
|
||||
printf("YES\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 方案四:交换左上和右上,左下和右下
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
big_matrix[i][j] = matrix2[i][j];
|
||||
big_matrix[i][j + n] = matrix1[i][j];
|
||||
big_matrix[i + n][j] = matrix4[i][j];
|
||||
big_matrix[i + n][j + n] = matrix3[i][j];
|
||||
}
|
||||
}
|
||||
if (check_symmetry(big_matrix, 2 * n))
|
||||
{
|
||||
printf("YES\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("NO\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// !检查矩阵是否为对称矩阵
|
||||
int check_symmetry(int matrix[][200], int size)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
for (int j = 0; j < size; j++)
|
||||
{
|
||||
if (matrix[i][j] != matrix[j][i])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a[10]={0};
|
||||
int count=0;
|
||||
while(1)
|
||||
{
|
||||
int b;
|
||||
printf("请输入一个数字:");
|
||||
scanf("%d",&b);
|
||||
|
||||
if(b==-1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (b<=-1 || b>=10)
|
||||
{
|
||||
printf("输入错误,请重新输入\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (count<10)
|
||||
{
|
||||
printf("数组已满\n");
|
||||
break;
|
||||
}
|
||||
|
||||
a[b]++;
|
||||
count++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
printf("%d: %d\n", i, a[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !判断能被已知的且为素数的数整除(构造素数表)
|
||||
/*
|
||||
欲构造n以内的素数表
|
||||
1.令x=2
|
||||
2.将2x、3x、4x直至ax<n的数标记为非素数
|
||||
3.令x为下一个没有被标记为非素数的数,重复步骤2;直到所有的数都已经尝试完毕
|
||||
*/
|
||||
|
||||
// !数组的赋值
|
||||
/*
|
||||
数组赋值有两种方式
|
||||
1.用赋值号直接赋值
|
||||
2.用循环赋值
|
||||
例如:
|
||||
int a[10]={1,2,3,4,5,6,7,8,9,10};
|
||||
int b[10];
|
||||
for(int i=0;i<10;i++)
|
||||
{
|
||||
b[i]=a[i];
|
||||
}
|
||||
数组不能用来赋另外一个数组的值
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
int a[10], i, j, temp;
|
||||
for (i = 0; i < 10; i++)
|
||||
{
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
|
||||
for (i = 0; i < 9; i++)
|
||||
{
|
||||
for (j = 0; j < 10 - i - 1; j++)
|
||||
{
|
||||
if (a[j] < a[j + 1])
|
||||
{
|
||||
temp = a[j];
|
||||
a[j] = a[j + 1];
|
||||
a[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 10; i++)
|
||||
{
|
||||
printf("%d ", a[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#define num 100
|
||||
|
||||
// !判断能被已知的且为素数的数整除
|
||||
|
||||
int sushu(int x, int primes[], int count);
|
||||
|
||||
int main()
|
||||
{
|
||||
//const int num=100;
|
||||
int prime[num] = {2};
|
||||
int count = 1;
|
||||
int i=3;
|
||||
|
||||
while (count<num)
|
||||
{
|
||||
if (sushu(i,prime,count))
|
||||
{
|
||||
prime[count] = i;
|
||||
count++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
for (i = 0; i < num;i++)
|
||||
{
|
||||
printf("%d", prime[i]);
|
||||
if ((i+1)%5==0)
|
||||
{
|
||||
printf("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\t");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 判断能被已知的且为素数的数整除
|
||||
int sushu(int x, int known_sushu[], int num_sushu)
|
||||
// x为要判断的数,known_sushu为已知的素数,num_sushu为已知的素数的个数
|
||||
{
|
||||
int ret = 1;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < num_sushu;i++)
|
||||
{
|
||||
if (x % known_sushu[i]==0)
|
||||
{
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !判断能被已知的且为素数的数整除(构造素数表)
|
||||
/*
|
||||
欲构造n以内的素数表
|
||||
1.令x=2
|
||||
2.将2x、3x、4x直至ax<n的数标记为非素数
|
||||
3.令x为下一个没有被标记为非素数的数,重复步骤2;直到所有的数都已经尝试完毕
|
||||
*/
|
||||
|
||||
// !数组的赋值
|
||||
/*
|
||||
数组赋值有两种方式
|
||||
1.用赋值号直接赋值
|
||||
2.用循环赋值
|
||||
例如:
|
||||
int a[10]={1,2,3,4,5,6,7,8,9,10};
|
||||
int b[10];
|
||||
for(int i=0;i<10;i++)
|
||||
{
|
||||
b[i]=a[i];
|
||||
}
|
||||
数组不能用来赋另外一个数组的值
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
int minnum,maxnum;
|
||||
printf("请输入一个数(即为最小和最大的区间):");
|
||||
scanf(" %d %d", &minnum, &maxnum);
|
||||
|
||||
int isprime[maxnum];
|
||||
int i, x;
|
||||
|
||||
if (maxnum<=0)
|
||||
{
|
||||
printf("输入错误!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < maxnum; i++)// 令isprime[i]=1,采用循环赋值,默认所有的数都是素数,标记为1
|
||||
{
|
||||
isprime[i] = 1;
|
||||
}
|
||||
|
||||
for (x = 2; x < maxnum; x++)
|
||||
{
|
||||
if (isprime[x]==1) // isprime[x]判断是否为素数
|
||||
{
|
||||
/* x的倍数不是素数 */
|
||||
for (i = 2; i*x < maxnum; i++)
|
||||
{
|
||||
isprime[i*x] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (x = 2; x < minnum;x++) // 将比minnum小的数标记为非素数,即不打印
|
||||
{
|
||||
isprime[x] = 0;
|
||||
}
|
||||
|
||||
for (x = 2; x < maxnum; x++)// 打印素数
|
||||
{
|
||||
if (isprime[x] == 1)
|
||||
{
|
||||
printf("%d是素数\n", x);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !数组的集成初始化
|
||||
/*
|
||||
例如:int a[10]={1,2,3,4,5,6,7,8,9,10};
|
||||
直接用大括号给出数组的所有元素值的初始值
|
||||
不需要给出数组的长度(大小),编译器会自动计算
|
||||
*/
|
||||
|
||||
// !集成初始化的定位
|
||||
/*
|
||||
用[n]在初始化数据中给出定位
|
||||
没有定位的数据接在前面的位置后面
|
||||
其他位置的值补零 也可以不给出数组大小,让编译器算
|
||||
特别适合初始数据稀疏的数组
|
||||
例如:int a[10]={[1]=1,4, [5]=6};他的数组为{0,1,4,0,0,6,0,0,0,0}
|
||||
*/
|
||||
|
||||
// !数组的大小
|
||||
/*
|
||||
sizeof(a)给出数组a所占据的内容的大小,单位是字节
|
||||
sizeof(a)/sizeof(a[0])给出数组a的大小
|
||||
*/
|
||||
|
||||
// !数组的赋值
|
||||
/*
|
||||
数组赋值有两种方式
|
||||
1.用赋值号直接赋值
|
||||
2.用循环赋值
|
||||
例如:
|
||||
int a[10]={1,2,3,4,5,6,7,8,9,10};
|
||||
int b[10];
|
||||
for(int i=0;i<10;i++)
|
||||
{
|
||||
b[i]=a[i];
|
||||
}
|
||||
数组不能用来赋另外一个数组的值
|
||||
*/
|
||||
|
||||
// !例题
|
||||
/*
|
||||
找出key在数组a中的位置
|
||||
@param key 要寻找的数字
|
||||
@param a 要寻找的数组
|
||||
@param length 数组a的长度
|
||||
@return 如果找到,返回其在a中的位置;如果找不到则返回-1
|
||||
*/
|
||||
|
||||
int search(int x, int a[], int len);
|
||||
|
||||
int main()
|
||||
{
|
||||
int a[] = {2, 4, 6, 7, 1, 3, 5, 9, 11, 13, 23, 14, 32}; //{[1]=2,4, [5]=6};
|
||||
{
|
||||
int i;
|
||||
printf("%lu\n", sizeof(a));
|
||||
printf("%lu\n", sizeof(a[0])); // sizeof(a[0])是数组中每个元素的大小,结果为4
|
||||
|
||||
for (i = 0; i < sizeof(a) / sizeof(a[0]); i++) // 相除得到了数组的单元个数,即sizeof(a)/sizeof(a[0])是数组的大小
|
||||
{
|
||||
printf("%d\t", a[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int x;
|
||||
int location;
|
||||
printf("请输入要查找的数字:");
|
||||
scanf("%d", &x);
|
||||
location = search(x,a,sizeof(a)/sizeof(a[0]));
|
||||
if (location!=-1)
|
||||
{
|
||||
printf("%d在%d个位置上\n", x, location);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d在数组中不存在\n", x);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int search(int x, int a[], int len)
|
||||
{
|
||||
int ret = -1;
|
||||
|
||||
if (len==0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i=0;i<len;i++)
|
||||
{
|
||||
if (x==a[i])
|
||||
{
|
||||
ret = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !二维数组
|
||||
/*
|
||||
int a[3][5];
|
||||
通常理解为a是一个3行5列的矩阵
|
||||
*/
|
||||
|
||||
// !二维数组的遍历
|
||||
/*
|
||||
for ( i=0; i<3; i++)
|
||||
{
|
||||
for ( j=0; j<5; j++ )
|
||||
{
|
||||
a[i][j] = i*j;
|
||||
}
|
||||
}
|
||||
a[i][i]是一个int
|
||||
表示第i行第j列上的单元
|
||||
但a[i,j]是数学上表示a是一个i行j列的矩阵,在C语言是a[i][j]
|
||||
*/
|
||||
|
||||
// !二维数组的初始化
|
||||
/*
|
||||
nt a[][5] =
|
||||
{
|
||||
{0,1,2,3,4},
|
||||
{2,3,4,5,6
|
||||
};
|
||||
列数是必须给出的,行数可以由编译器来数每行一个,逗号分隔
|
||||
最后的逗号可以存在,有古老的传统
|
||||
如果省略,表示补零
|
||||
也可以用定位(*C99 标准)
|
||||
*/
|
||||
|
||||
void yanghui(int rows);
|
||||
|
||||
int main()
|
||||
{
|
||||
int rows;
|
||||
printf("请输入杨辉三角的行数:");
|
||||
scanf("%d", &rows);
|
||||
|
||||
yanghui(rows);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void yanghui(int rows)
|
||||
{
|
||||
int triangle[rows][rows];
|
||||
triangle[0][0] = 1;
|
||||
|
||||
for (int i = 1; i < rows; i++)
|
||||
{
|
||||
// !第一列和最后一列都为1
|
||||
triangle[i][0] = 1;
|
||||
triangle[i][i] = 1;
|
||||
for (int j = 1; j < i; j++)
|
||||
{
|
||||
// !中间数为上一行相邻两数之和
|
||||
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
for (int j = 0; j <= i; j++)
|
||||
{
|
||||
printf("%d ", triangle[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !二维数组
|
||||
/*
|
||||
int a[3][5];
|
||||
通常理解为a是一个3行5列的矩阵
|
||||
*/
|
||||
|
||||
// !二维数组的遍历
|
||||
/*
|
||||
for ( i=0; i<3; i++)
|
||||
{
|
||||
for ( j=0; j<5; j++ )
|
||||
{
|
||||
a[i][j] = i*j;
|
||||
}
|
||||
}
|
||||
a[i][i]是一个int
|
||||
表示第i行第j列上的单元
|
||||
但a[i,j]是数学上表示a是一个i行j列的矩阵,在C语言是a[i][j]
|
||||
*/
|
||||
|
||||
// !二维数组的初始化
|
||||
/*
|
||||
nt a[] [5] =
|
||||
{
|
||||
{0,1,2,3,4},
|
||||
{2,3,4,5,6
|
||||
};
|
||||
列数是必须给出的,行数可以由编译器来数每行一个,逗号分隔
|
||||
最后的逗号可以存在,有古老的传统
|
||||
如果省略,表示补零
|
||||
也可以用定位(*C99 标准)
|
||||
*/
|
||||
|
||||
// ! 动态分配二维数组(int ** 类型)
|
||||
int **allocate_matrix(int rows, int cols);
|
||||
|
||||
// ! 输入l行m列的矩阵(适配 int ** 类型)
|
||||
void matrix_produce(int l, int m, int **a);
|
||||
|
||||
// ! 打印l行n列的矩阵(适配 int ** 类型)
|
||||
void matrix_print(int l, int n, int **c);
|
||||
|
||||
// ! 计算两个矩阵的和(补零相加)
|
||||
int **add_matrices(int **A, int n1, int m1, int **B, int n2, int m2, int *result_rows, int *result_cols);
|
||||
|
||||
// ! 释放二维数组内存
|
||||
void matrix_free(int **matrix, int rows);
|
||||
|
||||
int main()
|
||||
{
|
||||
// 矩阵A和B的行列数
|
||||
int n1, m1, n2, m2;
|
||||
|
||||
// 结果矩阵的行列数
|
||||
int result_rows, result_cols;
|
||||
|
||||
// ! 输入矩阵A的行列数并动态分配内存
|
||||
printf("输入矩阵A的行数和列数(用空格分隔): ");
|
||||
scanf("%d %d", &n1, &m1);
|
||||
|
||||
// 动态分配矩阵A(int ** 类型)
|
||||
int **A = allocate_matrix(n1, m1);
|
||||
printf("按行输入矩阵A的元素(每行%d个,用空格分隔):\n", m1);
|
||||
matrix_produce(n1, m1, A);
|
||||
|
||||
// ! 输入矩阵B的行列数并动态分配内存
|
||||
printf("输入矩阵B的行数和列数(用空格分隔): ");
|
||||
scanf("%d %d", &n2, &m2);
|
||||
|
||||
// 动态分配矩阵B(int ** 类型)
|
||||
int **B = allocate_matrix(n2, m2);
|
||||
printf("按行输入矩阵B的元素(每行%d个,用空格分隔):\n", m2);
|
||||
matrix_produce(n2, m2, B);
|
||||
|
||||
// ! 计算矩阵相加(补零相加)
|
||||
int **result = add_matrices(A, n1, m1, B, n2, m2, &result_rows, &result_cols);
|
||||
|
||||
// ! 输出结果矩阵
|
||||
printf("\n结果矩阵(行列数: %d×%d):\n", result_rows, result_cols);
|
||||
matrix_print(result_rows, result_cols, result);
|
||||
|
||||
// ! 释放所有动态分配的内存
|
||||
matrix_free(A, n1);
|
||||
matrix_free(B, n2);
|
||||
matrix_free(result, result_rows);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ! 动态分配二维数组(int ** 类型)
|
||||
int **allocate_matrix(int rows, int cols)
|
||||
{
|
||||
int **matrix = (int **)malloc(rows * sizeof(int *)); // 分配行指针数组
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
matrix[i] = (int *)malloc(cols * sizeof(int)); // 分配每行的列空间
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
// ! 输入l行m列的矩阵(适配 int ** 类型)
|
||||
void matrix_produce(int l, int m, int **a)
|
||||
{
|
||||
for (int i = 0; i < l; i++)
|
||||
{
|
||||
for (int j = 0; j < m; j++)
|
||||
{
|
||||
scanf("%d", &a[i][j]); // 通过指针访问动态分配的矩阵元素
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ! 计算两个矩阵的和(补零相加)
|
||||
int **add_matrices(int **A, int n1, int m1, int **B, int n2, int m2, int *result_rows, int *result_cols)
|
||||
{
|
||||
// 计算结果矩阵的尺寸(行数和列数的最大值)
|
||||
*result_rows = (n1 > n2) ? n1 : n2;
|
||||
*result_cols = (m1 > m2) ? m1 : m2;
|
||||
int **result = allocate_matrix(*result_rows, *result_cols); // 动态分配结果矩阵
|
||||
|
||||
// 逐位置计算相加结果(补零逻辑)
|
||||
for (int i = 0; i < *result_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < *result_cols; j++)
|
||||
{
|
||||
int a = (i < n1 && j < m1) ? A[i][j] : 0; // A超出范围则为0
|
||||
int b = (i < n2 && j < m2) ? B[i][j] : 0; // B超出范围则为0
|
||||
result[i][j] = a + b;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ! 打印l行n列的矩阵(适配 int ** 类型)
|
||||
void matrix_print(int l, int n, int **c)
|
||||
{
|
||||
for (int i = 0; i < l; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
printf("%d ", c[i][j]); // 通过指针访问动态分配的矩阵元素
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// ! 释放二维数组内存
|
||||
void matrix_free(int **matrix, int rows)
|
||||
{
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
free(matrix[i]); // 释放每行的内存
|
||||
}
|
||||
free(matrix); // 释放行指针数组
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !二维数组
|
||||
/*
|
||||
int a[3][5];
|
||||
通常理解为a是一个3行5列的矩阵
|
||||
*/
|
||||
|
||||
// !二维数组的遍历
|
||||
/*
|
||||
for ( i=0; i<3; i++)
|
||||
{
|
||||
for ( j=0; j<5; j++ )
|
||||
{
|
||||
a[i][j] = i*j;
|
||||
}
|
||||
}
|
||||
a[i][i]是一个int
|
||||
表示第i行第j列上的单元
|
||||
但a[i,j]是数学上表示a是一个i行j列的矩阵,在C语言是a[i][j]
|
||||
*/
|
||||
|
||||
// !二维数组的初始化
|
||||
/*
|
||||
nt a[] [5] =
|
||||
{
|
||||
{0,1,2,3,4},
|
||||
{2,3,4,5,6
|
||||
};
|
||||
列数是必须给出的,行数可以由编译器来数每行一个,逗号分隔
|
||||
最后的逗号可以存在,有古老的传统
|
||||
如果省略,表示补零
|
||||
也可以用定位(*C99 标准)
|
||||
*/
|
||||
|
||||
// !输入l行m列的矩阵
|
||||
void matrix_produce(int l, int m, int a[l][m])
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < m; j++)
|
||||
{
|
||||
scanf("%d", &a[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// !打印l行n列的矩阵
|
||||
void matrix_print(int l, int n, int c[l][n])
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
printf("%d ", c[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int l, m;
|
||||
printf("请输入方阵的行数l和列数m(必须相等):");
|
||||
scanf("%d %d", &l, &m);
|
||||
|
||||
if (l != m)
|
||||
{
|
||||
printf("错误:输入的不是方阵(行数≠列数)!\n");
|
||||
return 1;
|
||||
}
|
||||
int n = l;
|
||||
|
||||
int a[n][n];
|
||||
matrix_produce(n, n, a);
|
||||
|
||||
int sum_main = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
sum_main += a[i][i];
|
||||
}
|
||||
|
||||
int sum_anti = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
int j = n - 1 - i;
|
||||
sum_anti += a[i][j];
|
||||
}
|
||||
|
||||
int total_sum;
|
||||
if (n % 2 == 1)
|
||||
{
|
||||
int duplicate_i = (n - 1) / 2;
|
||||
total_sum = sum_main + sum_anti - a[duplicate_i][duplicate_i];
|
||||
}
|
||||
else
|
||||
{
|
||||
total_sum = sum_main + sum_anti;
|
||||
}
|
||||
|
||||
printf("主对角线和为:%d\n", sum_main);
|
||||
printf("副对角线和为:%d\n", sum_anti);
|
||||
printf("两条对角线总和(重复位置仅一次):%d\n", total_sum);
|
||||
|
||||
return 0;
|
||||
}
|
||||
+249
@@ -0,0 +1,249 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !动态分配二维数组(int ** 类型)
|
||||
int **allocate_matrix(int rows, int cols);
|
||||
|
||||
// !输入l行m列的矩阵
|
||||
void matrix_produce(int l, int m, int a[l][m]);
|
||||
|
||||
// !打印l行n列的矩阵
|
||||
void matrix_print(int l, int n, int c[l][n]);
|
||||
|
||||
// !矩阵的乘法运算
|
||||
void matrix_multiply(int l, int m, int n, int a[l][m], int b[m][n], int c[l][n]);
|
||||
|
||||
// !检查矩阵是否对称
|
||||
int matrix_check_symmetry(int matrix[][200], int size);
|
||||
|
||||
// !转置矩阵:将src[l][m]转置为dest[m][l]
|
||||
void matrix_transpose(int l, int m, int src[l][m], int dest[m][l]);
|
||||
|
||||
// !查找矩阵中的最大值
|
||||
int matrix_max_value(int l, int m, int mat[l][m]);
|
||||
|
||||
// !计算主对角线和(i == j)
|
||||
int calculate_main_diagonal(int n, int mat[n][n]);
|
||||
|
||||
// !计算副对角线和(i + j == n-1)
|
||||
int calculate_anti_diagonal(int n, int mat[n][n]);
|
||||
|
||||
// !计算两个矩阵的和(补零相加)
|
||||
int **add_matrices(int **A, int n1, int m1, int **B, int n2, int m2, int *result_rows, int *result_cols);
|
||||
|
||||
// !找出二维数组中的鞍点(行最大且列最小)
|
||||
void matrix_find_saddle_point(int rows, int cols, int matrix[rows][cols]);
|
||||
|
||||
// !释放二维数组内存
|
||||
void matrix_free(int **matrix, int rows);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int **allocate_matrix(int rows, int cols)
|
||||
{
|
||||
// !动态分配二维数组(int ** 类型)
|
||||
int **matrix = (int **)malloc(rows * sizeof(int *)); // 分配行指针数组
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
matrix[i] = (int *)malloc(cols * sizeof(int)); // 分配每行的列空间
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void matrix_produce(int l, int m, int a[l][m])
|
||||
{
|
||||
// !矩阵的输入
|
||||
int i, j;
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < m; j++)
|
||||
{
|
||||
scanf("%d", &a[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void matrix_multiply(int l, int m, int n, int a[l][m], int b[m][n], int c[l][n])
|
||||
{
|
||||
// !矩阵的乘法运算
|
||||
int i, j, k;
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
c[i][j] = 0;
|
||||
for (k = 0; k < m; k++)
|
||||
{
|
||||
c[i][j] += a[i][k] * b[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void matrix_print(int l, int n, int c[l][n])
|
||||
{
|
||||
// !矩阵的打印
|
||||
int i, j;
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
printf("%d ", c[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int matrix_check_symmetry(int matrix[][200], int size)
|
||||
{
|
||||
// !检查矩阵是否对称
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
for (int j = 0; j < size; j++)
|
||||
{
|
||||
if (matrix[i][j] != matrix[j][i])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void matrix_transpose(int l, int m, int src[l][m], int dest[m][l])
|
||||
{
|
||||
// !转置矩阵:将src[l][m]转置为dest[m][l]
|
||||
for (int i = 0; i < l; i++)
|
||||
{
|
||||
for (int j = 0; j < m; j++)
|
||||
{
|
||||
dest[j][i] = src[i][j]; // 核心转置逻辑
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int matrix_max_value(int l, int m, int mat[l][m])
|
||||
{
|
||||
// !查找mat矩阵中的最大值
|
||||
int max = mat[0][0]; // 初始化最大值为第一个元素
|
||||
for (int i = 0; i < l; i++)
|
||||
{
|
||||
for (int j = 0; j < m; j++)
|
||||
{
|
||||
if (mat[i][j] > max)
|
||||
{
|
||||
max = mat[i][j]; // 更新最大值
|
||||
}
|
||||
}
|
||||
}
|
||||
return max; // 返回最大值
|
||||
}
|
||||
|
||||
int calculate_main_diagonal(int n, int mat[n][n])
|
||||
{
|
||||
// !计算主对角线和(i == j)
|
||||
int sum = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
sum += mat[i][i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int calculate_anti_diagonal(int n, int mat[n][n])
|
||||
{
|
||||
// !计算副对角线和(i + j == n-1)
|
||||
int sum = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
int j = n - 1 - i;
|
||||
sum += mat[i][j];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int **add_matrices(int **A, int n1, int m1, int **B, int n2, int m2, int *result_rows, int *result_cols)
|
||||
{
|
||||
// !计算两个矩阵的和(补零相加)
|
||||
// 计算结果矩阵的尺寸(行数和列数的最大值)
|
||||
*result_rows = (n1 > n2) ? n1 : n2;
|
||||
*result_cols = (m1 > m2) ? m1 : m2;
|
||||
int **result = allocate_matrix(*result_rows, *result_cols); // 动态分配结果矩阵
|
||||
|
||||
// 逐位置计算相加结果(补零逻辑)
|
||||
for (int i = 0; i < *result_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < *result_cols; j++)
|
||||
{
|
||||
int a = (i < n1 && j < m1) ? A[i][j] : 0; // A超出范围则为0
|
||||
int b = (i < n2 && j < m2) ? B[i][j] : 0; // B超出范围则为0
|
||||
result[i][j] = a + b;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void matrix_find_saddle_point(int rows, int cols, int matrix[rows][cols])
|
||||
{
|
||||
// !找出二维数组中的鞍点(行最大且列最小)
|
||||
// 标记是否找到鞍点
|
||||
int has_saddle = 0;
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
// !步骤1:找到当前行的最大值
|
||||
int row_max = matrix[i][0];
|
||||
int col_max = 0;
|
||||
|
||||
for (int j = 0; j < cols; j++)
|
||||
{
|
||||
if (matrix[i][j] > row_max)
|
||||
{
|
||||
row_max = matrix[i][j];
|
||||
col_max = j;
|
||||
}
|
||||
}
|
||||
|
||||
// !步骤2:检查该行最大值是否是所在列的最小值
|
||||
int col_min = matrix[0][col_max]; // 初始化列最小值为当前列第一行
|
||||
for (int k = 1; k < rows; k++)
|
||||
{
|
||||
// 遍历当前列的所有行
|
||||
if (matrix[k][col_max] < col_min)
|
||||
{
|
||||
col_min = matrix[k][col_max]; // 更新列最小值
|
||||
}
|
||||
}
|
||||
|
||||
// !步骤3:判断是否为鞍点(行最大且列最小)
|
||||
if (row_max == col_min)
|
||||
{
|
||||
printf("鞍点坐标为(%d, %d),值为:%d\n", i, col_max, row_max);
|
||||
has_saddle = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 未找到鞍点时提示
|
||||
if (has_saddle == 0)
|
||||
{
|
||||
printf("该矩阵没有鞍点!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void matrix_free(int **matrix, int rows)
|
||||
{
|
||||
// !释放二维数组内存
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
free(matrix[i]); // 释放每行的内存
|
||||
}
|
||||
free(matrix); // 释放行指针数组
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !二维数组
|
||||
/*
|
||||
int a[3][5];
|
||||
通常理解为a是一个3行5列的矩阵
|
||||
*/
|
||||
|
||||
// !二维数组的遍历
|
||||
/*
|
||||
for ( i=0; i<3; i++)
|
||||
{
|
||||
for ( j=0; j<5; j++ )
|
||||
{
|
||||
a[i][j] = i*j;
|
||||
}
|
||||
}
|
||||
a[i][i]是一个int
|
||||
表示第i行第j列上的单元
|
||||
但a[i,j]是数学上表示a是一个i行j列的矩阵,在C语言是a[i][j]
|
||||
*/
|
||||
|
||||
// !二维数组的初始化
|
||||
/*
|
||||
nt a[] [5] =
|
||||
{
|
||||
{0,1,2,3,4},
|
||||
{2,3,4,5,6
|
||||
};
|
||||
列数是必须给出的,行数可以由编译器来数每行一个,逗号分隔
|
||||
最后的逗号可以存在,有古老的传统
|
||||
如果省略,表示补零
|
||||
也可以用定位(*C99 标准)
|
||||
*/
|
||||
|
||||
// !输入l行m列的矩阵
|
||||
void matrix_produce(int l, int m, int a[l][m])
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < m; j++)
|
||||
{
|
||||
scanf("%d", &a[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// !打印l行n列的矩阵
|
||||
void matrix_print(int l, int n, int c[l][n])
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
printf("%d ", c[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int l, m, max;
|
||||
printf("请输入矩阵的行数l和列数m:");
|
||||
scanf("%d %d", &l, &m);
|
||||
|
||||
int a[l][m];
|
||||
|
||||
printf("请输入%d行%d列的矩阵数据:\n", l, m);
|
||||
matrix_produce(l, m, a);
|
||||
|
||||
max = a[0][0];
|
||||
int row = 0;
|
||||
int col = 0;
|
||||
|
||||
for (int i = 0; i < l; i++)
|
||||
{
|
||||
for (int j = 0; j < m; j++)
|
||||
{
|
||||
if (a[i][j] > max)
|
||||
{
|
||||
max = a[i][j];
|
||||
row = i;
|
||||
col = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("矩阵为:\n");
|
||||
matrix_print(l, m, a);
|
||||
printf("矩阵的最大值为:%d,位置为(%d,%d)\n", max, row, col);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !二维数组
|
||||
/*
|
||||
int a[3][5];
|
||||
通常理解为a是一个3行5列的矩阵
|
||||
*/
|
||||
|
||||
// !二维数组的遍历
|
||||
/*
|
||||
for ( i=0; i<3; i++)
|
||||
{
|
||||
for ( j=0; j<5; j++ )
|
||||
{
|
||||
a[i][j] = i*j;
|
||||
}
|
||||
}
|
||||
a[i][i]是一个int
|
||||
表示第i行第j列上的单元
|
||||
但a[i,j]是数学上表示a是一个i行j列的矩阵,在C语言是a[i][j]
|
||||
*/
|
||||
|
||||
// !二维数组的初始化
|
||||
/*
|
||||
nt a[] [5] =
|
||||
{
|
||||
{0,1,2,3,4},
|
||||
{2,3,4,5,6
|
||||
};
|
||||
列数是必须给出的,行数可以由编译器来数每行一个,逗号分隔
|
||||
最后的逗号可以存在,有古老的传统
|
||||
如果省略,表示补零
|
||||
也可以用定位(*C99 标准)
|
||||
*/
|
||||
|
||||
// !输入l行m列的矩阵
|
||||
void matrix_produce(int l, int m, int a[l][m])
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < m; j++)
|
||||
{
|
||||
scanf("%d", &a[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// !打印l行n列的矩阵
|
||||
void matrix_print(int l, int n, int c[l][n])
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < l; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
printf("%d ", c[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int l, m;
|
||||
printf("请输入矩阵的行数l和列数m:");
|
||||
scanf("%d %d", &l, &m);
|
||||
|
||||
int a[l][m];
|
||||
int b[m][l];
|
||||
|
||||
printf("请输入%d行%d列的矩阵数据:\n", l, m);
|
||||
matrix_produce(l, m, a);
|
||||
|
||||
for (int i = 0; i < l; i++)
|
||||
{
|
||||
for (int j = 0; j < m; j++)
|
||||
{
|
||||
b[j][i] = a[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
printf("转置后的矩阵为:\n");
|
||||
matrix_print(m, l, b);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !指针函数
|
||||
/*
|
||||
* 函数指针
|
||||
* C语言允许将函数的地址存储在指针变量中,这种指针称为函数指针。函数指针可以指向任何符合特定签名的函数。
|
||||
* 例如,int (*func_ptr)(int, int) 表示一个指向接受两个int参数并返回int的函数的指针。
|
||||
* 函数指针的声明和使用
|
||||
* 1.声明函数指针
|
||||
* int (*func_ptr)(int, int);
|
||||
* 这表示func_ptr是一个指向接受两个int参数并返回int的函数的指针。
|
||||
* 函数指针是一种数据类型,可以像其他指针一样使用。
|
||||
* 函数指针变量不能进行算术运算,这是与数组指针变量不同的。数组指针变量加减一个整数可使指针移动,指向数组的其他元素,而函数指针的移动是毫无意义的。
|
||||
|
||||
1.动态灵活性
|
||||
可在运行时动态绑定不同函数,实现同一接口调用不同逻辑(如根据配置选择算法),避免静态代码膨胀。
|
||||
2.回调机制支持
|
||||
便于实现回调函数(如事件监听、异步处理),使代码结构更松耦合,增强模块间交互能力。
|
||||
3.函数作为数据存储
|
||||
可将函数存入数组、结构体等数据结构,方便批量管理或按条件调度(如状态机、菜单系统)。
|
||||
4.简化复杂逻辑
|
||||
在算法(如排序、搜索)中通过函数指针传递比较规则,避免为不同规则重复编写代码,提升复用性。
|
||||
5.底层编程优势
|
||||
在操作系统、驱动开发等场景中,可直接操作函数地址,实现动态链接、热更新等底层功能。
|
||||
*/
|
||||
|
||||
// 普通函数:加法
|
||||
int add(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// 普通函数:乘法
|
||||
int multiply(int a, int b)
|
||||
{
|
||||
return a * b;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// 声明函数指针,指向返回int、参数为(int, int)的函数
|
||||
int (*op)(int, int);
|
||||
|
||||
// 动态赋值为加法函数
|
||||
op = add;
|
||||
printf("5 + 3 = %d\n", op(5, 3)); // 输出:8
|
||||
|
||||
// 动态切换为乘法函数
|
||||
op = multiply;
|
||||
printf("5 * 3 = %d\n", op(5, 3)); // 输出:15
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !动态内存分配
|
||||
/*
|
||||
1.输入数据
|
||||
如果输入数据时,先告诉你个数,然后再输入,要记录每个数据
|
||||
C99可以用变量做数组定义的大小,C99之前呢?
|
||||
int *a = (int*)malloc(n*sizeof(int));
|
||||
2.malloc
|
||||
#include <stdlib.h>
|
||||
void* malloc(size_t size);
|
||||
向malloc申请的空间的大小是以字节为单位的
|
||||
返回的结果是void*,需要类型转换为自己需要的类型
|
||||
(int*)malloc(n*sizeof(int))
|
||||
float *pf;
|
||||
pf=(float *)malloc(5*sizeof(float));
|
||||
3.calloc
|
||||
#include <stdlib.h>
|
||||
void* calloc(size_t n, size_t size);
|
||||
分配n个元素,每个元素的大小是size字节
|
||||
calloc会把申请的空间初始化为0
|
||||
malloc不会初始化
|
||||
int *pi;
|
||||
pi=(int *)calloc(5,sizeof(int));
|
||||
4.free()
|
||||
把申请得来的空间还给“系统”
|
||||
申请过的空间,最终都应该要还
|
||||
混出来的,迟早都是要还的
|
||||
只能还申请来的空间的首地址
|
||||
void free(void *p);
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* 1.malloc */
|
||||
int number;
|
||||
int *a;
|
||||
int i;
|
||||
printf("请输入数据个数:");
|
||||
scanf("%d", &number);
|
||||
|
||||
// int a[number];
|
||||
// 定义可变数组
|
||||
a = (int *)malloc(number * sizeof(int));
|
||||
for (i = 0; i < number; i++)
|
||||
{
|
||||
scanf("%d", &a[i]);
|
||||
}
|
||||
|
||||
for (i = number - 1; i >= 0; i--)
|
||||
{
|
||||
printf("%d ", a[i]);
|
||||
}
|
||||
|
||||
free(a);
|
||||
|
||||
/* 2. */
|
||||
void *p;
|
||||
int cnt = 0;
|
||||
while (p = malloc(100 * 1024 * 1024))
|
||||
{
|
||||
cnt++;
|
||||
free(p);
|
||||
}
|
||||
printf("%d100MB的空间\n", cnt);
|
||||
|
||||
/* 3. */
|
||||
/*void *p;
|
||||
int cnt = 0;*/
|
||||
cnt = 0;
|
||||
p = malloc(100 * 1024 * 1024);
|
||||
p++;
|
||||
free(p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !运算符
|
||||
/*
|
||||
scanf("%d", &i);里的&
|
||||
获得变量的地址,它的操作数必须是变量
|
||||
int i; printf("%x",&i);
|
||||
地址的大小是否与int相同取决于编译器
|
||||
int i; printf("%p", &i);
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
// 变量的地址
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
printf("&a = %p\n", &a);
|
||||
printf("&b = %p\n", &b);
|
||||
|
||||
// &操作符的sizeof
|
||||
int x;
|
||||
printf("sizeof(&x) = %zu\n", sizeof(&x)); // 32位系统输出4,64位系统输出8
|
||||
|
||||
// 数组的地址
|
||||
int arr[3] = {1, 2, 3};
|
||||
printf("arr地址 = %p\n", (void *)arr); // 0x7ffee3a45680
|
||||
printf("&arr[0] = %p\n", &arr[0]); // 0x7ffee3a45680
|
||||
printf("&arr = %p\n", &arr); // 0x7ffee3a45680(值相同但类型不同)
|
||||
|
||||
// 数组单元的地址
|
||||
printf("&arr[0] = %p\n", &arr[0]); // 0x7ffee3a45680
|
||||
printf("&arr[1] = %p\n", &arr[1]); // 0x7ffee3a45684(相差4字节)
|
||||
printf("&arr[2] = %p\n", &arr[2]); // 0x7ffee3a45688(继续相差4字节)
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !指针和const
|
||||
/*
|
||||
指针本身和所指的变量都可能const
|
||||
1.指针是const:
|
||||
表示一旦得到了某个变量的地址,不能再指向其他变量
|
||||
int *const q= &i; // q是const
|
||||
*q=26; // OK
|
||||
q++; // ERROR
|
||||
2.所指是const
|
||||
表示不能通过这个指针去修改那个变量(并不 能使得那个变量成为const)
|
||||
const int *p = &i;
|
||||
*p = 26; // ERROR! (*p)是 const
|
||||
i=26; //OK
|
||||
p=&j; //OK
|
||||
3.判断:const在*前边,是表示不能通过这个指针去修改那个变量;const在*后边,是表示一旦得到了某个变量的地址,不能再指向其他变量
|
||||
*/
|
||||
|
||||
// !const数组
|
||||
/*
|
||||
const int a[]= {1,2,3,4,5,6,};
|
||||
数组变量已经是const的指针了,这里的const
|
||||
表明数组的每个单元都是const int
|
||||
所以必须通过初始化进行赋值
|
||||
*/
|
||||
|
||||
// !保护数组值
|
||||
/*
|
||||
因为把数组传入函数时传递的是地址,所以那个函数 内部可以修改数组的值
|
||||
为了保护数组不被函数破坏,可以设置参数为const
|
||||
int sum(const int a[], int length);
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !数组是一种特殊的指针,数组名就是数组的首地址
|
||||
/*
|
||||
数组变量本身表达地址,所以
|
||||
int a[10]; int*p=a;//无需用&取地址
|
||||
但是数组的单元表达的是变量,需要用&取地址
|
||||
a == &a[O]
|
||||
[]运算符可以对数组做,也可以对指针做:
|
||||
p[0] <==>a[0]
|
||||
*运算符可以对指针做,也可以对数组做:
|
||||
*a= 25:
|
||||
数组变量是const的指针,所以不能被赋值
|
||||
int a[] <==> int * const a=...
|
||||
*/
|
||||
|
||||
void minmax(int a[], int len, int *max, int *min);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int c[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 16, 17, 21, 23, 55};
|
||||
int min, max;
|
||||
printf("min sizeof(c) = %lu\n", sizeof(c));
|
||||
minmax(c, sizeof(c) / sizeof(c[0]), &max, &min);
|
||||
printf("max = %d, min = %d\n", max, min);
|
||||
|
||||
int *p = &min;
|
||||
printf("*p = %d\n", *p);
|
||||
printf("p[0]=%d\n", p[0]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void minmax(int a[], int len, int *max, int *min)
|
||||
{
|
||||
int i;
|
||||
printf("minmax sizeof(a) = %lu\n", sizeof(a));
|
||||
*max = *min = a[0];
|
||||
for (i = 1; i < len; i++)
|
||||
{
|
||||
if (a[i] < *min)
|
||||
{
|
||||
*min = a[i];
|
||||
}
|
||||
if (a[i] > *max)
|
||||
{
|
||||
*max = a[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !指针函数
|
||||
/*
|
||||
一个函数不仅可以返回整型、字符型等值,也可在C语言中,以返回指针类型的值,返回指针类型值的函数就称为指针函数。
|
||||
1.定义指针函数的形式如下:
|
||||
类型标识符 *函数名(形参表)
|
||||
{
|
||||
函数体
|
||||
}
|
||||
2.指针函数的调用与普通函数的调用相同,只要注意其返口值是一个指针。
|
||||
|
||||
* int (*p)()是一个变量声明,表示p是一个指向函数入口地址的指针变量(函数指针),该函数的返回值是整型数据,“(*p)”两边的括号不能少
|
||||
* int *p()则是函数声明,表示p是一个指针函数,其返回值是一个指向整型数据的指针,“*p”两边没有括号。作为函数声明,在括号内最好写入形式参数,这样便于与变量声明区别。
|
||||
*/
|
||||
|
||||
// !指针函数:返回指向整数的指针
|
||||
int *max(int *a, int *b)
|
||||
{
|
||||
if (*a > *b)
|
||||
return a;
|
||||
else
|
||||
return b;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x, y;
|
||||
printf("Enter two integers: ");
|
||||
scanf("%d %d", &x, &y);
|
||||
|
||||
// 调用指针函数
|
||||
int *result = max(&x, &y);
|
||||
printf("The larger number is %d\n", *result);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !指针变量
|
||||
/*
|
||||
变量的值是内存的地址
|
||||
普通变量的值是实际的值
|
||||
指针变量的值是具有实际值的变量的地址
|
||||
*/
|
||||
|
||||
// !访问那个地址上的变量*
|
||||
/*
|
||||
*p: p指向的变量的值*是一个单目运算符,用来访问指针的值所表示的地址上的变量
|
||||
可以做右值也可以做左值
|
||||
int k= *p;
|
||||
*p=k+1;
|
||||
*/
|
||||
|
||||
void f(int *p);
|
||||
void f2(int k);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i = 6;
|
||||
printf("i = %d\n", i);
|
||||
printf("&i = %p\n", &i); // %p打印i地址
|
||||
f(&i);
|
||||
f2(i);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void f(int *p)
|
||||
{
|
||||
printf("p = %p\n", p); // %p打印p地址
|
||||
printf("*p = %d\n", *p); // %d打印p指向的变量的值
|
||||
*p = 26; // 修改p指向的变量的值
|
||||
}
|
||||
|
||||
void f2(int k)
|
||||
{
|
||||
printf("k = %d\n", k);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !指针数组
|
||||
/*
|
||||
* 指针数组是一个数组,其每个元素都是指针变量(存储内存地址)。它的本质是 “存储指针的数组”,核心作用是批量管理多个指针,适用于需要集中操作多个地址的场景(如字符串集合、动态内存块管理等)
|
||||
1.类型* 数组名[数组长度]; // !类型* 表示数组元素是该类型的指针
|
||||
在 C 语言中,字符串本质是char*指针(指向字符数组的首地址)。因此,指针数组是存储多个字符串的高效方式(仅存储字符串的地址,而非复制整个字符串内容)
|
||||
2.本质是数组,元素是指针。
|
||||
存储多个独立指针(如多个变量地址)。
|
||||
3.指针数组通过集中存储多个指针,实现了对多数据的高效管理,尤其在处理字符串集合、动态内存块或需要批量操作指针的场景中,能显著提升内存 利用率和代码灵活性。
|
||||
4.
|
||||
int a = 10, b = 20, c = 30;
|
||||
int *ptr_arr[3]; // 声明一个包含3个int指针的数组
|
||||
|
||||
ptr_arr[0] = &a; // 存储a的地址
|
||||
ptr_arr[1] = &b; // 存储b的地址
|
||||
ptr_arr[2] = &c; // 存储c的地址
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
// 基本类型指针数组
|
||||
int num1 = 5, num2 = 10;
|
||||
int *num_ptrs[2] = {&num1, &num2};
|
||||
printf("Numbers: ");
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
printf("%d ", *num_ptrs[i]); // 输出:5 10
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// 字符串指针数组
|
||||
char *fruits[] = {"Apple", "Banana", "Cherry"};
|
||||
printf("Fruits: ");
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
printf("%s ", fruits[i]); // 输出:Apple Banana Cherry
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// 二维数组行指针数组
|
||||
int table[3][2] = {{1, 2}, {3, 4}, {5, 6}};
|
||||
int *row_ptrs[3] = {table[0], table[1], table[2]};
|
||||
printf("Table:\n");
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
for (int j = 0; j < 2; j++)
|
||||
{
|
||||
printf("%d ", row_ptrs[i][j]); // 输出每行元素
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
// 输出:
|
||||
// 1 2
|
||||
// 3 4
|
||||
// 5 6
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !指针应用场景
|
||||
/*
|
||||
1.函数返回多个值,某些值不方便直接返回,只能通过指针返回,传入的参数实际上是需要保存带回的结果的变量
|
||||
2.函数返回运算的状态,结果通过指针返回。常用的套路是让函数返回特殊的不属于有效范围内的 值来表示出错:-1或0(在文件操作会看到大量的例子)。当任何数值都是有效的可能结果时,就得分开返
|
||||
|
||||
*/
|
||||
|
||||
void swap(int *pa, int *pb);
|
||||
void minmax(int c[], int len, int *max, int *min);
|
||||
int divide(int a, int b, int *result);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* 1.交换两个变量的值 */
|
||||
int a = 5;
|
||||
int b = 6;
|
||||
|
||||
swap(&a, &b);
|
||||
printf("a = %d, b = %d\n", a, b);
|
||||
|
||||
/* 2.返回多个值*/
|
||||
int c[] = {1,2,3,4,5,6,7,8,9,12,13,14,16,17,21,23,55};
|
||||
int min, max;
|
||||
minmax(c, sizeof(c) / sizeof(c[0]), &max, &min);
|
||||
printf("max = %d, min = %d\n", max, min);
|
||||
|
||||
/* 3.返回运算的状态 */
|
||||
int d=5;
|
||||
int e=6;
|
||||
int f;
|
||||
if (divide(d,e, &f))
|
||||
{
|
||||
printf("%d/%d = %d\n", d, e, f);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void swap(int *pa, int *pb)
|
||||
{
|
||||
int t = *pa;
|
||||
*pa = *pb;
|
||||
*pb = t;
|
||||
}
|
||||
|
||||
void minmax(int a[], int len, int *max, int *min)
|
||||
{
|
||||
int i;
|
||||
*max = *min = a[0];
|
||||
for (i = 1; i < len; i++)
|
||||
{
|
||||
if (a[i] < *min)
|
||||
{
|
||||
*min = a[i];
|
||||
}
|
||||
if (a[i] > *max)
|
||||
{
|
||||
*max = a[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int divide(int a, int b, int *result)
|
||||
{
|
||||
if (b==0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
*result = a / b;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// !指针运算
|
||||
/*
|
||||
1.指针p+1,
|
||||
指针p的值会自动增加一个存储单元的大小(sizeof(数据类型)),即指向下一个元素的存储单元
|
||||
给一个指针加1表示要让指针指向下一个变量
|
||||
int a[10];
|
||||
int *p = a;
|
||||
*(p+1)—>a[1]
|
||||
如果指针不是指向一片连续分配的空间,如数组,则这种运算没有意义
|
||||
即*(p+n)<——>ac[n]
|
||||
2.*p++
|
||||
取出p所指的那个数据来,完事之后顺便把p移到下一个位置去
|
||||
*的优先级虽然高,但是没有++高
|
||||
常用于数组类的连续空间操作
|
||||
在某些CPU上,这可以直接被翻译成一条汇编指令
|
||||
3.指针比较
|
||||
<,<=, ==,>,>=,!=都可以对指针做
|
||||
比较它们在内存中的地址
|
||||
数组中的单元的地址肯定是线性递增的
|
||||
4.0地址
|
||||
当然你的内存中有0地址,但是0地址通常是不能随便碰的地址,所以你的指针不应该具有0值
|
||||
因此可以用0地址来表示特殊的事情: 返回的指针是无效的,指针没有被真正初始化(先初始化为0)
|
||||
NULL是一个预定定义的符号,表示0地址
|
||||
有的编译器不愿意你用0来表示0地址
|
||||
5.指针的类型
|
||||
无论指向什么类型,所有的指针的大小都是一样的,因为都是地址
|
||||
但是指向不同类型的指针是不能直接互相
|
||||
赋值的 这是为了避免用错指针
|
||||
6.指针的类型转换
|
||||
void*表示不知道指向什么东西的指针
|
||||
计算时与char*相同(但不相通)
|
||||
指针也可以转换类型
|
||||
int *p = &i;void*q = (void*)p;
|
||||
这并没有改变p所指的变量的类型,而是让后人用不同的眼光通过p看它所指的变量
|
||||
我不再当你是int啦,我认为你就是个void!
|
||||
7.用指针来做什么
|
||||
需要传入较大的数据时用作参数
|
||||
传入数组后对数组做操作
|
||||
函数返回不止一个结果
|
||||
需要用函数来修改不止一个变量
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* 1.指针p+1 */
|
||||
char ac[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
char *p = ac; // 指向数组首元素的指针,相当于char *p = &ac[0]
|
||||
char *p1 = &ac[5];
|
||||
printf("p = %p\n", p); // %p是以16进制输出地址
|
||||
printf("p+1 = %p\n", p + 1);
|
||||
printf("p1-p = %d\n", p1 - p);
|
||||
|
||||
int ad[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
int *q = ad;
|
||||
int *q1 = &ad[6];
|
||||
printf("q = %p\n", q); // %p是以16进制输出地址
|
||||
printf("q+1 = %p\n", q + 1);
|
||||
printf("q1-q = %d\n", q1 - q);
|
||||
|
||||
/* 2.*p++ */
|
||||
char ae[] = {0,1,2,3,4,5,6,7,8,9,-1};
|
||||
char *p2 = ae;
|
||||
int i = 0;
|
||||
for (i = 0; i < sizeof(ae) / sizeof(ae[0]); i++)
|
||||
{
|
||||
printf("%d\n", ae[i]);
|
||||
}
|
||||
for (p2 = ae;*p2!=-1;p2++) // *p2++先取值,再指向下一个元素
|
||||
{
|
||||
printf("%d\n", *p2);
|
||||
}
|
||||
|
||||
/* 4.0地址 */
|
||||
char ai[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
char *q3 = ai;
|
||||
q3=p; //
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !单字符的输入输出
|
||||
/*
|
||||
1.putchar
|
||||
int putchar(int c);
|
||||
向标准输出写一个字符
|
||||
返回写了几个字符,EOF(-1)表示写失败
|
||||
2.getchar
|
||||
int getchar(void);
|
||||
从标准输入读入一个字符
|
||||
返回类型是int是为了返回EOF (-1)
|
||||
Windows->Ctrl-Z
|
||||
Unix—>Ctrl-D
|
||||
3.
|
||||
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int ch;
|
||||
|
||||
while ((ch=getchar())!=EOF)
|
||||
{
|
||||
putchar(ch);
|
||||
}
|
||||
|
||||
printf("EOF\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !
|
||||
/*
|
||||
1.字符串
|
||||
以0(整数0)结尾的一串字符
|
||||
0或'\0'是一样的,但是和'0'不同
|
||||
O标志字符串的结束,但它不是字符串的一部分
|
||||
计算字符串长度的时候不包含这个0
|
||||
字符串以数组的形式存在,以数组或指针的形式访问
|
||||
更多的是以指针的形式
|
||||
string.h 里有很多处理字符串的函数
|
||||
2.字符串常量
|
||||
“Hello”
|
||||
"He11o”会被编译器变成一个字符数组放在某处,这个数组的长度是6,结尾还有表示结束的0
|
||||
两个相邻的字符串常量会被自动连接起来
|
||||
3.字符串
|
||||
C语言的字符串是以字符数组的形态存在的
|
||||
不能用运算符对字符串做运算
|
||||
通过数组的方式可以遍历字符串
|
||||
唯一特殊的地方是字符串字面量可以用来初始化字符数组
|
||||
以及标准库提供了一系列字符串函数
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* 1.字符串的定义 */
|
||||
char word[] = {'H', 'e', 'l', 'l', 'l', 'o', '\0'};
|
||||
|
||||
/* 2.字符串变量 */
|
||||
char *p = "Hello";
|
||||
char word2[] = "Hello";
|
||||
char line[10] = "Hello";
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串函数string.h
|
||||
/*
|
||||
1.string.h
|
||||
strlen、strcmp、strcpy、strcat、strchr、strstr
|
||||
2.strlen
|
||||
size_t strlen(const char *s);
|
||||
返回s的字符串长度(不包括结尾的0)
|
||||
3.strcmp
|
||||
int strcmp(const char *s1, const char *s2); 比较两个字符串,返回:
|
||||
0:s1==S2
|
||||
1:s1>s2
|
||||
-1:s1<s2
|
||||
4.strcpy
|
||||
char * strcpy(char *restrict dst, const char *restrict src);
|
||||
把src的字符串拷贝到dst
|
||||
restrict表明src和dst不重叠(C99)
|
||||
返回dst 为了能链起代码来
|
||||
* 复制一个字符串
|
||||
* char *dst =(char*)malloc(strlen(src)+1);
|
||||
* strcpy(dst, src);
|
||||
5.字符串中找字符
|
||||
char*strchr(const char *s, int c);
|
||||
char*strrchr(const char *s, int c);
|
||||
返回NULL表示没有找到
|
||||
如何寻找第2个?
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char s[]="hello";
|
||||
char *p1 = strchr( s, 'l' );
|
||||
printf("%s\n", p1);
|
||||
p1 = strchr(p1+1, 'l' ); // !找到p1的下一个
|
||||
printf("%s\n", p1);
|
||||
|
||||
char *p2 = strrchr( s, 'l' );
|
||||
printf("%s\n", p2);
|
||||
|
||||
char *t = (char *)malloc(strlen(p1)+1);
|
||||
printf("%s\n", t);
|
||||
free(t);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串函数string.h
|
||||
/*
|
||||
1.string.h
|
||||
strlen、strcmp、strcpy、strcat、strchr、strstr
|
||||
2.strlen
|
||||
size_t strlen(const char *s);
|
||||
返回s的字符串长度(不包括结尾的0)
|
||||
3.strcmp
|
||||
int strcmp(const char *s1, const char *s2); 比较两个字符串,返回:
|
||||
0:s1==S2
|
||||
1:s1>s2
|
||||
-1:s1<s2
|
||||
4.strcpy
|
||||
char * strcpy(char *restrict dst, const char *restrict src);
|
||||
把src的字符串拷贝到dst
|
||||
restrict表明src和dst不重叠(C99)
|
||||
返回dst 为了能链起代码来
|
||||
* 复制一个字符串
|
||||
* char *dst =(char*)malloc(strlen(src)+1);
|
||||
* strcpy(dst, src);
|
||||
5.字符串中找字符
|
||||
char*strchr(const char *s, int c);
|
||||
char*strrchr(const char *s, int c);
|
||||
返回NULL表示没有找到
|
||||
如何寻找第2个?
|
||||
6.字符串中找字符串
|
||||
char * strstr(const char *s1, const char *s2);
|
||||
char * strcasestr(const char *s1, const char *s2);
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串函数string.h
|
||||
/*
|
||||
1.string.h
|
||||
strlen、strcmp、strcpy、strcat、strchr、strstr
|
||||
2.strlen
|
||||
size_t strlen(const char *s);
|
||||
返回s的字符串长度(不包括结尾的0)
|
||||
3.strcmp
|
||||
int strcmp(const char *s1, const char *s2); 比较两个字符串,返回:
|
||||
0:s1==S2
|
||||
1:s1>s2
|
||||
-1:s1<s2
|
||||
*/
|
||||
|
||||
int my_strcmp(const char *s1, const char *s2)
|
||||
{
|
||||
int idx = 0;
|
||||
while (s1[idx]!='\0' && s1[idx] == s2[idx])
|
||||
{
|
||||
idx++;
|
||||
}
|
||||
return s1[idx] - s2[idx];
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* strcmp */
|
||||
char s1[] = "abc";
|
||||
char s2[] = "abc";
|
||||
printf("strcmp=%d\n", strcmp(s1, s2));
|
||||
|
||||
char s3[] = "abc";
|
||||
char s4[] = "bbc";
|
||||
printf("strcmp=%d\n", strcmp(s3, s4));
|
||||
|
||||
char s5[] = "abc";
|
||||
char s6[] = "Abc";
|
||||
printf("strcmp=%d\n", strcmp(s5, s6));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// !字符串函数string.h
|
||||
/*
|
||||
1.string.h
|
||||
strlen、strcmp、strcpy、strcat、strchr、strstr
|
||||
2.strlen
|
||||
size_t strlen(const char *s);
|
||||
返回s的字符串长度(不包括结尾的0)
|
||||
3.strcmp
|
||||
int strcmp(const char *s1, const char *s2); 比较两个字符串,返回:
|
||||
0:s1==S2
|
||||
1:s1>s2
|
||||
-1:s1<s2
|
||||
4.strcpy
|
||||
char * strcpy(char *restrict dst, const char *restrict src);
|
||||
把src的字符串拷贝到dst
|
||||
restrict表明src和dst不重叠(C99)
|
||||
返回dst 为了能链起代码来
|
||||
* 复制一个字符串
|
||||
* char *dst =(char*)malloc(strlen(src)+1);
|
||||
* strcpy(dst, src);
|
||||
*/
|
||||
|
||||
int my_strcpy(char *restrict dst, const char *restrict src)
|
||||
{
|
||||
int idx = 0;
|
||||
while (src[idx]!='\0')
|
||||
{
|
||||
dst[idx]=src[idx];
|
||||
idx++;
|
||||
}
|
||||
dst[idx]='\0';
|
||||
return dst;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* strcpy */
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user