目录
我还在完善中,边复习边完善
一、 字符串
C语言
读入一个字符: ch = getchar();
读入一行字符:while((ch = getchar()) != '\n');
将带有空格的字符串读入数组中:gets(str);
将带有空格的字符数组输出:puts(str);
将小写字母转成大写:'a' - 32;
将大写字母转成小写:'a' + 32;
一些函数
加上头文件:#include<string.h>
有些有返回值,有些不用返回值
1. 连接:strcat(str1, str2); //将str2连接到str1上;
2. 复制:strcpy(str1, str2); //将str2所有字符(包括'\0'复制到str1中,覆盖完str1所有字符)
strcpy(str1, str2, n); //将str2中的前n个字符复制到str1上
3. 比较:n = strcmp(str1, str2);
返回值:相等为0, str1>str2为正数,str1<str2为负数
4. 长度:n = strlen(str);
注意要点:
1. 字符串以'\0'为结尾,删完字符时别忘了加上它
对于'\0', 不能写成'\n';
不管啥操作,啥函数,读到'\0'操作就会结束
2. 字符串可以整个进行输入输出——利用%s
3. 二维字符串可以整行整行赋值
1. 分类统计字符个数
输入一行字符,统计出其中的英文字母、空格、数字和其它字符的个数
#include<stdio.h>
int main()
{
char ch;
int a = 0, b = 0, c = 0, d = 0;
while((ch = getchar()) != '\n')
{
if((ch >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z')) a ++;
else if(ch == ' ')b ++;
else if(ch >= '0' && ch <= '9') c ++;
else d ++;
}
printf("字符=%d\n空格=%d\n数字=%d\n其他字符=%d\n", a, b, c, d);
return 0;
}
2. 将单词首字母改成大写
统计一行字符,将每个单词的首字母改为大写后输出,各单词之间用空格分隔
#include<stdio.h>
#include<string.h>
int main()
{
char str[105];
// bool flag = 1; //判断是否为第一个单词
int i = 0, j = 0;
gets(str);
while(str[i] != '\0')
{
if(str[i] == ' ')
{
i ++;
continue;
}
j = i;
while(str[j] != ' ' && str[j] != '\0') //找单词的尾巴
j ++;
//将开头转为大写
if(str[i] >= 'a' && str[i] <= 'z')
str[i] -= 32;
for(int k = i; k < j; k ++)
printf("%c", str[k]);
printf(" ");
i = j;
}
return 0;
}
3. 字符串替换
输入一个以回车结束的字符串,将其中的大写字母用下面列出的对应大写字母替换,其余字符不变,输出替换后的字符串。
A->Z B->Y C->X ........Z->A
#include<stdio.h>
#include<string.h>
int main()
{
char str[105];
gets(str);
for(int i = 0; str[i] != '\0'; i ++)
{
if(str[i] >= 'A' && str[i] <= 'Z') //转换
str[i] = 'A' + 'Z' - str[i];
}
puts(str);
return 0;
}
4. 删除字符
输入一个字符串,再输入一个字符ch,将字符串中所有的ch字符删除后输出该字符串。要求定义和调用函数delchar(s, c);
算法思想:从左向右遍历,第i个不等于ch的字符放在字符串的第i个位置
删完后别忘了增加换行符
#include<stdio.h>
int delchar(char s[100], char ch) //返回该字符串长度
{
int pos = 0;
for(int i = 0; s[i] != '\0'; i ++)
{
if(s[i] != ch)
s[pos ++] = s[i];
}
//增加换行符
s[pos] = '\0';
return pos;
}
int main()
{
char s[100];
char ch;
scanf("%s %c", s, &ch); //不能写成%s%c 要有空格
int n = delchar(s, ch);
printf("%s", s);
}
5. 字符串排序
输入5个字符串,按照从小到大的顺序输出
#include<stdio.h>
#include<string.h>
void work(char s[100][100]) //相当于引用了
{
char b[100];
for(int i = 0; i < 4; i ++) //利用冒泡排序
for(int j = i+1; j < 4; j ++)
{
if(strcmp(s[i], s[j]) > 0) //比较函数
{
strcpy(b, s[i]); //赋值函数
strcpy(s[i], s[j]);
strcpy(s[j], b);
}
}
// return s;
}
int main()
{
char a[100][100];
for(int i = 0; i < 4; i ++)
scanf("%s", a[i]);
work(a);
for(int i = 0; i < 4; i ++)
printf("%s ", a[i]);
}
二、 其它
1. 使用函数验证哥德巴赫猜想
任何一个不小于6的偶数均可以表示为两个奇素数之和,例如18=5+13, 8=3+5.将6~100之间的偶数都表示成两个素数之和,打印时一行打印5个。
/*
预处理,判断2-100中哪些数是素数
*/
#include<stdio.h>
#include<math.h>
int str[105]; //哈希判断该数是否为素数
int check(int x)
{
for(int i = 2; i <= sqrt(x); i ++)
{
if(x % i == 0) return 0;
}
return 1;
}
int main()
{
int cnt = 0;
for(int i = 2; i <= 100; i ++)
{
if(check(i)) str[i] = 1;
}
for(int i = 6; i <= 100; i += 2)
{
for(int j = 2; j <= i/2; j ++)
{
if(str[j] && str[i-j])
{
cnt ++;
printf("%d=%d+%d ", i, j, i-j);
if(cnt % 5 == 0) printf("\n");
break;
}
}
}
return 0;
}
2. 循环后移
有n个整数,使前边各数顺序向后移m个位置,移除的数再从头移入。
例:12345向右移3位为:45123
/*
翻转3次的思想
不过要注意翻转的位置
*/
#include<stdio.h>
int a[105];
void reverse(int l, int r)
{
while(l < r)
{
int temp = a[l];
a[l] = a[r];
a[r] = temp;
l ++, r --;
}
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i ++)
{
scanf("%d", &a[i]);
}
reverse(0, n-m-1);
reverse(n-m, n-1);
reverse(0, n-1);
for(int i = 0; i < n; i ++)
{
printf("%d", a[i]);
}
return 0;
}
3. 报数
有n个人围成一圈,按顺序从1到n编好号。从第一个人开始报数,报到m(m<n)的人退出圈子,下一个人从一开始报数,如此下去,直到留下最后一个人,输入整数n和m,并按退出顺序输出退出圈子的人的编号。
/*
数组型链表的思想
不过要注意很多细节
*/
#include<stdio.h>
int ne[105];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++)
{
ne[i] = i+1;
if(i == n) ne[i] = 1;
}
int cnt = 0;
int i;
for(i = n; i != ne[i]; ) //注意
{
cnt ++;
int j = ne[i];
if(cnt % m == 0)
{
printf("%d ", j);
ne[i] = ne[j]; //注意
}
else i = ne[i]; //注意
}
printf("%d", i);
return 0;
}
三、 文件
总结:
1. 普通文件用fscanf()从文件中读取,用fprintf()写进文件
2. 二进制文件用fread() 和 fwrite( );
与非文件操作基本一致,只是读取要判断临界值
p 为文件指针
- ch = fgetc( f ); //返回值为字符
临界判断:
while(ch != EOF) //不用双引号 ch = fgetc(p); //获取一个字符
2. fgetc(ch, f);
3. fputs(s, p);
4. fgets(s, n, f); //f为读取的长度,读的时候也会读到空格
5. fscanf(p, "%s", s[pos]);
临界判断
while(fscanf(p, "%s", s[pos]) == 1) pos ++;
6. fprintf(p,"%d %s %.1f\n", stu[i].id, stu[i].name, stu[i].score);
对于二进制文件
1. fwrite(buffer, size, count, fp);
buffer:读入数据块的起始地址
size: 一个数据块所占空间字节数
count:要读的数据块的个数
(与二维数组的顺序相反)
fp:文件指针
fwrite(&stu[i], sizeof(struct node), 1, p);
别忘了用的是rb或者wb
2. fread (buffer, size, count, fp);
while(fread(&x, sizeof(x), 1, p) == 1) sum += x;
1. 读字符
#include <stdio.h>
int main ()
{
FILE *p; //创建文件指针 不是FILP
char ch = 0;
int a = 0, b = 0, c = 0; //统计字母, 数字, 其他字符
p = fopen("E:\\test.txt", "r"); //打开文件
if(p == NULL) perror("Error opening file"); //打开失败
else
{
while(fscanf(p, "%c", &ch) == 1) //获取一个字符
{
if(ch >= 'A' && ch <= 'z') a ++;
else if(ch >= '0' && ch <= '9') b ++;
else c ++;
}
fclose(p); //一定要记得关闭文件。
}
printf("%d %d %d\n", a, b, c);
return 0;
}
2. 向文件中写入字符
(1)写入一个字符
#include <stdio.h>
int main ()
{
FILE *p; //创建文件指针
p = fopen("E:\\test.txt", "w"); //打开文件
if(p == NULL) perror("Error opening file"); //打开失败
else
{
char ch = 'a';
fprintf(p, "%c", ch); //写入一个字符
fclose(p); //一定要记得关闭文件。
}
return 0;
}
(2)写入字符串
char s[100] = "good";
fputs(s, p); //写入字符串
3. 二进制读写
用fwrite和fprinf时,文件要以二进制的方式打开
(1)fwrite( )
fwrite(buffer, size, count, fp);
buffer:读入数据块的起始地址
size: 一个数据块所占空间字节数
count:要读的数据块的个数
(与二维数组的顺序相反)
fp:文件指针
fwrite(&stu[i], sizeof(struct node), 1, p);
别忘了用的是rb或者wb
#include <stdio.h>
struct node
{
int id;
char name[100];
double score;
}stu[100] = {{1, "lan", 90.1},{ 2, "juan", 100.4},{ 3, "jiao", 91.1}};
int main() {
FILE *p; // 创建文件指针
p = fopen("E:\\test.txt", "wb"); // 以二进制写入模式打开文件
if (p == NULL) {
perror("Error opening file"); // 打开失败
return 1; // 返回错误码,而不是0
}
for(int i = 0; i < 3; i ++)
fwrite(&stu[i], sizeof(struct node), 1, p); //写入一个学生的信息
fclose(p); // 关闭文件
return 0;
}
(2)fread( )
结构体
for(int i = 0; i < 3; i ++)
fread(&stu[i], sizeof(struct node), 1, p); //读入一个学生的信息
一个一个读
int a;
char b[100];
double c;
fread(&a, sizeof(a), 1, p); //写入一个学生的信息
fread(b, sizeof(b), 1, p); //写入一个学生的信息
fread(&c, sizeof(c), 1, p); //写入一个学生的信息
有临界值的情况
while(fread(&x, sizeof(x), 1, p) == 1)
sum += x;
3. 计算文件数字的的和
不用考虑空格啥的,大胆用,遇到数字自己回合理放进去的
将文件中所有数据相加,并把累加和写入该文件的最后
#include <stdio.h>
int main() {
FILE *p; // 创建文件指针
p = fopen("E:\\test.txt", "r"); // 以读取模式打开文件
if (p == NULL) {
perror("Error opening file"); // 打开失败
}
else {
int sum = 0;
int num; // 使用 int 类型变量来存储读取的整数
while (fscanf(p, "%d", &num) == 1) { // 检查是否成功读取了一个整数
sum += num;
}
printf("%d\n", sum);
fclose(p); // 关闭文件
}
return 0;
}
四、枚举类型
#include <stdio.h>
enum day{mon = 1, tue, web}today, yesterday;
int main ()
{
today = tue;
printf("%d", today);
return 0;
}
五、结构体内嵌
struct juan
{
int x;
};
struct node
{
char name[10];
struct juan good;
}g;