/* binsearch函数:在v[0]<=v[1]<=v[2]<'''<=v[n-1]中查找X */
int binsearch(intx,int v[],int n)
{
int low,high,mid;
low=0;
high=n-1;
while(low<high){
mid=(low+high)/2;
if(x<v[mid])
high=mid-1;
else if(x>v[mid])
low=mid+1;
else //找到了匹配的值
return mid;
}
return -1;// 没有找到匹配的值
}
#include<stdio.h>
main() //统计数字、空白符及其他字符
{
int c,i,nwhite,nother,ndigit[10];
nwhite=nother=0;
for(i=0;i<10;i++)
ndigit[i]=0;
while((c=getchar())!=EOF){
switch(c){
case '0':case'1':case'2':case'3':case'4':
case'5':case'6':case'7':case'8':case'9':
ndigit[c-'0']++;
break;
case' ':case'/n':case'/t':
nwhite++;
break;
default:
nother++;
break;
}
}
printf("digits=");
for(i=0;i<10;i++)
printf(" %d",ndigit[i]);
printf(",white space =%d,other =%d/n",nwhite,nother);
return 0;
}
/*escape:expand newline and tab into visible sequences*/
/* while copying the string t to s */
void escape(char s[],char t[])
{
int i,j;
for(i=j=0;t[i]!='/0';i++) //控制整个循环
switch(t[i]){
case '/n': //newline
s[j++]='//'; //j是目标字符串s的索引
s[j++]='n';
break;
case '/t': //tab
s[j++]='//';
s[j++]='t' ;
break;
default: //all other chars
s[j++]=t[i]; // i是源字符串的索引
break;
}
s[j]='/0';
}
/*unescape:convert escape sequences into real characters*/
/* while copying the string t to s */
void unescape(char s[],char t[])
{
int i,j;
for(i=j=0;t[i]!='/0';i++)
if(t[i]!='//')
s[j++]=t[i];
else //it is a backslash
switch(t[++i]){
case 'n': //real newline
s[j++]='/n'; //把/n转化为换行符
break;
case 't': //把/t转化为制表符
s[j++]='/t' ;
break;
default: //all other chars
s[j++]='//';//把反斜杠后面的是其他字符的情况——
s[j++]=t[i];//把反斜杠和t[i] 复制到字符串s中去
break;
}
s[j]='/0';
/*unescape:convert escape sequences into real characters*/
/* while copying the string t to s */
void unescape(char s[],char t[])
{
int i,j;
for(i=j=0;t[i]!='/0';i++)
switch(t[i]){
case '//': //it is a backslash
switch(t[++i]){ //switch嵌套
case 'n': //real newline
s[j++]='/n'; //把/n转化为换行符
break;
case 't': //把/t转化为制表符
s[j++]='/t' ;
break;
default: //all other chars
s[j++]='//';//把反斜杠后面的是其他字符的情况——
s[j++]=t[i];//把反斜杠和t[i] 复制到字符串s中去
break;
}
break;
default:
s[j++]=t[i];
break;
}
s[j] ='/0';
}