題目1:
在linux系統(tǒng)中,請用C語言編寫接口函數(shù),調(diào)用該接口函數(shù)可返回系統(tǒng)硬盤的大小、分區(qū)、使用情況等信息。請不要調(diào)用系統(tǒng)命令
返回相關信息。另外,請?zhí)峁┖唵蔚脑O計說明書(說明設計思路,設計中的重點和難點等)、使用說明書以及測試用
重點:
.找到相應的接口和結(jié)構(gòu)體了解相互它們相互之間的聯(lián)系
難點:各種參數(shù)之間的算法
[cpp] view plain copy#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; }注:該代碼只實現(xiàn)了顯示磁盤的相關信息,分區(qū)功能未實現(xiàn)
題目2:
例。
請用C語言編寫程序,實現(xiàn)非負整數(shù)四則運算的功能。另外,請?zhí)峁┖唵蔚脑O計說明書(說明設計思路,設計中的重點和難點等)、使用說明書以及測試用例。
程序要求:
輸入:一個算式。
輸出:該算式的四則運算結(jié)果。
此外,我們有如下的約定:
1. 算式中的數(shù)為非負整數(shù),位數(shù)不超過2位,個數(shù)不超過4個。
2. 算式中的運算只包括加減乘除,且無括號。
重點:
1.確立棧的思想
2.定義棧內(nèi)優(yōu)先級與棧外優(yōu)先級數(shù)組
難點:輸入中的糾錯處理
[cpp] view plain copy#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');//mult題目3:
寫一個鏈表的遍歷歷過程。
[cpp] view plain copy#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:
寫一個程序,在程序運行過程中把程序的源代碼讀出并存到另一個文件中(不能采用打開源文件讀寫的方式來完成操作)
[cpp] view plain copy#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; } 注意:以上兩個語句不能同時執(zhí)行,這到exec的機制(exec函數(shù)族提供了在進程中啟動另一個進程的方法),而本函數(shù)沒有子進程的開啟----------------
第一部分:UNIX基本常識
----------------
1-1)(5分)解釋為什么Unix shell命令“l(fā)s > ls.out”
導致ls.out被包括在名單中
生成的ls.out文件默認為當前路徑
1-2)(5分)如果拼錯了文件名,比如本希望鍵入“who”,
卻輸入了“woh >temp”會發(fā)生什么?
No command ‘woh ’found .
1-3) (10 分)
假設當前目錄有以下內(nèi)容:
$ 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 .
文件夾1、2、kernel的../ 占3個連接,當前文件夾中的./和當前文件夾的名字占2個連接
---------------
第二部分:C語言基礎知識
---------------
2-1) (8分)請說明以下語句的含義:
a+++++b
先 ++b,再a+b,再(a+b)++
2-2) (8分)以下程序含有一個錯誤,能說出錯誤是什么嗎?
[cpp] view plain copy#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時,會發(fā)生什么:
if (n<3)
return
date = x[0];
time = x[1];
code = x[2];
結(jié)果:
date = x[0];
2-4) (8分)下面程序的作用是把它的輸入復制到輸出:
[cpp] view plain copy#include <stdio.h> int main() { register int c; while ((c = getchar()) != EOF) putchar(c); }從這個程序中去掉#include語句,將導致程序不能通過編譯,因為這時EOF是未定義的。
假定我們手工定義了EOF(當然,這是一種不好的做法):
[cpp] view plain copy#define EOF -1 int main() { register int c; while ((c = getchar()) != EOF) putchar(c); }這個程序在許多系統(tǒng)中可以運行,但是在某些系統(tǒng)運行起來卻慢得多。這是為什么?
putchar(c),getchar();必須要到所有的頭文件中去找,浪費大量時間
2-5)(8分)請寫出下面程序的標準輸出的結(jié)果:
[cpp] view plain copy#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); } 結(jié)果:-1---------------------
第三部分:程序設計
--------------------
3-1)(15 分 每題3分)改進下面的函數(shù),使它更清晰簡練:
a)
[cpp] view plain copyint 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 )?SUCCSESS:retval;
3-2)(15分)任選一種你所熟悉的語言,寫出一個程序,分析/etc/passwd文件和/etc/group文件,輸出系統(tǒng)中每一個組所包含的用戶。
以下列出一個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
注意,需要考慮一個組中有多個用戶的情況。
[cpp] view plain copy#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. 翻譯: 每天感覺過去這一年,我們在自由工程遭到應用軟件攻擊的消息醒來:公司偷竊的我們共享的自由軟件,設備制造商在讀者背后遠程刪除電子書,大媒體商為了數(shù)字限制和間諜服務孵化新的方案,和世界各地的政府密謀擴大和協(xié)調(diào)他們的對公民的數(shù)字征服。 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. 翻譯: “社區(qū)”這個詞被傳播,但在這些時間,真的重要的是,我們圍繞一個核心的理念建立專業(yè)的和社會的團結(jié)。這是關鍵,我們團結(jié)在一起,為了一個更加美好的世界,雙方促進我們的積極的想法,停止那些嘗試把軟件用來針對對其用戶的做法。新聞熱點
疑難解答