1、当你拨打一个长途电话的时候,电话公司所保存的信息包括你拨打电话的时间和日期。它还包括三个号码,你使用的那个号码,你呼叫的那个号码,以及你付账的那个号码,这些号码的每一个都由三个部分组成:区号,交换台和站号码,请为这些记帐信息编写一个结构声明。
void main()
{
struct telephone {
int area;
int exchange;
int number;
};
struct infomation {
int year;
int month;
int day;
int time;
struct telephone use_phone;
struct telephone call_phone;
struct telephone pay_phone;
};
struct infomation info;
}
2、为一个信息系统编写一个声明,它用于记录每个汽车零售商的销售情况。每份销售记录必须包括下列数据。字符串值的最大长度不包括其结尾的NUL字节。
销售记录 | 声明类型 |
---|---|
customer’s name | string(20) |
customer’s address | string (40) |
model | string (20) |
销售时可能出现三种不同类型的交易:全额现金销售,贷款销售和租赁。对于全额现金销售,你还必须保存下面这些附加信息:
附加信息 | 声明类型 |
---|---|
manufacturer’s suggested retail price | float |
actual selling price | float |
sales tax | float |
licensing fee | float |
对于租赁,你必须保存下面这些附件信息:
附加信息 | 声明类型 |
---|---|
manufacturer’s suggested retail price | float |
actual selling price | float |
down payment | float |
security deposit | float |
monthly payment | float |
lease term | int |
对于贷款销售,你必须保存下面这些附件信息:
附加信息 | 声明类型 |
---|---|
manufacturer’s suggested retail price | float |
actual selling price | float |
sales tax | float |
licensing fee | float |
down payment | float |
loan duration | int |
interest rate | float |
monthly payment | float |
name of bank | string(20) |
void main()
{
struct fullsale {
float retail_price;
float selling_price;
float sales_tax;
float lisence_fee;
};
struct leasesale {
float retail_price;
float selling_price;
float down_payment;
float deposit;
float month_payment;
int lease_term;
};
struct loansale {
float retail_price;
float selling_price;
float sales_tax;
float lisence_fee;
float down_payment;
int loan_term;
float loan_rate;
float month_payment;
char bank_name[20];
};
union typeinfo {
struct fullsale full;
struct leasesale lease;
struct loansale loan;
};
struct saleinfo {
char customer_name[20];
char customer_address[40];
char model[20];
enum {FULL_SALE, LEASE_SALE, LOAN_SALE} type;
union typeinfo info;
};
struct saleinfo sale;
}
3、计算机的任务之一就是对程序的指令进行解码,确定采取何种操作。在许多机器中,由于不同的指令具有不同的格式,解码过程被复杂化了,在某个特定的机器上,每个指令的长度都是16位,并实现了下列各种不同的指令格式。位是从右向左进行标记的。
你的任务是编写一个声明,允许程序用这些格式中的任何一种形式对指令进行解释。你的声明同时必须有一个名叫addr的unsigned short类型字段,可以访问所有的16位值,在你的声明中使用typedef来创建一个新类型,称为machine_inst。给定下面的声明:
machine_inst x;
下面的表达式应该访问它所指定的位
Expression | Bits |
---|---|
x.addr | 0-15 |
x.misc.opcode | 0-15 |
x.branch.opcode | 8-15 |
x.sgl_op.dst_mode | 3-5 |
x.reg_src.src_reg | 6-8 |
x.dbl_op.opcode | 12-15 |
typedef union {
unsigned short addr;
struct {
unsigned short dst_reg : 3;
unsigned short dst_mode : 3;
unsigned short opcode : 10;
} sgl_op;
struct {
unsigned short dst_reg : 3;
unsigned short dst_mode : 3;
unsigned short src_reg : 3;
unsigned short src_mode : 3;
unsigned short opcode : 4;
} dpl_op;
struct {
unsigned short offset : 8;
unsigned short opcode : 8;
} branch;
struct {
unsigned short dst_reg : 3;
unsigned short dst_mode : 3;
unsigned short src_reg : 3;
unsigned short opcode : 7;
} rag_src;
struct {
unsigned short opcode : 16;
} misc;
} machine_inst;
void main()
{
machine_inst machine;
}