zoukankan      html  css  js  c++  java
  • 习题5

    1、编程实现任意三个整数按大到小的顺序输出。

       (用条件运算实现)

    #include <stdio.h>

    void main()

    {

             int a,b,c,max,min,mid;

             printf("请输入三个整数:");

             scanf("%d%d%d",&a,&b,&c);

             max=(a>b?a:b)>c?(a>b?a:b):c;

             min=(a<b?a:b)<c?(a<b?a:b):c;

             mid=a+b+c-max-min;

             printf("从大到小为:%d%d%d\n",max,mid,min);

    }

    2、编程实现输入年份和月份,输出该年份和月份的天数。

      (switch语句实现)

    #include <stdio.h>

    void main()

    {       

             int year,month;

             printf("请输入年份和月份:");

             scanf("%d%d",&year,&month);

                       switch(month)

                       {

                       case 2 :

                                if(year%400==0 ||(year%4==0 && year%100!=0))

                                         printf("%d年有366天,%d月有29天\n",year,month);

                                else

                                         printf("%d年有365天,%d月有28天\n",year,month);

                       break;

                       case 4 :

                       case 6 :

                       case 9 :

                       case 11 : printf("%d年有365天,%d月有30天\n",year,month);

                       break;

                       default : printf("%d年有365天,%d月有31天\n",year,month);

                       }

    }

    3、编程实现从键盘输入一个算术表达式,输出计算结果,结果保留2位小数。若输入4.5+9则输出13.50,若输入4/0则输出除数为零错误。若输入8.5%5则输出输入的数据不是整数不能求余。(运算符包含+、-、*、/、%,switch语句实现)

    #include <stdio.h>

    void main()

    {

             float a,c,x;

             char b;

             printf("请输入三个数:");

             scanf("%f%c%f",&a,&b,&c);

             switch(b)

             {

             case '+' :

                       printf("%.2f\n",x=a+c);

             break;

             case '-' :

                       printf("%.2f\n",x=a-c);

             break;

             case '/' :

                       if(c==0)

                                printf("输入错误");

                       else

                                x=(int)a/(int)c;

                                printf("%.2f\n",x);

             break;

             case '%' :

                       if((a-(int)a>0) || (c-(int)c>0))

                                printf("输入错误");

                       else

                                x=(int)a%(int)c;

                                printf("%.2f\n",x);

             break;

             case '*' :

                       printf("%.2f\n",x=a*c);

             break;

             }

    }

    4、统计从键盘输入的一行字符中的大写字母、小写字母、数字个数与其它字符个数。(while实现)

    #include <stdio.h>

    void main()

    {       

             char x;

             int a=0,b=0,c=0,d=0;

             printf("请输入一行字符: \n");

             scanf("%d",&x);

             while((x=getchar())!='\n')

             {

                       if('0'<=x && x<='9')

                       {

                                a++;}

                       else if('A'<=x && x<='Z')

                       {

                                b++;}

                       else if('a'<=x && x<='z')

                       {

                                c++;}

                       else

                                d++;

             }

             printf("数字个数为:%d\n",a);

             printf("大写字母个数为%d\n:",b);

             printf("小写字母个数为:%d\n",c);

             printf("其他字符个数为:%d\n",d);

    }

     

    5、输入一个长整形数,计算该数的每位数字之和。若输入的是12345则输出15,若输入的是736则输出16。(while实现)

    #include <stdio.h>

    int main()

    {

      unsigned long s = 0;

      char c;

      printf("请输入一个长整型数字: ");

      while (scanf("%c", &c))

        if (c > 47 && c < 58)

                       s += c - 48;

             else if (c == 10)

             {

                       printf("%d\n", s); break;

             }

             else

        {

                       printf("输出错误!\n"); break;

             }

             return 0;

    }

    小胖专属学习分享
  • 相关阅读:
    配置Kickstart无人值守安装centos5.9 天高地厚
    数据库是什么,它是做什么用的? 天高地厚
    Mysql主从复制 天高地厚
    android开发中eclipse里xml的自动提示
    "error: device not found" and "error:device offline"
    gentoo中emerge失效:File "/usr/bin/emerge", line 43
    android:修改preference中view属性
    gerrit上利用sshkeygen公钥
    git 基本命令介绍
    prebuilt/linuxx86/toolchain/armeabi4.4.3/bin/armeabigcc: /lib/libc.so.6: version `GLIBC_2.11' not found:解决办法
  • 原文地址:https://www.cnblogs.com/xpl520/p/15689770.html
Copyright © 2011-2022 走看看