北京凝思科技笔试题(带答案)

题目1

   在Linux系统中,请用C语言编写接口函数,调用该接口函数可返回系统硬盘的大小、分区、使用情况等信息。请不要调用系统命令

   返回相关信息。另外,请提供简单的设计说明书(说明设计思路,设计中的重点和难点等)、使用说明书以及测试用

重点:

.找到相应的接口和结构体了解相互它们相互之间的联系

难点:各种参数之间的算法

#include<stdio.h>
#include<sys/vfs.h>
//#include<statfs.h>
#include <errno.h>
#include <stdlib.h>
int main()
{
struct statfs sys; 
int ret,percent;
double disk_size,disk_avail,all;
ret = statfs("/home",&sys);
if(ret < 0)
{
perror("fail to statfs\n");
return -1;
}
all = sys.f_blocks - sys.f_bfree + sys.f_bavail;
percent = (sys.f_blocks - sys.f_bfree )*100 / all  + 1;
disk_size = sys.f_blocks/1024 * sys.f_bsize/1024 /1024 ;
disk_avail = sys.f_bavail/1024 * sys.f_bsize/1024 /1024;
printf("%s\t\t%.1fG\t%.1fG  \t%.1fG\t%d%%\t%s\n","/dev/sda1",disk_size,
(disk_size - disk_avail),disk_avail,percent,"/home");
system("df /home");
return 0;
}

注:该代码只实现了显示磁盘的相关信息,分区功能未实现

题目2

例。

   请用C语言编写程序,实现非负整数四则运算的功能。另外,请提供简单的设计说明书(说明设计思路,设计中的重点和难点等)、使用说明书以及测试用例。

   程序要求:

      输入:一个算式。

      输出:该算式的四则运算结果。

   此外,我们有如下的约定:

       1. 算式中的数为非负整数,位数不超过2位,个数不超过4个。

       2. 算式中的运算只包括加减乘除,且无括号。

重点:

1.确立栈的思想

2.定义栈内优先级与栈外优先级数组

难点:输入中的纠错处理

#include<stdio.h>
#include<ctype.h>
#include<strings.h>
#include <unistd.h>
#define maxsize 30
int in[7] = {3,3,5,5,0}; //set the priority arry
int out[7] = {2,2,4,4,0};
struct _stack_{     //buid the sequnce stack
int top;
int data[maxsize];
};
typedef struct _stack_ mystack;
void init_stack(mystack *stack )
{
stack->top = 0;
return ;
}
void push(mystack *stack,int data)
{
stack->data[stack->top++ ] = data;
return;
}
void pop(mystack *stack,int *data)
{
*data = stack->data[--stack->top ];
return ;
}
int gettop_stack(mystack *stack)
{
int top = stack->top;
return stack->data[top -1];
}
int  trans(char ch)  
{
switch(ch)
{
case '+': 
return 0;
break;
case '-':
return 1;
break;
case '*':
return 2;
break;
case '/':
return 3;
break;
case '#':
return 4;
}
return -1;
}
char compare(int op1,int  op2 ) //the comparation of the two operators
{
if(in[op1] > out[op2])
{
return '>';
}
else if(in[op1] <  out[op2]) 
{
return '<';
}
return -1;
}
int operate(int a,int  op, int b)//calculate operations
{
switch(op)
{
case 0:
return a + b;
break;
case 1:
return a - b;
break;
case 2:
return a * b;
break;
case 3: 
return a / b;
break;
}
return -1;
}
int  evaluate(void)  //deal with the expresions char by char
{
char ch;
int a,b,op,x,temp;
int flag = 0,sum = 0;
mystack  character,operator;
init_stack(&character);
init_stack(&operator);
push(&operator,trans('#'));
ch = getchar();
if(ch == '+') //ignore the initial input '+'
ch = getchar();
else if(ch == '-')
{
ch = getchar();//ignore the initial input '-'
flag = 1; //set the negative data flag
}
while(ch != '#' || gettop_stack(&operator) !=trans( '#'))
{
if(isdigit(ch))
{
sum  = sum*10 + (int )(ch - '0');//multiple bits data process
ch = getchar();
if(!isdigit(ch))
{
if(flag)  
{
sum =  (-1)* sum;
flag =0;
}
push(&character,sum);
sum = 0;
}
} 
else
{
if((temp = trans(ch)) < 0 ) //the judgement of the input errors
{
printf("please input the correct operator\n");
_exit(1);
}
switch(compare(gettop_stack(&operator),temp ))
{
case '>':
pop(&character,&a);
pop(&character,&b);
pop(&operator,&op);
printf("op =%d\n",op);
push(&character,operate(a,op,b));
break;
case '<':
push(&operator,trans(ch));
ch = getchar();
break;
default:
break;
}
}
}
return gettop_stack(&character);
}
int main(int argc, const char *argv[])
{
int result ;
printf("please input the expresions:\n"); //输出表达式一“#”结束
result = evaluate();
printf("The result is %d\n",result);
return 0;
}

题目3:

写一个链表的遍历历过程。

#include<stdio.h>
#include<stdlib.h>
#include"bitree.h"
typedef int lnDataType;
typedef struct _link_node_
{
lnDataType  data;
struct _link_node_ *next;
}LinkNode;
LinkNode *creat_node(lnDataType data)
{
LinkNode *node = NULL;
node = (LinkNode *)malloc(sizeof(LinkNode));
node->data = data;
node->next = NULL;
return node;
}
int insert_node(LinkNode *head,lnDataType data)
{
LinkNode *new = NULL;
new = creat_node(data);
head->next = new;
return 0;
}
int delete_node(LinkNode *list)
{
LinkNode *tmp = NULL;
tmp  =  list;
list = tmp->next;
free(tmp);
return 0;
}
int main()
{
int  i;
LinkNode *my_linknode = NULL;
my_linknode = creat_node(0);
for(i = 0; i< 10; i++)
{
insert_node(my_linknode,i);
}
while(head->next != NULL)
{
LinkNode *temp = NULL;
temp = head -> next;
printf(“the link_quence is %d\t ”,temp->data;); 
}
putchar(‘\n’);
return 0;
}

题目4:

写一个程序,在程序运行过程中把程序的源代码读出并存到另一个文件中(不能采用打开源文件读写的方式来完成操作)

#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>


int main(int argc, const char *argv[])
{

	char *arg_v[4] = {"cp","copy2.c","copy1",NULL};
	char *arg_p[4] ={"ln","copy2.c","copy",NULL};
	
	if(execv("/bin/ln",arg_p) < 0)
	{
		perror("fail to execv /bin/ln");
		exit(EXIT_FAILURE);
	}


	if(execv("/bin/cp",arg_v) < 0)
	{
		perror("fail to execv /bin/cp");
		exit(EXIT_FAILURE);
	}


	return 0;
}
注意:以上两个语句不能同时执行,这到exec的机制(exec函数族提供了在进程中启动另一个进程的方法),而本函数没有子进程的开启

----------------

第一部分:UNIX基本常识

----------------

1-1)(5分)解释为什么Unix shell命令“ls > ls.out

导致ls.out被包括在名单中

 生成的ls.out文件默认为当前路径

1-2)(5分)如果拼错了文件名,比如本希望键入“who”,

却输入了“woh >temp”会发生什么? 

No command  woh found .

1-3) 10 分)

假设当前目录有以下内容:

$ ls -l

drwxr-xr-x  2 user1 users    4096 2009-09-25 15:39 1

drwxr-xr-x  2 user1 users    4096 2009-09-25 15:39 2

drwxr-xr-x 10 user1 users    4096 2009-10-21 16:33 kernel

为什么ls -ld报告当前目录有5条链?

$ ls -ld

drwxr-xr-x 5 lizhi-rocky users 4096 2009-10-23 14:02 .

文件夹12kernel../ 3个连接,当前文件夹中的./和当前文件夹的名字占2个连接

---------------

第二部分:C语言基础知识

---------------

2-1) 8分)请说明以下语句的含义:

a+++++b

先 ++b,a+b,再(a+b++

2-2) 8分)以下程序含有一个错误,能说出错误是什么吗?

#define FLAG 4
int flag;
scanf("%d", &flag);
if (flag & FLAG !=0 )    //(flag & FLAG)
{      
     printf("FLAG set\n");
}
else {
     printf("FLAG not set\n");
}

2-3) 8分)仔细读下面这段程序,请说出当n=2时,会发生什么:

if (n<3)

   return

date = x[0];

time = x[1];

code = x[2];

结果:

date = x[0];

2-4) 8分)下面程序的作用是把它的输入复制到输出:

#include <stdio.h>
int main()
{
  register int c;
  while ((c = getchar()) != EOF)
    putchar(c);
}

从这个程序中去掉#include语句,将导致程序不能通过编译,因为这时EOF是未定义的。

假定我们手工定义了EOF(当然,这是一种不好的做法):

#define EOF -1
int main()
{
  register int c;
  while ((c = getchar()) != EOF)
    putchar(c);
}

这个程序在许多系统中可以运行,但是在某些系统运行起来却慢得多。这是为什么?

putchar(c),getchar();必须要到所有的头文件中去找,浪费大量时间

2-5)(8分)请写出下面程序的标准输出的结果:

#include <stdio.h>
#define max(a,b) ((a)>(b)?(a):(b))
int main()
{
    int x[5]={0, 2, 1, 3, -1};
    int biggest = x[0];
    int i = 1;
    while ( i < 5)
        biggest = max(biggest, x[i++]);
    printf("%d\n", biggest);
}
结果:-1

---------------------

第三部分:程序设计

--------------------

3-1)(15 分 每题3分)改进下面的函数,使它更清晰简练:

a)

int smaller(char *s, char *t) {
    if (strcmp(s, t) < 1)
        return 1;
    else
        return 0;
}
改进:
int smaller(char *s, char *t) {
    if (strcmp(s, t) < 1)
        return 1;
        return 0;
}
b)
flag = flag ? 0 : 1;
改进:
flag = (!flag)&&1;
c)
if ( val & 1)
    bit = 1;
else
    bit = 0;
改进:
bit = (val&0x01)&&1;
d)
if (istty(stdin)) ;
else if (istty(stdout)) ;
     else if (istty(stderr)) ;
          else return(0);
改进:
if (istty(stdin)) ;
else if (istty(stdout)) ;
     else  (istty(stderr)) ;
       
e)
if (retval != SUCCESS )
{
    return retval);
}
/* All went well! */
return SUCCESS;

改进:

return retval = = SUCCESS )?SUCCSESSretval

3-2)(15分)任选一种你所熟悉的语言,写出一个程序,分析/etc/passwd文件和/etc/group文件,输出系统中每一个组所包含的用户。

以下列出一个passwd文件例子:

root:x:0:0:root:/root:/bin/bash

daemon:x:1:1:daemon:/usr/sbin:/bin/sh

bin:x:2:2:bin:/bin:/bin/sh

sys:x:3:3:sys:/dev:/bin/sh

sync:x:4:65534:sync:/bin:/bin/sync

games:x:5:60:games:/usr/games:/bin/sh

man:x:6:12:man:/var/cache/man:/bin/sh

lp:x:7:7:lp:/var/spool/lpd:/bin/sh

mail:x:8:8:mail:/var/mail:/bin/sh

news:x:9:9:news:/var/spool/news:/bin/sh

uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh

proxy:x:13:13:proxy:/bin:/bin/sh

以下列出一个group文件例子:

root:x:0:

daemon:x:1:

bin:x:2:

sys:x:3:

adm:x:4:

tty:x:5:

disk:x:6:

lp:x:7:

mail:x:8:

news:x:9:

uucp:x:10:

man:x:12:

proxy:x:13:

kmem:x:15:

输出的格式如下:

root: root

daemon: daemon

注意,需要考虑一个组中有多个用户的情况。

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include"link_node.h"
#include <stdlib.h>
#define  N 1024
typedef struct _link_node_
{
char  data[50];
char  id[10];
struct _link_node_ *next;
}LinkNode;
LinkNode *creat_node()
{
LinkNode *node = NULL;
node = (LinkNode *)malloc(sizeof(LinkNode));
node ->next = NULL;
return node;
}
int insert_node(LinkNode *head,char *name,char *id)
{
LinkNode *new = NULL ;
new = (LinkNode *)malloc(sizeof(LinkNode));
strcpy(new->id ,id);
strcpy(new->data , name);
new->next = head->next;
head->next = new;
return 0 ;
}
int search_node(LinkNode *head,char *id)
{
LinkNode *temp = head;
while(temp->next != NULL && temp != NULL )
{
if(strcmp(id ,temp->next->id) == 0 )
{
printf(" %-10s\n",temp->data);
}
temp = temp-> next;
}
return 0;
}
int delete_node(LinkNode *head,char *id)
{
LinkNode *temp = NULL;
while(head->next != NULL && head != NULL)
{
if(strcmp( id, head->next->id) == 0)
{
temp = head->next;
head->next = temp->next;
free(temp);
}
}
return 0;
}
int free_linknode(LinkNode *head)
{
LinkNode *temp = NULL;
while(head != NULL )
{
temp = head;
head = temp ->next;
free(temp);
}
return 0;
}
int display_node(LinkNode *head)
{
while(head->next)
{
printf("id = %s,name= %s\n",head->next->id
,head->next->data);
head = head->next;
}
return 0;
} 
int main(int argc, const char *argv[])
{
LinkNode  *Node = NULL;
Node = creat_node();
FILE *fp = NULL;
char buff[N];
char *name;
char *ch_id;
char *groupname;
int count = 0;
if((fp = fopen("/etc/passwd","r") ) == NULL)
{
perror("fail to fopen");
return -1;
}
while(fgets(buff,sizeof(buff),fp) != NULL  )
{
count = 0;
buff[strlen(buff)] = '\0';
strtok(buff,":");
strtok(NULL,":");
strtok(NULL,":");
ch_id = strtok(NULL,":");
name = strtok(NULL,":");
insert_node(Node,name,ch_id);
}
if((fp = fopen("/etc/group","r")) == NULL)
{
perror("fail to open /etc/group");
return -1;
}
while(fgets(buff,sizeof(buff),fp) != NULL)
{
groupname = strtok(buff,":");
strtok(NULL,":");
ch_id = strtok(NULL,":");
  printf("%-15s",groupname);
search_node(Node,ch_id);
}
free_linknode(Node);
return 0;
}
---------------------
第四部分:英译汉
---------------------
4-1)(10分)请将下面这段文字翻译成中文。注,专有名词可以不翻译。
"My rule is, if I can't share it with you, I won't take it."
 --Richard M. Stallman
翻译:
我的原则是,“如果我不能与你分享它,那么我不会采纳它”。
---理查德.斯托曼
It feels like every day this past year, we woke to news of an assault
on our freedoms engineered through software: companies pilfering from
our free software commons, device manufacturers remotely deleting
ebooks behind readers' backs, Big Media hatching new schemes for
digital restrictions and spying, and governments around the world
conspiring to expand and coordinate their digital subjugation of
citizens.
翻译:
每天感觉过去这一年,我们在自由工程遭到应用软件攻击的消息醒来:公司偷窃的我们共享的自由软件,设备制造商在读者背后远程删除电子书,大媒体商为了数字限制和间谍服务孵化新的方案,和世界各地的政府密谋扩大和协调他们的对公民的数字征服。
The word "community" gets bandied about, but in these times, it really
is important that we build professional and social solidarity around a
core set of ideals. It's critical that we hang together, both to
advance our positive ideas for a better world and to stop those trying
to turn software against its users.
翻译:
  “社区”这个词被传播,但在这些时间,真的重要的是,我们围绕一个核心的理念建立专业的和社会的团结。这是关键,我们团结在一起,为了一个更加美好的世界,双方促进我们的积极的想法,停止那些尝试把软件用来针对对其用户的做法。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值