《白书读书笔记》

//第一部分,语言篇

第一章 程序设计入门

//1-2 三位数反转
#include<stdio.h>
int main()
{
    int n;
    scanf("%d", &n);
    printf("%d%d%d
", n%10,n/10%10,n/100);
    return 0;    
}
//1-3变量交换 
#include<stdio.h>
int main()
{
    int a, b, t;
    scanf("%d%d" , &a, &b);
    t=a;
    a=b;
    b=t;
    printf("%d %d
", a, b);
    return 0;
}
//1-14 三整数排序
#include<stdio.h>
int main()
{
    int a, b, c, t;
    scanf("%d%d%d", &a, &b, &c);
    if(a > b) { t = a; a = b; b = t;}
    if(a > c) { t = a; a = c; c = t;}
    if(b > c) { t = b; b = c; c ; t;}
    printf("%d %d %d
", a, b, c);
    
/*
    int a, b, c, x, y, z;
    scanf("%d%d%d", &a, &b, &c);
    x = a; if(b < x) x = b; if(c > x) x = c;
    z = a; if(b > z) z = b; if(c > z) z = c'
    y = a + b + c - x - z;
    printf("%d %d %d
", x, y, z);
    return 0;    
*/    
    return 0;
} 
//int范围[-2^31 , 2^31 -1] 即 [-2147483648,2147483647]。
//10E 

第二章 循环结构程序设计

//C++的文件读写
ifstream fin("text.in");
ofstream fout("text.out");

//重定向
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);


//fopen
FILE *fin, *fout;
fin = fopen("data.in","r");
fout = fopen("data.out","w");

fsacnf(fin,"%d",&n);
fprintf(fout,"%d",n);

fclose(fin);
fclose(fout);


memcpy(b,a,sizeof(a))
memcpy(b,a,sizeof(double)*k)

memset(a,0,sizeof(a));
C语言
定义语法
函数名: floor
功 能: 返回小于或者等于指定表达式的最大整数
用 法: double floor(double x);
头文件:math.h

#include <stdio.h>
int main()
{
    int a, b, c;
    double x;
    scanf("%d%d%d", &a, &b, &c);
    x = 1.0*a/b;
    printf("%.*lf
", c, x);    //printf("%*.*lf", x, y, z) 第一个*对应x,第二个*对应y,lf对应z
    return 0;
}

第三章  数组和字符串

memset()的深刻内涵:用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’;例:char a[100];memset(a, '/0', sizeof(a));

memcpy用来做内存拷贝,你可以拿它拷贝任何数据类型的对象,可以指定拷贝的数据长度;例:char a[100],b[50]; memcpy(b, a, sizeof(b));注意如用sizeof(a),会造成b的内存地址溢出。

strcpy就只能拷贝字符串了,它遇到'/0'就结束拷贝;例:char a[100],b[50];strcpy(a,b);如用strcpy(b,a),要注意a中的字符串长度(第一个‘/0’之前)是否超过50位,如超过,则会造成b的内存地址溢出。

5.补充:一点心得
memset可以方便的清空一个结构类型的变量或数组。
string 常用函数
//strcpy
原型声明:extern char *strcpy(char* dest, const char *src);
头文件:#include <string.h>
功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。

//strcmp
原型:extern int strcmp(const char *s1,const char * s2);
所在头文件:string.h
功能:比较字符串s1和s2。
一般形式:strcmp(字符串1,字符串2)
说明:
当s1<s2时,返回值= -1
当s1==s2时,返回值= 0
当s1>s2时,返回值 = 1
注:c++ 中
当s1<s2时,返回值小于0
当s1==s2时,返回值等于0
当s1>s2时,返回值 大于0
即:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇''为止。如:
"A"<"B" "a">"A" "computer">"compare"
特别注意:strcmp(const char *s1,const char * s2)这里面只能比较字符串,不能比较数字等其他形式的参数。


//strlen
原型:extern unsigned int strlen(char *s);
在Visual C++ 6.0中,原型为size_tstrlen(const char *string); ,其中size_t实际上是unsigned int,在VC6.0中可以看到这样的代码:typedef unsigned int size_t; 。
头文件:string.h
格式:strlen (字符数组名)
功能:计算字符串s的(unsigned int型)长度,不包括''在内
说明:返回s的长度,不包括结束符NULL。

//strchr
原型:extern char *strchr(const char *s,char c);
const char *strchr(const char* _Str,int _Val)
char *strchr(char* _Str,int _Ch)
头文件:#include <string.h>
功能:查找字符串s中首次出现字符c的位置
说明:返回首次出现c的位置的指针,返回的地址是字符串在内存中随机分配的地址再加上你所搜索的字符在字符串位置,如果s中不存在c则返回NULL。
返回值:Returns the address of the first occurrence of the character in the string if successful, or NULL otherwise

//strcat
原型
extern char *strcat(char *dest,char *src);
用法
#include <string.h>
在C++中,则存在于<cstring>头文件中。
功能
把src所指字符串添加到dest结尾处(覆盖dest结尾处的'')并添加''。
说明
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。


//memcpy

原型:extern void *memcpy(void *dest, void *src, unsigned int count);

用法:#include <string.h>

功能:由src所指内存区域复制count个字节到dest所指内存区域。

说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。可以拿它拷贝任何数据类型的对象。

举例:char a[100],b[50]; memcpy(b, a, sizeof(b));注意如用sizeof(a),会造成b的内存地址溢出。



//memset

原型:extern void *memset(void *buffer, int c, int count);

用法:#include <string.h>

功能:把buffer所指内存区域的前count个字节(buffer指针后的前count个字节)设置成字符c。

说明:返回指向buffer的指针。用来对一段内存空间全部设置为某个字符。

举例:char a[100];memset(a, '', sizeof(a));

memset可以方便的清空一个结构类型的变量或数组。



//strrev
函数名: strrev
功 能: 串倒转
用 法: char *strrev(char *str);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *forward = "string";
printf("Before strrev(): %s
", forward);
strrev(forward);
printf("After strrev(): %s
", forward);
return 0;
}



//strset
函数名: strset
功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strset(char *str, char c);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10] = "123456789";
char symbol = 'c';
printf("Before strset(): %s
", string);
strset(string, symbol);
printf("After strset(): %s
", string);
return 0;
}

竖式问题

题目:

找出所有形如abc*de(三位数乘以两位数)的算式,使得在完整的竖式中,所有数字都属于一个特定的数字集合。输入数字集合(相邻数字之间没有空格),输出所有竖式。每个竖式前应有编号,之后应有一个空行。最后输出解的总数。具体格式见样例输出(为了便于观察,竖式中的空格改用小数点显示,但你的程序应该输出空格,而非小数点)。

样例输入:2357

样例输出:

<1>

..775

X..33

-----

.2325

2325.

-----

25575

The number of solutions = 1

 题意:[code=C/C++][/code] 
      775                                       7   ,5在{2,   3,   5,   7}集合中 
    X   33                                       3在{2,   3,   5,   7}集合中 
----------------------------------------------------- 
    2325                                         2,3,5在{2,   3,   5,   7}集合中 
  2325 
------------------------------------------------------ 
  25575                             2,   5,   7在{2,   3,   5,   7}集合中 
输入一个数,表示这个数组中含有这几个数的元素。
#include<stdio.h>
#include<string.h>
int main()
{
    int i, ok, abc, de ,x, y, z, count = 0;
    char s[20], buf[99];
    scanf("%s", s);
    for(abc = 111; abc <= 999; abc++)
        for(de = 11; de <= 99; de++)
        {
            x = abc*(de%10);
            y = abc*(de/10);
            z = abc*de;
            sprintf(buf, "%d%d%d%d", abc, de, x, y, z);
            ok = 1;
            for(i = 0; i < strlen(buf); i++)
                if(strchr(s, buf[i]) == NULL)  ok = 0;
            if(ok)
            {
                printf("<%d>
",++count);
                printf("%5d
X%4d
----
%5d
%4d
----
%5d

", abc, de, x, y, z);
            }
        }
    printf("The number of solutions = %d
", count);
    return 0;
}

函数原型:char *fgets(char *buf, int bufsize, FILE *stream);
参数:
*buf: 字符型指针,指向用来存储所得数据的地址。
bufsize: 整型数据,指明buf指向的字符数组的大小。
*stream: 文件结构体指针,将要读取的文件流。
原文地址:https://www.cnblogs.com/Lee-geeker/p/3217096.html