您的位置:首页 > 其它

c programe language learn notes 3

2006-09-13 21:50 561 查看
爱,突然的昨天晚上寝室停电

看到第二章了,感觉英文水平不够啊

还是把自己觉得要留意的总结一下

1Externalandstaticvariablesareinitializedtozerobydefault.Automaticvariablesforwhichisnoexplicitinitializerhaveundefined(i.e.,garbage)values.
外部变量和静态变量初始的时候默认值为零,自动变量在初始的时候是没有默认值的。

2The%operatorcannotbeappliedtoafloatordouble.
c>='0'&&c<='9'
canbereplacedby
isdigit(c)
Wewillusethe<ctype.h>functionsfromnowon.
c>='0'&&c<='9'可以被isdigit(c)代替,当然要包括头文件<ctype.h>

3Ifeitheroperandislongdouble,converttheothertolongdouble.
Otherwise,ifeitheroperandisdouble,converttheothertodouble.
Otherwise,ifeitheroperandisfloat,converttheothertofloat.
Otherwise,convertcharandshorttoint.
Then,ifeitheroperandislong,converttheothertolong.
应该是用于双目运算符时候,低一级的数值类型要向高以及转化

4The%operatorcannotbeappliedtoafloatordouble.
取模不能用于浮点型或双精度型,也就是说只能用于int型的数值

5Sinceanargumentofafunctioncallisanexpression,
typeconversionalsotakesplacewhenargumentsarepassedtofunctions.
Intheabsenceofafunctionprototype,charandshortbecomeint,
andfloatbecomesdouble.
Thisiswhywehavedeclaredfunctionargumentstobeintanddoubleevenwhenthefunctioniscalledwithcharandfloat.
重点解释一下我认为的吧,
如果你的函数没有定义返回类型,那么你的函数自变量在返回的时候会自动从char,short转化为int。而float会转化为double型的。
所以这就是为什么我们定义函数自变量的为int和double,当这个函数被char和float调用的时候。
//不过自己还是懵懵懂懂的,希望那位大哥可以说的明白一些^。^

2.1
Writeaprogramtodeterminetherangesof
char
,
short
,
int
,and
long
variables,both
signed
and
unsigned
,byprintingappropriatevaluesfromstandardheadersandbydirectcomputation.Harderifyoucomputethem:determinetherangesofthevariousfloating-pointtypes.

#include<stdio.h>
#include<limits.h>

int

main()

{

printf("SizeofChar%d/n",CHAR_BIT);

printf("SizeofCharMax%d/n",CHAR_MAX);

printf("SizeofCharMin%d/n",CHAR_MIN);

printf("Sizeofintmin%d/n",INT_MIN);

printf("Sizeofintmax%d/n",INT_MAX);

printf("Sizeoflongmin%ld/n",LONG_MIN);/*RB*/

printf("Sizeoflongmax%ld/n",LONG_MAX);/*RB*/

printf("Sizeofshortmin%d/n",SHRT_MIN);

printf("Sizeofshortmax%d/n",SHRT_MAX);

printf("Sizeofunsignedchar%u/n",UCHAR_MAX);/*SF*/

printf("Sizeofunsignedlong%lu/n",ULONG_MAX);/*RB*/

printf("Sizeofunsignedint%u/n",UINT_MAX);/*RB*/

printf("Sizeofunsignedshort%u/n",USHRT_MAX);/*SF*/

return0;

}
这个函数是copy来的,主要是不懂得某些c中的标准函数,还是要多看附录啊

2.2
Exercise2-2discussesa[code]for
loopfromthetext.Hereitis:

for(i=0;i<lim-1;)
{
if((getchar())!='/n')
if(c!=EOF)

s[i++]=c;
}
和参考答案不一样,觉得这样也是可以的哦,而且比较简洁的说

2.3
Writethefunction[code]htoi(s)
,whichconvertsastringofhexadecimaldigits(includinganoptional
0x
or
0X)
intoitsequivalentintegervalue.Theallowabledigitsare
0
through
9,
a
through
f,
and
A
through
F
.[/code]
程序思路:
将一个十六进制数,依次从左到右将其各自的数据乘以16的n次幂,
这里n的取值为:从右往左起顺序排列数再减去1,如123那么1排第2位,
2排第1位,3排第0位,如果有小数位,那么依次从小数点往后是-1、-2、-3...

为了简单就写整数位的

longhtoi(chars[]){
inti;
intlen=strlen(s);
intpos=len-1;
longsum=0;
for(i=0;i<len;i++)
{
sum+=s[pos]*power(16,i);
pos--;
}
returnsum;
}

longpower(intm,intn){
inti;
longsum=1;
for(i=1;i<n;i++)
{
sum*=m;
}
returnsum;
}

2.4
Writeanalternateversionof[code]squeeze(s1,s2)
thatdeleteseachcharacterinthestring
s1
thatmatchesanycharacterinthestring
s2
.
voidsqueeze2(chars1[],chars2[])

{

inti,j,k;

intinstr2=0;


for(i=j=0;s1[i]!='/0';i++)

{

instr2=0;

for(k=0;s2[k]!='/0'&&!instr2;k++)

{

if(s2[k]==s1[i])

{

instr2=1;

}

}


if(!instr2)

{

s1[j++]=s1[i];//觉得这里是这个程序思想的要点,如果没有相同的就放入字符数组中。

//可能这样些会更明白一些

//s1[j]=s1[i];

//j++;

}

}

s1[j]='/0';

}


觉得这个程序比我自己想的要好许多[/code]
2.5
Writethefunction[code]any(s1,s2)
,whichreturnsthefirstlocationinthestring
s1
whereanycharacterfromthestring
s2
occurs,or
-1
if
s1
containsnocharactersfrom
s2
.(Thestandardlibraryfunction
strpbrk
doesthesamejobbutreturnsapointertothelocation.)[/code]
intsqueeze1(chars1[],chars2[]){
inti,j;
intpos=0;
for(i=0;flag==-1&&s1[i]!='/0';i++)

for(j=0;flag==-1&&s2[j]!='/0';j++)

if(s1[i]==s2[j])

pos=i;

returnpos;
}

觉得这样写也不个办法啊,算了只要看的懂就可以了的说,该写的时候写吧

……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: