首页 > 用户发贴区 > 编程问题提问区 > 急急急急!!C的题目
2008
08-10

急急急急!!C的题目

这几道题只能用C不能用C++
(1) List some ways for a C function to pass a string back to its caller. Say what advantages or disadvantages
each way might have.   

(2)The following function counts the number of bits that are set in an integer (i.e. the number of ’1′ bits). For
example if the input is 9 (in decimal) then the return value will be 2 (as 9 in binary is 1001 which has two 1
bits).  Explain how the function works. Do not just show that the function works for one example, but try to
prove that the function will work for all possible inputs.
 
   int count_bits (unsigned int n)
   {
     int count = 0;
     while (n) {
       n = (n-1) & n;
    count++;
     }
     return count;
   }

(3)Write a C function that multiplies two integers together and returns the result, i.e.
 
   int multiply (int a, int b)
   {
 
     …
   }
 
 You can not use the multiplication (*) operator in your implementation, since we will assume that this
function needs to run on an embedded CPU that has no multiply instruction. Thus function must also be as
efficient as possible.

(4)Write an implementation of the function my_strncat:
 
   void my_strncat (char *DST, const char *SRC, int MAX_LENGTH);
 
 ’my_strncat’ appends the null-terminated string in SRC to the end of the null-terminated string in DST. On
exit the length of the string DST, including its terminating null character, must not be more than
MAX_LENGTH.  In other words the string DST sits in a buffer of size MAX_LENGTH characters and this
buffer must not be overflowed. On exit the string DST must always be null-terminated.
 
 You should not use any C standard library functions.

(5) Write an implementation of the following two functions that are used to set up and check for timer events:
 
   void alarm_set (void (*callback)(void), int alarm_tick_count)
   alarm_check (int system_tick_count)
 
 The alarm_set() function is used to set an alarm. It requests that the callback function be called when the
system tick count reaches alarm_tick_count.
 
 The alarm_check() function is called by the operating system each time the system tick count is advanced (for
example, this could be every 1/100th of a second).
 
 The system must be able to track more than one alarm at a time – assume that we wish to accomodate
many alarms being set, though there is _no_ need for the alarm SETTING efficiency to be better than
O(n).  Also assume that the alarm check algorithm is time sensitive
 
 Please define any data structures that are required. Assume that the environment has limited memory, and that
both constrained memory usage and high throughput are important. 


急急急急!!C的题目》有 2 条评论

  1. whos 说:

    这周刚翻译了一个英文图形库的参考手册,我给你把前几个题目翻译过来吧,至于解题,我是没有那个能力啦。

    1.列出一些C函数返回一个字符串给它的调用者的方法。说出每种方法可能好的和不好的地方。

    2.下面的函数计算一个整数的字数(例如1).例如,输入9(十进制),返回的值是2(9的二进制为1001,有两个1)
    说明函数怎样工作。不要只用一个例子来表现函数的运行,设法证明对于所有可能的输入,函数可执行。

    3.写一个C函数,乘上两个在一起的整数并且返回结果,例如
      int multiply (int a, int b)
       {
     
         …
       }
    在执行中,你不能使用乘法运算符(*),因为我们假设这个函数需要运行在一个没有乘法指令的嵌入式CPU上。因此,这个函数要尽可能高效。

  2. amusementer 说:

    (1) for(i=0;i<n,i++)
            return a[i];
    < ?:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

           

           return puts(pf);

     

    (2)统计位为1的个数,

    关键是这个循环:
    n = (n-1) & n;
        count++;
    假设1200001100
        11
    00001011
    可以看到,一次位与操作,n1的位数就会少1个(这个解释一下。2进制的数,一位只能是01,我这样理解(100..)这样一个数与(?100..-1)进行&操作,那么肯定是(?000..,就相当于少了一个1,依次类推),当n1的位数全部&掉了,那就是0了,跳出循环。

     

    3)不能*,那只能+ 用一个循环

    int multiply (int a, int b)
    {
     int i,sum=0;
     for(i=0;i<b;i++)
      sum+=a;
    }

     

     

    (4) 编写一个my_strncat()的函数

    void my_strncat(char *DST,const char *SRC,int MAX_LENGTH)

    my_strncat()函数要求将在SRC中以\0结尾的字符串添加到在DST中以\0结尾的字符串的末尾(真是废话,就是将字符串SRC添加到DST的末尾,后面我就不直译了)。要求添加字符串后DST的长度(包含\0)不能超过MAX_LENGTH.(不能使用标准库函数strncat()

    这个很多教材里都会讲 比如C Primer里就有

     

     

     

    (5) 编写下面2个函数以用来建立和检查记时装置

    void alarm_set(void(*callback)(void),int alarm_tick_count)

    alarm_check(int system_tick_count)

    alarm_set()函数用来建立一个触发器。它请求 当系统时钟计数到alarm_tick_count时调用callback函数

     

    每次系统时钟计算开始,操作系统就调用alarm_check()函数

     

    系统必须能在同意时间跟踪多个触发,假设我们想要建立多个触发器,尽管这样做的效率并不高

     

    这个是嵌入式开发里的?,不太明白,请高手来说吧,呵呵 C也没多久,然后英语也很烂。。。

留下一个回复