C语言程序设计作业2023/10/29

本次作业提交内容为第五章所有的例代码

【实验过程】

//【例5-1】使用putchar函数实现输出字符串“Hello”,并且在字符串输出完毕之后进行换行

#include <stdio.h>

int main() {

    char cChar1, cChar2, cChar3, cChar4;

    cChar1 = ‘H’;

    cChar2 = ‘e’;

    cChar3 = ‘l’;

    cChar4 = ‘o’;

    putchar(cChar1);

    putchar(cChar2);

    putchar(cChar3);

    putchar(cChar3);

    putchar(cChar4);

    putchar(‘\n’);

    return 0;

}

【实验过程】

//【例5-2】使用getchar函数实现字符数据输入

#include <stdio.h>

int main() {

    char cChar1;

    cChar1 = getchar();

    putchar(cChar1);

    putchar(‘\n’);

    getchar();

    putchar(getchar());

    putchar(‘\n’);

    return 0;

}

【实验过程】

//【例5-3】使用getchar函数取消获取回车符

#include <stdio.h>

int main() {

    char cChar1;

    cChar1 = getchar();

    putchar(cChar1);

    putchar(‘\n’);

    putchar(getchar());

    putchar(‘\n’);

    return 0;

}

【实验过程】

//【例5-4】使用字符串输出函数显示信息提示

#include <stdio.h>

int main() {

    const char* Char = “ILOVECHINA”;

    puts(“ILOVECHINA!”);

    puts(“I\0LOVE\0CHINA!”);

    puts(Char);

    Char = “ILOVE\0CHINA!”;

    puts(Char);

    return 0;

}

【实验过程】

//【例5-5】使用字符串输入函数gets获取输入信息

#include <stdio.h>

int main() {

    char cString[30];

    gets_s(cString);

    puts(cString);

    return 0;

}

【实验过程】

//【例5-6】使用printf函数输出字符花

#include <stdio.h>

#include <Windows.h>

HANDLE hOut;

void gotoxy(int x, int y){

    COORD pos;

    pos.X = x;

    pos.Y = y;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

}

int color(int c) {

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);

    return 0;

}

int main() {

    gotoxy(66, 11);

    color(12);

    printf(“(_)”);

    gotoxy(64, 12);

    printf(“(_)”);

    gotoxy(68, 12);

    printf(“(_)”);

    gotoxy(66, 13);

    printf(“(_)”);

    gotoxy(67, 12);

    color(6);

    printf(“@”);

    gotoxy(72, 10);

    color(13);

    printf(“(_)”);

    gotoxy(76, 10);

    printf(“(_)”);

    gotoxy(74, 9);

    printf(“(_)”);

    gotoxy(74, 11);

    printf(“(_)”);

    gotoxy(75, 10);

    color(6);

    printf(“@”);

    gotoxy(71, 12);

    printf(“|”);

    gotoxy(72, 11);

    printf(“/”);

    gotoxy(70, 13);

    printf(“\\|”);

    gotoxy(70, 14);

    printf(“`/”);

    gotoxy(70, 15);

    printf(“\\|”);

    gotoxy(71, 16);

    printf(“| /”);

    gotoxy(71, 17);

    printf(“|”);

    gotoxy(67, 17);

    color(10);

    printf(“\\\\\\\\”);

    gotoxy(73, 17);

    printf(“//”);

    gotoxy(67, 18);

    color(2);

    printf(“^^^^^^^^”);

}

【实验过程】

//【例5-7】使用scanf格式输入函数得到用户输入的数据

#include <stdio.h>

int main() {

    int iInt1, iInt2;

    puts(“Please enter two numbers:”);

    scanf_s(“%d%d”, &iInt1, &iInt2);

    printf(“The first is : %d\n”, iInt1);

    printf(“The second is : %d\n”, iInt2);

    return 0;

}

【实验过程】

//【例5-8】使用附加格式说明scanf函数的格式输入

#include <stdio.h>

int main() {

    long iLong;

    short iShort;

    int iNumber1 = 1;

    int iNumber2 = 2;

    char cChar[10];

    printf(“Enter the long integer\n”);

    scanf_s(“%ld”, &iLong);

    printf(“Enter the short integer\n”);

    scanf_s(“%hd”, &iShort);

    printf(“Enter the number\n”);

    scanf_s(“%d*%d”, &iNumber1, &iNumber2);

    printf(“Enter the string but only show three character\n”);

    scanf_s(“%3s”, cChar,10);

    printf(“the long interger is : %ld\n”, iLong);

    printf(“the short interger is : %hd\n”, iShort);

    printf(“the Number1 is : %d\n”, iNumber1);

    printf(“the Number2 is : %d\n”, iNumber2);

    printf_s(“the three chatacter are : %s\n”, cChar);

    return 0;

}

【实验过程】

//【例5-9】计算圆的面积

#include <stdio.h>

int main() {

    float Pie = 3.14f;

    float fArea;

    float fRadius;

    puts(“Enter the radius:”);

    scanf_s(“%f”, &fRadius);

    fArea = fRadius * fRadius * Pie;

    printf(“The Area is : %0.2f\n”, fArea);

    return 0;

}

【实验过程】

//【例5-10】将大写字母转换成小写字母

#include <stdio.h>

int main() {

    char cBig;

    char cSmall;

    puts(“Please enter capital character:”);

    cBig = getchar();

    puts(“Minuscule character is :”);

    cSmall = cBig + 32;

    printf(“%c\n”, cSmall);

    return 0;

}


评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注