实验二 C语言表达式编程应用及输入输出函数

实验任务1

//ex1.cpp
#include <stdio.h>
#include<stdlib.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\n",d,e,f);
    system("pause");
    return 0;
}

   

运算符*的优先级为2,运算符/优先级为3,先运算*(此为用VS2010敲的代码,之后为DEV C++)

ex2.cpp

//ex2.cpp
#include<stdio.h>
#include<stdlib.h>
int main(){
    int x=1234;
    float f=123.456;
    double m=123.456;
    char ch='a';
    char a[]="Hello,world!";
    int y=3,z=4;

    printf("%d %d\n",y,z);
    printf("y=%d, z=%d\n",y,z);
    printf("%8d,%2d\n",x,x);
    printf("%f, %8f, %8.1f, %0.2f, %.2e\n",f,f,f,f,f);
    printf("%lf\n",m);
    printf("%3c\n",ch);
    printf("%s\n%15s\n%10.5s\n%2.5s\n%.3s\n",a,a,a,a,a);

    system("pause");
    return 0;
}

                              ex3.cpp

//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\n",a1,a2,a3);
    scanf("%c%c%c",&c1,&c2,&c3);
    printf("%c%c%c\n",c1,c2,c3);
    scanf("%lf,%lf",&x,&y);
    printf("%f,%lf\n",x,y);
    return 0;
} 

ex4.cpp

//ex4.cpp
#include<stdio.h>
int main(){
    char x;

    x=getchar();

    if(x>='0'&&x<'9')
        printf("%c是数字字符\n");
    else if(x>='A'&&x<='Z'||x>='a'&&x<='z')
        printf("%c是英文字母\n",x);
    else
        printf("%c是其他字符\n",x);
        return 0;
}

ex5.cpp

//ex5.cpp
#include <stdio.h>
int main(){
    char ans1,ans2;
    
    printf("复习好了没?(输入y或Y代表复习了,输入n或N表示没复习)"); 
    ans1 = getchar();
    getchar() ;
    
    printf("\n动手敲代码了没?(输入y或者Y表示敲了,输入n或N表示木有敲):");
    ans2 = getchar();
    
    if(ans1=='y'||ans1=='Y'&&ans2=='n'||ans2=='N')
       printf("\n罗马不是一天建成的:)");
    else
       printf("\n罗马不是一天毁灭的。。。\n") ;
       
       return 0;
}

 

ex6.cpp

//ex6.cpp
#include<stdio.h>
#include<math.h>
int main(){
    const int x=2;
int sum;
    double n;
    printf("请输入一个1~10的n:\n");
    scanf("%lf",&n);
    sum=1*(1-pow(x,n+1))/(1-x);

    printf("n=%lf,sum=%d",n,sum);

    return 0;
}

任务六为请教同学所作。

任务七暂时不会。

posted on 2020-11-05 19:50  helianthus-JUN  阅读(187)  评论(1编辑  收藏  举报

导航