重拾C,一天一点点_10

来博客园今天刚好两年了,两年前开始学编程。

忙碌近两个月,项目昨天上线了,真心不容易,也不敢懈怠,接下来的问题会更多。这两天调试服务器,遇到不少麻烦。

刚出去溜达了一下,晚上天凉了,现在手感觉凉的有点不灵活了都。大伙多注意身体!

继续我的C。发现个问题,自己的文章排版很丑,以后也要多注意。

printf("hello world");

printf接受的是一个指向字符数组第一个字符的指针。也就是说,字符串常量可通过一个指向其第一个元素的指针访问。

char *p;

p = "hello world";   //将一个指向字符串数组的指针赋值给p。该过程没有进行字符串的复制,只是涉及到指针的操作。C语言没有提供将整个字符串作为一个整体进行处理的运算符。

char s[] = "hello world";  //定义一个字符数组

char *p = "hello world";  //定义一个指针

两种声明的区别:

s是一个仅足以存放初始化字符串及空字符''的一维数组,数组中的单个字符可以修改。

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
",s);    //hello world    
 9 }
10 /******将指针t指向的字符串复制到指针s指向的位置,使用数组下标实现***/
11 void strcpy1(char *s, char *t){
12     int i = 0;
13     while((s[i] = t[i]) != ''){
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
",s);    //hello world    
 9 }
10 /******将指针t指向的字符串复制到指针s指向的位置,使用指针实现***/
11 void strcpy2(char *s, char *t){
12     while((*s = *t) != ''){
13         s++;
14         t++;
15     }    
16     /**
17     //简写 
18     while((*s++=*t++) != '')
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
",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] == ''){
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
",strcmp(s,t));		//65
}
/****比较两字符串顺序***/
int strcmp(char *s, char *t){
	for(; *s==*t; s++,t++) {
		if(*s == ''){
			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]==‘’的话,说明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
", 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日
",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/

原文地址:https://www.cnblogs.com/lltong/p/3390240.html