我正在写一个简单的结构数组程序。给了一个字符串,我想解析它。字符串由几个字符组成。
例如,字符串“A:bc:D:E”有5个唯一字符。冒号“:”表示该字符有一个值。
结构数组大小为256((选项[256]),其中包含所有ASCII字符。
从给定的字符串中,我想找到字符并在其ASCII位置用值“1”填充结构数组。如果字符串中不存在字符,则指定值“0”。
此外,我想设置这个结构的“hasVal”字段。例如,a=1(给定字符串中有冒号)、b=0(字符串中“b”后没有冒号)、c=1、D=1、E=1。
最后,按预期输出打印此结构。
我不是很擅长编程。我刚开始学C语言。我试过了,但没有得到预期的结果。如果我不能转达我的问题陈述,我深表歉意。
任何帮助都非常感谢。提前谢谢。
样品c
#include <stdio.h>
#include <stdlib.h>
#define MAX_CHAR 256
typedef struct {
int hasVal;
char *defaultVal;
char *desc;
} validOpt;
validOpt option[MAX_CHAR] = {};
char *optStr = "a:bc:D:E";
int main() {
int i;
for(i = 0; *(optStr + i); i++)
{
/* Not Sure how to check this....
* check the "char" and ":",
* if both are present, set the field "hasVal" to 1 or "0".
*/
if((optStr[i]++) == ":")
option[optStr[i]--].hasVal = 1;
else
option[optStr[i]--].hasVal = 0;
}
printf(“Printing structure…\n”);
printf("\n");
for(i=0; i< MAX_CHAR; i++)
{
if(option[optStr[i]].hasVal == 1) {
printf(" %d -- %c\n", i , option[optStr[i]].hasVal);
}
}
return 0;
}
实际产量:
[rock12/C_Prog]$ ./sample
Printing structure…
在这条线之后什么也得不到。
预期产量:
1) If user enters invalid character, give an error.
For Example, "q" -> not valid option
2) For Valid options, print:
a - 1
b - 0
c - 1
D - 1
E - 1
最佳答案
if(option[optStr[i]].hasVal == 1) {
printf(" %d -- %c\n", i , option[optStr[i]].hasVal);
这个条件还不够。您需要区分字符串中出现和不出现的字符以及具有或不具有值的字符。
您需要一个附加变量,例如
is_shown
来确定字符是否出现。#define MAX_CHAR 256
char
范围从0到128。在您的示例中,从A
转到z
似乎就足够了。但我们把它保持在128。for(i = 0; *(optStr + i); i++)
{
if((optStr[i]++) == ":")
{
...
}
}
上面有个错误。当您到达字符串中的最后一个字符时,您将再次递增以检查下一个字符,它将超出范围。
修改代码如下:
typedef struct
{
int is_show;
int hasVal;
char *defaultVal;
char *desc;
} validOpt;
#define MAX_CHAR 128
int main()
{
validOpt option[MAX_CHAR] = { 0 };
char *optStr = "a:bc:D:E";
while (*optStr)
{
char ch = *optStr;
option[ch].hasVal = 0;
if (ch != ':')
option[ch].is_show = 1;
if (*(optStr + 1))
if (*(optStr + 1)== ':')
option[ch].hasVal = 1;
*optStr++;
}
printf("Printing structure\n\n");
int i;
for (i = 0; i < 128; i++)
if (option[i].is_show == 1)
printf(" %c -- %d\n", i, option[i].hasVal);
return 0;
}
输出如下:
D -- 1
E -- 0
a -- 1
b -- 0
c -- 1
你必须分类才能得到你想要的。注意,未设置
E
,因为在最后一个字符:
之后没有E
。编辑:替代方法:
使用
malloc(strlen(optStr) * sizeof(validOpt))
创建option
数组并填充它。在这种情况下,option
的数组大小为8,它只需要数组大小为5。效率更高。typedef struct {
char ch;
int hasVal;
} validOpt;
int main()
{
char *optStr = "a:bc:D:E";
//option's array size is always larger than optStr's size, so it's safe
validOpt *option = malloc(strlen(optStr) * sizeof(validOpt));
int i, count = 0, len = strlen(optStr);
for (i = 0; i < len - 1; i++)
{
if (optStr[i] == ':') //ignore this character
continue;
option[count].hasVal = optStr[i + 1] == ':';
option[count].ch = optStr[i];
count++;
}
if (optStr[i] != ':') //handle the last character in optStr
{
option[count].hasVal = 0;
option[count].ch = optStr[i];
count++;
}
for (i = 0; i < count; i++)
printf(" %c -- %d\n", option[i].ch, option[i].hasVal);
free(option);
return 0;
}
输出:
a -- 1
b -- 0
c -- 1
D -- 1
E -- 0
关于c - 在C中设置结构数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40333871/