SCAU high level language programming -- Experiment 3 basic input and output

Keywords: Programming ascii

SCAU high level language programming -- Experiment 3 basic input and output

1, Exercises for upper limit of class

1. Character input and output

/*Version 1*/
#include<stdio.h>
int main(){
    char a;
    scanf("%c",&a);
    printf("%c\n",a);
    return 0;
}

/*Version 2*/
#include<stdio.h>
int main(){
    char a;
    a = getchar();
    putchar(a);
    return 0;
}

2. Add calculation

#include<stdio.h>
int main(){
    char n;
    int a,b;
    printf("Please enter an addition expression:");
    scanf("%d%c%d",&a,&n,&b);
    printf("%d\n",a+b);
    return 0;
}

Refer to book P48

3. Circle area

#include<stdio.h>
#define PI 3.14159 / / this is the definition
//float PI = 3.14159 / / this is a global variable
int main(){
    float r;
    printf("Enter circle radius r: ");
    scanf("%f",&r);
    printf("%.2f",PI*r*r);
    return 0;
}

Note: PI can be a definition or a global variable. It is recommended to use a definition here, because it will not change, and the global variable is variable.

4. Calculate the temperature in centigrade

Title: input a Fahrenheit temperature value from the keyboard, and output the corresponding Celsius temperature value according to the format, accurate to two decimal places. The mathematical formula is described as the product of 5 / 9 of the temperature in Celsius multiplied by 32 of the temperature in Fahrenheit.

#include<stdio.h>
int main(){
    float f;
    scanf("%f",&f);
    printf("%.2f",(5.0/9.0)*(f-32));
    return 0;
}

Note: five out of nine cannot be 5 / 9, 5.0 / 9.0 is required, because the calculation is of float type, otherwise the result is wrong.

2, Practice in class

Title: input a 3-digit positive integer from the keyboard. It is required to output the 100 digit number and the one digit number successively, each occupying one line.

#include<stdio.h>
int main(){
    int a;
    scanf("%d",&a);
    printf("%d\n%d\n",a/100%10,a%10);
    return 0;
}

3, Unit test

Topic 1: display ASCII code

#include <stdio.h>
int main()
{
    char c;
    scanf("%c",&c);
    printf("%d\n",c);
    return 0;
}

Topic 2: printing diamond

/*Basic version*/
#include<stdio.h>
int main()
{
    printf("   *\n");
    printf("  ***\n");
    printf(" *****\n");
    printf("*******\n");
    printf(" *****\n");
    printf("  ***\n");
    printf("   *\n");
}

/*Any layer version*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,i,j;
    scanf("%d",&n);//Enter the number of half layers. For example, the figure above is 4
    for(i=1-n;i<=n-1;i++){//What layer
        for(j=0;j<abs(i);j++)
           printf(" ");//Enter space first
        for(j=0;j<2*(n-abs(i))-1;j++)
           printf("*");//Enter * number again
        printf("\n");//Remember to wrap
    }
    return 0;
}

Note: the abs() absolute value function is used. i realize that the upper triangle i is negative, and the lower triangle i is positive. Therefore, the absolute value can be considered for symmetrical images.

Posted by aouriques on Fri, 20 Dec 2019 07:52:44 -0800