气到学习的一天

1.#include<> #include""

前者针对系统头文件 后者针对自己编写的头文件

2.#ifndef 之间定义一个宏 #endif

防止一个头文件被多次调用导致编译错误,如果宏在在#inndef之后没有被定义过,则执行头文件 如果前面已经被定义过 则不执行。

1.rewind函数用于清楚缓存区字符 在头文件#include<stdio.h>中

欧耶 制作了一个计算器

#include"stdafx.h"
#include<stdio.h>
#include<iostream>
using namespace std;
int Sum_Int(int a, int b){
	return a + b;
}
int Sub_Int(int a, int b){
	return a - b;
}
int Mul_Int(int a, int b){
	return a*b;
}
int Div_Int(int a, int b){
	return a / b;
}
int   sum = 0;
int Counter(int a, int b, char p)
{

	switch (p){
	case '+':sum = Sum_Int(a, b); break;
	case '-':sum = Sub_Int(a, b); break;
	case '*':sum = Mul_Int(a, b); break;
	case '/':
		if (b == 0){
			cout << "除0错误!";
		}
		else sum = Div_Int(a, b);
		break;
	default:
		cout << "操作数错误!";
		break;

	}
	return sum;
}

int main(){
	int a, b;
	char p;
	char tag;
	do{
		cout << "请输入a,b,p(6 7 +)" << endl;
		scanf_s("%d %d %c", &a, &b, &p);
		Counter(a, b, p);
		cout << sum << endl;

		rewind(stdin);
		tag = getchar();
	} while (tag == 'y'||tag=='Y');
}

3.

 assert  可以终止程序(括号内为真的话向下执行)(括号内为假的话被终止)

assert  在头文件#include<assert.h>中

4.数组名在sizeof中代表整个数组  其他代表首元素的地址!又忘了嘤嘤嘤

5.

 指针存放的数据位于数据区的只可读区,输出结果为  0  1;1.编写出一个程序的幸福感真的爆棚i  爱可抵岁月漫长!

这个是字符串中的函数strcmp---->在头文件#include<string.h>中

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
//计数变量  任何变量
int my_strcmp(const char *stra, const char *strb){
	if (stra == NULL&&strb!=NULL) return -1;
	else{
		int a = strlen(stra);
		int b = strlen(strb);
		int t = a > b ? a : b;
		int i = 0;
		while (i < t){
			if (stra[i] == strb[i])
			{
				i++;
				if (i == t - 1)  return 0;
			}
			else if (stra[i]>strb[i])  return 1;
			else return -1;
		}
	}
}
int main(){
	char stra[] = { "abcde" };
	char strb[] = { "abc" };
	int x = my_strcmp(stra, strb);
	cout << x << endl;
	return 0;
}

2.strchr  判断某个字符首次出现的位置

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
//查找某个字符第一次出现的位置
int my_strchr(const char *str, int ch){
	if (str == NULL)  return;
	for (int i = 0; str[i] != '\0'; i++){
		if (*(str + i) == ch){
			return i;
			break;
		}
	}
}
int main(){
	char *str = { "blingbling" };
	int  index = my_strchr(str, 'g');
	cout << index << endl;
	return 0;
}

3.strrchr  判断某个字符最后一次出现的位置

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
//查找某个字符第一次出现的位置
int my_strrchr(const char *str, int ch){
	if (str == NULL)  return NULL;
	for (int i = strlen(str); i>0; i--){
		if (*(str + i) == ch){
			return i;
			break;
		}
	}
}
int main(){
	char *str = { "blingbling" };
	int  index = my_strchr(str, 'b');
	cout << index << endl;
	return 0;
}

4.strcmp气死我啦 理解错了老师的意思 写了好久  最后才发现巨简单

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
//拼接字符串
void my_strcpy(char *a, char *b){
	int i = 0;
	for (i = 0; b[i] != '\0'; i++){
		a[i] = b[i];
	}
	a[i] = '\0';
	
}
int main(){
	char a[30];
	char b[] = { "bling" };
	char c[30];
	my_strcpy(a, b);
	my_strcpy(c, b);
	int i = 0;
	for (i = 0; a[i] != '\0'; i++){
		cout << a[i];
	}
	cout << endl;
	for (i = 0; a[i] != '\0'; i++){
		cout << c[i];
	}
}

1.strstr  判断一个字符串是否为另一个字符串的子串

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
//判断是否为子串
char *my_strstr(char *stra, char *strb){
	int m = strlen(stra);
	int n = strlen(strb);
	for (int i = 0; i < m - n + 1; i++){
		int j = 0;
		while (strb[j] != '\0')
		{
			if (stra[i] == strb[j])
			{
				i++;
				j++;
			}
			else{
				i = i - j + 1; 
				j = 0;
			}
		}
		if (j == n)  return (char*)stra + i - j;
		else return NULL;
	}
	
}
int main(){
	char stra[] = {"onetwothree"};
	char strb[] = { "two" };
	char *p=my_strstr(stra, strb);
	if (p != NULL){
		cout << p;
	}
	else cout << "strb 不是 stra的子串";
	
	return 0;

}

第二种算法  没写出来嘤嘤嘤

 赋值语句也有返回值  返回值为*cp  当*cp==0时退出while循环

4.王布丁2022  2 10 日最大的成就  自己打印出来了字符串类的题

strcat  把一个字符串连接到另一个字符串上

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
//把字符串b的字符连接到a后面
char  *my_strcat(char *stra, char *strb){
	int n = strlen(stra);
	int m = strlen(strb);
	char *ch=stra;
	int i = 0;
	for (int i = 0; i <= n + m; i++)
	{
		for (int k = 0; k < n; k++){
			ch[i] = stra[i];
		}
		for (int j = 0; j <=m; j++){
			ch[j + n] = strb[j];
		}
	}
	return (char *)stra;
}
int main(){
	char a[30] = { "Hello" };
	char b[6] = { "bling" };
	char *p=my_strcat(a, b);
	cout << p << endl;

}

strcat 

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
#include<assert.h>
using namespace std;
//把字符串b的字符连接到a后面
void my_strcat(char *stra, char *strb){
	assert(stra != NULL || strb != NULL);
	int n = strlen(stra);
	int m = strlen(strb);
	char *ch=stra;
	int i = 0;
	for (int i = 0; i <= n + m; i++)
	{
		for (int k = 0; k < n; k++){
			ch[i] = stra[i];
		}
		for (int j = 0; j <=m; j++){
			ch[j + n] = strb[j];
		}
	}
}
int main(){
	char a[30] = { "Hello" };
	char b[6] = { "bling" };
	my_strcat(a, b);
	cout << a << endl;

}

5.山路十八弯  弄出来啦

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
#include<assert.h>
using namespace std;
//把字符串b的字符连接到a后面
char *my_strcat(char *stra, char *strb){
	assert(stra != NULL || strb != NULL);
	int n = strlen(stra);
	int m = strlen(strb);
	char *ch=stra;
	while (*ch != '\0'){
		ch++;
	}
	while (*ch = *strb){
		strb++;
		ch++;
	}
	return (char*)stra;

	
}
int main(){
	char a[40] = { " Hello " };
	char b[6] = { "bling" };
	char c[20] = { "beautiful" };
	char *p= my_strcat(a, b);
	cout << p << endl;

}

6.strdup   

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
//把字符串b的字符连接到a后面
void my_strcpy(char *a, char *b){
	int i = 0;
	for (i = 0; b[i] != '\0'; i++){
		a[i] = b[i];
	}
	a[i] = '\0';

}
char *my_strdup(char *stra){
	assert(stra != NULL );
	int len = strlen(stra) + 1;//字符串一定要加1  '\0'
	char *ch= (char*)malloc(sizeof(char)*len);
	my_strcpy(ch, stra);
	return ch;
}
int main(){
	char a[] = { "Hellobling" };
	char *p= my_strdup(a);
	cout << p << endl;

}

1.拷贝函数是strcpy   

2.计算空间

(类型) 变量名=(类型)malloc(sizeof(存放数据的类型)*数据长度))

3.字符串

strcat------->连接字符串

strlen ------->计算字符串长度

strdup------->把一个字符串赋值给另一个字符串,在堆区申请存储空间,字符串可读可写,即可以改变某一个位置的值,在使用完成后用free还给系统。

strcpy------->拷贝

strchr------->判断某个字符首次出现的位置

strrchr------->判断一个字符最后一次出现的位置

strstr------->判断一个字符串是否为另一个的子串

1.空字符串  存放NULL

空串的区别    “\0”------->长度为0

“        ”------->空格字符串

2.不定义计数器来写strlen函数

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
//不定义计数器来写strlen函数
int my_atrlen(char *stra){
	char *ch = stra;
	while (*ch != '\0')
	{
		ch++;
	}
	return ch - stra;

}
int main(){
	char stra[] = { "bling" };
	int c = my_atrlen(stra);
	cout << c << endl;
	return 0;
}

3.

函数在被调用时从右往左传递值 所以fun函数只能接收到12

4。

1.memset------>

泛型函数  将参数设置为某一值

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;

void my_memset(void *vp, unsigned char ch, size_t count){
	assert(vp != NULL);
	unsigned char* cp = (unsigned char  *)vp;
for (int i = 0; i < count; i++){
*cp = ch;
cp++;
}
}

int main(){
	char name[20];
	int ege[20];
	memset(name, 'a', sizeof(name));
	memset(ege, 5, sizeof(ege));
	for (int i = 0; i < 20; i++){
		cout << name[i] << "  ";
	}

}

 指针一定要进行判空  再忘记就打屁屁

2.memcmp

比较存储区的字节哪个大

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
//比较存储区的前n个字节
int my_memcmp(void *stra, void *strb,size_t count ){
	assert(stra != NULL || strb != NULL);
	 char *cp1 = ( char*)stra;
	 char *cp2 = ( char*)strb;
	 int t = strlen(cp1) > strlen(cp2) ? strlen(cp1) : strlen(cp2);
	 int i = 0;
	 while (i < count){
		 if (cp1[i] == cp2[i])
		 {
			 i++;
			 if (i == count - 1)
				 return 0;
		 }
		 else if (cp1[i]>cp2[i]){
			 return 1;
		 }
		 else return -1;
	 }
		
	 
}
int main(){
	char stra[] = { 2, 3, 5, 6, 7, 8, 8 };
	char strb[] = { 2, 3, 5, 7, 7, 8, 8 };
	int c = my_memcmp(stra, strb,sizeof(stra));
	cout << c << endl;
}

2.memcpy-------->内存拷贝函数

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;
//比较存储区的前n个字节
 struct student{
	char name[20];
	int  ege;

};
void  my_memcpy(void *strb,const void *stra, size_t count){
	char *cb = (char *)strb;
	char *ca = (char *)stra;
	size_t i = 0;
	while (i < count){
		*cb = *ca;
		cb++;
		ca++;
		i++;
	}
}
int main(){
	 struct student s1={  "wangbd", 19 };
	 struct student s2;
	 my_memcpy(&s2, &s1, sizeof(s1));
	 cout << s2.name << "   " << s2.ege << endl;
	 return 0;
}

1.memmove

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
#include<math.h>
using namespace std;

//memmove->>>和memcpy差不多  但是可以把值赋值给自己
void *my_memmove(void *strb,const void *stra, size_t count){
	assert(strb != NULL || stra != NULL);
	if (count == 0)  return  strb;
	const char *ca = (const char*)stra;
	char *cb = (char*)strb;
	if ((cb-ca)>count){
		
		while (count--){
			*cb = *ca;
			cb++;
			ca++;
		}
	}
	
	else{
		ca = ca + count - 1;
		cb = cb + count - 1;
		while (count--){
			*cb = *ca;
			cb--;
			ca--;
		}
	}
	return strb;

}
int main(){
	char stra[20] = { "hellobling" };//
	char strb[20];
	//memmove(stra+4, stra, 2);
	my_memmove(stra+4 , stra, 2);
	cout << stra << endl;
	return 0;
}

2.atoi    -------->将字符串转化为整形。

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
#include<math.h>
#include<stdio.h>
using namespace std;
//将字符串转化成一个整形
int  my_atoi(const char *str){
	assert(str != NULL);
	int c = 0;
	int i = 0;
	while (str[i] != '\0')
	{
		if (isdigit(str[i]))
		{
			c = c * 10 + str[i]-'0';
			
			i++;

		}
		
		else if (!isdigit(str[i])) {
			break;
		}
	}
	if (c != NULL)  return c;
	else  return 0;
}
int main(){
	char str[] = { "y5456.wsdtyg" };
	int c = my_atoi(str);
	//int c = atoi(str);
	cout << c << endl;
	
	
	
}

1.字符串转整形需要-'0'

2.整形转字符串需要+'0'

int main(){
	char buffer[30];
	int c = 8, h = 8;
	sprintf_s(buffer, "c=%d,h=%d\n", c, h);
	cout << buffer << endl;
	return 0;
}

printf的功能是将打印取的字符串送到屏幕---->stdout

sprintf的功能是将打印取的字符串送到字符串里面  代码如上

2.ssanf_s

//sprintf
int main(){
	char buffer[30] = {"12.32"};
	int c=0,h=0;
	sscanf_s(buffer, "%d.%d", &c, &h);
	cout << c<<"    " <<h  << endl;
	return 0;
}

记得设置取地址符,,不然会死的很惨呀!

-------------------------------------------------------------------------------------------------------

整形二级指针+1--->加4字节(因为在32位系统中一级指针占4字节)

加多少是根据指向元素类型来判断

如果是double型的指针,一级指针将加8字节 

 

 

int (*p)[4]------>代表的是 指针指向四个整形数组

int *p[4]----->一个数组中存放4个整形指针

 2.一个数组名位ar   在sizeof中,数组名代表的是整个数组

其余代表   ------>ar  首元素的大小      如果是二维数组  则代表第一个一维数组

&ar  代表整个数组的大小

stra[3][4]------>存放数据

*stra[3]------>存放3个数据的地址 

3.八皇后问题打印出来啦!

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
#include<math.h>
#include<stdio.h>
using namespace std;
//一维数组的八皇后问题
int main(){
	int arr[4] = { 2, 0, 3, 1 };
	             //0  1  2  3
	for (int i = 0; i < 4; i++){
		for (int j = 0; j < 4;j++){
			if (j == arr[i]){
				cout << 'Q' << "\t";
			}
			else cout << '#' << "\t";

		}
		puts("");
	}
	cout << "王布丁超级腻害!" << endl;
	return 0;
}

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
#include<math.h>
#include<stdio.h>
using namespace std;
//打印蛇形数组
int main(){
	int arr[4][4];
	int t = 1;
	for (int j = 0; j <= 3; j++){
		arr[0][j] = t;
		t++;
	}
	for (int i = 1; i <= 3; i++){
		arr[i][3] = t;
		t++;
	}
	for (int j = 2; j >= 0; j--){
		arr[3][j] = t;
		t++;
	}
	for (int i = 2; i >= 1; i--){
		arr[i][0] = t;
		t++;
	}
	for (int j = 1; j <= 2; j++){
		arr[1][j] = t;
		t++;
	}
	for (int j = 2; j >= 1; j--){
		arr[2][j] = t;
		t++;
	}
	//
	for (int i = 0; i < 4; i++){
		for (int j = 0; j < 4; j++){
			cout << arr[i][j] << "\t";

		}
		puts("");
	}
	cout << "王布丁要加油哇!" << endl;
}

 3.对字符串进行排序------->冒泡排序

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
//指针需要判空
//对字符串进行冒泡排序
void Init_str(char (*str)[30], int h){
	assert(str != NULL);

	for (int i = 0; i < h; i++){
		gets_s(str[i],30);
	}
}
void Print_str(char str[][30], int h){
	assert(str != NULL);

	for (int i = 0; i < h; i++){
		cout << str[i] << endl;
	}
}
void Bubble_sort_str( char(*str)[30], int h, int l){
	assert(str != NULL);

	 char * tmp = (char *)malloc(sizeof(char)*l);
	for (int i = 1; i < h; i++){
		for (int j = 0; j < h-i; j++){
			if ((strcmp(str[j],str[j+1])>0)){
				*tmp = *str[j];
				*str[j] = *str[j + 1];
				*str[j + 1] = *tmp;
			}
		}
	}
	free(tmp);
	tmp = NULL;
}
int main(){
	char str[5][30];
	Init_str(str, 5);
	Bubble_sort_str(str, 5,30);
	Print_str(str, 5); 

}

 3.用一维数组打印杨辉三角

#include"stdafx.h"
#include<iostream>
#include<string.h>
#include<assert.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define N 20
//用一维数组打印杨辉三角
int main(){
	int arr[N];
	for (int i = 0; i <N; i++){
		for (int j = i; j >= 0; j--){
			if (j == i || j == 0){
				arr[j] = 1;
			}
			else{
				arr[j] = arr[j] + arr[j - 1];
			}
		
		}
	
	
	}
	
	for (int i = 0; i < N; i++){
		for (int j = 0; j <= i; j++){
			cout << arr[j] << "   ";
		}
		puts("");
	}
}

1.将一维数组向右移1位

#include"stdafx.h"
#include<iostream>
#include <stdio.h>
#define N 7
using namespace std;
//1.指针判空   2.右移一个数据元素
void Print_Arr(int *arr, int n){
	for (int i = 0; i < n; i++){
		cout << arr[i] << "  ";
	}
	cout << endl;
}

void Right_Move_Array(int *arr, int n){
	int i = n - 1;
	int k = arr[i];
    while (i >= 1){
		arr[i] = arr[i - 1];
		i--;
	}
	arr[i] = k;
}

int main(){
	
	int arr[N] = { 12, 23, 34, 45, 56, 67, 78 };
	Print_Arr(arr, N);
	Right_Move_Array(arr, N);
	Print_Arr(arr, N);
	return 0;
}

2. 数组向右移动k位

#include"stdafx.h"
#include<iostream>
#include <stdio.h>
#include<assert.h>
#define N 7
using namespace std;
//1.指针判空   2.数组向右移K位
void Print_Arr(int *arr, int n){
assert(arr!=NULL);
	for (int i = 0; i < n; i++){
		cout << arr[i] << "  ";
	}
	cout << endl;
}

void Right_Move_Array_k(int *arr, int n,int k){
assert(arr!=NULL);
	for (int j = 0; j < k; j++){
		int i = n - 1;
		int t = arr[i];
		while (i >= 1){
			arr[i] = arr[i - 1];
			i--;
		}
		arr[i] = t;
	}
}

int main(){
	
	int arr[N] = { 12, 23, 34, 45, 56, 67, 78 };
	Print_Arr(arr, N);
	int k;
	cout << "k=" << endl;
	cin >> k;
	Right_Move_Array_k(arr, N,k);
	Print_Arr(arr, N);
	return 0;
}


#if 0
int main(){

	int arr[N] = { 12, 23, 34, 45, 56, 67, 78 };
	Print_Arr(arr, N);
	int val;
	cout << "请输入你要查找的数值" << endl;
	cin >> val;
	int index = Binary_Serrch(arr, N, val);
	if (index != -1){
		cout << "找到了  下标为:" << index << endl;

	}
	else{
		cout << "没找到!" << endl;
	}
	return 0;
}
#endif

2.将数组左移k 位

#include"stdafx.h"
#include<iostream>
#include <stdio.h>
#include<assert.h>
#define N 7
using namespace std;
//1.指针判空   2.数组向左移K位
	
void Print_Arr(int *arr, int n){
	assert(arr != NULL);
	for (int i = 0; i < n; i++){
		cout << arr[i] << "  ";
	}
	cout << endl;
}

void Left_Move_Array_k(int *arr, int n,int k){
	assert(arr != NULL);

	for (int j = 0; j < k; j++){
		int i = 0;
		int t = arr[0];
		while (i <= n-2){

		arr[i] = arr[i+1];
			i++;
		}
		arr[i] = t;
	}
}

int main(){
	
	int arr[N] = { 12, 23, 34, 45, 56, 67, 78 };
	Print_Arr(arr, N);
	int k;
	cout << "k=" << endl;
	cin >> k;
	Left_Move_Array_k(arr, N,k);
	Print_Arr(arr, N);
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值