美文网首页
2顺序程序设计

2顺序程序设计

作者: 别花春水 | 来源:发表于2019-01-19 18:30 被阅读0次
    #include <stdio.h> //input & output
    #include <stdlib.h> //pause
    #include <math.h>
    
    /*
    1. 表达式语句    y=x+2;
    2. 函数调用语句   printf("hello world");
    3. 控制语句:
        条件(eg:
                if((x=y+5)>0) z=x;
                switch
            )
        循环(when,while)
        转向语句(break,goto,continue,return)
    4. 复合语句:把多个语句用{}复合在一起
    5. 空语句
        eg:
            while(getchar()!='\n')
            {
                ;
            }   //只要从键盘输入的字符不是回车则重新输入
    */
    

    printf 格式输出函数

    /*
    输出格式:
    %d  按十进制输出带符号整数
    %ld 按十进制长整型输出
    %o  八进制
    %x  十六进制
    %u  以十进制形式输出无符号整数
    %c  按字符型输出
    %f  浮点
    %e  指数
    %s  输出字符串
    %g  以%f或%e中较短的输出宽度输出单、双精度实数
    -   结果左对齐,右边填空格
    +   输出符号
    空格  输出值是正时冠以空格,为负时冠以负号
    $#$ 对egf类当结果有小数时才给出小数点;对o类,输出时加前缀o;x类,输出时加前缀ox;
    *   表示该输入项在读入后不赋予相应变量。
    例如scanf("%$*$d,%d",&a)会跳过第一个d直接把a付给第二个d
    */
    
    • 例子
    void main()
    {
        int a = 15;
        float b = 123.1234567;
        double c = 12345678.1234567;
        char d = 'p';
        printf("a=%d,%5d,%o,%x\n", a, a, a, a);
        printf("b=%f,%lf,%5.4lf,%e\n", b, b, b, b);
        printf("c=%lf,%f,%8.4f\n", c, c, c);
        printf("d=%c,%8c\n",d,d);
    
        system("pause");
    }
    
    void main()
    {
        int a = 8;
        printf("%d\n%d\n%d\n%d\n%d\n%d\n%d\n",++a, ++a, --a, a++, a--,-a++,-a--);
        system("pause");
    }
    //++i和--i是把所有加加减减算一遍再赋值。
    

    putchar 字符输出函数:输出单个字符

    /*
        putchar('A')    //输出大写字母A
        putchar(x)  //输出字符变量x的值
        putchar('\101') //输出大写字母A
        putchar('\n')   //换行
        putchar('\t')   //tab
    */
    

    getchar 从键盘输入函数

    void main()
    {
        char c;
        printf("input a char\n");
        c = getchar();
        putchar(c);
        system("pause");
    }
    

    scanf 格式输入函数:按指定格式从键盘输入到指定变量

    scanf("格式控制字符串",&地址表列);

    没有精度控制不能加%5.2f

    &a表示变量a的地址

        void main()
        {
            int a, b, c;
            printf("input a,b,c\n");
            scanf_s("%d,%d,%d", &a, &b, &c);    //输入时用逗号分隔
            printf("a=%d,b=%d,c=%d", a, b, c);
            system("pause");
        }
    
        void main()
        {
            int a, b, c;
            printf("input a,b,c\n");
            scanf_s("%d %d %d", &a, &b, &c);    //输入时用空格隔开abc
            printf("a=%d,b=%d,c=%d", a, b, c);
            system("pause");
        }
    
    /*与scanf_s("%c%c%c", &a, &b, &c);有区别*/
    
    • 输入小写字母转换成大写字母
        void main()
        {
            char a, b, c;
            printf("input character a,b,c\n");
            scanf_s("%c,%c,%c", &a, &b, &c);
            printf("%d,%d,%d\n%c,%c,%c\n", a, b, c, a - 32, b - 32, c - 32);
            system("pause");
        }
    
    • 输入三角形三边长,求三角形面积
    #include <math.h>
        void main()
        {
            float a, b, c, s, area;
            scanf_s("%f,%f,%f", &a, &b, &c);
            s = 1.0 / 2 * (a + b + c);
            area = sqrt(s*(s - a)*(s - b)*(s - c));
            printf("a=%7.2f,b=%7.2f,c=%7.2f,s=%7.2f\n", a, b, c, s);
            printf("area=%7.2f\n", area);
            system("pause");
        }
    
    • 求方程ax^2+bx+c=0的根
        void main()
        {
            double a, b, c, d, e, x1, x2, dis;
            scanf_s("%lf,%lf,%lf", &a, &b, &c);
            dis = b * b - 4 * a*c;
            {
                if (dis < 0)
                {
                    printf("dis<0 : No Result");
                }
                else
                {
                    d = sqrt(dis) / (2 * a);
                    e = -b / (2 * a);
                    x1 = e + d;
                    x2 = e - d;
                    printf("x1=%7.2f\nx2=%7.2f\n", x1, x2);
                }
                system("pause");
                return 0;
            }
        }
    
    

    相关文章

      网友评论

          本文标题:2顺序程序设计

          本文链接:https://www.haomeiwen.com/subject/iemzdqtx.html