zoukankan      html  css  js  c++  java
  • 字符串与格式化输入/输出

    //主要是数组的scanf用法和strlen sizeof 的区别
    #include<stdio.h>
    #define DENSITY 62.4
    int main(void)
    {
    float weight ,volume;
    int size ,letters;
    char name[40];

    printf("Hi!What's your first name? ");
    scanf("%s",name);
    printf("%s,what's your weight in pounds? ",name);
    scanf("%f",&weight);
    size = sizeof name;
    letters = strlen(name);
    volume = weight /DENSITY;
    printf("Well,%s,your volume is %2.2f cubic feet. ",
    name,volume);
    printf("Also,your first name has %d letters, ",
    letters);
    printf("and we have %d bytes to store it in. ",size);
    return 0;

    }


    /*
    scanf()开始读取输入以后,会在遇到的第一个空白字符(bank)、制表符(tab)或者换行符(newline)处停止读取。
    %s只会把一个单词而不是整个语句作为字符串读入,其他会存入内存缓冲区内
    */


    #include<stdio.h>
    #define PRAISE "What a super marvelous name!"
    int main(void)
    {
    char name[40];
    while(1)
    {
    printf("What's your name? ");
    scanf("%s",name);
    printf("Hello,%s.%s ",name,PRAISE);
    }

    }


    //strlen()会比用sizeof()少一位''


    #include<stdio.h>
    #include<string.h>
    #define PRAISE "What a super marvelous name!"
    int main(void)
    {
    char name[40];

    printf("What's your name? ");
    scanf("%s",name);
    printf("Hello,%s. %s ",name,PRAISE);
    printf("Your name of %d letters occupies %d memory cells. ",
    strlen(name),sizeof name);
    printf("The phrase of praise has %d letters",
    strlen(PRAISE));
    printf("and occupies %d memory cells. ",sizeof PRAISE);
    return 0;

    }


    /* 符号常量所用的名字必须满足变量的命名规则,可以使用大写和小写字母、数字和下划线字符,第一个字符不能是数字
    printf()语句中的%1.2f使输出结果四舍五入为保留两位小数 */


    #include<stdio.h>
    #define PI 3.14159
    int main(void)
    {
    float area,circum,radius;

    printf("What is the radius of your pizza? ");
    scanf("%f",&radius);
    area = PI * radius * radius;
    circum = 2.0 * PI * radius;
    printf("Your basic pizza parameters are as follow: ");
    printf("circumference = %1.2f,area = %1.2f ",circum,
    area);
    return 0;

    }


    //系统定义的明显常量--------------以下程序使用limit.h和float.h中定义的常量

    #include<stdio.h>
    #include<limits.h>
    #include<float.h>

    int main(void)
    {
    printf("Some number limits for this system: ");
    printf("Biggest int :%d ",INT_MAX);
    // printf("Smallest unsigned long: %lld ",LLONG_MIN); //我的系统不支持
    printf("One byte = %d bits on this system. ",CHAR_BIT);
    printf("Largest double: %e ",DBL_MAX);
    printf("Smallest normal float: %e ",FLT_MIN);
    printf("float precision = %d digits ",FLT_DIG);
    printf("float epsilon = %e ",FLT_EPSILON);
    return 0;
    }

    关于%g在群里的一些讨论:

    %.2g不是一共两位的,要分情况的,比如是整数,会打印06,指的是两位有效数字,此时可以它决定位数为2;
    而7.982会打印7.98 ,指的是保留两位小数 ,位数是由.前面点前面的数决定的,比如%6.2d是显示6位。

    c中又有一个规定:如果你指定的位数小于实际的位数,那么字段自动拓展以适应数字的长度,指的是明明3位,你指定2位,系统自动变为3位。p71


    %f是四舍五入,小数点后面6位为有效数字
    %g根据数值的不同自动选择%f或%e。%e格式在指数小于-4或者大于等于精度时使用
    %g不显示无效的0,不管精度时多少,它的有效位数包括整数 如3.2的格式说明符就是%.2g。33.141434234用%.2g会输出33

    题目中第一个不行,我猜想:0.0000009979被指定用%f,它先化成0.000001(因为系统%f有效位数6位--四舍五入了)之后变为科学计数法,即1e-006,
    至于为什么不是1.00e-006不清楚

    我发几个代码图片,你可以研究下


    //使用转换说明符,printf()中第二项可以是变量,常量,表达式
    #include<stdio.h>
    #define PI 3.141593
    int main(void)
    {
    int number = 5;
    float expresso = 13.5;
    int cost = 3100;
    printf("The %d CEOs drank %f cups of expresso. ",number,
    expresso);
    printf("The value of PI is %f. ",PI);
    printf("Farewell!thou art too dear for my possessing, ");
    printf("%c%d ",'$',2 * cost);
    return 0 ;

    }

    /* 字段宽度
    第二个转换说明符是%2d,它指示应该产生宽度为2的字段,但是由于该整数有3位数字,所以字段应该自动拓展以适应数字的长度*/

    #include<stdio.h>
    #define PAGES 931
    int main()
    {
    printf("*%d* ",PAGES);
    printf("*%2d* ",PAGES);
    printf("*%10d* ",PAGES);
    printf("*%-10d* ",PAGES);
    return 0;


    }

    //float.c--一些浮点数的组合

    #include<stdio.h>
    int main(void)
    {
    const double RENT = 3852.99;

    printf("*%f* ",RENT);
    printf("*%e* ",RENT);
    printf("*%4.2f* ",RENT);
    printf("*%3.1f* ",RENT);
    printf("*%10.3f* ",RENT);
    printf("*%10.3e* ",RENT);
    printf("*%+4.2f* ",RENT);
    printf("*%010.2f* ",RENT);
    return 0;
    }


    /*
    第二行说明符中使用空格以在正值之前产生一个前导空格(在负值前不产生前导空格),这将使有效位相同的正值和负值以相同的宽度打印输出,因此结果看起来会舒服一些。
    第三行产生足够的前导零以填满要求的最小数字(这里是3);而使用0标志将会用前导零填满整个字符宽度;最后,如果0标志和精度说明符同时出现,则0标志将会被忽略
    */

    #include<stdio.h>
    int main(void)
    {
    printf("%x %X %#x ",31,31,31);
    printf("**%d**% d**% d** ",42,42,-42);
    printf("**%5d**%5.3d**%05d**%05.3d** ",6,6,6,6);
    return 0;

    }

    //字符串的格式化--格式说明符中的.5告诉printf()只打印5个字符
    #include<stdio.h>
    #define BLURB "Authentic imitation! "
    int main(void)
    {
    printf("/%2s/ ",BLURB);
    printf("/%24s/ ",BLURB);
    printf("/%24.5s/ ",BLURB);
    printf("/%-24.5s/ ",BLURB);
    return 0;
    }


    不匹配的浮点数转换
    /*
    n1~n4的值先放到堆栈中,放的时候是他们类型字节,long四字节,double八字节。再根据说明符读取%ld四字节,说明符不对读取的时候就不对了
    float在print被用作参数时会进行转变为double */



    #include<stdio.h>

    int main(void)
    {
    float n1 = 3.0;
    double n2 = 3.0;
    long n3 = 2000000000;
    long n4 = 1234567890;

    printf("%.1e %.1e %.1e %.1e ",n1,n2,n3,n4);
    printf("%ld %ld ",n3,n4);
    printf("%ld %ld %ld %ld ",n1,n2,n3,n4);
    return 0;

    }

    printf函数的返回值
    /*
    printf()返回所打印字符的个数,如果有输出错误,返回一个负数 */


    #include<stdio.h>
    int main(void)
    {
    int bph2o = 212;
    int rv;

    rv = printf("%d F is water's boiling point. ",bph2o);
    printf("The printf() function printed %d characters. ",
    rv);

    return 0;
    }

    //打印较长的字符串--三种方法
    #include<stdio.h>
    int main(void)
    {
    printf("Here's one way to print a ");
    printf("long string. ");
    printf("Here's another way to print a
    long string. ");
    printf("Here's the newest way to print a"
    "long string. ");
    return 0;

    }

    什么情况下使用&和scanf()的一些注意点
    /*
    如果少参数的话,会出错。
    scanf()使用空格(换行、制表符、空格)来决定怎样把输入分为几个字段,%c除外。double要用%lf。
    char数组不需要使用&
    scanf(“%d,%d”,&n,&m);运行输入时要用88,121的形式,格式说明符中的空格意味着跳过下一个输入项之前的任何空格
    scanf("%d%d",&n,&m);与scanf("%d %d",&n,&m);的行为是相同的。%c会有区别,scanf(" %c",&ch);会读取遇到的第一个非空白字符


    */


    #include<stdio.h>
    int main(void)
    {
    int age;
    float assets;
    char pet[30];

    printf("Enter your age,assets,and favourite pet. ");
    scanf("%d %f",&age,&assets);
    scanf("%s",pet);
    printf("%d $%.2f %s ",age,assets,pet);
    return 0;

    }


    //使用可变宽度的输出字段

    #include<stdio.h>
    int main(void)
    {
    unsigned width,precision;
    int number = 256;
    double weight = 242.5;

    printf("What field width? ");
    scanf("%d",&width);

    printf("The number is: %*d: ",width,number);
    printf("Now enter a width and a precision: ");
    scanf("%d %d",&width,&precision);

    printf("Weight = %*.*f ",width,precision,weight);
    return 0;

    }


    /*
    跳过输入的头两个整数--下面程序会把第三个整数复制给n
    如果程序需要读取文件中特定的列(该文件的数据以统一的列排列),那么该功能将非常有用 */

    #include<stdio.h>
    int main(void )
    {
    int n;

    printf("Please enter three integers: ");
    scanf("%*d %*d %d",&n);
    printf("The last integer was %d ",n);
    return 0;
    }
    小技巧:%9d等格式使输出对齐,指定一个和期望的数字宽度同样小或者更小的字段宽度会使数字的宽度正合适。

  • 相关阅读:
    【NOIP2017】蚯蚓
    【CF407B】Long Path
    【NOIP2017】奶酪
    【NOIP2018】赛道修建(正解)
    【NOIP2018】旅行
    【SDOI2010】地精部落
    【NOIP2017】逛公园
    百度云网盘进行注销操作
    百度超级会员租借.我租给你。
    如何在dos中运行java中的jar包
  • 原文地址:https://www.cnblogs.com/zxj-262410/p/6691221.html
Copyright © 2011-2022 走看看