笔记及上机题常用(一)

常用头文件:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stdafx.h"
#include <iostream>

#include <math.h>


常用函数

1、strcat


头文件:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
功能
把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
说明
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。

举例:
// strcat.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
  char d[20]="Golden Global";
  char *s=" View";
  system("cls");
  strcat(d,s);
  printf("%s",d);
  getchar();
  return 0;
}
程序执行结果为:

Golden Global View


2、atoi/atof


功 能: 把字符串转换成整型数/浮点数。
名字来源:ASCII to integer 的缩写。


函数说明
参数nptr字符串,如果第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。否则,返回零,
头文件: #include <stdlib.h>
程序例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
  float n;
  char *str = "12345.67";
  n = atoi(str);
  printf("string = %s integer = %f\n", str, n);
  return 0;
}
执行结果:
string = 12345.67 integer = 12345.0


简单的实现atoi函数源代码:
#include <cctype>
int my_atoi(const char* p){
  assert(p != NULL);
  bool neg_flag = false;// 符号标记
  int res = 0;// 结果
  if(p[0] == '+' || p[0] == '-')
    neg_flag = (*p++ != '+');
  while(isdigit(*p)) res = res*10 + (*p++ - '0');
  return neg_flag ?0 -res : res;
}


3、itoa
说明
itoa是广泛应用的非标准C语言扩展函数。由于它不是标准C语言函数,所以不能在所有的编译器中使用。但是,大多数的编译器(如Windows上的)通常在<stdlib.h>头文件中包含这个函数。功能:将任意类型的数字转换为字符串。在<stdlib.h>中与之有相反功能的函数是atoi。


用法
char *itoa(int value, char *string, int radix);
int value 被转换的整数,char *string 转换后储存的字符数组,int radix 转换进制数,如2,8,10,16 进制等
头文件: <stdlib.h>
itoa操作使用
  itoa操作使用
程序例:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int number = 123456;
char string[25];
itoa(number, string, 10);
printf("integer = %d string = %s\n", number, string);
return 0;
}


sscanf
sscanf() - 从一个字符串中读进与指定格式相符的数据.
头文件
#include<stdio.h>
例子
1. 常见用法。
char buf[512] ;
sscanf("123456 ", "%s", buf);//此处buf是数组名,它的意思是将123456以%s的形式存入buf中!
printf("%s\n", buf);
结果为:123456
2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。
sscanf("123456 ", "%4s", buf);
printf("%s\n", buf);
结果为:1234


字符串处理:

strchr

头文件:#include <string.h>
功能:查找字符串s中首次出现字符c的位置
说明:返回首次出现c的位置的指针,返回的地址是字符串在内存中随机分配的地址再加上你所搜索的字符在字符串位置,如果s中不存在c则返回NULL。
例子


举例:
#include <string.h>
#include <stdio.h>
int main(void)
{
    char string[17];
    char *ptr, c = 'r';
    strcpy(string, "This is a string");
    ptr = strchr(string, c);
    if (ptr)
        printf("The character %c is at position: %d\n", c, ptr-string);
    else
        printf("The character was not found\n");
    return 0;
}
运行结果:
The character r is at position: 12

strcpy
头文件:#include <string.h>
功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
举例:
char string[17];
strcpy(string, "This is a string");


#include<math.h>
主要是数学方面的函数。如sin,cos,tan,绝对值等等。
如:abs(number)


strlen
头文件:string.h
格式:strlen (字符数组名)
功能:计算字符串s的(unsigned int型)长度,不包括'\0'在内
说明:返回s的长度,不包括结束符NULL。
程序举例


举例:(在Visual C++6.0中运行通过)
#include <string.h>
#include<stdio.h>
int main(void)
{
  char *s="Golden Global View";
  printf("%s has %d chars",s,strlen(s));
  getchar();
  return 0;
}


strstr
包含文件:string.h
函数原型:extern char *strstr(char *str1, char *str2);
功能:从字符串str1中查找是否有字符串str2,如果有,从str1中的str2位置起,返回str1中str2起始位置的指针,如果没有,返回null。
返回值:返回该位置的指针,如找不到,返回空指针。
例子:
char str[]="1234 xyz";
char* str1=strstr(str,"34");
cout<<str1<<endl;
显示:    34 xyz


memcpy
功能
从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中
编辑本段
所需头文件
C语言中使用#include <string.h>;
C++中使用#include <cstring>; [cstring是C++在移植C的头文件时前面会加字母'c' ]
编辑本段
返回值
函数返回dest的值。
编辑本段
说明
1.source和destin所指的内存区域可以重叠,但是如果source和destin所指的内存区域重叠,那么这个函数并不能够确保source所在重叠区域在拷贝之前被覆盖。而使用memmove可以用来处理重叠区域。函数返回指向destin的指针。
2.strcpy和memcpy主要有以下3方面的区别。
2.1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。
2.2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符"\0"才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。
2.3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy
3.如果目标数组destin本身已有数据,执行memcpy()后,将覆盖原有数据(最多覆盖n)。如果要追加数据,则每次执行memcpy后,要将目标数组地址增加到你要追加数据的地址。
注意:source和destin都不一定是数组,任意的可读写的空间均可。


程序例


作用:将s中的字符串复制到字符数组d中。
// memcpy.c
#include <stdio.h>
#include <string.h>
 
int main()
{    
    char *s="Golden Global View";
    char d[20];
    clrscr();
    memcpy(d,s,(strlen(s)+1));
    printf("%s",d);
    getchar();
    return 0;
}
输出结果:Golden Global View


GetLine
参数
is 进行读入操作的输入流
str 存储读入的内容
delim 终结符
返回值
与参数is是一样的
功能
将输入流is中读到的字符存入str中,直到遇到终结符delim才结束。对于第一个函数delim是可以由用户自己定义的终结符;对于第二个函数delim默认为 '\n'(换行符)。
函数在输入流is中遇到文件结束符(EOF)或者在读入字符的过程中遇到错误都会结束。
在遇到终结符delim后,delim会被丢弃,不存入str中。在下次读入操作时,将在delim的下个字符开始读入。
举例:
#include<string>//getline包含在string头文件里
#include<iostream>
using namespace std;
int main()
{
    string str;
    getline(cin,str,'#');
    char c=getchar();
    cout<<str<<' '<<c<<endl;
    return 0;
}
输入为:aa#b
输出为:aa b


substr
功能:从一个字符串复制一个从指定位置开始,并具有指定长度的子字符串。
Code : C++中 的代码如下
// basic_string_substr.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
using namespace std;
int main( )
{
using namespace std;
string str1 ("Heterological paradoxes are persistent.");
cout << "The original string str1 is: \n " << str1
<< endl << endl;
basic_string <char> str2 = str1.substr ( 6 , 7 );
cout << "The substring str1 copied is: " << str2
<< endl << endl;
basic_string <char> str3 = str1.substr ( );
cout << "The default substring str3 is: \n " << str3
<< "\n which is the entire original string." << endl;
输出结果:
The original string str1 is:
Heterological paradoxes are persistent.
The substring str1 copied is: logical
The default substring str3 is:
Heterological paradoxes are persistent.
which is the entire original string.
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值