《C语言程序设计》江宝钏著 实验三 2020.春.NBU.C语言


注:我写的代码有些要求超出题目需求,可适当减去部分功能

一、实验目的与要求

  1. 理解C语言表示逻辑量的方法,学会正确使用逻辑运算符和逻辑表达式。

  2. 掌握利用if结构实现选择结构的方法。

  3. 掌握利用switch结构实现多分支选择结构。

  4. 结合程序掌握一些简单的算法,学习调试程序。

  5. 实验4学时。


二、实验内容(基础实验)

编程题1

输入任意三个整数a,b,c,求3个数中的最大值和最小值。

code:

#include <stdio.h>
int main(int argc, char *argv[])
{
    int a, b, c, tmp_max, tmp_min;
    printf("请输入三个数:\n");
    scanf("%d%d%d", &a, &b, &c);
    tmp_max=(a >= b ? a : b);
    tmp_min = a + b - tmp_max;
    tmp_min=(tmp_min <= c ? tmp_min : c);
    tmp_max=(tmp_max >= c ? tmp_max : c);
    printf("最大值为:%d,最小值为:%d\n", tmp_max, tmp_min);
    return 0;
}

console:


编程题2

输入x,计算并输出下面分段函数f(x)的值(保留2位小数)。请调用sqrt函数求平方根,调用pow函数求幂。

\[f(x)=\begin{cases}(x+1)^2+2x+\frac{1}{x}(x<0)\\\sqrt{x}(x≥0)\\\end{cases} \]

*输入输出示例*(共运行3次)

Enter x:10
f(10.00) = 3.16
Enter x:-0.5
f(-0.50) = -2.75
Enter x:0
f(0.00) = 0.00

code:

#include <stdio.h>
#include <math.h> //包含pow();和sqrt();函数
int main(int argc, char* argv[])
{
	double x, y; //使用高精度
    for (int i = 1; i <= 3; i++)
    {
        printf("Enter x:\t"); //\t为了美观
        scanf("%lf", &x);
        if (x < 0)
        {
            y = pow((x + 1), 2) + 2 * x + (1.0 / x);
        }
        else
        {
            y = sqrt(x);
        }
        printf("f(%.2f)=\t%.2f\n", x, y);  //\t为了美观
    }
    return 0; 
}

console:


编程题3

有一函数:

\[y=\begin{cases}x\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (-5<x<0)\\x-1\ \ \ \ \ \ \ \ (x=0)\\x+1\ \ \ \ \ \ \ \ (0<x<10)\end{cases} \]

编写程序,要求用scanf函数输入x的值,输出y的值。

具体要求如下:

(1)用if语句实现分支,自变量和函数值均用实数类型。

(2)自变量用scanf函数输入,给一些输入提示。

(3)运行程序,输入x的值(分别为-5<x<0,x=0,0<x<10)三种情况,检查输出的y值是否正确。

code:

#include <stdio.h>
#include <conio.h> //包含getche();函数
int main(int argc, char* argv[])
{
	double x, y;
    char control; //设置控制器,用于控制函数计算的进行
    while (1)    
    {
        printf("Please Enter the Number of x: ");
        scanf("%lf", &x);
        if (x > -5 && x < 0)
        {
            y = x;
            printf("y= %.2f\n", y);
        }
        else if (x == 0)
        {
            y = x - 1;
            printf("y= %.2f\n", y);
        }
        else if (x > 0 && x < 10)
        {
        y = x + 1;
        printf("y= %.2f\n", y);
        }
        else
        {
        printf("N/A\n");
        }
        //进入循环后更改控制器的值,用于存放下次判断条件
        printf("Enter q to quit or other character to continue: \n");
        control=getche(); //使用getche();(带回显)或getch();(不带回显)
        printf ("\n");
        if (control =='q' || control=='Q')    break;
    }
    return 0; 
}

console:


编程题4

从键盘上输入一个字符,如果它是大写字母,则把它转换成小写字母输出;否则,直接输出。

code:

#include <stdio.h>
int main(int argc, char* argv[])
{
    char ch;
    printf("Please enter a letter:\n");
    ch=getchar();
    /******************************
    0x20表示16进制的20,'A'和'a'的ascii码相差十六进制的20,用0x20
    使用十六进制记忆英文字母的ascii码比较方便,'A'=0x41,'a'=0x61
    ******************************/
    if(ch>=65 && ch <=90) printf("%c\n",ch+0x20);  //判断大小写并转换
    else printf("%c\n",ch);
    //该if-else可改写为:?语句,使代码更简洁
    //示例:ch >= 65 && ch <= 90 ? printf("%c\n",ch+0x20) : printf("%c\n",ch);
    //示例未进行验证,仅供参考
    return 0;
}

console:


编程题5

编写程序实现如下功能:输入二个运算数(exp1,exp2)和一个运算符(op),计算表达式exp1 op exp2的值,其中op(格式)可以为+、-、*、/、%五个运算符中的任一种(提示:用switch语句实现)。

code:

#include <stdio.h>
int main(int argc, char* argv[])
{
    double exp1,exp2,tot;
    char op;
    scanf("%lf%c%lf",&exp1,&op,&exp2); 
    //注意此处输入需要紧贴,示例:2+3,三个字符间无空格,如有格式需要,可在scanf();中修改
    switch (op)
    {
    case '+':
        tot=exp1+exp2;
        printf("%.2f",tot);
        break;
    case '-':
        tot=exp1-exp2;
        printf("%.2f",tot);
        break;
    case '*':
        tot=exp1*exp2;
        printf("%.2f",tot);
        break;
    case '/':
        tot=exp1/exp2;
        printf("%.2f",tot);
        break;
    case '%':
        tot=(int)exp1%(int)exp2;
        printf("%d",(int)tot);
        break;
    default:
        printf("Can not match!\n");
        break;
    }
    return 0; 
}

console:


三、实验内容(提高实验)

编程题1

​ 某服装店经营成套服装的买卖,同时也允许单件销售。若一次购买不少于70套,则每套90元;若不足70套,则每套110元;只买上衣每件70元;只买裤子每条60元。请编写程序假设用户买入x件上衣和y条裤子,请编写程序计算相应的应付款是多少?
code:

#include <stdio.h>
int main(int argc, char* argv[]);
{
    double x,y,z;
    printf("请输入上衣和裤子的件数:\n");
    printf("上衣:\t\t件\b\b\b\b\b\b\b\b"); //\t和\b是为了美观
    scanf("%lf",&x);
    printf("裤子:\t\t件\b\b\b\b\b\b\b\b");
    scanf("%lf",&y);
    if (x>=y&&y>=70)
    {
        z=90*y+70*(x-y);
    }else
    if (y>=x&&x>=70)
    {
        z=90*x+60*(y-x);
    }else
    if (x>y&&y<70)
    {
        z=110*y+70*(x-y);
    }else
    if (y>x&&x<70)
    {
        z=110*x+60*(y-x);
    }
    printf("应付:%.2f(元)\n",z);
    return 0; 
}

console:


编程题2

编写程序实现:输入一个整数,判断它能否被3 、5 、7整除,同时输出如下信息。

(1)能同时被3 、5 、7整除;

(2)能同时被3、5、7中的任二个数整除;

(3)能被3、5、7中的任一个数整除;

(4)不能被3 、5 、7任一个数整除。

code:

//本例使用了自定义函数
#include <stdio.h>
int judge(int x, int y); //judge为自定义函数,int judge(int x, int y),返回值为1或0
int main(int argc, char* argv[])
{
    int Num_integer, totl;
    int a[3] = {0}; //定义长度为3的数组,并赋初值为0
    printf("请输入一个整数:\n");
    scanf("%d", &Num_integer);
    a[0] = judge(Num_integer, 3); 
    a[1] = judge(Num_integer, 5);
    a[2] = judge(Num_integer, 7);
    totl = a[0] + a[1] + a[2];
    if (totl == 0)
        printf("不能被3 、5 、7任一个数整除。\n");
    else if (totl == 1)
        printf("能被3、5、7中的任一个数整除。\n");
    else if (totl == 2)
        printf("能同时被3、5、7中的任二个数整除。\n");
    else
        printf("能同时被3 、5 、7整除。\n");
    return 0;
}
//judge函数用于提高实验的编程题2中,判断x是否能被y整除,能整除返回1,否则返回0
int judge(int x, int y)
{
    if (x % y == 0)
        return 1;
    else
        return 0;
}

console:


编程题3

请用switch语句编程计算员工每月的薪水。

已知某公司员工的保底薪水为500,某月所接工程的利润profit(整数)与提成的关系如下(计量单位:元):

profit≤1000     \ 没有提成;

1000<profit≤2000  提成10%;

2000<profit≤5000  提成15%;

5000<profit≤10000 \ 提成20%;

10000<profit    \ 提成25%。

分析:为使用switch语句,必须将利润profit与提成的关系,转换成某些整数与提成的关系。分析本题可知,提成的变化点都是1000的整数倍(1000、2000、5000、……),如果将利润profit整除1000,则相邻两个区间的重叠。为解决相邻两个区间的重叠问题,最简单的方法就是:利润profit先减1(最小增量),然后再整除1000即可

profit≤1000     \ 对应0

1000<profit≤2000  对应1

2000<profit≤5000  对应2、3、4

5000<profit≤10000 \ 对应5、6、7、8、9

10000<profit    \ 对应10、11、12、……

不听不听我不听

code:

#include <stdio.h>
int main(int argc, char* argv[])
{
    long int profit;
    int flag; //设标记区分薪水档次
    double salary;
    printf("请输入该员工该月所接工程的利润:\n");
    scanf("%ld",&profit);
    if (profit<=1000) flag=0;
    else if(profit>1000&&profit<=2000) flag=1;
    else if(profit>1000&&profit<=2000) flag=2;
    else if(profit>5000&&profit<=10000) flag=3;
    else if(profit>10000) flag=4;
    switch (flag) //根据不同档次计算薪水
    {
    case 1:
        salary=500+10*profit/100.0;
        printf("薪水为:%.2f\n",salary);
        break;
    case 2:
        salary=500+15*profit/100.0;
        printf("薪水为:%.2f\n",salary);
        break;
    case 3:
        salary=500+20*profit/100.0;
        printf("薪水为:%.2f\n",salary);
        break;
    case 4:
        salary=500+25*profit/100.0;
        printf("薪水为:%.2f\n",salary);
        break;
    default:
        salary=500;
        printf("薪水为:%.2f",salary);
        break;
    }
    return 0;
}

console:


编程题4

求一元二次方程ax2+bx+c=0的根。

这憨批题目也不说明白要求,怎么输入怎么输出,求得值的精确度,复数是否要求通通没有

code:

//利用了求根公式,可以输出复数根
#include <stdio.h>
#include <math.h>
int main(int argc, char* argv[])
{
    double a, b, c, delta, x1, x2, m, n, i, j;
    printf("请输入一元二次函数ax^2+bx+c=0中常数a,b,c的值:\n");
    scanf("%lf%lf%lf", &a, &b, &c);
    //1e-6(0.000001)叫做epslon,用来抵消浮点运算中因为误差造成的相等无法判断的情况,在此处配合fabs()可用于判定是否为零
    if (fabs(a) <= 1e-6) 
    {
        if (fabs(b) <= 1e-6) //到此则可判定a==0,b==0, 因此构不成方程式
            puts("不是方程式"); //或者printf("不是方程式");理论可行,博主未验证
        else
            printf("%.2lf", -c / b);
    }
    else
    {
        delta = b * b - 4 * a * c; //根的判别式
        m = -b / (2 * a);
        n = sqrt(fabs(delta)) / (2 * fabs(a)); //求根公式分解计算
        i = m + n;
        j = m - n;
        if (delta < 0) //判断是否为复根
            printf("x1 = %.2lf+%.2lfi\tx2 = %.2lf-%.2lfi\n", m, n, m, n); //输出复数根
        else
        {
            if (i == j) //判断两根是否相等
                printf("x1 = %.2lf\tx2 = %.2lf\n", i, i);  //直接输出相等的两根x1,x2
            else //两根不相等的情况
            {
                x1 = (i > j) ? i : j; //将较大根放x1输出
                x2 = (i > j) ? j : i; //将较小根放x2输出
                printf("x1 = %.2lf\tx2 = %.2lf\n", x1, x2);
            }
        }
    }
    return 0;
}

console:


*编程题5 (NBU OJ题目)

1206 水费问题

为了加强居民的节水意识,某地区制定了以下生活用水收费标准:每户每月用水未超过7吨时,每吨收费1.0元,并加收0.2元每吨的城市污水处理费;用水若超过7吨(含7吨),则每吨收费1.5元,并加收0.4元每吨的城市污水处理费。输入用水量并计算应交水费。(注:非分段计费)

NBUOJ 问题源地址

code:

#include <stdio.h>
int main(int argc, char* argv[])
{
    double water_use, fare;
    printf("请输入用水量(吨):\n"); //提交oj时删除该提示性输出语句
    scanf("%lf", &water_use);
    if (water_use < 7)
        fare = 1.0 * water_use + 0.2 * water_use;
    else if (water_use >= 7)
        fare = 1.5 * water_use + 0.4 * water_use;
    printf("应交水费:%.2f 元\n", fare); //提交oj时删除提示性字符
    return 0;
}

console:


*编程题6 (NBU OJ题目)

1060 五级制成绩评级
在学生成绩管理中,成绩经常需要在百分制与等级制之间进行转换。输入一个表示成绩的百分制分数,将其转换为对应的等级制并输出。 
A:[90-100] 
B:[80-89] 
C:[70-79] 
D:[60-69] 
E:[0-59]

NBUOJ问题源地址

code:

#include <stdio.h>
int main(int argc, char* argv[])
{
    int grades;
    scanf("%d", &grades);
    if (grades >= 90 && grades <= 100)
        printf("A\n");
    else if (grades >= 80 && grades <= 89)
        printf("B\n");
    else if (grades >= 70 && grades <= 79)
        printf("C\n");
    else if (grades >= 60 && grades <= 69)
        printf("D\n");
    else
        printf("E\n");
    return 0;
}

console:


*编程题7 (NBU OJ题目)

1218 正方形还是圆形

首先从键盘读入一个浮点数x,然后再读入一个小写字母(s或c),如果读入的字母是s,则计算并输出正方形面积(此时x作为边长);如果读入的字母是c,则计算并输出圆面积(此时x作为半径)。

NBUOJ问题源地址

code:

#include <stdio.h>
int main(int argc, char* argv)
{
    double r;
    char ch;
    double area;
    scanf("%lf%c", &r, &ch);
    if (ch == 's')
        area = r * r;
    else if (ch == 'c')
        area = pi * r * r;
    printf("%.2f\n", area);
    return 0;
}

console:


感谢收看!请多多支持!点个关注吧!!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM