重拾C,一天一点点_10

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

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

刚出去溜达了一下,晚上天凉了,如今手感受凉的有点不灵活了都。大伙多注意身体!数组

继续个人C。发现个问题,本身的文章排版很丑,之后也要多注意。服务器

printf("hello world");函数

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

char *p;ui

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

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/

相关文章
相关标签/搜索