重拾C,一天一點點_10


來博客園今天剛好兩年了,兩年前開始學編程。

忙碌近兩個月,項目昨天上線了,真心不容易,也不敢懈怠,接下來的問題會更多。這兩天調試服務器,遇到不少麻煩。

剛出去溜達了一下,晚上天涼了,現在手感覺涼的有點不靈活了都。大伙多注意身體!

繼續我的C。發現個問題,自己的文章排版很丑,以后也要多注意。

printf("hello world");

printf接受的是一個指向字符數組第一個字符的指針。也就是說,字符串常量可通過一個指向其第一個元素的指針訪問。

char *p;

p = "hello world";   //將一個指向字符串數組的指針賦值給p。該過程沒有進行字符串的復制,只是涉及到指針的操作。C語言沒有提供將整個字符串作為一個整體進行處理的運算符。

char s[] = "hello world";  //定義一個字符數組

char *p = "hello world";  //定義一個指針

兩種聲明的區別:

s是一個僅足以存放初始化字符串及空字符'\0'的一維數組,數組中的單個字符可以修改。

p始終指向同一個存儲位置,其初始值指向一個字符串常量,之后它可以被修改以指向其他地址,如果試圖修改字符串的內容,結果是沒有定義的。

//復制字符串

 1 #include <stdio.h>
 2 void strcpy1(char *s, char *t);
 3 
 4 main(){ 
 5     char t[] = "hello world";
 6     char s[] = "";
 7     strcpy1(s,t); 
 8     printf("%s\n",s);    //hello world    
 9 }
10 /******將指針t指向的字符串復制到指針s指向的位置,使用數組下標實現***/
11 void strcpy1(char *s, char *t){
12     int i = 0;
13     while((s[i] = t[i]) != '\0'){
14         i++;
15     }
16 }
 1 #include <stdio.h>
 2 void strcpy2(char *s, char *t);
 3 
 4 main(){ 
 5     char t[] = "hello world";
 6     char s[] = "";
 7     strcpy2(s,t); 
 8     printf("%s\n",s);    //hello world    
 9 }
10 /******將指針t指向的字符串復制到指針s指向的位置,使用指針實現***/
11 void strcpy2(char *s, char *t){
12     while((*s = *t) != '\0'){
13         s++;
14         t++;
15     }    
16     /**
17     //簡寫 
18     while((*s++=*t++) != '\0')
19         ;
20     **/
21     /**
22     //再簡寫 
23     while(*s++=*t++)
24         ;
25     **/
26 }

剛遇到這個警告:conflicting types for built-in function 'strcpy'

  函數命名沖突了

//比較兩字符串

 1 #include <stdio.h>
 2 int strcmp(char *s, char *t);
 3 
 4 main(){ 
 5     char t[] = "hello world";
 6     char s[] = "helloabc";    
 7     printf("%d\n",strcmp(s,t));        //65
 8 }
 9 /****比較兩字符串順序***/
10 int strcmp(char *s, char *t){
11     int i;
12     for(i=0; s[i]==t[i]; i++){
13         if(s[i] == '\0'){
14             return 0;
15         }
16     }
17     return s[i] - t[i];
18 }
#include <stdio.h>
int strcmp(char *s, char *t);

main(){ 
	char t[] = "hello world";
	char s[] = "helloabc";	
	printf("%d\n",strcmp(s,t));		//65
}
/****比較兩字符串順序***/
int strcmp(char *s, char *t){
	for(; *s==*t; s++,t++) {
		if(*s == '\0'){
			return 0;
		}
	}	
	return *s - *t;
}

一個函數實現或一種算法的實現,還是需要用數據去模擬,然后找出規律。就上例,作簡單分析:

s1 "hello world";

s2 "helloabc";

for循環,i=0,s[0]=t[0],依此類推,s[4]=t[4],當i=5時,s[5]是一個空格,t[5]=a,s[5]!=t[5],跳出for循環,返回a字符與空格字符的差,97-32=65。假如t[5]也是一個空格的話,繼續下一個比較,如果s[6]==‘\0’的話,說明s[5]還是等於t[5],返回0。

以后盡量都要去多分析原理,加深記憶。

指針數組及指向指針的指針

  指針本身也是變量,所以它也可以其他變量一樣存儲在數組中。

二維數組

今天是2013年的第300天,今年只剩65天,大家多多珍惜吧!很巧的是,之前的測試中字符a-空格剛好也是65。

 1 #include <stdio.h>
 2 int day_of_year(int year, int month, int day);
 3 void month_day(int year, int yearday, int *pmonth, int *pday);
 4 
 5 static char daytab[2][13] = {
 6     {0,31,28,31,30,31,30,31,31,30,31,30,31},
 7     {0,31,29,31,30,31,30,31,31,30,31,30,31}
 8 };
 9 main(){
10     printf("%d\n", day_of_year(2013, 10, 27));    //300 
11     int pmonth = 0;    
12     int pday = 0;
13     int year = 2013;
14     int yearday = 300;
15     month_day(year, yearday, &pmonth, &pday);
16     printf("%d年第%d天是%d月%d日\n",year, yearday,pmonth,pday);        //2013年第300天是10月27日
17     return 0;
18 }
19 
20 int day_of_year(int year, int month, int day){
21     int i, leap;
22     leap = (year%4 == 0 && year%100 != 0) || (year %400 == 0);
23     for(i=1; i<month; i++){
24         day += daytab[leap][i];
25     }
26     return day;
27 }
28 
29 void month_day(int year, int yearday, int *pmonth, int *pday){
30     int i, leap;
31     leap = (year%4 == 0 && year%100 != 0) || (year %400 == 0);
32     for(i=1; yearday>daytab[leap][i]; i++){
33         yearday -= daytab[leap][i];
34     }
35     *pmonth = i;
36     *pday = yearday;
37 }

 附:

一個人晚上出去打了10斤酒,回家的路上碰到了一個朋友,恰巧這個朋友也是去打酒的。不過,酒家已經沒有多余的酒了,且此時天色已晚,別的酒家也都已經打烊了,朋友看起來十分着急。於是,這個人便決定將自己的酒分給他一半,可是朋友手中只有一個7斤和3斤的酒桶,兩人又都沒有帶稱,如何才能將酒平均分開呢?

一天,小趙的店里來了一位顧客,挑了20元的貨,顧客拿出50元,小趙沒零錢找不開,就到隔壁小韓的店里把這50元換成零錢,回來給顧客找了30元零錢。過一會,小韓來找小趙,說剛才的是假錢,小趙馬上給小李換了張真錢。問:在這一過程中小趙賠了多少錢?

原文作者:lltong,博客園地址:http://www.cnblogs.com/lltong/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM