读程序,写出程序运行的结果

  1.    1 #include <stdio.h>
       2 int dec(int *a) {
       3     return (*a)--;  
       4 }
       5 int inc(int a) {
       6     return a++;
       7 }
       8 main() {
       9     int x = 1, y;
      10     y = dec( &x );
      11     y = inc(x);
      12     printf("%d\n%d\n", x, y);
      13 
      14 }
    
  2.    1 #include <stdio.h>
       2 #define min(x,y) (((x)<(y))?(x):(y))
       3 #ifdef EOF
       4 #define square(x)  x*x
       5 #else
       6 #define square(x)  ((x)*(x))
       7 #endif
       8 int main() {
       9     int a = 1, b = 2;
      10     printf("%d\n", square(a+b));
      11     min(a++, b++);
      12     printf("%d\n ", min(a, b));
      13 }
    
  3.    1 #include <stdio.h>
       2 void selectsort(int v[], int n) {
       3     int i, j, min, temp;
       4     for( i = 0; i < n-1; i++) {
       5         min = i;
       6         for( j = i + 1; j < n; j++)
       7             if(v[j] < v[min])
       8                 min = j;
       9         temp = v[min];
      10         v[min] = v[i];
      11         v[i] = temp;
      12     }
      13 }
      14 int main() {
      15     int i;
      16     int s[] = { 5, 4, 3, 2, 1};
      17     selectsort(s, 5);
      18     for(i = 0; i < 5; i++)
      19         printf("%3d", s[i]);
      20 }
    
  4. 假定int, unsigned是32位的。

       1 #include <stdio.h>
       2 int bitcount(unsigned x) {
       3     int b;
       4     x = ~x;
       5     for(b = 0; x != 0; x >>= 1)
       6         if(x & 1)
       7             b++;
       8     return b;
       9 }
      10 main() {
      11     printf("%d\n%d\n%d\n", bitcount( ~ 0xA), bitcount(017), bitcount(255) );
      12 }
    
  5.    1 #include <stdio.h>
       2 #define MAXVAL 10
       3 int queue[MAXVAL];
       4 int front = 0;
       5 int back = 0;
       6 int empty() {
       7     return front == back;
       8 }
       9 int full() {
      10     return (back + 1)%MAXVAL == front;
      11 }
      12 int size() {
      13     return (back - front + MAXVAL) % MAXVAL;
      14 }
      15 void enqueue(int v) {
      16     if(full())
      17         return;
      18     queue[back++] = v;
      19     back = back % MAXVAL;
      20 }
      21 int dequeue() {
      22     int v;
      23     if( empty() )
      24         return 0;
      25     v = queue[front++];
      26     front = front % MAXVAL;
      27     return v;
      28 }
      29 main() {
      30     int i;
      31     for(i = 1; !full(); i++)
      32         enqueue(i);
      33     print("%d\n", size());
      34     while(!empty())
      35         printf("%d\n", dequeue()); 
      36 }
    
  6.    1 #include <stdio.h>
       2 #include <ctype.h>
       3 int stricmp(char s[], char t[]) {
       4     int i;
       5     for( i = 0; s[i] != '\0' && t[i] != '\0'; i++)
       6         if(tolower(s[i]) != tolower(t[i]))
       7             break;
       8     return tolower(s[i]) - tolower(t[i]);
       9 }
      10 main() {
      11     char s1[] = { 'a', 'b', 'c', 'd', '\0'};
      12     char s2[] = "ABCD";
      13     char s3[] = "abcde";
      14     int s1s2 = stricmp(s1, s2);
      15     int s2s3 = stricmp(s2, s3);
      16     printf("%s is %s %s\n", s1, s1s2==0 ? "equal to" : s1s2 > 0 ? "great than" : "less than" , s2);
      17     printf("%s is %s %s\n", s2, s2s3==0 ? "equal to" : s2s3 > 0 ? "great than" : "less than" , s3);
      18 }
    
  7.    1 #include <stdio.h>
       2 #define INITVAL 5
       3 int count() {
       4     static int c = INITVAL;
       5     return c--;
       6 }
       7 main() {
       8     int i;
       9     while(i = count() )
      10         printf("%d\n", i);
      11 }
    
ch3n2k.com | Copyright (c) 2004-2020 czk.