结构和ASCII“数据库” [英] Structure and ASCII "Database"

查看:50
本文介绍了结构和ASCII“数据库”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了一项学校作业,而其他一切都很简单

有一个话题我完全迷失了。


我已经获得了一个看起来像这样的ASCII文件。在启动期间,

程序需要读取库存记录的ASCII数据文件,并使用结构和指针构建

适当的动态数据结构。


10112 MICROPROCESSOR 2100 2 B 000008.50 000012.50

10235 CHARACTER_PRINTER 4013 4 C 000995.00 001295.00

10450 FLOPPY_DISK_DRIVE 6023 6 B 000650.00 000885.00

10683 VIDEO_DISPLAY_TERMIN 5000 5 C 000550.00 000750.00

格式为:


5位数项目编号

空间

20个字符项目描述

空间

4位数量

空间

1数字分类代码

空间

........等等。


我的结构应该是什么样的?我以为我可以使用长度为55左右的

数组。但这似乎不符合

结构的要求。那么为什么不只是有一个二维数组。结构

可以为每个项目设置变量(项目编号,项目描述等),但

我怎样才能正确解析文件呢?

I''ve been given a school assignment and while everything else is easy
there''s one topic I''m completley lost on.

I''ve been given an ASCII file that looks like this. During start-up, the
program needs to read an ASCII data file of inventory records and build an
appropriate dynamic data structure using structs and pointers.

10112 MICROPROCESSOR 2100 2 B 000008.50 000012.50
10235 CHARACTER_PRINTER 4013 4 C 000995.00 001295.00
10450 FLOPPY_DISK_DRIVE 6023 6 B 000650.00 000885.00
10683 VIDEO_DISPLAY_TERMIN 5000 5 C 000550.00 000750.00

The format is:

5 digits Item Number
space
20 chars Item description
space
4 digits Quantity
space
1 digit Category code
space
........ and so on.

What should my strucutre look like? I was thinking I could just use an
array of length 55 or so. But that doesn''t seem to fit the requirment for a
strucutre. Why not just have a two dimensional array then. The strucutre
could have variable for each item (Item Number, Item Description, etc), but
how can I parse the file correctly then?

推荐答案

>我得到了一份学校作业,而其他一切都很简单
> I''ve been given a school assignment and while everything else is easy
有一个话题我完全迷失了。

我去过给出一个看起来像这样的ASCII文件。在启动期间,
程序需要读取库存记录的ASCII数据文件,并使用结构和指针构建适当的动态数据结构。

10112 MICROPROCESSOR 2100 2 B 000008.50 000012.50
10235 CHARACTER_PRINTER 4013 4 C 000995.00 001295.00
10450 FLOPPY_DISK_DRIVE 6023 6 B 000650.00 000885.00
10683 VIDEO_DISPLAY_TERMIN 5000 5 C 000550.00 000750.00

格式为:

5位数项目数量
空间
20个字符项目描述
空间
4位数量
空间
1位数分类代码空间
.......等等。

我的结构应该是什么样的?我以为我可以使用长度为55左右的
数组。但这似乎不符合
结构的要求。那么为什么不只是有一个二维数组。结构可以为每个项目设置变量(项目编号,项目描述等),
但是如何正确解析文件呢?
there''s one topic I''m completley lost on.

I''ve been given an ASCII file that looks like this. During start-up, the
program needs to read an ASCII data file of inventory records and build an
appropriate dynamic data structure using structs and pointers.

10112 MICROPROCESSOR 2100 2 B 000008.50 000012.50
10235 CHARACTER_PRINTER 4013 4 C 000995.00 001295.00
10450 FLOPPY_DISK_DRIVE 6023 6 B 000650.00 000885.00
10683 VIDEO_DISPLAY_TERMIN 5000 5 C 000550.00 000750.00

The format is:

5 digits Item Number
space
20 chars Item description
space
4 digits Quantity
space
1 digit Category code
space
....... and so on.

What should my strucutre look like? I was thinking I could just use an
array of length 55 or so. But that doesn''t seem to fit the requirment for a strucutre. Why not just have a two dimensional array then. The strucutre
could have variable for each item (Item Number, Item Description, etc), but how can I parse the file correctly then?




据我所知,ASCII码中的每一行代表一条记录。你可以定义一个结构,其中每个项目都在结构中表示。

结构也可以包含指向结构的指针,这样你就可以使用

它建立一个链表。逐个阅读每个项目并将它们存储在

项目中。如果物品有固定的长度,您可以使用该信息

来读取数据。


问候,

Nathan



As far as I understand, each line in the ASCII code represents a record. You
could define a structure in which each item is represented in the structure.
The structure could also contain a pointer to structure so that you can use
it to build up a linked list. Read each item one by one and store them in
the items. If the items have fixed length, you could use that information
for reading in the data.

Regards,
Nathan


2003年9月6日星期六18:28:21 GMT, - Steve - "

< se **** @ foundation.sdsu.edu>写道:
On Sat, 06 Sep 2003 18:28:21 GMT, "- Steve -"
<se****@foundation.sdsu.edu> wrote:
我已经完成了一项学校作业,而其他一切都很简单
有一个话题我完全迷失了。

我收到了一个看起来像这样的ASCII文件。在启动期间,
程序需要读取库存记录的ASCII数据文件,并使用结构和指针构建适当的动态数据结构。

10112 MICROPROCESSOR 2100 2 B 000008.50 000012.50
10235 CHARACTER_PRINTER 4013 4 C 000995.00 001295.00
10450 FLOPPY_DISK_DRIVE 6023 6 B 000650.00 000885.00
10683 VIDEO_DISPLAY_TERMIN 5000 5 C 000550.00 000750.00

格式为:

5位数项目数量
空间
20个字符项目描述
空间
4位数量
空间
1位数分类代码空间
.......等等。


我们称这些字段,因此数据文件的每一行代表一条

记录,每条记录有7个字段。处理固定的

宽度字段更容易,因此我假设例如项目描述

最长可达20个字符。

我的结构应该是什么样的?我以为我可以使用长度为55左右的
数组。但这似乎不符合
结构的要求。那么为什么不只是有一个二维数组。结构可以为每个项目设置变量(项目编号,项目描述等),但是如何才能正确解析文件呢?
I''ve been given a school assignment and while everything else is easy
there''s one topic I''m completley lost on.

I''ve been given an ASCII file that looks like this. During start-up, the
program needs to read an ASCII data file of inventory records and build an
appropriate dynamic data structure using structs and pointers.

10112 MICROPROCESSOR 2100 2 B 000008.50 000012.50
10235 CHARACTER_PRINTER 4013 4 C 000995.00 001295.00
10450 FLOPPY_DISK_DRIVE 6023 6 B 000650.00 000885.00
10683 VIDEO_DISPLAY_TERMIN 5000 5 C 000550.00 000750.00

The format is:

5 digits Item Number
space
20 chars Item description
space
4 digits Quantity
space
1 digit Category code
space
....... and so on.
We call these fields, so each line of the data file represents one
record, each record has 7 fields. It is easier to handle fixed
width fields so I''ll assume that for example "Item description"
can be up to 20 characters in length.
What should my strucutre look like? I was thinking I could just use an
array of length 55 or so. But that doesn''t seem to fit the requirment for a
strucutre. Why not just have a two dimensional array then. The strucutre
could have variable for each item (Item Number, Item Description, etc), but
how can I parse the file correctly then?




#define MAX_LEN_ITEM_DESCRIPTION 20

结构记录

{

unsigned long ItemNumber;

char ItemDescription [MAX_LEN_ITEM_DESCRIPTION + 1];

unsigned short数量;

unsigned char类别;

unsigned char Field5;

unsigned long Field6_X100 ;

unsigned long Field7_X100;

};


请注意,我对第6和第7个字段使用整数类型,尽管

数字实际上有两位小数。这避免了使用可能导致舍入错误的浮点类型



所以例如值000008.50。在文件中将存储

作为整数850.


如果你知道文件中的最大记录数,那么定义一个

数组用于保存所有记录:


#define MAX_RECORDS 10000

struct record DataFile [MAX_RECORDS];


尼克。



#define MAX_LEN_ITEM_DESCRIPTION 20
struct record
{
unsigned long ItemNumber;
char ItemDescription[ MAX_LEN_ITEM_DESCRIPTION + 1 ];
unsigned short Quantity;
unsigned char Category;
unsigned char Field5;
unsigned long Field6_X100;
unsigned long Field7_X100;
};

Note that I use integer types for the 6th and 7th fields, although
the numbers actually have two decimal places. This avoids having
to use a floating point type which could cause rounding errors.
So for example the value "000008.50" in the file would be stored
as the integer 850.

If you know the maximum number of records in the file then define an
array to hold all the records:

#define MAX_RECORDS 10000
struct record DataFile[ MAX_RECORDS ];

Nick.




" Nick Austin" < NI ********** @ nildram.co.uk>在消息中写道

新闻:9s ******************************** @ 4ax.com ...

"Nick Austin" <ni**********@nildram.co.uk> wrote in message
news:9s********************************@4ax.com...
2003年9月6日星期六18:28:21 GMT, - Steve - "
< se **** @ foundation.sdsu.edu>写道:
On Sat, 06 Sep 2003 18:28:21 GMT, "- Steve -"
<se****@foundation.sdsu.edu> wrote:
我已经完成了一项学校作业,而其他一切都很简单
有一个话题我完全迷失了。

我收到了一个看起来像这样的ASCII文件。在启动期间,
程序需要读取库存记录的ASCII数据文件,并使用结构和指针构建
适当的动态数据结构。

10112 MICROPROCESSOR 2100 2 B 000008.50 000012.50
10235 CHARACTER_PRINTER 4013 4 C 000995.00 001295.00
10450 FLOPPY_DISK_DRIVE 6023 6 B 000650.00 000885.00
10683 VIDEO_DISPLAY_TERMIN 5000 5 C 000550.00 000750.00

格式为:

5位数项目数量
空间
20个字符项目描述
空间
4位数量
空间
1位数分类代码
空间
.......等等。
I''ve been given a school assignment and while everything else is easy
there''s one topic I''m completley lost on.

I''ve been given an ASCII file that looks like this. During start-up, the
program needs to read an ASCII data file of inventory records and build anappropriate dynamic data structure using structs and pointers.

10112 MICROPROCESSOR 2100 2 B 000008.50 000012.50
10235 CHARACTER_PRINTER 4013 4 C 000995.00 001295.00
10450 FLOPPY_DISK_DRIVE 6023 6 B 000650.00 000885.00
10683 VIDEO_DISPLAY_TERMIN 5000 5 C 000550.00 000750.00

The format is:

5 digits Item Number
space
20 chars Item description
space
4 digits Quantity
space
1 digit Category code
space
....... and so on.



我们称之为这些字段,因此数据文件的每一行代表一条
记录,每条记录有7个字段。处理固定的宽度字段更容易,所以我假设例如项目描述
最多可以包含20个字符。



We call these fields, so each line of the data file represents one
record, each record has 7 fields. It is easier to handle fixed
width fields so I''ll assume that for example "Item description"
can be up to 20 characters in length.

我的结构应该是什么样的?我以为我可以使用长度为55左右的
数组。但这似乎不符合astrucutre的要求
。那么为什么不只是有一个二维数组。
strucut应该有每个项目的变量(项目编号,项目描述等),
但是我可以正确地解析文件吗?
What should my strucutre look like? I was thinking I could just use an
array of length 55 or so. But that doesn''t seem to fit the requirment for astrucutre. Why not just have a two dimensional array then. The strucutrecould have variable for each item (Item Number, Item Description, etc), buthow can I parse the file correctly then?



#define MAX_LEN_ITEM_DESCRIPTION 20
struct record
{
unsigned long ItemNumber;
char ItemDescription [MAX_LEN_ITEM_DESCRIPTION + 1];
unsigned short Quantity;
unsigned char Category;
unsigned char Field5;
unsigned long Field6_X100;
unsigned long Field7_X100;
};

请注意,我对第6和第7个字段使用整数类型,尽管<这些数字实际上有两位小数。这避免了使用可能导致舍入误差的浮点类型。
因此,例如值000008.50。在文件中将存储为整数850.

如果你知道文件中的最大记录数,那么定义一个
数组来保存所有记录:

#define MAX_RECORDS 10000
struct record DataFile [MAX_RECORDS];

尼克。



#define MAX_LEN_ITEM_DESCRIPTION 20
struct record
{
unsigned long ItemNumber;
char ItemDescription[ MAX_LEN_ITEM_DESCRIPTION + 1 ];
unsigned short Quantity;
unsigned char Category;
unsigned char Field5;
unsigned long Field6_X100;
unsigned long Field7_X100;
};

Note that I use integer types for the 6th and 7th fields, although
the numbers actually have two decimal places. This avoids having
to use a floating point type which could cause rounding errors.
So for example the value "000008.50" in the file would be stored
as the integer 850.

If you know the maximum number of records in the file then define an
array to hold all the records:

#define MAX_RECORDS 10000
struct record DataFile[ MAX_RECORDS ];

Nick.



我明白了,但怎么做我将ASCII表中的数据导入

变量。


所以我声明了一个变量:


记录myData [100]; //例如


现在如何在myData [0]中输入第一行?我是否使用cin将

整行输入一个字符串,然后根据空格解析该数组?


I understand that, but how do I import the data from the ASCII table into
the variables.

So I declare a variable:

record myData[100]; //for example

Now how do I enter the first line into myData[0]? Do I use cin to enter the
entire line into a string, then parse that array based on spaces?


这篇关于结构和ASCII“数据库”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆