字符串和数组
在开辟数组的时候要满足大开小用
char ch1[] = “yangheping”;
char ch2[] = { “yangheping” };
两个字符串是放在栈区的,(ch1 == ch2) << endl; // 0是因为ch1和ch2
都指向两个字符串的首元素地址,然而在栈区中两个字符串的变量名字不同,且ch1[]和ch2[]让系统在内存中为其开辟了两个空间来存放字符串,尽管这两个字符串的内容完全相同。但因为他们的他们存放的字符串首元素地址不同,所以比较结果为0
而char *str1 = “yangheping”;
char *str2 = “yangheping”;这种情况使得编译器认为他是字符串常量,所以"yangheping"是存在数据区的,且在数据区中相同常量只存在一个并且不能给改变,所以str1和str2所指向的首地址,所以他们俩相同。
相关例题:计算字符串的长度
#define STRPNULL -1
int my_strlen(const char *ch)
{
const char *p=ch+1;
if (ch == NULL) return STRPNULL;
for ( ;*ch++!='\0';){}
return (ch - p);
}
void main()
{
char ch1[] = { "yhping" };
const char *str = "yhping";
int x = my_strlen(str);
switch (x)
{
case STRPNULL:cout << "POINT ERROR" << endl;
case 0:cout << "字符串为空" << endl;
}
cout << my_strlen(ch1) << endl;
cout << my_strlen(str) << endl;
}
*进制计算公式total=total*进制+str-‘0’
例如
int total = 0;
while ('\0' != *str)
{
if (!isdigit(*str))break;
total = total * 10 + (*str - '0');
++str;
}
my_meset()函数是一个按字节赋值的函数,而不是按给类型赋值。
例如
int ar[10];
my_meset(ar,10,10);
的设想是将10个存储单元给赋值为10;
然而这样是无法实现的,他的实际作用是给10个字节赋值为10,而int ar[10];一共又40个字节所以,赋值10个字节是不符合int类型了储存规则的。所以打印ar[0]时不会出现10,而会出现40。
但是
char str[10];
my_meset(str,0,10);则可以完成对10个空间的赋值,应为char类型的数组一共只有10个字节。
另外memset()无法办到给数组赋值,只能作为初始化数组为全00000000或者
全ffffffff。把每个字节空间设置为0或者f
char str[10];
int ar[10];
int br[10];
//my_meset(ar, 10.10);
my_meset(str, 48, sizeof(str));
倒叙排位
#define ARRAYSIZE 10//数组进行倒叙排位
void Swap(int *a)
{
for (int i = 0; i < ARRAYSIZE / 2; i++)
{
if (a == NULL)return;
int temp;
temp = a[i];
a[i] = a[ARRAYSIZE - 1-i];
a[ARRAYSIZE - 1-i] = temp;
}
}
数字倒三角
void print_f(int x)
{
int i =1;//现在的行数
for (; i <= x; i++)
{
int flag = 0;//标记1:用于判断每行的输出每组数的个数
int flag2 = 0;//标记2:用于判断每行要输出几组数
for (int j = 1; j <= x;)
{
cout << j++;
flag++;
if ((flag == i)&&(flag2<x-i))//每两个数之间又1个空格,所以总共又x-i个
{
flag = 0;
flag2++;
cout<<setw(x+1-i);
j = j -(i- 1);
}
}
cout << endl;
}
}
void main()
{
while (1)
{
system("cls");
int x;
cout << "输入数的个数";
cin >> x;
print_f(x);
char y;
cout << "是否继续:y/n" << endl;
cin >> y;
if (y == 'n')
{
break;
}
}
}
二分法查找值
二分法查找:的条件是需要数组从小到大排序
11 22 33 44 55 66 77 88 99 100 //数组
0 1 2 3 4 5 6 7 8 9 //下标
left mid right
当输入的数value>mid时,left=mid+1;
当value<mid时,right=mid-1;
如果最后right<left错位了;说明在这个数组中没有查找的值
#define POINTERROR -1
#define NUMBERERROR -2
int find(int const *ar,int const value,int n)
{
if (ar == NULL) return POINTERROR;
int left = 0, mid = n / 2, right = n - 1;
while (1)
{
mid = (left + right) / 2;
if (ar[mid] == value)
{
return mid;
}
else if (value > ar[mid])
{
left = mid + 1;
}
else if (value <ar[mid])
{
right = mid - 1;
}
if (right < left)
{
return NUMBERERROR;
}
}
}
void main()
{
while (1)
{
system("cls");
int ar[] = { 12, 23, 34, 45, 56, 67, 78, 89, 90, 100 };
int value;
cin >> value;
int pos;
pos = find(ar, value, sizeof(ar) / sizeof(ar[0]));
switch (pos)
{
case POINTERROR:cout << "指针无效" << endl; break;
case NUMBERERROR:cout << "不存在该数" << endl; break;
default:cout << "下标为"<<pos << endl;
}
char ch;
cout << "是否继续:y/n" << endl;
cin >> ch;
if (ch == 'n')
{
break;
}
}
}
关于转义字符的内容
void main()
{
//char a = '\'',b='\\',c='\r';// \是转义符
//cout << a << endl;
//cout << b<< endl;
//cout << c << endl;
//int x = 'avb';
//int y = 'a';
//int z = 'v';
//int f = 'b';
//int h = y*z*f;
char ch1[] = "yangheping";
char ch2[] = { "yangheping" };
char *str1 = "yangheping";
char *str2 = "yangheping";
cout << (ch1 == ch2) << endl; // 0
cout << (str1 == str2) << endl; // 1
}
my_strcat()
#define POINTERROR -1
char *my_strcat( char *str1, const char *str2)
{
if (str1 == NULL || str2 == NULL) return POINTERROR;
char *p = str1;
while (*str1!='\0')
{
str1++;
}
while (*str1++ = *str2++){}
return p;
}
void main()
{
char str1[20] = { "yhping" };
char str2[20] = { "hellow" };
my_strcat(str1, str2);
}
手写的my_strcmp()
int my_strcmp(const char *str1, const char *str2)
{
if (str1 == NULL || str2 == NULL)
{
exit(1);
}
int k = 0;
while (((k = *str1 - *str2)) == 0 && *str1++&&*str2++);
return k;
}
void main()
{
char str1[20] = { "in" };
char str2[20] = { "lo" };
char str3[20] = { "yhping" };
my_strcat(str1, str2);
cout << my_strcmp(str1, str2);
}