zoukankan      html  css  js  c++  java
  • 实验2 C语言表达式编程应用及输入输出函数

    1.实验任务1

    #include <stdio.h>
    int main() {
        int a=5, b=7, c=100, d, e, f;
    
        d = a/b*c;
        e = a*c/b;
        f = c/b*a;
        
        printf("d=%d, e=%d, f=%d
    ",d,e,f);
    
        return 0;
    }

    line6-8中c语言表达式对应数学公式是ac/b     变量d,e,f计算结果不同的原因是line6-8中整形变量除法计算结果没有小数

    2.实验任务2

    // ex2.cpp
    
    #include <stdio.h>
    int main() {
        int x=1234;
        float f=123.456;
        double m=123.456;
        char ch='a';
        char a[]="Hello, world!"; // 定义一个数组a,数组中存放字符串常量hello,world!
        int y=3, z=4; 
        
        printf("%d %d
    ", y, z);
        printf("y=%d, z=%d
    ", y,z);
        printf("%8d,%2d
    ", x,x);
        printf("%f,%8f,%8.1f,%0.2f,%.2e
    ",f,f,f,f,f);
        printf("%lf
    ",m);
        printf("%3c
    ", ch);
        printf("%s
    %15s
    %10.5s
    %2.5s
    %.3s
    ",a,a,a,a,a);
        
        return 0;
    }

    d:按十进制整数输出   数字m:输出数据域宽,数据长度<m,左补空格;否则按实际输出    .n:对实数,指定小数点后位数(四舍五入),对字符串,指定实际输出位数  c:按字符输出   s:按字符串输出

    3.实验任务3

    // ex3.cpp
    #include <stdio.h>
    int main() {
        double x,y;
        char c1,c2,c3;
        int a1,a2,a3;
        
        scanf("%d%d%d",&a1,&a2,&a3);
        printf("%d,%d,%d
    ",a1,a2,a3);
        scanf("%c%c%c",&c1,&c2,&c3);
        printf("%c%c%c
    ",c1,c2,c3);
        scanf("%lf%lf",&x,&y);
        printf("%.1lf,%.1lf
    ",x,y);
        
        return 0;
    } 

    4.实验任务4

    // ex4.cpp
    // 判断字符类型 
    #include <stdio.h>
    int main() {
        char x;
        
        x = getchar();
        
        if(x>='0'&&x<='9' ) // 判断x是数字字符表达式 
            printf("%c是数字字符
    ", x);
        else if(x>='a'&&x<='z'||x>='A'&&x<='Z' ) // 判断x是大写或小写英文字母的表达式 
            printf("%c是英文字母
    ", x);
        else
            printf("%c是其它字符
    ", x);
        
        
        return 0;
    } 

    5.实验任务5

    // ex5.cpp
    #include <stdio.h>
    int main() {
        char ans1, ans2;
        
        printf("复习了没? (输入y或Y表示复习了,输入n或N表示没复习) :  ");
        ans1 = getchar();  // 从键盘输入一个字符,赋值给ans1
        
        getchar(); // 思考这里为什么要加这一行 
        
        printf("
    动手敲代码了没? (输入y或Y表示敲了,输入n或N表示木有敲) :  ");
        ans2 = getchar();
        
        if((ans1=='y'||ans1=='Y')&&(ans2=='y'||ans2=='Y'))
            printf("
    罗马不是一天建成的:)
    ");    
        else
            printf("
    罗马不是一天毁灭的。。。
    ");
    
        return 0;
    } 

    6.实验任务6

    #include<stdio.h>
    #include<math.h>
    int main(){
        int n,s;
        scanf("%d",&n);
        s=1*(1-pow(2,n))/(1-2);
        printf("s=%d",s);
        return 0;
    }

  • 相关阅读:
    RE最全面的正则表达式----字符验证
    Ajax tips(my_jquery_function.js)
    Python 分布式执行测试用例
    Python black + flake8 自动化规范代码
    JavaScript学习笔记-ES5
    pytest-assume 测试用例某一断言失败继续执行后面的代码
    pytest常用命令行
    [pretty_errors] Prettifies Python exception output to make it legible
    requests-html
    Python 类 继承与重写
  • 原文地址:https://www.cnblogs.com/qiansen/p/13898994.html
Copyright © 2011-2022 走看看