[英]present dimension array in C programming
我正在做一個分配,這是我們必須用[]和的組合形式推送任何字符串,然后將字符串顯示到多維數組。
例如,輸入字符串是: [qw,good,hello,hey,[we,need,to,eat],pretty,blue]
然后
如果我們輸入命令: array
我們將得到輸出: [qw,good,hello,hey,[we,need,to,eat],pretty,blue]
。
如果輸入命令array [3],我們將得到輸出: hey
。
如果輸入命令:array [4],我們將獲得輸出[we,need,to,eat]
。
如果我們輸入的命令陣列[4] [2]我們會得到的輸出是to
我的想法是使用令牌刪除特殊字符,但是麻煩是逗號既在多維數組中又在維數組之外。另外一個問題是我們不知道輸入字符串,因此它可能是二維數組或3D或4D。謝謝
您不能簡單地解析並讀取[qw,good,hello,hey,[we,need,to,eat],pretty,blue]
轉換成多維數組並以您建議的方式訪問元素,因為您正在混合存儲an array of character arrays
與an array of character arrays
an array of an array of character arrays
(用於子字符串[we,need,to,eat]
)。 它們具有不同的指針,不能簡單地添加到通用array
。 (是的,您可以設置一些虛假的void pointers
數組和一些復雜的測試機制,以嘗試優雅地弄清是什么(可能是帶tagged union
),但這似乎完全超出了您的要求。
滿足您要求的更干凈的解決方案array of structs
其中該結構跟蹤其array indexes
以及它是否是sub-array
。 幸運的是,這是問題的瑣碎部分。
問題的重要部分是解析輸入字符串,並根據讀取的字符采取適當的措施。 這並不困難,僅是乏味,並且需要從頭到尾進行工作,基本上是逐個字符地工作。
乍一看,看起來很瑣碎,帶有定界符"[,]"
strtok
可能是一種簡單的方法,但是要確定是將條目分配給main array
還是sub-array
,需要知道遇到了哪個終止字符。 基本上,這使您回到brute-force, character-by-character
閱讀的brute-force, character-by-character
。
在解析並填充了結構數組之后,您將不得不提出一個輸出例程,該例程接受1 (or 2) indexes
,搜索結構數組,如果索引匹配,則輸出匹配的成員。
同樣,這都不困難,但是您需要執行相同類型的index accounting
。 以下是一種滿足您需求的方法。 它不會充滿error checking
,仍然必須釋放分配給array of structs
的內存,並且您需要檢查MAXS
並根據需要重新分配。 看一看,我認為這可以幫助您入門:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXS 128
typedef struct {
char *str;
int subs;
int maj;
int min;
} sarray;
int main (int argc, char **argv) {
if (argc < 2) {
fprintf (stderr, "error: insufficient input. Usage: %s <delimited string> [,[,],]\n", argv[0]);
return 1;
}
char *input = strdup (argv[1]);
char *sp = input;
sarray **array = NULL;
char *p = NULL;
char sc = 0;
int idx = 0;
int sidx = 0;
int pcnt = 0;
char start_array = 0;
if (*sp++ != '[') {
fprintf (stderr, "error: invalid sp, no opening '['. Usage: %s <delimited string> [,[,],]\n", argv[0]);
return 1;
}
printf ("\nCreating array from input: %s\n\n", input);
array = calloc (MAXS, sizeof (*array));
if (!array) {
printf ("\n calloc failed\n\n");
return 1;
}
while (*sp)
{
p = sp; /* assign pointer equal start pointer (sp) */
array[pcnt] = malloc (sizeof (array)); /* allocate storage for new element */
while (*p != 0 && *p != '[' && *p != ',' && *p != ']') p++; /* scan for delimiter */
/* test for double character (meaning beginning or end of sub-array */
if (*(p+1) != 0)
if (*(p+1) == '[' || *(p+1) == ',' || *(p+1) == ']') { *p=0; p++; }
sc = *p; /* save the current character (to restore) */
*p = 0; /* set the ending delimiter to NULL */
array[pcnt]->str = strdup (sp); /* allocate str and copy sp */
array[pcnt]->subs = (start_array) ? 1 : 0; /* set flag indicating it is a sub array */
array[pcnt]->maj = idx; /* assign major index for array */
array[pcnt]->min = sidx; /* assign minor index for array */
if (start_array) sidx++; /* increment major or minor per start_array */
else idx++;
pcnt++; /* increment pointer index */
*p = sc; /* restor the saved character */
if (*p == '[') { /* if current is '[' begin sub array */
sidx = 0;
start_array = 1;
}
if (*(p-1) == 0 && *p == ',') { /* if current is ']' end sub array */
idx++; sidx = 0;
start_array = 0;
}
sp = ++p; /* set sp to next new character */
}
if (input) free (input); /* free memory allocated to input */
/* output indexed array */
idx = 0;
while (array[idx])
{
if (array[idx]->subs)
printf (" array[%d][%d] : %s\n", array[idx]->maj, array[idx]->min, array[idx]->str);
else
printf (" array[%d] : %s\n", array[idx]->maj, array[idx]->str);
idx++;
}
/* prompt for input of index(s) and output matching element */
int maj = 0;
int min = 0;
int printed = 0;
idx = 0;
printf ("\nEnter index to retrieve element (index [sub-index]): ");
scanf ("%m[^\n]%*c", &input);
p = input;
while (*p++) idx++;
if (idx > 1) {
maj = input[0] - '0';
min = input[idx-1] - '0';
}
else if (idx == 1) {
maj = input[0] - '0';
min = 0;
}
else {
printf ("\nerror: invalid index input, format X [Y]\n\n");
return 1;
}
idx = 0;
if (min > 0) {
while (array[idx])
{
if (array[idx]->maj == maj && array[idx]->min == min) {
printf ("\n array[%d][%d] : %s\n\n", array[idx]->maj, array[idx]->min, array[idx]->str);
printed = 1;
break;
}
idx++;
}
}
else {
while (array[idx])
{
if (array[idx]->maj == maj) {
if (array[idx]->subs == 1 && array[idx]->min == 0j) {
printf ("\n array[%d] :", idx);
while (array[idx]->subs == 1) {
printf (" %s", array[idx]->str);
idx++;
}
printf ("\n\n");
}
else {
printf ("\n array[%d] : %s\n\n", array[idx]->maj, array[idx]->str);
}
printed = 1;
break;
}
idx++;
}
}
if (!printed)
printf ("\nerror: index not found.\n\n");
return 0;
}
輸出:
$ ./bin/strrdmulti [qw,good,hello,hey,[we,need,to,eat],pretty,blue]
Creating array from input: [qw,good,hello,hey,[we,need,to,eat],pretty,blue]
array[0] : qw
array[1] : good
array[2] : hello
array[3] : hey
array[4][0] : we
array[4][1] : need
array[4][2] : to
array[4][3] : eat
array[5] : pretty
array[6] : blue
Enter index to retrieve element (index [sub-index]): 2
array[2] : hello
$ ./bin/strrdmulti [qw,good,hello,hey,[we,need,to,eat],pretty,blue]
Creating array from input: [qw,good,hello,hey,[we,need,to,eat],pretty,blue]
{array omitted}
Enter index to retrieve element (index [sub-index]): 4
array[4] : we need to eat
$ ./bin/strrdmulti [qw,good,hello,hey,[we,need,to,eat],pretty,blue]
Creating array from input: [qw,good,hello,hey,[we,need,to,eat],pretty,blue]
{array omitted}
Enter index to retrieve element (index [sub-index]): 4 2
array[4][2] : to
$ ./bin/strrdmulti [qw,good,hello,hey,[we,need,to,eat],pretty,blue]
Creating array from input: [qw,good,hello,hey,[we,need,to,eat],pretty,blue]
{array omitted}
Enter index to retrieve element (index [sub-index]): 4 4
error: index not found.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.