实验6 指针
TASK1.1
#include<stdio.h> #define N 4 int main() { int x[N]={1,9,8,4}; int i; int *p; for(i=0;i<N;++i) printf("%d",x[i]); printf("\n"); for(p=x;p<x+N;++p) printf("%d",*p); printf("\n"); p=x; for(i=0;i<N;++i) printf("%d",*(p+i)); printf("\n"); p=x; for(i=0;i<N;++i) printf("%d",p[i]); printf("\n"); return 0; }
TASK 1.2
#include<stdio.h> #define N 4 int main() { char x[N]={'1','9','8','4'}; int i; char *p; for(i=0;i<N;++i) printf("%c",x[i]); printf("\n"); for(p=x;p<x+N;++p) printf("%c",*p); printf("\n"); p=x; for(i=0;i<N;++i) printf("%c",*(p+i)); printf("\n"); p=x; for(i=0;i<N;++i) printf("%c",p[i]); printf("\n"); return 0; }
2004
2001
因为定义数组的数据类型不同
TASK 2.1
#include<stdio.h> int main() { int x[2][4]={{1,9,8,4},{2,0,2,2}}; int i,j; int *p; int (*q)[4]; for(i=0;i<2;++i) { for(j=0;j<4;++j) printf("%d",x[i][j]); printf("\n"); } for(p=&x[0][0],i=0;p<&x[0][0]+8;++p,++i) { printf("%d",*p); if((i+1)%4==0) printf("\n"); } for(q=x;q<x+2;++q) { for(j=0;j<4;++j) printf("%d",*(*q+j)); printf("\n"); } return 0; }
TASK 2.2
#include<stdio.h> int main() { char x[2][4]={{'1','9','8','4'},{'2','0','2','2'}}; int i,j; char *p; char (*q)[4]; for(i=0;i<2;++i) { for(j=0;j<4;++j) printf("%c",x[i][j]); printf("\n"); } for(p=&x[0][0],i=0;p<&x[0][0]+8;++p,++i) { printf("%c",*p); if((i+1)%4==0) printf("\n"); } for(q=x;q<x+2;++q) { for(j=0;j<4;++j) printf("%c",*(*q+j)); printf("\n"); } return 0; }
2.1:2004;2016
2.2:2001;2004
TASK 3.1
#include<stdio.h> #include<string.h> #define N 80 int main() { char s1[]="C,I love u."; char s2[]="C,I hate u."; char tmp[N]; printf("sizeof(s1) vs.strlen(s1):\n"); printf("sizeof(s1)=%d\n",sizeof(s1)); printf("strlen(s1)=%d\n",strlen(s1)); printf("\nbefore swap:\n"); printf("s1:%s\n",s1); printf("s2:%s\n",s2); printf("\nswapping...\n"); strcpy(tmp,s1); strcpy(s1,s2); strcpy(s2,tmp); printf("\nafter swap:\n"); printf("s1:%s\n",s1); printf("s2:%s\n",s2); return 0; }
1.12,;计算s1数组长度;计算s1字符串长度
2.不能
3.交换了
TASK 3.2
#include<stdio.h> #include<string.h> #define N 80 int main() { char *s1="C,I love u."; char *s2="C,I hate u."; char *tmp; printf("sizeof(s1) vs.strlen(s1):\n"); printf("sizeof(s1)=%d\n",sizeof(s1)); printf("strlen(s1)=%d\n",strlen(s1)); printf("\nbefore swap:\n"); printf("s1:%s\n",s1); printf("s2:%s\n",s2); printf("\nswapping...\n"); tmp=s1; s1=s2; s2=tmp; printf("\nafter swap:\n"); printf("s1:%s\n",s1); printf("s2:%s\n",s2); return 0; }
1.指向输入字符串的地址;实际分配的内存数;指针变量指向的字符串的长度
2.能
3.交换的是两个字符串的地址;没有。
TASK 4
#include <stdio.h> #include <string.h> #define N 5 int check_id(char *str); // 函数声明 int main() { char *pid[N] = {"31010120000721656X", "330106199609203301", "53010220051126571", "510104199211197977", "53010220051126133Y"}; int i; for(i=0; i<N; ++i) if( check_id(pid[i]) ) // 函数调用 printf("%s\tTrue\n", pid[i]); else printf("%s\tFalse\n", pid[i]); return 0; } // 函数定义 // 功能: 检查指针str指向的身份证号码串形式上是否合法。 // 形式合法,返回1,否则,返回0 int check_id(char *str) { char *p,*temp; p=str; if(strlen(p) != 18) { return 0; } else { temp = p; for(;temp<p+18;temp++) { if(*temp>='0'&&*temp<='9'||*temp == 'X'); else { return 0; } } return 1; } }
TASK 5
#include <stdio.h> #include <string.h> #define N 80 int is_palindrome(char *s); // 函数声明 int main() { char str[N]; int flag; printf("Enter a string:\n"); gets(str); flag = is_palindrome(str); // 函数调用 if (flag) printf("YES\n"); else printf("NO\n"); return 0; } // 函数定义 // 功能:判断s指向的字符串是否是回文串 // 如果是,返回1;否则,返回0 int is_palindrome(char *s) { int i,j; i=strlen(s); for(j=0;j<i;) { if(*(s+j)==*(s+i-1-j)) j++; else return 0; } return 1; }
TASK 6
#include <stdio.h> #define N 80 void encoder(char *s); // 函数声明 void decoder(char *s); // 函数声明 int main() { char words[N]; printf("输入英文文本: "); gets(words); printf("编码后的英文文本: "); encoder(words); // 函数调用 printf("%s\n", words); printf("对编码后的英文文本解码: "); decoder(words); // 函数调用 printf("%s\n", words); return 0; } /*函数定义 功能:对s指向的字符串进行编码处理 编码规则: 对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换 其它非字母字符,保持不变 */ void encoder(char *s) { for(s;*s!='\0';s++) { if(*s>='a'&&*s<'z'||*s>='A'&&*s<'Z') *s=*s+1; else if(*s=='z'||*s=='Z') *s=*s-25; } } /*函数定义 功能:对s指向的字符串进行解码处理 解码规则: 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换 其它非字母字符,保持不变 */ void decoder(char *s) { for(s;*s!='\0';s++) { if(*s>'a'&&*s<='z'||*s>'A'&&*s<='Z') *s=*s-1; else if(*s=='a'||*s=='A') *s=*s+25; } }
实验总结
迷茫痛苦地做过实验后,突然感觉一直不太学得上的指针有点头绪了。(?)