bin554385863
发表于 2019-6-10 17:36:25
本帖最后由 bin554385863 于 2019-6-10 19:38 编辑
2019年6月10日17:36:14
指针详解
https://blog.csdn.net/qq_34930488/article/details/81030062
总结:
设,有程序:
void main ()
{
...;
int a = 1;
int array[] = {...,1...};
int array2 = {
{...,1,...},
{...,2,...}
};
int *p = NULL;
...;
}
p = &a;
指针p指向变量a的地址;
p = array
或
p = &array
指针p指向数组array的首地址;
则有
array[ i ]= * ( p + i );
或
array[ i ] = * ( array + i );
p = array2;
或
p = &array2[ 0 ];
或
p = &array2[ 0 ][ 0 ];
则有
array2[ m ][ n ] = *(*(p+m)+n);
或
array2[ m ][ n ] = *(*(array2+m)+n);
或
转化为一维数组array3[]
int i = m*n;
array3[ i ] = *(*array2+i);
bin554385863
发表于 2019-6-11 19:05:46
本帖最后由 bin554385863 于 2019-6-12 22:30 编辑
指针的一些复杂说明:
int p; -- 这是一个普通的整型变量
int *p; -- 首先从 p 处开始,先与*结合,所以说明 p 是一个指针, 然后再与 int 结合, 说明指针所指向的内容的类型为 int 型。所以 p 是一个返回整型数据的指针。
int p -- 首先从 p 处开始,先与[] 结合,说明 p 是一个数组, 然后与 int 结合, 说明数组里的元素是整型的, 所以 p 是一个由整型数据组成的数组。
int *p; -- 首先从 p 处开始, 先与 [] 结合, 因为其优先级比 * 高,所以 p 是一个数组, 然后再与 * 结合, 说明数组里的元素是指针类型, 然后再与 int 结合, 说明指针所指向的内容的类型是整型的, 所以 p 是一个由返回整型数据的指针所组成的数组。
int (*p); -- 首先从 p 处开始, 先与 * 结合,说明 p 是一个指针然后再与 [] 结合(与"()"这步可以忽略,只是为了改变优先级), 说明指针所指向的内容是一个数组, 然后再与int 结合, 说明数组里的元素是整型的。所以 p 是一个指向由整型数据组成的数组的指针。
int **p; -- 首先从 p 开始, 先与 * 结合, 说是 p 是一个指针, 然后再与 * 结合, 说明指针所指向的元素是指针, 然后再与 int 结合, 说明该指针所指向的元素是整型数据。由于二级指针以及更高级的指针极少用在复杂的类型中, 所以后面更复杂的类型我们就不考虑多级指针了, 最多只考虑一级指针。
int p(int); -- 从 p 处起,先与 () 结合, 说明 p 是一个函数, 然后进入 () 里分析, 说明该函数有一个整型变量的参数, 然后再与外面的 int 结合, 说明函数的返回值是一个整型数据。
int (*p)(int); -- 从 p 处开始, 先与指针结合, 说明 p 是一个指针, 然后与()结合, 说明指针指向的是一个函数, 然后再与()里的 int 结合, 说明函数有一个int 型的参数, 再与最外层的 int 结合, 说明函数的返回类型是整型, 所以 p 是一个指向有一个整型参数且返回类型为整型的函数的指针。
int *(*p(int)); -- 可以先跳过, 不看这个类型, 过于复杂从 p 开始,先与 () 结合, 说明 p 是一个函数, 然后进入 () 里面, 与 int 结合, 说明函数有一个整型变量参数, 然后再与外面的 * 结合, 说明函数返回的是一个指针, 然后到最外面一层, 先与[]结合, 说明返回的指针指向的是一个数组, 然后再与 * 结合, 说明数组里的元素是指针, 然后再与 int 结合, 说明指针指向的内容是整型数据。所以 p 是一个参数为一个整数据且返回一个指向由整型指针变量组成的数组的指针变量的函数。
2019年6月11日19:03:54
指针与函数
指针作为函数的参数
#include <stdio.h>
void swap(int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
int main()
{
int a = 20, b = 100;
int *i, *j;
i = &a;
j = &b;
printf("交换前\na = %d b = %d\n", a, b);
swap(i, j); //等效于swap(&a, &b)
printf("交换后\na = %d b = %d\n", a, b);
return 0;
}
----------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-nwo3ppih.t3q --stdout=Microsoft-MIEngine-Out-orc5bl0n.fz5 --stderr=Microsoft-MIEngine-Error-umt0mwel.xzb --pid=Microsoft-MIEngine-Pid-4wxeuray.ng4 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
交换前
a = 20 b = 100
交换后
a = 100 b = 20
E:\Administrator\Documents\My C>
================================================================
指针作为函数的返回值:
#include <stdio.h>
char* search(char *str, char c)
{
char *p = str;
while (*p != '\0')
{
if (*p == c)
{
return p;
}
p++;
}
return NULL;
}
int main()
{
char *stra = "this is a test text", *j;
char m;
int a;
printf("请输入查找的字符:");
m = getchar();
j = search(stra, m);
a = (j - stra) +1;
printf("%c在字符串的第%d个位置", m, a );
return 0;
}
-----------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-m0mcz5ks.sc0 --stdout=Microsoft-MIEngine-Out-drfpigtq.m3m --stderr=Microsoft-MIEngine-Error-h2uync3s.5l3 --pid=Microsoft-MIEngine-Pid-fpsjec0r.ilc --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
请输入查找的字符:a
a在字符串的第9个位置
E:\Administrator\Documents\My C>
===================================================================================
函数指针
函数指针是指向函数的指针变量。
通常我们说的指针变量是指向一个整型、字符型或数组等变量,而函数指针是指向函数。
函数指针可以像一般函数一样,用于调用函数、传递参数。
函数指针变量的声明:
typedef int (*fun_ptr)(int,int); // 声明一个指向同样参数、返回值的函数指针类型
通过函数指针实现函数调用函数
#include <stdio.h>
int max(int a, int b)
{
return a+b;
}
int min(int a, int b)
{
return a - b;
}
int sum(int (*p)(int a, int b), int (*k)(int a, int b), int a, int b)//函数指针作为函数的参数;
{
return (*p)(a, b)+(*k)(a, b);
}
int main()
{
int m = 5, n = 10, c;
c = sum(max, min, m, n);//将函数名传递给指针;
printf("%d", c);
return 0;
}
---------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-oyt2e01i.jib --stdout=Microsoft-MIEngine-Out-bzv1bnmt.dpl --stderr=Microsoft-MIEngine-Error-tjrk4xtk.5s5 --pid=Microsoft-MIEngine-Pid-ussezm2z.aph --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
10
E:\Administrator\Documents\My C>
==============================================================
le1314
发表于 2019-6-13 23:30:27
赞,太棒了{:5_106:}
le1314
发表于 2019-6-13 23:35:27
WOJIAOCHENXU 发表于 2019-5-21 16:53
你好 请问你有这方面的视频完整的视频吗 我在论坛里发现好多0基础C语言都打开不了.有的话可否发份链接给我...
链接:https://pan.baidu.com/s/19NC9Xdt5JaqL4JkUKcNc4w
提取码:kw1m
复制这段内容后打开百度网盘手机App,操作更方便哦
虽然是旧版的,但也不错哦
bin554385863
发表于 2019-6-14 11:28:31
le1314 发表于 2019-6-13 23:30
赞,太棒了
{:10_256:}谢谢
bin554385863
发表于 2019-6-20 21:13:48
2019年6月20日21:10:18
[指针练习]
#include <stdio.h>
/*随机输入三个数字大小排序后按正序和错位输出 */
int main(int argc, char const *argv[])
{
int i, j, k, a;
int *pi = &i, *pj = &j, *pk = &k;
printf("请输入三个数字:\n");
for (size_t o = 0, t = 0; o < 3; o++)
{
scanf("%d", &a);
for (size_t x = 0; x < o; x++)
{
if (a > a)
{
t = a;
a = a;
a = t;
}
}
}
i = a;
j = a;
k = a;
printf("原值:\ni = %d\nj = %d\nk = %d\n", i, j, k);
*pi = a;
*pj = a;
*pk = a;
printf("新值:\ni = %d\nj = %d\nk = %d\n", i, j, k);
return 0;
}
------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-mabvuvob.te0 --stdout=Microsoft-MIEngine-Out-1qe2mqqj.zck --stderr=Microsoft-MIEngine-Error-bnrj151y.wva --pid=Microsoft-MIEngine-Pid-gyrjppro.t0k --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
请输入三个数字:
96
63
125
原值:
i = 63
j = 96
k = 125
新值:
i = 96
j = 125
k = 63
E:\Administrator\Documents\My C>
==========================================================================
bin554385863
发表于 2019-6-22 17:27:57
本帖最后由 bin554385863 于 2019-6-22 17:29 编辑
2019年6月22日17:28:03
约瑟夫问题
/*30个人围坐一圈,按顺序编号,从1开始循环报数凡报7的人就退出圈子,按照顺序输出退出人的编号*/
#include <stdio.h>
#define sz 30
void goout(int pp[], int po[], int n) //将数组pp中的数据从第一个按n循环输出下标值到po[]中
{
int i, temp, *p;
p = pp; //p指向数组的首地址
for (size_t i = 0; i < sz; i++)
{
temp = 0;
while (temp < n) //循环报数
{
if (*p != NULL)
{
if (p == (pp + sz)) //如果到达数组尾部则返回数组头部
{
p = pp;
}
else
{
p = p + 1;
temp = temp + 1;
}
}
else
{
if (p == (pp + sz))
{
p = pp; //如果到达数组尾部则返回数组头部
}
p = p + 1;
}
}
p = p - 1;
po = *p; //生成输出顺序
*p = 0; //标记退出数列
}
}
int main(int argc, char const *argv[])
{
int persion;
int pout;
int n;
printf("请输入循环数n(n > 0)\n");
scanf("%d", &n);
for (size_t i = 0; i < sz; i++)
{
persion = i + 1;
}
printf("数组原始编号: \n");
for (size_t i = 0; i < sz; i++)
{
printf("person[%d] = %d\t\t\t", i, persion);
}
printf("\n");
goout(persion, pout, n);
printf("输出顺序\n");
for (size_t i = 0; i < sz; i++)
{
printf("poout[%d] = %d \t\t\t", i, pout);
}
printf("\n");
return 0;
}
--------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-z5ens1gd.rpw --stdout=Microsoft-MIEngine-Out-trx40uwk.nzv --stderr=Microsoft-MIEngine-Error-f1ckheoz.w4t --pid=Microsoft-MIEngine-Pid-n2rzycnk.pdm --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
请输入循环数n(n > 0)
7
数组原始编号:
person = 1 person = 2 person = 3 person = 4 person = 5 person = 6 person = 7
person = 8 person = 9 person = 10 person = 11 person = 12 person = 13 person = 14
person = 15 person = 16 person = 17 person = 18 person = 19 person = 20 person = 21
person = 22 person = 23 person = 24 person = 25 person = 26 person = 27 person = 28
person = 29 person = 30
输出顺序
poout = 7 poout = 14 poout = 21 poout = 28 poout = 5 poout = 13 poout = 22
poout = 30 poout = 9 poout = 18 poout = 27 poout = 8 poout = 19 poout = 1
poout = 12 poout = 25 poout = 10 poout = 24 poout = 11 poout = 29 poout = 17
poout = 6 poout = 3 poout = 2 poout = 4 poout = 16 poout = 26 poout = 15
poout = 20 poout = 23
E:\Administrator\Documents\My C>
bin554385863
发表于 2019-6-25 18:16:45
本帖最后由 bin554385863 于 2019-6-29 23:40 编辑
2019年6月25日17:50:36
第九章 结构体
利用结构体使函数返回多个值
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
/*在字符串内查找一个字符,并输出其出现的次数和下标 */
typedef struct
{
int index;
int count;
} strsch;
strsch *strch(char str[], char key, strsch *s) //定义一个返回值为结构体指针的函数
{
int cnt = 0; //保存字符出现的次数
int inx = 0; //保存字符首次出现的下标
char *p = NULL; //初始化临时指针
p = str;
for (size_t i = 0; *(p + i) != '\0'; i++)
{
if (*(p + i) == key)
{
cnt++; //判断字符出现的次数
}
}
for (size_t i = 0; *(p + i) != '\0'; i++)
{
if (*(p + i) == key)
{
inx = i; //判断字符首次出现的下标
break;
}
}
if (cnt < 1) //字符不存在的情况
{
cnt = -1;
inx = -1;
}
s->index = inx;
s->count = cnt;
return s;
}
int main(int argc, char const *argv[])
{
strsch *i;//改为strsch i
char c;
srand(time(NULL));
char str;//改为27
for (size_t j = 0; j < 26(改为27); j++)
{
str = (j / 2) > sqrt(j) ? (rand() % 13 + 78) : (rand() % 13 + 110); //给字符串赋值
}
str = '\0'; //手动添加结束字符
printf("字符串:\n%s", str);
printf("\n");
scanf("%c", &c);
strch(str, c, i);//改为 strch(str, c, &i)
printf("字符:%c\n出现次数: %d\n出现位置: %d", c, i->count, i->index);//改为i.counti.index
return 0;
}
================================================================
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-k05awheq.lzf --stdout=Microsoft-MIEngine-Out-dcxkgn2k.ens --stderr=Microsoft-MIEngine-Error-ivcy4nkx.pcm --pid=Microsoft-MIEngine-Pid-utjapkxe.wez --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
字符串:
ztxvrzQXOPPXSZNVOPZWUYRYQQ
P
字符:P
出现次数: 4
出现位置: 9
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-xtomnapx.y2r --stdout=Microsoft-MIEngine-Out-zscchclu.wsy --stderr=Microsoft-MIEngine-Error-hfkpnrtf.z25 --pid=Microsoft-MIEngine-Pid-nqh3nzax.0hr --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
字符串:
nuoyztTRSSVWQTYOTUSZXVRZSP
u
字符:u
出现次数: 2
出现位置: 1
E:\Administrator\Documents\My C>
---------------------------------------------------------------------------------------------------------------------------------
改过后输出正常
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-rrw5akhh.eky --stdout=Microsoft-MIEngine-Out-5q4jcdig.3ro --stderr=Microsoft-MIEngine-Error-w5bkhmm0.etq --pid=Microsoft-MIEngine-Pid-1qpfbbxk.25x --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
字符串:
xuwztnTSVNYSYZNNOWRWVYUPVY
x
字符:x
出现次数: 1
首次位置: 0
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-1oubivzw.ayi --stdout=Microsoft-MIEngine-Out-onb04m3f.snf --stderr=Microsoft-MIEngine-Error-k0vlxad1.ev1 --pid=Microsoft-MIEngine-Pid-udsj3jy2.xrf --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
字符串:
xounoyYNOYYTOYVPXPRWWXUQVS
o
字符:o
出现次数: 2
首次位置: 1
E:\Administrator\Documents\My C>
--------------------------------------------------------------------------------------------------
bin554385863
发表于 2019-6-29 23:42:02
本帖最后由 bin554385863 于 2019-6-30 00:01 编辑
2019年6月29日23:41:47
单向链表
最原始的链表
#include <stdio.h>
typedef struct Student
{
char name;
struct Student *next;
} stu;
int main(int argc, char const *argv[])
{
stu s1 = {"hello", NULL};
stu s2 = {"world", NULL};
stu s3 = {"C", NULL};
stu s4 = {"language", NULL};
s1.next = &s2;
s2.next = &s3;
s3.next = &s4;
stu *p = &s1;
while (1)
{
printf("%s ", p->name);
if (p->next != NULL)
{
p = p->next;
}
else
{
break;
}
}
return 0;
}
-------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-ux12kiqm.rv0 --stdout=Microsoft-MIEngine-Out-p2i2qrrz.1qa --stderr=Microsoft-MIEngine-Error-vbt1v0gu.sk0 --pid=Microsoft-MIEngine-Pid-jj53h2bf.4rw --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
hello world C language
E:\Administrator\Documents\My C>
====================================================================
bin554385863
发表于 2019-7-13 23:32:09
本帖最后由 bin554385863 于 2019-7-14 11:39 编辑
2019年7月13日23:30:53
第十章
1
文件指针变量的定义
标识符 文件指针变量名字
FILE *myfile;
2
文件操作函数
FILE *fopen(char *filename,char *type)
作用:用指定方式打开一个文件.
fopen函数返回指向文件流的指针,若有错误则返回NULL,为了防止错误发生,一般都要对fopen函数进行返回值[判断]
fclose(FILE *stream)
文件关闭函数,为了防止文件操作完成后发生意外,应该在结束对文件操作后关闭文件
type 含义 文件不存在 文件存在
r 只读 返回错误 打开文件
r+ 读写 返回错误 打开文件
w 只写 建立新文件 打开文件,清空内容
w+ 读写 建立新文件 打开文件,清空内容
a 追加 建立新文件 从尾部追加数据
a+ 读写 建立新文件 打开文件,可对文件进行读写操作
以a+方式打开文件进行读写操作必须手动对文件指针进行定位!
还有对应的rb/rb+ wb/wb+ ab/ab+,含义跟上面一样,不过操作对象是二进制问价
/*以只写"w"方式打开文件 */
/*文件打开函数 fopen("文件名", "方式参数") */
/*文件关闭函数 fclose(文件指针变量) */
#include <stdio.h>
int main(int argc, char const *argv[])
{
FILE *fp;//文件指针变量
if (fp = fopen("text.txt", "w") == NULL)//以只写方式打开一个文件,若不存在则新建一个文件
{
printf("文件打开错误");
exit(0);
}
else
{
printf("文件以打开");
}
fclose(fp);//关闭文件
return 0;
}
-------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-x0qmgtch.05g --stdout=Microsoft-MIEngine-Out-dsmnxrqy.jd1 --stderr=Microsoft-MIEngine-Error-ewnllm1a.2xz --pid=Microsoft-MIEngine-Pid-abdlk0ol.nwu --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
文件以打开
E:\Administrator\Documents\My C>
==================================================================
bin554385863
发表于 2019-7-14 11:45:57
本帖最后由 bin554385863 于 2019-7-14 12:00 编辑
2019年7月14日11:42:03
2.2
文件错误的检测
函数: int ferror(FILE *stream)
作用:判断文件流是否出错.
返回值:正确(0),错误(非0随机数).
函数: int feof(FILE *stream)
作用:判断是否到达文件尾.
返回值:正确(0),错误(非0随机数).
2.3
文件顺序的读写
函数: int fgetc(FILE *stream)
调用: c = fgetc(fp).
作用:从fp所指的文件中读取字符,赋予变量c.
返回值: 出错/读到文件尾(-1).
函数: fputc(c, fp)
作用: 将字符变量c的值写入到指针fp所指的文件中去(不包括'\0').
返回值: 出错(EOF).
bin554385863
发表于 2019-7-21 01:12:12
本帖最后由 bin554385863 于 2019-7-21 11:59 编辑
2019年7月21日01:12:35
链表
#include <stdio.h>
#include <stdlib.h>
#define MALLOC (node *)malloc(sizeof(node))
/*创建自引用结构体 */
typedef struct _node
{
int value;
struct _node *next;
} node;
int main(int argc, char const *argv[])
{
/*定义头结点 */
node *p1 = NULL;
/*定义尾结点 */
node *p2 = NULL;
/*定义待插入节点 */
node *p3 = NULL;
/*分配第一个节点空间 */
p2 = MALLOC;
/*判断是否分配节点空间成功 */
if (p2 != NULL)
{
/*输入第一个节点的数据 */
scanf("%d", &p2->value);
/*头结点指向第一个节点 */
p1 = p2;
/*循环创建链表 */
while (p2->value != -1)
{
/*开辟新节点空间 */
p3 = MALLOC;
/*判断新节点是否开辟成功 */
if (p3 != NULL)
{
/*继续输入新节点的数据 */
scanf("%d", &p3->value);
/*尾结点指针成员指向新开辟的节点 */
p2->next = p3;
/*使用递归的方法将所有的节点串联起来 */
p2 = p2->next;
}
}
/*将尾结点置空 */
p2->next = NULL;
}
/*输出链表 */
while (p1 != NULL)
{
printf("%d ", p1->value);
p1 = p1->next;
}
return 0;
}
--------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-yt5oda2l.xyq --stdout=Microsoft-MIEngine-Out-xhajtxsu.kjk --stderr=Microsoft-MIEngine-Error-wkdz1qrn.sbz --pid=Microsoft-MIEngine-Pid-22m45eqh.qb4 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
1 2 3 4 5 6 7 8 9 -1
1 2 3 4 5 6 7 8 9 -1
E:\Administrator\Documents\My C>
--------------------------------------------------------------------------------------------
2019年7月21日11:59:21
--------最终版--------
#include <stdio.h>
#include <stdlib.h>
/*创建自引用结构体 */
typedef struct _node
{
int value;
struct _node *next;
} node;
#define MALLOC (node *)malloc(sizeof(node))
/*链表创建函数 */
node *Creatnode(void)
{
/*定义头结点 */
node *p0 = NULL;
/*定义临时头结点 */
node *p1 = NULL;
/*定义尾结点 */
node *p2 = NULL;
/*定义待插入节点 */
node *p3 = NULL;
/*分配第一个节点空间 */
p2 = MALLOC;
/*判断是否分配节点空间成功 */
if (p2 != NULL)
{
/*临时头结点指向第一个节点 */
p1 = p2;
/*临时头结点数据成员为空 */
p1->value = NULL;
/*循环创建链表 */
printf("--------输入链表数据--------\n");
while (1)
{
/*开辟新节点空间 */
p3 = MALLOC;
/*判断新节点是否开辟成功 */
if (p3 != NULL)
{
/*继续输入新节点的数据 */
scanf("%d", &p3->value);
/*-1为结束符 */
if (p3->value == -1)
{
break;
}
/*尾结点指针成员指向新开辟的节点 */
p2->next = p3;
/*使用递归的方法将所有的节点串联起来 */
p2 = p2->next;
}
}
/*将尾结点置空 */
p2->next = NULL;
}
/*头结点指向第一个有效节点 */
p0 = p1->next;
/*释放临时头结点的内存空间 */
free(p1);
return p0;
}
/*链表输出函数 */
void PrintNode(node *t_node)
{
node *t = t_node;
while (t != NULL)
{
printf("%d ", t->value);
t = t->next;
}
}
int main(int argc, char const *argv[])
{
node *linklist = NULL;
/*调用创建链表函数 */
linklist = Creatnode();
/*调用链表输出函数 */
PrintNode(linklist);
return 0;
}
------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-h0wqugoj.e51 --stdout=Microsoft-MIEngine-Out-pqsbx1jx.pnk --stderr=Microsoft-MIEngine-Error-ilxijexf.tzq --pid=Microsoft-MIEngine-Pid-jtmk4tw5.f5w --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
1 2 3 4 5 6 7 8 9 -1
1 2 3 4 5 6 7 8 9
E:\Administrator\Documents\My C>
bin554385863
发表于 2019-7-22 23:41:16
本帖最后由 bin554385863 于 2019-7-23 01:15 编辑
2019年7月22日23:39:39
第一个例子
#include <stdio.h>
#include <stdlib.h>
/*文件函数fgetc()he fputc()和fopen()以及fclose()的使用 */
int main(int argc, char const *argv[])
{
FILE *fp1, *fp2;
int ch;
/*打开第一个文件 */
if ((fp1 = fopen("E:\\Administrator\\Documents\\My C\\10-Tenth Chapter\\FishC\\Testfile\\text1.txt", "r")) == NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
/*打开第二个文件 */
if ((fp2 = fopen("E:\\Administrator\\Documents\\My C\\10-Tenth Chapter\\FishC\\Testfile\\text2.txt", "w")) == NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
/*读取第一个文件的字符 */
while ((ch = fgetc(fp1)) != EOF)
{
/*写入第二个文件 */
fputc(ch, fp2);
}
/*关闭两个文件 */
fclose(fp1);
fclose(fp2);
return 0;
}
----------------------------------------------------------------------------------------------------
这个代码没有输出所就不复制运行结果了
第二个例子
#include <stdio.h>
#include<stdlib.h>
#define MAX 1024
/*文件函数fgets()和fputs()的用法 */
int main(int argc, char const *argv[])
{
FILE *fp;
char str;
/*打开文件 */
if ((fp = fopen("E:\\Administrator\\Documents\\My C\\10-Tenth Chapter\\FishC\\Testfile\\lines.txt", "w")) == NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
/*写入字符串 */
fputs("line one: file function fputs().\n", fp);
fputs("line two: file function fgets().\n", fp);
fputs("line three: file function fclose()\0.\n", fp);
/*写入结束,关闭文件 */
/*文件关闭之前字符串存储在缓冲区,文件关闭后才会真正写入文件 */
fclose(fp);
/*重新以只读模式打开文件 */
if ((fp = fopen("E:\\Administrator\\Documents\\My C\\10-Tenth Chapter\\FishC\\Testfile\\lines.txt", "r")) == NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
while (!feof(fp))
{
/*fgets(char *, size_t StringSize, FILE *fp)函数遇到换行符会结束读取,但是当还没遇到EOF就结束的时候回吧最后的内容重复一遍.
像这种情况最后手动在最后加上\0,这样当fgets()函数读到\0的时候就会结束 */
fgets(str, MAX, fp);
printf("%s", str);
}
return 0;
}
--------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-tiawbjqe.alb --stdout=Microsoft-MIEngine-Out-vz5rlmc2.k3d --stderr=Microsoft-MIEngine-Error-2e0ir44g.ynk --pid=Microsoft-MIEngine-Pid-01nyldzi.oqn --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
line one: file function fputs().
line two: file function fgets().
line three: file function fclose()
E:\Administrator\Documents\My C>
------------------------------------------------------------------------------------------------------
用到的函数
操作对象--------文件
单个字符写入函数: fputc(chat c, FILE *fp)
单个字符读取函数:char var =fgetc( FILE *fp)
var: 用来保存读取的单个字符的变量
字符串写入函数: fputs(char *str, FILE *fp)
使用fputs()函数写入字符串时建议手动输入结束符\0,
以防fgets()读取字符串时出现意外的结果
字符串读取函数: fgets(char *str , size_t strSize, FILE *fp)
size_t strSize :字符数组的宽度,字符数组要有足够的空间来存放读取的数据.
bin554385863
发表于 2019-7-24 00:31:15
本帖最后由 bin554385863 于 2019-7-24 00:34 编辑
2019年7月24日00:22:28
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*在文件中写入一个结构体 */
typedef struct
{
int year;
int mounth;
int day;
} Date;
typedef struct
{
char name;
char author;
Date date;
} Book;
int main(int argc, char const *argv[])
{
Book *book_write, *book_read;
book_write = (Book *)malloc(sizeof(Book));
book_read = (Book *)malloc(sizeof(Book));
if ((book_write == NULL) || (book_read == NULL))
{
printf("内存分配失败\n");
exit(EXIT_FAILURE);
}
strcpy(book_write->name, "C language");
strcpy(book_write->author, "unknow");
book_write->date.year = 2019;
book_write->date.mounth = 07;
book_write->date.day = 23;
FILE *fp;
if ((fp = fopen("E:\\Administrator\\Documents\\My C\\10-Tenth Chapter\\FishC\\Testfile\\text1.txt", "w")) == NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
fwrite(book_write, sizeof(Book), 1, fp);
fclose(fp);
if ((fp = fopen("E:\\Administrator\\Documents\\My C\\10-Tenth Chapter\\FishC\\Testfile\\text1.txt", "r")) == NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
fread(book_read, sizeof(Book), 1, fp);
printf("书名: %s\n作者: %s\n日期: %d--%d--%d", book_read->name, book_read->author, book_read->date.year, book_read->date.mounth, book_read->date.day);
fclose(fp);
return 0;
}
-----------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-qyxjanbr.hgt --stdout=Microsoft-MIEngine-Out-maciqhbm.u1g --stderr=Microsoft-MIEngine-Error-fvfkugsf.wvi --pid=Microsoft-MIEngine-Pid-3yrlda02.4og --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
书名: C language
作者: unknow
日期: 2019--7--23
E:\Administrator\Documents\My C>
================================================================
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char const *argv[])
{
struct tm *p;
time_t t;
time(&t);
p = localtime(&t);
FILE *fp;
if ((fp = fopen("E:\\Administrator\\Documents\\My C\\10-Tenth Chapter\\FishC\\Testfile\\date.txt", "w")) == NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
fprintf(fp, "%d--%d--%d", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday);
fclose(fp);
if ((fp = fopen("E:\\Administrator\\Documents\\My C\\10-Tenth Chapter\\FishC\\Testfile\\date.txt", "r")) == NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
int year, mounth, day;
fscanf(fp, "%d--%d--%d", &year, &mounth, &day);
printf("%d---%d---%d", year, mounth, day);
fclose(fp);
return 0;
}
-----------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-0zsises0.tvp --stdout=Microsoft-MIEngine-Out-u3da0tef.uvx --stderr=Microsoft-MIEngine-Error-4gffdadf.chx --pid=Microsoft-MIEngine-Pid-rnfrylot.rtk --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
2019---7---24
E:\Administrator\Documents\My C>
================================================
文件格式化输入输出函数
输出:fprintf(文件指针, "格式符", 变量列表 );
读取:fscanf(文件指针, "格式符", 变量地址列表);
将变量写入文件
写入函数:fwrite(变量指针, sizeof(变量类型), 长度, 文件指针);
读取文件保存在变量里面
读取函数:fread(变量指针, sizeof(变量类型), 长度, 文件指针);
bin554385863
发表于 2019-7-26 23:13:09
2019年7月26日23:14:38/*--------文件操作函数----------- */
fputc(字符, 文件指针)----向指针指向的文件中写入一个字符.
fgetc(文件指针)----向指针指向的文件中单个读取字符.
fputs(字符串, 文件指针)----向指针指向的文件中写入一个字符串.
fgets(FILE *fp)----向指针指向的文件中读取一个字符串.
fprintf(文件指针, 格式符, 字符串)----格式化向文件中写入字符串.
fscanf(文件指针, 格式符, 字符数组)----在文件中读取字符串并保存在数组中.
fwrite(数据指针, sizeof(数据类型), 数据个数,文件指针)----向文件中写入指定长度的数组,结构体等等数据.
fread(变量指针, sizeof(数据类型), 数据个数,文件指针)----读取文件中的指定长度的数据并储存在变量中.
fclose(文件指针)----关闭文件.
feof(文件指针)----判断文件读取是否结束/出错.返回1表示成功,0表示失败.
fseek(文件指针, 位移量, 参照位置p)----文件指针移动函数. p = 1/SEEK_SET,表示在文件开头开始移动;p = 2/SEEK_CUR 表示在当前位置开始移动;p = 2/SEEK_END表示文件指针在文件末尾.
loc = ftell(文件指针)----返回文件指针距离文件头的偏移量.
rewind(文件指针)----将文件指针重新指向文件开始处.
bin554385863
发表于 2019-7-31 11:23:22
2019年7月31日11:24:07
至此,C语言告一段落,正式开始C++