在开发某嵌入式系统时,设计人员根据系统要求,分别编写了相关程序,其中:
[程序1] 是李工编写的一个数据交换子程序。
[程序2] 是赵工编写的一段利用递归方法判别链表表示的两个数据结构是否相等的程序,若相等则返回值为1,否则返回为0。判别的准则是若两个链表的首指针相等,则相等;否则进行内部元素比较,元素中的itemid表示后面联合体un的有效性,itemid为0时,联合体un里面的data项有效,否则联合体un中的link项有效。
[程序1]#includeimage.pngstdio.h>swap(int x,int y) {int t;t=x;x=y;y=t;}main17 {int a, b;a=3;b=4;swap (a,b);printf("%d, %d\n", a, b);}
[程序2]typedef struct Item {int itemid;union {char data;struct Item *link;} un;struct Item *nextItem;} ItemNode;int EqualItem(ItemNode *x, ItemNode *y) {int res;if x==y) return 1;if (12) {if (13) {if (x->itemid==0) {res=14;}else {res=15}if (res) {return 16}}}return 0;}
问题1:执行[程序1]后,打印出来的a=3,b=4;并没有完成数据交换,请指出李工的问题?并改正程序错误。
问题2:仔细阅读并分析[程序2]中的C语言代码,完成其中(1)~(5)空白填空。
问题3:李工编写某嵌入式软件时,遇到了一些问题,请帮助李工解答下面2个问题。
(1)李工在编译时,程序没有通过编译,经检查程序后将文件头的#include image.pngfilename.h>改为#include "filename.h"后编译通过,请问#includeimage.pngfilename.h>和#include "filename.h"的区别是什么?
(2)李工在编写一段c++程序时,需要调用一段已编译的C语言函数,为什么要加extern "C"?
正确答案及解析
正确答案
解析
问题1:李工的问题:形参不能作为函数的输出参数使用。
对应的swap函数修改如下:swap (int *x, int *y){int t;t=*x;*x=*y;*y=t;}调用时修改为swap(&a,&b)
本题考查嵌入式C/C++编程知识与应用。函数swap采用值传递,虽然将形参x和y交换了,但是并不影响到实参,所以执行[程序1]后,打印出来的a=3,b=4;并没有完成数据交换。将值传递改成指针传递就可以了。对应的swap函数应修改如下:swap (int *x, int *y){int t;t=*x;*x=*y;*y=t;}调用时应修改为swap(&a,&b)
问题2:(1)x!=NULL&&y!=NULL(2)x->itemid==y->itemid(3)x->un.data==y->un.data(4)EqualItem(x->un.link, y->un.link);(5)EqualItem(x->nextItem, y->nextItem);
该段C语言程序是一段利用递归方法判别链表表示的两个数据结构是否相等的程序,若相等则返回值为1,否则返回为0。判别的准则是若两个链表的首指针相等,则相等;否则进行内部元素比较,元素中的itemid表示后面联合体un的有效性,itemid为0时,联合体un里面的data项有效,否则联合体un中的link项有效。
该程序具体如下:如果x==y,则返回1,表示断定两个数据实际指向是同一地方,当然相等;如果x和y都不为空则继续,否则返回0;如果x->itemid==y->itemid,则继续,否则返回0;如果x->itemid==0,则若x->un.data==y->un.data,res=1,否则res=0;如果x->itemid不等于0,联合体un中的link项有效,递归调用EqualItem(x->un.link, y->un.link),继续判断;如果res=1,也就是x->un.data=y->un.data,则递归调用EqualItem(x->nextItem, y->nextItem),判断下一级链表;如果res=0,也就是x->un.data不等与y->un.data,则即可断定两个数据不相等,返回为0。
问题3:(1)对于#include<filename.h>,编译器从工程指定路径搜索filename.h;对于#include "filename.h",编译器从当前路径和工程指定路径搜索filename.h;
(2)C++语言支持函数重载,C语言不支持函数重载,函数被C++编译后在库中的名字与C语言不同。C++提供了C连接交换指定符号extern "C"来解决名字匹配问题。本题考查考生对预处理指令include的正确理解。对于#include<filename.h>来说,编译器会从工程指定路径搜索filename.h wen件;对于#include "filename.h"来说,编译器会从当前路径和工程指定路径搜索filename.h wen件。C++语言支持函数重载,C语言不支持函数重载,函数被C++编译后在库中的名字与C语言不同。为了C++程序中能够调用C程序,就需要C++提供C连接交换指定符号extern "C"来解决名字匹配问题。
包含此试题的试卷
你可能感兴趣的试题
Advancements in ( )have contributed to the growth of the automotive industry through the creation and evolution of self-driving vehicles.
-
- A.Artificial Intelligence
- B.Cloud Computing
- C.Internet of Things
- D.Big Data
- 查看答案
In project human resource management , ( )is not a source of power for the project manager.
-
- A.referent power
- B.expert power
- C.reward power
- D.audit power
- 查看答案
At the project establishment stage , the feasibility study mainly includes techinical feasibility analysis , ( ), operation environment feasibility analysis and other aspects of feasibility analysis.
-
- A.detail feasibility analysis
- B.opportunity analysis
- C.economic feasibility analysis
- D.risk analysis
- 查看答案
( )is a grid that shows the project resources assigned to each work package.
-
- A.Stakeholder engagement assessment matrix
- B.Requirements traceability matrix
- C.Probability and impact matrix
- D.Responsibility assignment matrix
- 查看答案
Xinhua News Agency reported in January 2022,Chian will further promote the developmet of a digital economy during the 14th Five-Year Plan eriod(2021-2025). The plan also emphasized industrial ( )transformation.
-
- A.digital
- B.networking
- C.intelligentize
- D.informatization
- 查看答案