《The C Programming Language II》练习题代码

来源:互联网 发布:22级研究所升级数据 编辑:程序博客网 时间:2024/06/05 11:01

《c程序设计语言》的代码基本都实现了,很多没贴上来,懒得粘贴了。这是我第二次写博客,求大家顶我。写了非常多的函数,算是提高对c语言的了解吧,毕竟算法竞赛经常用到c/c++,不能吃了语言的亏。而且这本书对字符串处理什么的,真是基础啊,有很多教你怎么写代码格式的,非常棒,以前用了那么久c++都不知道可以这样写。



//file: test2.cpp

#include<stdio.h>


main(){
char c;
// c = getchar();
// while(c!=EOF){
// putchar(c);
// c = getchar();
// }


//练习1-8 统计空白字符个数 
// int nb = 0,nsp = 0,nt = 0;
// while((c=getchar())!=EOF){
// if(c=='\n')nb++;
// else if(c=='\t')nt++;
// else if(c==' ')nsp++; 
// }
// printf("%d %d %d\n",nsp,nt,nb);


//练习1-9 单词间的多个空格缩减为一个 
// char pre;
// while((c=getchar())!=EOF){
// if(c==' '&&pre==' ')continue;
// putchar(c);
// pre = c;
// }
// printf("%c\n",-127);


//练习1-10 
while((c=getchar())!=EOF){
if(c=='\t')printf("\\t");
else putchar(c);
}
}




//test3.cpp

#include<stdio.h>

#define IN 0
#define OUT 1

//求幂函数,这里没有用快速幂实现 
long long power(int m,int n){
long long res = 1;
while(n>0){
res = res*m;
--n;
}
return res;
}


main(){
//练习1-11 单词计数 
// int c,state = OUT;
// int nc,nw,nl;
// nc = nw = nl = 0;
// while((c=getchar())!=EOF){
// ++nc;
// if(c=='\n')++nl;
// if(c==' '||c=='\t'||c=='\n'){
// state = OUT;
// }
// else{
// state = IN;
// ++nw;
// }
// }
// printf("%d %d %d\n",nl,nw,nc);
//
//练习1-12 每行只打印一个输入的单词 
// while((c=getchar())!=EOF){
// ++nc;
// if(c=='\n')++nl;
// if(c==' '||c=='\t'||c=='\n'){
// if(state==IN){
// putchar('\n');
// }
// state = OUT;
// }
// else{
// state = IN;
// ++nw;
// putchar(c);
// }
// }


//幂函数测试
// int n,m;
// while(scanf("%d%d",&m,&n)==2){
// printf("%lld\n",power(m,n));
// }


}


//test4.cpp

#include<stdio.h>


//自制getline函数 
int getline(char s[],int lim){
for(int i=0;i<lim-1;++i){
if((s[i]=getchar())==EOF||s[i]=='\n'){
if(s[i]=='\n'){
s[i] = '\0';
return i;
}
s[i] = '\0';
return -1;
}
}
s[lim-1]='\0';
return lim;
}


void copy(char from[],char to[]){
for(int i=0;(to[i]=from[i])!='\0';++i);
}


int main(){
// char s[100],lim = 99;
// if(getline(s,lim)!=0){
// printf("%s\n",s);j
// }
const int maxn = 1000;
char s[maxn],ans[maxn];
int len,maxLen = 0;
while((len=getline(s,1000))!=-1){
if(len>maxLen){
maxLen = len;
copy(s,ans);
}
}
printf("%s\n",ans);
return 0;



//test13.cpp

#include<stdio.h>
#include<ctype.h>


int atoi(char s[]){
int i,n,sign;
for(i=0;isspace(s[i]);i++)
;
sign = (s[i]=='-') ? -1 : 1;
if(s[i]=='+' || s[i]=='-')++i;
for(n=0;isdigit(s[i]);++i){
n = n*10+(s[i]-'0');
}
return sign*n;
}


int check(int a,int c){
if(a>='a'&&a<='z'&&c>a&&c<='z'){
return 1;
}else if(a>='A'&&a<='Z'&&c>a&&c<='Z'){
return 1;
}else if(a>='0'&&a<='9'&&c>a&&c<='9'){
return 1;
}
return 0;
}


void expand(char s[],char t[]){
int cnt = 0;
t[cnt++] = s[0];
for(int i=1;s[i]!='\0';++i){
if(s[i]=='-'&&check(s[i-1],s[i+1])){
for(int k=s[i-1]+1;k<s[i+1];++k){
t[cnt++] = (char)k;
}
}else{
t[cnt++] = s[i];
}
}
t[cnt] = '\0';
}


void expand2(char s[],char z[]){
int i,j;
char c;

i = j = 0;
while((c=s[i++])!='\0'){
if(s[i]=='-'&&s[i+1]>=c){
i++;
while(c<s[i]){
z[j++] = c++; 
}
}else{
z[j++] = c;

}
z[j] = '\0';
}


#include<string.h>
void reverse(char s[]){
int c,i,j;
for(i =0,j=strlen(s)-1;i<j;i++,j--){
c = s[i];
s[i] = s[j];
s[j] = c;
}
}


#define abs(x) ((x)<0?-(x):(x)) 


void itoa(int n,char s[]){
int i,sign;
sign = n;
i=0;
do{
s[i++] = abs(n%10)+'0';
}while((n/=10) != 0);
if(sign<0){
s[i++] = '-';
}
s[i] = '\0';
reverse(s);
}


void itoa(int n,char s[],int minl){
int i,sign;
sign = n;
i=0;
do{
s[i++] = abs(n%10)+'0';
}while((n/=10) != 0);
if(sign<0){
s[i++] = '-';
}
for(;i<minl;++i)s[i] = ' '; 
s[i] = '\0';
reverse(s);
}


void itob(int n,char s[],int b){
int i,j,sign;
sign = n;
i=0;
do{
j = abs(n%b);
s[i++] = (j<=9) ? j+'0' : j-10+'a';
}while((n/=b) != 0);
if(sign<0){
s[i++] = '-';
}
s[i] = '\0';
reverse(s);
}


int trim(char s[]){
int n;
for(n=strlen(s)-1;n>=0;n--)
if(s[n]!=' '&&s[n]!='\t'&&s[n]!='\n')
break;

s[n+1] = '\0';
return 0;
}






int main(){
test:
printf("%d\n",atoi("   -87437"));
char s[100] = "a-z0-9";
char z[100] = "g";
expand2(s,z);
reverse(z);
printf("%s\n",z);
itoa(-56778897,s,16);
printf("%s\n",s);
itob(77343785,s,15);
printf("%s\n",s);
goto test;
// printf("%d\n",-88%7);
return 0;
}