1. 程式人生 > >字串和陣列

字串和陣列

字串和陣列

在開闢陣列的時候要滿足大開小用
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);
}