C语言程序设计作业2023/11/5

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

【实验过程】

//【例6-1】使用if语句模拟信号灯指挥车辆行驶

#include <stdio.h>

int main() {

    int iSignal;

    printf(“the Red Light is 0,the Green Light is 1\n”);

    scanf_s(“%d”, &iSignal);

    if (iSignal == 1) {

         printf(“the Light is green,cars can run\n”);

    }

    return 0;

}

【实验过程】

//【例6-2】完善if语句的使用

#include <stdio.h>

int main() {

    int iSignal;

    printf(“the Red Light is 0,the Green Light is 1\n”);

    scanf_s(“%d”, &iSignal);

    if (iSignal == 1)

         printf(“the Light is green,cars can run\n”);

    if (iSignal == 0)

         printf(“the Light is red,cars can run\n”);

    return 0;

}

【实验过程】

//【例6-3】使用if。。。else语句进行选择判断

#include <stdio.h>

int main() {

    int iNumber;

    printf(“Enter a number\n”);

    scanf_s(“%d”, &iNumber);

    if (iNumber)

         printf(“the value is true and the number is:%d\n”, iNumber);

    else

         printf(“the value is flase and the number is:%d\n”, iNumber);

    return 0;

}

【实验过程】

//【例6-4】使用if。。。else语句得到两个数的最大值

#include <stdio.h>

int main() {

    int iNumber1, iNumber2;

    printf(“please enter two numbers:\n”);

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

    if (iNumber1 > iNumber2)

         printf(“the bigger number is %d\n”, iNumber1);

    else

         printf(“the bigger number is %d\n”, iNumber2);

    return 0;

}

【实验过程】

//【例6-5】使用if。。。else语句模拟信号灯的决策过程

#include <stdio.h>

int main() {

    int iSignal;

    printf(“the Red Light is 0,\nthe Green Light is 1,\nthe Yellow Light is other number\n”);

    scanf_s(“%d”, &iSignal);

    if (iSignal == 1)

         printf(“the Light is green,cars can run\n”);

    if (iSignal == 0)

         printf(“the Light is red,cars can not run\n”);

    else

         printf(“the Light is yellow,cars are ready\n”);

    return 0;

}

【实验过程】

//【例6-6】使用else if语句编写屏幕菜单程序

#include <stdio.h>

int main() {

    int iSelection;

    printf(“—Menu—\n”);

    printf(“1 = Load\n”);

    printf(“2 = Save\n”);

    printf(“3 = Open\n”);

    printf(“other = Quit\n”);

    printf(“enter selection\n”);

    scanf_s(“%d”, &iSelection);

    if (iSelection == 1)

         printf(“Processing Load\n”);

    else if (iSelection == 2)

         printf(“Processing Save\n”);

    else if (iSelection == 3)

         printf(“Processing Open\n”);

    else

         printf(“Processing Quit\n”);

    return 0;

}

【实验过程】

//【例6-7】使用else if语句正确修改信号灯程序

#include <stdio.h>

int main() {

    int iSignal;

    printf(“the Red Light is 0,\nthe Green Light is 1,\nthe Yellow Light is other number\n”);

    scanf_s(“%d”, &iSignal);

    if (iSignal == 1)

         printf(“the Light is green,cars can run\n”);

    else if (iSignal == 0)

         printf(“the Light is red,cars can not run\n”);

    else

         printf(“the Light is yellow,cars are ready\n”);

    return 0;

}

【实验过程】

//【例6-8】使用if嵌套语句选择日程安排

#include <stdio.h>

int main() {

    int iDay = 0;

    int Monday = 1, Tuseday = 2, Wednesday = 3, Thurday = 4, Friday = 5, Saturday = 6, Sunday = 7;

    printf(“enter a day of week to get course:\n”);

    scanf_s(“%d”, &iDay);

    if (iDay > Friday)

         if (iDay == Saturday)

             printf(“Go shopping with friend\n”);

         else

             printf(“At home with families\n”);

    else

         if (iDay == Monday)

             printf(“Have a meeting in the company\n”);

         else

             printf(“Working with partner\n”);

    return 0;

}

【实验过程】

//【例6-9】使用条件运算符计算欠款金额

#include <stdio.h>

int main() {

    float fDues;

    float fAmount;

    int iOntime;

    char cChar;

    printf(“Enter dues amount:\n”);

    scanf_s(“%f”, &fDues);

    printf(“On Time?(y/n)\n”);

    getchar();

    cChar = getchar();

    iOntime = (cChar == ‘y’ ? 1 : 0);

    fAmount = iOntime ? fDues : (fDues * 1.1);

    printf(“the Amount is:%.2f\n”, fAmount);

    return 0;

}

【实验过程】

//【例6-10】使用switch语句输出分数段

#include <stdio.h>

int main() {

    char cGrade;

    printf(“please enter your grade\n”);

    scanf_s(“%c”,&cGrade,1);

    printf(“Grade is about:”);

    switch (cGrade) {

         case ‘A’:

             printf(“90-100\n”);

             break;

         case ‘B’:

             printf(“80-89\n”);

             break;

         case ‘C’:

             printf(“70-79\n”);

             break;

         case ‘D’:

             printf(“60-69\n”);

             break;

         case ‘F’:

             printf(“<60\n”);

             break;

         default:

         printf(“You enter the char is wrong!\n”);

    }

    return 0;

}

【实验过程】

//【例6-11】修改日程安排程序

#include <stdio.h>

int main() {

    int iDay = 0;

    printf(“enter a day of week to get course:\n”);

    scanf_s(“%d”, &iDay);

    switch (iDay) {

         case 1:

             printf(“Have a meeting in the company\n”);

             break;

         case 6:

             printf(“Go shopping with firends\n”);

             break;

         case 7:

             printf(“At home with Families\n”);

             break;

         default:

             printf(“Working with parner\n”);

             break;

    }

    return 0;

}

【实验过程】

//【例6-12】使用switch语句设计欢迎界面的菜单选项

#include <stdio.h>

#include <conio.h>

int main() {

    int n;

    printf(“\n\n\t1.开始游戏”);

    printf(“\t2.按键说明\n”);

    printf(“\t3.游戏规则”);

    printf(“\t4.退出\n\n”);

    printf(“\t 请选择[1 2 3 4]:[]\b\b”);

    scanf_s(“%d”, &n);

    switch (n){

         case 1:

             printf(“\n\t您选择了’1.开始游戏’选项”);

             break;

         case 2:

             printf(“\n\t您选择了’2.按键说明’选项”);

             break;

         case 3:

             printf(“\n\t您选择了’3.游戏规则’选项”);

             break;

         case 4:

             printf(“\n\t您选择了’4.退出’选项”);

             break;

    }

    return 0;

}

【实验过程】

//【例6-13】if。。。else语句和switch语句的综合应用

#include <stdio.h>

int main() {

    int iMonth = 0, iDay = 0;

    printf(“enter the month you want to know the days\n”);

    scanf_s(“%d”, &iMonth);

    switch (iMonth) {

         case 1:

         case 3:

         case 5:

         case 7:

         case 8:

         case 10:

         case 12:

             iDay = 31;

             break;

         case 4:

         case 6:

         case 9:

         case 11:

             iDay = 30;

             break;

         case 2:

             iDay = 28;

             break;

         default:

             iDay = -1;

             break;

    }

    if (iDay == -1)

         printf(“there is a error with you enter\n”);

    else

         printf(“2010.%d has %d days\n”, iMonth, iDay);

    return 0;

}

【实验过程】

//【例6-14】使用switch语句计算运输公司的计费

#include <stdio.h>

int main() {

    int iDiscount;

    int iSpace;

    int iSwitch;

    float fPrice, fWeight, fAllprice;

    printf(“enter the price,weight and space\n”);

    scanf_s(“%f%f%d”, &fPrice, &fWeight, &iSpace);

    if (iSpace > 3000)

         iSwitch = 12;

    else

         iSwitch = iSpace / 250;

    switch (iSwitch) {

         case 0:

             iDiscount = 0;

             break;

         case 1:

             iDiscount = 2;

             break;

         case 4:

         case 5:

         case 6:

         case 7:

             iDiscount = 8;

             break;

         case 8:

         case 9:

         case 10:

         case 11:

             iDiscount = 10;

             break;

         case 12:

             iDiscount = 15;

             break;

         default:

             break;

    }

    fAllprice = fPrice * fWeight * iSpace * (1 – iDiscount / 100.0);

    printf(“Allprice is :%.4f\n”, fAllprice);

    return 0;

}


评论

发表回复

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