Answers to Tan Haoqiang's C language design exercises (4)

Use the following scanf function to input data, so that a=3, b=7, x=8.5, y=71.82, c1='A', c2='a'. How to type on the keyboard?

#include<stdio.h>

int main()

{
    int a,b;
    float x,y;
    char c1,c2;
    scanf("a=%d b=%d",&a,&b);
    scanf("%f %e",&a,&y);
    scanf("%c%c",&c1,&c2);
    printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\n",a,b,x,y,c1,c2);
    return 0;
}

answer:


a=3 b=7
8.5 71.82Aa

Please write a program to translate "China" into a password. The password rule is: replace the original letter with the fourth letter after the original letter. For example, if the fourth letter after the letter "A" is "E", use "E" instead of "A". Therefore, "China" should be translated as "Glmre". Use the putchar function and printf function to output these 5 characters respectively to complete this program.


#include <stdio.h>

int main() {

    char text[] = "China"; // Original text

    char password[6]; // Array to store passwords, one more position is used to store the string terminator '\0'

    int shift = 4; //Number of letters to shift back

    for (int i = 0; i < 5; i++) {

        char originalChar = text[i]; // original character

        password[i] = shift + originalChar;

    }

    password[5] = '\0'; // Add string terminator

    printf("Use putchar function to output password: ");

    for (int i = 0; i < 5; i++) {

        putchar(password[i]);

    }

    putchar('\n');

    printf("Use printf function to output password: %s\n", password);

    return 0;

}

//以下是一个C程序,将"China"翻译成密码并使用 putchar 函数和 printf 函数输出这 5 个字符:

#include <stdio.h>

int main() {
    char text[] = "China"; // 原始文本
    char password[6]; // 存储密码的数组,多一个位置用于存储字符串结束符'\0'
    int shift = 4; // 后移的字母数

    for (int i = 0; i < 5; i++) {
        char originalChar = text[i]; // 原始字符

        // 判断字符是否是大写字母
        if (originalChar >= 'A' && originalChar <= 'Z') {
            password[i] = ((originalChar - 'A' + shift) % 26) + 'A';
        }
        // 判断字符是否是小写字母
        else if (originalChar >= 'a' && originalChar <= 'z') {
            password[i] = ((originalChar - 'a' + shift) % 26) + 'a';
        }
        // 如果不是字母,直接复制
        else {
            password[i] = originalChar;
        }
    }

    password[5] = '\0'; // 添加字符串结束符

    printf("使用 putchar 函数输出密码: ");
    for (int i = 0; i < 5; i++) {
        putchar(password[i]);
    }
    putchar('\n');

    printf("使用 printf 函数输出密码: %s\n", password);

    return 0;
}


//这个程序首先定义了一个原始文本 "China" 和一个存储密码的字符数组 password,然后使用循环对//每个字符进行后移操作,根据字符是大写字母、小写字母还是其他字符,进行相应的处理,最后输//出密码。

Assume the radius of the circle r=1.5 and the height of the cylinder h=3. Find the circumference, area of ​​the circle, surface area of ​​the sphere, volume of the sphere and volume of the cylinder. Use scanf to input data and output the calculation results. The output requires text description and two digits after the decimal point. Please write a c program.


Below is a C program that calculates the circumference and area of ​​circles and cylinders and the surface area and volume of a sphere for given radius and height. The program uses the `scanf` function to input radius and height values, then calculates and outputs the results:

```c
#include <stdio.h>
#include <math.h>

int main() {
    double r, h;
    const double pi = 3.14159265359;

    // Enter the radius of the circle and the height of the cylinder
    printf("Please enter the radius of the circle (r) and the height of the cylinder (h):");
    scanf("%lf %lf", &r, &h);

    // Calculate the circumference and area of ​​a circle
    double circle_circumference = 2 * pi * r;
    double circle_area = pi * pow(r, 2);

    // Calculate the surface area and volume of the sphere
    double sphere_surface_area = 4 * pi * pow(r, 2);
    double sphere_volume = (4.0 / 3.0) * pi * pow(r, 3);

    // Calculate the volume of the cylinder
    double cylinder_volume = circle_area * h;

    // Output the calculation result
    printf("The circumference of the circle is: %.2lf\n", circle_circumference);
    printf("The area of ​​the circle is: %.2lf\n", circle_area);
    printf("The surface area of ​​the sphere is :%.2lf\n", sphere_surface_area);
    printf("The volume of the sphere is: %.2lf\n", sphere_volume);
    printf("The volume of the cylinder is: %.2lf\n", cylinder_volume);

    return 0;
}

```

You can run this program, follow the prompts to enter the radius of the circle and the height of the cylinder, and the program will calculate and output the desired results.

Write a program and use the getchar function to read two characters into c1 and c2, and then use the putchar function and printf function to output these two characters respectively. Consider the following questions:

(1) Should variables c1 and c2 be defined as characters or integers? Or both?

(2) How to handle the request to output the ASCII code of c1 and c2 values? Use putchar function or printf function?

(3) Can integer variables and character variables be substituted for each other under any circumstances? like:

char c1,c2; and int c1,c2;

Are they unconditionally equivalent?


(1) Variables `c1` and `c2` can be defined as character type. Character variables are usually used to store a single character and can be declared with the `char` type.

```c
char c1, c2;
```

(2) To output the ASCII codes of `c1` and `c2`, you can use the `putchar` function or the `printf` function. Here are examples of both methods:

Use the `putchar` function:

```c
#include <stdio.h>

int main() {     char c1, c2;     printf("Please enter two characters:");     c1 = getchar();     c2 = getchar();

    


    putchar(c1);
    putchar(c2);

    printf("\nThe ASCII code of %c is: %d\n", c1, c1);
    printf("The ASCII code of %c is: %d\n", c2, c2);

    return 0;
}

```

Use the `printf` function:

```c
#include <stdio.h>

int main() {     char c1, c2;     printf("Please enter two characters:");     c1 = getchar();     c2 = getchar();

    


    printf("The ASCII code of %c is: %d\n", c1, c1);
    printf("The ASCII code of %c is: %d\n", c2, c2);

    return 0;
}

```

Both can output the ASCII code of characters.

(3) Integer variables and character variables cannot be substituted for each other in all cases. Although the `char` type is commonly used to represent characters in the C language, it is actually an integer type that can be used to store integer values, but it has a smaller range. The `int` type is used to represent integers, usually with a larger range.

In some cases, the `char` type can be implicitly converted to the `int` type, for example:

```c
char c = 'A';
int i = c; // char can be assigned to int

```

However, you need to pay attention to the range when explicitly converting the `int` type to the `char` type. If the value of `int` exceeds the range of the `char` type, truncation or unexpected results may result.

```c
int i = 1000;
char c = i; // Note: may cause

truncation```

Therefore, it is generally recommended to use the `char` type when dealing with characters, and the `int` type when dealing with integers. Depending on the situation, type conversion needs to be performed with caution.

Guess you like

Origin blog.csdn.net/qq_50942093/article/details/132981437