用定长顺序串编写下列算法:
(1)将顺序串s中所有值为ch1的字符转换成ch2的字符,其中参数ch1和ch2传入的均为字符量。
int charReplace(SString *s,char ch1,char ch2)
(2)将顺序串s中所有字符按照相反的次序仍存放在s中,即实现字符串的逆置。
int strInverse(SString *s)
(3)从顺序串s中删除其值等于ch的所有字符,其中参数ch传入的为字符量。
int charDelete(SString *s,char ch)
(1)我的答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 #define MAXSIZE 20 typedef int QueueElementType; typedef struct SeqQueue //定义循环队列 { QueueElementType element[MAXSIZE]; int front; int rear; int tag; }SeqQueue;
void InitQueue(SeqQueue *Q) { Q->front=Q->rear=0; Q->tag=0; }
int EnterQueue(SeqQueue *Q,QueueElementType x) { if(((Q->rear)%MAXSIZE)==(Q->front) && Q->tag==1) return ERROR; else { Q->element[Q->rear]=x; Q->rear=(Q->rear+1)%MAXSIZE; if((Q->rear)==(Q->front)) Q->tag=1; return OK; } }
int DeleteQueue(SeqQueue *Q,QueueElementType *x) { if(((Q->rear)%MAXSIZE)==(Q->front) && Q->tag==0) return ERROR; else { (*x)=Q->element[Q->front]; Q->front=(Q->front+1)%MAXSIZE; if((Q->front)==(Q->rear)) Q->tag=0; return OK; } }
int main() { SeqQueue s; InitQueue(&s); printf("Please input int xing numbers: \n"); QueueElementType x; do { scanf("%d",&x); } while(EnterQueue(&s,x)); while(DeleteQueue(&s,&x)) printf("%d ",x); putchar('\n'); system("pause"); return 0; }
|
正确答案:
1 2 3 4 5 6 7 8
| int charReplace(SString *s,char ch1,char ch2) { int i; if(!s) return ERROR; for(i=0;i<(s->len);i++) if(s->ch[i]==ch1) s->ch[i]=ch2; return OK; }
|
(2)我的答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <stdio.h> #include <stdlib.h> #include "ds.h" int isReverse(char *c) { int i=0,j,k,count=0; printf("Please input characters and input @ means endinput: \n"); do { scanf("%c",&c[i]); i++; } while(c[i-1]!='@'); i--; for(j=0;j<i;j++) { if(c[j]=='&') count++; } if(count) { for(j=0;c[j]!='&';j++) { k=i-1-j; if(c[k]==c[j]) continue; else { return ERROR; } } return OK; } else return ERROR; }
int main() { char c[Stack_Size]; if(isReverse(c)) printf("Yes! \n"); else printf("NO! \n"); system("pause"); return 0; }
|
其中,”ds.h”文件源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define Stack_Size 20
typedef char StackElementType;
typedef struct SeqStack { StackElementType elem[Stack_Size]; int top; }SeqStack;
void InitStack(SeqStack *S);
int IsEmpty(SeqStack *S);
int IsFull(SeqStack *S); void Push(SeqStack *S,StackElementType x); void Pop(SeqStack *S,StackElementType *x);
void InitStack(SeqStack *S) { S->top=-1; }
int IsEmpty(SeqStack *S) { if((S->top)==-1) return TRUE; else return FALSE; }
int IsFull(SeqStack *S) { if((S->top)==(Stack_Size-1)) return TRUE; else return FALSE; }
void Push(SeqStack *S,StackElementType x) { S->top++; S->elem[S->top]=x; }
void Pop(SeqStack *S,StackElementType *x) { (*x)=S->elem[S->top]; S->top--; }
|
正确答案如下:
1 2 3 4 5 6 7 8 9 10 11
| int strInverse(SString *s) { int i,j; char ch; if(!s) return ERROR; for(i=0,j=s->len-1;i<j;i++,j--) { ch=s->ch[i]; s->ch[i]=s->ch[j]; s->ch[j]=ch; } return OK; }
|
(3)我的答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include <stdio.h> #include <stdlib.h> #define Stack_Size 50 #define TRUE 1 #define FALSE 0 typedef char StackElementType; typedef char QueueElementType;
typedef struct //声明顺序栈 { StackElementType elem[Stack_Size]; int top; }SeqStack;
void InitStack(SeqStack *S) { S->top=-1; }
int Push(SeqStack *S,StackElementType x) { if(S->top==(Stack_Size-1)) return FALSE; S->top++; S->elem[S->top]=x; return TRUE; }
int Pop(SeqStack *S,StackElementType *x) { if(S->top==-1) return FALSE; else { (*x)=S->elem[S->top]; S->top--; return TRUE; } }
typedef struct //声明循环队列 { QueueElementType element[Stack_Size]; int front; int rear; }SeqQueue;
void InitQueue(SeqQueue *Q) { Q->front=Q->rear=0; }
int EnterQueue(SeqQueue *Q,QueueElementType x) { if(((Q->rear+1)%Stack_Size)==Q->front) return FALSE; Q->element[Q->rear]=x; Q->rear=(Q->rear+1)%Stack_Size; return TRUE; }
int DleteQueue(SeqQueue *Q,QueueElementType *x) { if(Q->front==Q->rear) return FALSE; (*x)=Q->element[Q->front]; Q->front=(Q->front+1)%Stack_Size; return TRUE; }
int Palindrome_Test(SeqStack *S,SeqQueue *Q) { printf("Please input characters and input @ means endinput: \n"); StackElementType c,d; int count=0,i; scanf("%c",&c); while(c!='@') { count++; Push(S,c); EnterQueue(Q,c); scanf("%c",&c); } for(i=0;i<count/2;i++) { if(Pop(S,&c)); if(DleteQueue(Q,&d)); if(c==d) continue; else return FALSE; } return TRUE; }
int main() { SeqStack s; SeqQueue q; InitStack(&s); InitQueue(&q); if(Palindrome_Test(&s,&q)) puts("Yes! "); else puts("No! "); system("pause"); return 0; }
|
正确答案如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| int charDelete(SString *s,char ch) { int i,j; if(!s) return ERROR; i=j=0; while(j<(s->len)) { if(s->ch[j]!=ch) { s->ch[i]=s->ch[j]; i++;j++; } else j++; } s->len=i; return OK; }
|
完!