发布时间 : 星期六 文章C璇█绋嬪簭璁捐(绗簩鐗?涔犻鍙傝冪瓟妗?- 鐧惧害鏂囧簱更新完毕开始阅读97814d3d27c52cc58bd63186bceb19e8b9f6ec00
head=NULL;
while((str[i]=getchar())!=’.’)
{ if(str[i]>=’A’&& str[i]<=’Z’|| str[i]>=’a’&& str[i]<=’z’) { p=head;
while(p!=NULL)
if(p->c==str[i]) break; else { q=p; p=p->next; } if(p==NULL)
{ p=(struct node *)malloc(sizeof(struct node)); p->c=str[i];
p->next=NULL;
if(head==NULL) head=p; else q->next=p; } } i++; }
p=head;
while(p!=NULL)
{ printf(“,”,p->c); p=p->next; } }
*11.将一个链表按逆序排列,即将原来链头当作新链表的链尾,而原链尾当作链头。
解:#include <>
struct link { int data; struct link *next;}; struct link *f(struct link *head)
{ struct link *p,*q,newhead=NULL,*s;
while(head->next!=NULL) { p=head;
while(p->next!=NULL) { q=p; p=p->next;}
if(newhead==NULL) newhead=p; else s->next=p; s=p;
q->next=NULL; }
s->next=head;
return newhead; }
习 题 10
一、计算下列表达式的值 1.3&6 2.-12&7 3.3|9 4.-02||12 5.(x=13)^9 6.15/2^1 7.~-14 8.~15/3 9.7<<2 10.-9<<2 11.(x=13)>>2 12.-9>>3 解:(1) 3&6 值为 2 (2) -12&7 值为 4 (3) 3|9 值为 11 (4) -02||12 值为 1 (5) (x=13)^9 值为 4 (6) 15/2^1 值为 6 (7) ~-14 值为 13 (8) ~15/3 值为 -5 (9) 7<<2 值为 28 (10) -9<<2 值为 -36 (11) (x=13)>>2 值为 3 (12) -9>>3 值为 –2
二、若有语句“short a,b;”,分别写出执行下列语句后a、b的值 1.a=2;b=7;a&b; 2.a=-3;b=a|4; 3.a=5;b=a<<2; 4.a=-15;b=~a>>2 解:(1) a为 2,b为 7 (2) a为 -3,b为 –3
(3) a为 5,b为 20 (4) a为 -15,b为 3
三、写出下列程序的输出结果 1.
#include <> int main(void) { union
{ char s[2]; int i; }g;
=0x4142;
printf(\
printf(\ [0]=1; [1]=0;
printf(\
return 0; }
解:=4142
[0]=42 g.s[1]=41 =1
2.
#include <> void main()
{ union { char n[12];int m[4];double x[2];} a; printf(\ }
解:16
3.
void main() {
int x=3,y=9; x=x^y; y=x^y; x=x^y;
printf(\
}
解:9,3
4.
#include <>
void swp(int *x,int *y) {
*x=*x^*y;
*y=*x^*y; *x=*x^*y;
} void main() {
int a,b,i;
for(i=0;i<4;i++){
scanf(\ swp(&a,&b);
printf(\
} }
运行时输入:
9 –9↙ 8 4↙ 1 –5↙ 23 –1↙
解:-9,9
4,8 -5,1 -1,23
四、编程题
1.输入一个整数,并输出该数的机内码。
解:#include <> void main() { int a;
scanf(“%d”,&a); printf(“%xH”,a);
}
2.输入一个整数,将其低8位全置为1,高8位保留原样,并以十六进制输出该数。
解:#include <> void main() { int a;
scanf(“%d”,&a); a|=0xff;
printf(“%xH”,a);
}
3.输入一个字符串,删除字符串中的所有非汉字的字符,并输出该字符串。
解:#include <> #include <>
void main() { char str[81]; int i; gets(str);
for(i=0;str[i]!=’\\0’;)
if((str[i]&0x80)==0) strcpy(str+i,str+i+1); else i++; puts(str);
}
*4. 对文本的简单加密与解密。输入一个文本及任意一个密码值,对该文本加密后,输出密文;重新输入密值后,输出解密后的原文。
解:#include <> void main() { char s[81]; int m1,m2,i; printf(“请输入要加密的文本:\\n”);
gets(s); 0’0’后面增加一个空格符。
【提示】参考例,逐个读入字符写入到临时文件,当读到“.”时多写一个空格符到临时文件。
解:#include <> #include <> void main()
{ FILE *fp1,*fp2;
char str[81],name[80],c;
printf(“请输入文件名:”); gets(name);
if((fp1=fopen(name,”r”))==NULL)
{ printf(“不能打开文件%s !”,name); exit(0); } if((fp2=fopen(“d:\\\\”,”w”))==NULL)
{ printf(“不能打开文件d:\\\\!”); exit(0); } while((c=getc(fp1))!=EOF) { fputc(c,fp2);
if(c==’.’) putc(‘ ‘,fp2); }
fclose(fp1); fclose(fp2); remove(name);
rename(“d:\\\\”,name); fp1=fopen(name,”r”);
while(fgets(str,80,fp1)!=NULL) puts(str); fclose(fp1); }