Chinaunix首页 | 论坛 | 博客
  • 博客访问: 475444
  • 博文数量: 112
  • 博客积分: 2436
  • 博客等级: 大尉
  • 技术积分: 2769
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-04 19:48
文章分类

全部博文(112)

文章存档

2013年(7)

2012年(105)

分类: C/C++

2012-03-22 12:45:56


点击(此处)折叠或打开

  1. //1.#测试无break
  2. #include <stdio.h>
  3. int main(int argc, const char *argv[])
  4. {
  5.     int a=0;
  6.     switch(a)
  7.     {
  8.         case 0:printf("a is a zero.\n");
  9.         case 1:printf("a is one.\n");
  10.         default:printf("a is not a zero or one.\n");
  11.     }
  12.     return 0;
  13. } 

  14. //2.#输入一个分数,显示其等级
  15. #include <stdio.h>

  16. int main(int argc, const char *argv[])
  17. {
  18.     
  19.     int i;
  20.     printf("enter a scole:");
  21.     scanf("%d",&i);
  22.     switch(i)
  23.     {
  24.         case 0:printf("E\n");break;
  25.         case 1:printf("D\n");break;
  26.         case 2:printf("C\n");break;
  27.         case 3:printf("B\n");break;
  28.         case 4:printf("A\n");break;
  29.         default:printf("error entering!(0~4)\n");
  30.     }
  31.     return 0;
  32. }

  33. //3.#输入0~100,显示其等级
  34. #include <stdio.h>

  35. int main(int argc, const char *argv[])
  36. {
  37.     int sc,n;
  38.     printf("enter a scole:");
  39.     scanf("%d",&sc);
  40.     n=sc/10;
  41.     switch(n)
  42.     {
  43.         case 10:printf("100\n");break;
  44.         case 9:printf("90~99\n");break;
  45.         case 8:printf("80~89\n");break;
  46.         case 7:printf("70~79\n");break;
  47.         case 6:printf("60~69\n");break;
  48.         case 5:
  49.         case 4:
  50.         case 3:
  51.         case 2:
  52.         case 1:
  53.         case 0:printf("not ok!\n");break;
  54.         default:printf("error entering!!!(0~100)\n");
  55.     }
  56.     return 0;
  57. }

  58. //4#输入一个表达式,实现四则运算
  59. #include <stdio.h>

  60. int main(int argc, const char *argv[])
  61. {
  62.     int a,b;
  63.     char ch;
  64.     printf("enter an operation:");
  65.     scanf("%d%c%d",&a,&ch,&b);
  66.     switch(ch)
  67.     {
  68.         case '+':printf("%d+%d=%d\n",a,b,a+b);break;
  69.         case '-':printf("%d-%d=%d\n",a,b,a-b);break;
  70.         case '*':printf("%d*%d=%d\n",a,b,a*b);break;
  71.         case '/' :printf("%d/%d=%d\n",a,b,a/b);break;
  72.         default:printf("illegal operator!\n");
  73.     }
  74.     return 0;
  75. }

  76. //5#用终端模拟一个时钟,用终端打印时间,指定开始时间为23:59:50,显示格式为xx:xx:xx。(提示:sleep(1)用来延时一秒)
  77. #include <stdio.h>

  78. int main(int argc, const char *argv[])
  79. {
  80.     int h=23,m=59,s=55;
  81.     while(1)
  82.     {
  83.         printf("\r%02d:%02d:%02d",h,m,s);
  84.         fflush(stdout);
  85.         sleep(1);
  86.         s++;
  87.         if(s==60)
  88.         {
  89.             s=0;
  90.             m++;
  91.             if(m==60)
  92.             {
  93.                 m=0;
  94.                 h++;
  95.                 if(h==24)
  96.                     h=0;
  97.             }
  98.         }
  99.     }
  100.     return 0;
  101. }

  102. //6.#从键盘输入一个年份,判断是否为闰年。
  103. #include <stdio.h>

  104. int main(int argc, const char *argv[])
  105. {
  106.     int y;
  107.     printf("enter a year:");
  108.     scanf("%d",&y);
  109.     if(((y%100!=0)&&(y%4==0))||(y%400==0))
  110.         printf("%d is a leap year.\n",y);
  111.     else
  112.         printf("%d is not a leap year.\n",y);
  113.     return 0;
  114. }

  115. //7#编写函数将大写字母转换为小写字母
  116. #include <stdio.h>

  117. int main(int argc, const char *argv[])
  118. {
  119.     char ch;
  120.     printf("enter a character:");
  121.     scanf("%c",&ch);
  122.     if((ch>=65)&&(ch<=90))
  123.         ch=ch+32;
  124.     printf("%c\n",ch);
  125.     return 0;
  126. }
  127. //8.#验证printf函数的返回值
  128. #include<stdio.h>
  129. int main()

  130. {
  131.     int ret;
  132.     ret=printf("hello!\n");
  133.     printf("ret=%d\n",ret);
  134.     return 0;
  135. }

  136. //9.#用printf格式化输出数据
  137. #include <stdio.h>

  138. int main(int argc, const char *argv[])
  139. {
  140.     int a=10;
  141.     long b=100;
  142.     float c=3.14;
  143.     char d='A';
  144.     unsigned int e=100;
  145.     printf("a=%d\n",a);
  146.     printf("b=%ld\n",b);
  147.     printf("c=%f\n",c);
  148.     printf("d=%c\n",d);
  149.     printf("e=%u\n",e);
  150.     printf("\0100\n");
  151.     return 0;
  152. } 

  153. //10.#输入一个数,输出其十进制数和十六进制数
  154. #include <stdio.h>

  155. int main(int argc, const char *argv[])
  156. {
  157.     int i;
  158.     printf("enter a number:");
  159.     scanf("%d",&i);
  160.     printf("Decimal=%d,Hex=%#x。\n",i,i);
  161.     return 0;
  162. }

  163. //11.#输入两个数,实现其加法运算
  164. #include <stdio.h>

  165. int main(int argc, const char *argv[])
  166. {
  167.     int a,b;
  168.     printf("enter 2 numbers(by a space):");
  169.     scanf("%d %d",&a,&b);
  170.     printf("%d+%d=%d;\n",a,b,a+b);
  171.     return 0;
  172. }

  173. //12.#观察float数输出的形式
  174. #include <stdio.h>

  175. int main(int argc, const char *argv[])
  176. {
  177.     float j=10.1;
  178.     printf("float_j=%f\n",j);
  179.     return 0;
  180. }

  181. //13.#用scanf格式化输入数据,日期以02/08/2012输入,以20120208形式输出
  182. #include <stdio.h>

  183. int main(int argc, const char *argv[])
  184. {
  185.     int y;int m;int d;
  186.     printf("enter a date(mm/dd/yy):");
  187.     scanf("%d/%d/%d",&m,&d,&y);
  188.     printf("you entered the date:%04d%02d%02d\n",y,m,d);
  189.     return 0;
  190. }


  191. //14.#验证getchar()与putchar(c)
  192. #include <stdio.h>
  193.     
  194. int main(int argc, const char *argv[])
  195. {
  196.     char c;
  197.     c=getchar();
  198.     putchar(c);
  199.     printf("\n");
  200.     return 0;
  201. }

  202. //15.#验证gets(str)与puts(str)
  203. #include <stdio.h>

  204. int main(int argc, const char *argv[])
  205. {
  206.     int i;
  207.     char str[5];
  208.     gets(str);
  209.     for(i=0;i<5;i++)
  210.     {
  211.         printf("%c",str[i]);
  212.     }
  213.     printf("\n");
  214.     puts(str);
  215.     return 0;
  216. }

  217. //16.#判断数据的奇偶性
  218. #include <stdio.h>

  219. int main(int argc, const char *argv[])
  220. {
  221.     int a;
  222.     printf("enter a number(判断奇偶性):");
  223.     scanf("%d",&a);
  224.     (a%2)==0?printf("%d是一个偶数\n",a):printf("%d是一个奇数\n",a);
  225.     return 0;
  226. }

  227. //17.#比较3个数的最大值
  228. #include <stdio.h>

  229. int main(int argc, const char *argv[])
  230. {
  231.     int x,y,z;
  232.     printf("enter 3 numbers(以空格隔开,判断最大数):");
  233.     scanf("%d %d %d",&x,&y,&z);
  234.     if(x>y)
  235.     {
  236.         if(x>z)
  237.             printf("%d最大\n",x);
  238.         else
  239.             printf("%d最大\n",z);
  240.     }
  241.     else
  242.     {
  243.         if(y>z)
  244.             printf("%d最大\n",y);
  245.         else
  246.             printf("%d最大\n",z);
  247.     }
  248.     if(x<y)
  249.         x=y;
  250.     else
  251.     {
  252.         if(x>z)
  253.             printf("%d最大\n",x);
  254.         else
  255.             printf("%d最大\n",z);
  256.     }
  257.     return 0;
  258. }

  259. //18.#判断3个数能否构成三角形
  260. #include <stdio.h>

  261. int main(int argc, const char *argv[])
  262. {
  263.     int i,j,k;
  264.     printf("enter 3 number(以空格隔开,判断能否构成三角形):");
  265.     scanf("%d %d %d",&i,&j,&k);
  266.     if(i<(j+k))
  267.     {
  268.         (i>abs(j-k))?printf("%d %d %d能构成三角形\n",i,j,k):printf("%d %d %d构不成三角形\n",i,j,k);
  269.     }
  270.     else
  271.         printf("%d %d %d构不成三角形\n",i,j,k);
  272.     return 0;
  273. }

阅读(951) | 评论(0) | 转发(2) |
0

上一篇:bash编程_练习

下一篇:数组 _0211

给主人留下些什么吧!~~