分支程序设计05 - 零基础入门学习C语言14

第四章:分支程序设计05

让编程改变世界

Change the world by program


 

switch语句

 

练习1:输入三个整数,输出最大数和最小数。

  答案与解析: [codesyntax lang="c"]
#include <stdio.h>

void main()
{
    int a,b,c,max,min;

    printf("input three numbers:    ");
    scanf("%d%d%d",&a,&b,&c);

    if( a > b )
    {
        max = a;
        min = b;
    }
    else
    {
        max = b;
        min = a;
    }

    if( max < c )
        max=c;
    else if( min > c )
        min=c;

    printf("max=%dnmin=%d",max,min);
}
[/codesyntax]  

练习2:计算器程序。用户输入运算数和四则运算符,输出计算结果。

  答案与解析: [codesyntax lang="c"]
#include <stdio.h>

void main()
{
    float a,b;
    char c;

    printf("input expression: a+(-,*,/)b n");
    scanf("%f%c%f",&a,&c,&b);
    switch(c)
    {
        case '+': printf("%fn",a+b);break;
        case '-': printf("%fn",a-b);break;
        case '*': printf("%fn",a*b);break;
        case '/': printf("%fn",a/b);break;
        default: printf("input errorn");
    }
}
[/codesyntax]   [buy] 获得所有教学视频、课件、源代码等资源打包 [/buy] [Downlink href='http://kuai.xunlei.com/d/LYFCNCHVJKGE']视频下载[/Downlink]
posted @ 2010-10-17 09:01  我就爱小甲鱼  阅读(140)  评论(0编辑  收藏  举报