[英]why is my function encodeChar does not work?
下面的代码应该允许用户输入两个字符串 s 和 t,一个结构数组和数组的大小作为参数,将 s 中的字符编码为 t,并将编码后的字符串 t 通过 call by 传递给调用者参考。 我不明白为什么我的函数 encodeChar 是错误的。 我已经尝试使用索引方法,它类似于正确答案。 但是当我输入
1
3
a
b
b
c
c
d
3
abcd
4
,输出为 abdd。 我不明白我的错误在哪里。 请让我解决我的误解。
#include <string.h>
typedef struct {
char source;
char code;
} Rule;
void createTable(Rule *table, int *size);
void printTable(Rule *table, int size);
void encodeChar(Rule *table, int size, char *s, char *t);
int main()
{
char s[80], t[80], dummychar, *p;
int size, choice;
Rule table[100];
printf("Select one of the following options:\n");
printf("1: createTable()\n");
printf("2: printTable()\n");
printf("3: encodeChar()\n");
printf("4: exit()\n");
do {
printf("Enter your choice: \n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("createTable(): \n");
createTable(table, &size);
break;
case 2:
printf("printTable(): \n");
printTable(table, size);
break;
case 3:
scanf("%c",&dummychar);
printf("Source string: \n");
fgets(s, 80, stdin);
if (p=strchr(s,'\n')) *p = '\0';
encodeChar(table,size,s,t);
printf("Encoded string: %s\n", t);
break;
default:
break;
}
} while (choice < 4);
return 0;
}
void printTable(Rule *table, int size)
{
int i;
for (i=0; i<size; i++)
{
printf("%d: %c->%c\n", i+1, table->source, table->code);
table++;
}
}
void createTable(Rule *table, int *size)
{
/*edit*/
/* Write your code here */
/*edit*/
/* Write your code here */
int i;
char dummy[80];
printf("Enter number of rules: \n");
scanf("%d", size); // dont careless here
for(i=0; i< *size; i++){
printf("Enter rule %d: \n", i+1);
fgets(dummy, 80,stdin); // why need to use this twice
printf("Enter source character: \n");
scanf("%c", &table[i].source);
fgets(dummy,80,stdin);// why
printf("Enter code character: \n ");
scanf("%c", &table[i].code);
}
/*end_edit*/
/*end_edit*/
}
void encodeChar(Rule *table, int size, char *s, char *t)
{
/*edit*/
/* Write your code here */// this is wrong
int i, j;
for(i = 0 ; s[i] != '\0'; i++){
for(j = 0; j < size; j++){
if(s[i] == table[j].source){
*t = table[j].code;
}
else{
*t = s[i];
}
}
t++;
}
*t = '\0';
/*edit*/
/* this is correct
int i;
while(*s!='\0'){
for(i = 0; i < size; i++){
if(*s != table[i].source)
*t = *s;
else
*t = table[i].code;
t++;
s++;
}
}
*t = '\0';
*/
}
这里一张纸和一支铅笔就足够了。
只需按照您的abcd
代码进行操作。 相关部分是
for(i = 0 ; s[i] != '\0'; i++){
for(j = 0; j < size; j++){
if(s[i] == table[j].source){
*t = table[j].code;
}
else{
*t = s[i];
}
}
t++;
}
*t = '\0';
我们走吧:
i
是 0, s[i]
是a
j
是 0, table[j].source
是a
: *t
接收b
j
是 1, table[j].source
是b
: *t
recieves (back) a
!...只有最后一条规则可以应用于您的代码...
一个简单的解决方法是在应用规则以防止以下规则回滚更改时跳出循环:
for(i = 0 ; s[i] != '\0'; i++){
for(j = 0; j < size; j++){
if(s[i] == table[j].source){
*t = table[j].code;
break; // do not examine other rules
}
else{
*t = s[i];
}
}
t++;
}
*t = '\0';
但是如果同一个字符被多个规则改变,这个代码将使用第一个,而正确的代码将使用最后一个,所以这会更一致:
for(i = 0 ; s[i] != '\0'; i++){
for(j = size-1; j >= 0; j--){ // examine rules in descending order
...
无论如何,这里仍然有可能的改进,所以你可以将它发布到 CodeReview 以获得最佳实践的建议......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.