C语言作业

发布于 2023-03-28  154 次阅读


第一次作业

第一题

例4.8

#include<stdio.h>

int main()

{

    int year, leap;            //自定义和是否闰年

    printf("输入年份:");

    scanf("%d", &year);        //给变量year赋值

    if (year % 4 == 0)                    //判断年份是否被4整除,{}内为可整除后命令

    {

        if (year % 100 == 0)              //判断是否被100整除,{}内为可整除后命令

        {

             if (year % 400 == 0)          //年份被100整除,判断是否被400整除

                     leap = 1;            //年份被400整除

                 else leap = 0;            //年份不被400整除

        }

        else leap = 1;                    //年份不被100整除

    }

    else leap = 0;                        //年份不被4整除

    if (leap) printf("%d是闰年.\n", year);

    else      printf("%d不是闰年.\n",year);

例5.1

#include<stdio.h>

int main()

    {

    int i = 1, sum = 0;

        while (i <= 100)

        {

             sum = i + sum;

             i++;

        }

        printf("1+2+...+100=%d", sum);

    return 0;

    }

例5.2

#include<stdio.h>

int main()

    {

    int i = 1, sum = 0;

    do

    {

        sum = sum + i;

        i++;

    } while (i <= 100);

    printf("1-100总和为%d.\n", sum);

  }

例5.7

#include<stdio.h>

#include<math.h>

int main()

{

    double pi = 0, k = 1, n = 1;

    int z = 0;

    do

    {

        z = z + 1;

        pi = pi + k / n;

       k = -k;

        n = n + 2;

    } while (fabs(k / n) >= pow(10, -6));

    pi = pi * 4;

    printf("圆周率为%10.8f\n.\n并且循环体一共循环了%d次", pi,z);

}

例5.9

#include<stdio.h>

int main()

{

    int n,i;

    printf("输入一个大于3的数字:");

    scanf("%d",&n);

    for (i = 2; i <= n; i++)

        if (n % i == 0)break;

    if (i < n)printf("%d不是素数", n);

    else printf("%d是素数",n);

}

第二题

#include<stdio.h>

#include<math.h>                                  

#include<conio.h>                            //第二行与第三行引用含有cos的函数

int main(void)

{

    double m, n;                             //定义变量m与n,浮点计算,数据类型用“%lf”

    printf("需要计算的角度(弧度制)=");       //输入前要

    scanf("%lf.\n", &m);                     //输入需要计算的角度

    n = cos(m);                              //计算输入的角度的cos值

    printf("cos(%lf)是 %lf.\n", m, n);       //输出计算的角度的cos值

    return 0;

}

第三题

#include<stdio.h>

#include<math.h>                                  

int main(void)

{

    float d = 300000, p = 6000, r = 0.01, m;

    m = log10(p / (p - d * r)) / log10(1 + r);

    printf("还清需要月数为%.1f",m);

    return 0;

}

第二次作业

第一题

#include<stdio.h>

int main()

{

    double a, b, ave;

    printf("请输入两个数字且中间以空格隔开.\n");

    scanf("%lf %lf",&a,&b);

    ave = (a + b) / 2;

    printf("%lf和%lf的平均值为%lf", ave);

}

第二题

#include<stdio.h>

int main()

{

    printf("请依次输入abcdefg(支持小数).\n");

    double a, b, c, d, e, f, g, x;

    printf("a="); scanf("%lf.\n", &a);

    printf("b="); scanf("%lf.\n", &b);

    printf("c="); scanf("%lf.\n", &c);

    printf("d="); scanf("%lf.\n", &d);

    printf("e="); scanf("%lf.\n", &e);

    printf("f="); scanf("%lf.\n", &f);

    printf("g="); scanf("%lf.\n", &g);

    x = a + b * (c - d) / e * f - g;

    printf("a + b * (c - d) / e * f - g=%lf", x);

    return x;

}

第三题

#include <stdio.h>
int main()
{
    double i,j,a,b,c,d,e,f,g;
    scanf("%lf,%lf",&i,&j);
    a=i+1;
    b=j+1;
    c=i++;
    d=j++;
    e=++i;
    f=++j;
    g=(i++)+(++j);
    printf("i+1=%lf,j+1=%lf,i++=%lf,j++=%lf,++i=%lf,++j=%lf,(i++)+(++j)=%lf\n",a,b,c,d,e,f,g);
    return 0;
}

第四题

#include<stdio.h>

#include<math.h>

int main()

{

    printf("按提示输入对应数据,回车确认");

    double year, money, rate, interest;

    printf("存款金额为"); scanf("%lf.\n", &money);

    printf("存款年数为"); scanf("%lf.\n", &year);

    printf("年利率为"); scanf("%lf.\n", &rate);

    interest = (money * pow(1 + rate, year) - money)/10;

    printf("税前利息为%0.2f\n", interest);

    return 0;

}

第五题

#include <stdio.h>
int main()
{
    double c,f;
    scanf("%lf",&f);
    c=5*(f-32)/9;
    printf("c=%lf\n",c);
    return 0;
}

韩信点兵

#include<stdio.h>

#include<math.h>

int main()

{

    int n = 0;

    do {

        n = n + 1;

    }

    while (n%5!=1||n%6!=5||n%7!=4||n%11!=10);

    printf("韩信至少拥有的士兵数为%d",n);

    return 0;

}

第三次作业

实验4

#include<stdio.h>

int main()

{

    int a , b , c , d , k1, k2, k;

    printf("求最大值,请输入四个整数");

    scanf("%d%d%d%d", &a, &b, &c, &d);

    int max(int x, int y);

    k1 = max(a, b);

    k2 = max(c, d);

    k = max(k1, k2);

    printf("输入的四个数中最大的是 % d", k);

    return (k);

}

int max(int x, int y)

{

    int m ;

    if (x >= y) m = x;

    else m = y;

    return m;

}

第四次作业

例题3.4

#include<stdio.h>

#include<math.h>

int main()

{

    printf("请输入满足三角形三遍关系的三条边的长度:");

    double a, b, c, s, area;

    scanf("%lf%lf%lf", &a, &b, &c);

    s = (a + b + c) / 2;

    area = sqrt(s * (s - a) * (s - b) * (s - c));

    printf("三角形的面积为%f", area);

    return 0;

}

例题3.5

#include<stdio.h>

#include<math.h>

int main()

{

    double a, b, c;

    printf("请输入一元二次方程各系数a,b,c,求解");

    scanf("%lf%lf%lf", &a, &b, &c);

    double der = sqrt(b * b - 4*a*c);

    double x1 = (-b + der) / 2 * a,

           x2 = (-b - der) / 2 * a;

    printf("x1=%7.2f\nx2=%7.2f", x1, x2);

    return 0;

}

例题3.6

#include<stdio.h>

int main()

{

    double a = 1;

    printf("%f\n", a / 3);

    return 0;

}

例题3.7

#include<stdio.h>

int main()

{

    float a;

    a = 10000 / 3.0;

    printf("%f\n", a);

    return 0;

}

例题3.9

#include<stdio.h>

int main()

{

    char a, b, c;

    a = getchar();

    b = getchar();

    c = getchar();

    putchar(a);

    putchar(b);

    putchar(c);

    return 0;

}

例题3.10

#include<stdio.h>

int main()

{

    char c1, c2;

    c1 = getchar();

    c2 = c1 + 32;

    putchar(c2);

    putchar('.\n');

    return 0;

}

例题3.8

#include<stdio.h>

int main()

{

    char a = 'B', b = 'O', c = 'Y';

    putchar(a);

    putchar(b);

    putchar(c);

    return 0;

}

韩信点兵

#include<stdio.h>

#include<math.h>

int main()

{

    int n = 0;

    do {

        n = n + 1;

    }

    while (n%5!=1||n%6!=5||n%7!=4||n%11!=10);

    printf("韩信至少拥有的士兵数为%d",n);

    return 0;
}

实验四

#include<stdio.h>

int main()

{

    char a, b, c;

    scanf("%c %c %c", &a, &b, &c);

    int num1=0, num2=0, num3=0;

    num1 = int(a - '\0');

    num2 = int(b - '\0');

    num3 = int(c - '\0');

    printf("平均值为%d", (num1 + num2 + num3) / 3);

    return 0;

}

c语言判断是否为闰年的三种方法

第一种

#include<stdio.h>

int main()

{

    int year, leap;

    printf("输入年份:");

    scanf("%d", &year);

    if (year % 4 == 0)

    {

        if (year % 100 == 0)

        {

             if (year % 400 == 0)

                 leap = 1;

             else leap = 0;

        }

        else leap = 1;

    }

    else leap = 0;

    if (leap) printf("%d是闰年.\n", year);

    else printf("%d不是闰年.\n", year);

    return 0;

}

第二种

#include<stdio.h>

int main()

{

    int x, y, z;

    printf("请输入一个年份");

    scanf("%d", &x);

    z = x % 4;

    if (0 == x % 100) z = z + 1;

    y = x % 400;

    switch (y)

    {

    case 0:printf("%d是闰年", x); break;

    default:

       {

        switch (z)

        {

        case 0:printf("%d是闰年", x); break;

        case 1:printf("%d不是闰年", x); break;

        default:printf("%d不是闰年", x); break;

        }

       }

    }

}

第三种

#include<stdio.h>

int main()

{

    int year, leap;

    printf("输入一个年份");

    scanf("%d", &year);

    if (year % 4 == 0 && year % 100 != 0 || (year % 400 == 0))

        leap = 1;

    else

        leap = 0;

    if (leap);

    printf("%d is leap year", year);

    printf("%d is not leap year", year);

    return 0; }

第四章例题作业

4.1

#include<stdio.h>

#include<math.h>

int main()

{

       double a = 0, b = 0, c = 0, disc = 0, x1, x2;

       printf("请分别输入方程的二次系数一次系数与常数,需要回车\n");

       scanf("%lf %lf %lf",&a,&b,&c);

       disc = pow(b, 2) - 4 * a * c;

       if (disc < 0)

       {

              printf("此方程无解\n");

       }

       else

       {

              x1 = ((-b / (2 * a)) + (sqrt(disc) / (2 * a)));

              x2 = ((-b / (2 * a)) - (sqrt(disc) / (2 * a)));

              printf("方程的解为%7.2f,%7.2f", x1, x2);

       }

       return 0;

}

4.2

#include<stdio.h>

Int main()

{

       float a,b,c;

       scanf (“%f,%f”,&a,&b);

       if(a>b)

       {

c=a;

a=b,

b=c;

}

printf(“%5.2f,%5.2f\n”,a,b);

return 0;

}

4.3

#include<stdio.h>

int main()

 {

    float a, b, c, d;

    scanf("%f,%f,%f", &a, &b, &c);

    if (a > b)

    {

        d = a, a = b, b = d;

    }

    if (a > c)

    {

        d = a, a = c, c = d;

    }

    if (b > c)

    {

        d = b, b = c, c = d;

    }

    printf("%5.2f,%5.2f,%5.2f", a, b, c);

    return 0;

 }

4.4

#include<stdio.h>

int main()

    {

    char ch;

    scanf(" % c", &ch);

    ch = (ch >= 'A' && ch <= 'Z') ? (ch + 32) : ch;

    printf("%c\n", ch);

    return 0;

    }

4.5

#include<stdio.h>

int main()

 {

       int x, y;

       scanf("%d", &x);

       if (x >= 0)

              if (x > 0)y = 1;

              else      y = 0;

       else          y = -1;

       printf("x=%d,y=%d\n", x, y);

       return 0;

 }

4.6

#include<stdio.h>

int main()

{

    char grade;

    scanf("%c", &grade);

    printf("你的分数为:");

    switch (grade)

    {

    case'A':printf("85~100\n"); break;

    case'B':printf("70~84\n"); break;

    case'C':printf("60~69\n"); break;

    case'D':printf("<60\n"); break;

    default:printf("输错啦!!!\n"); break;

    }

    return 0;

}

4.8

#include<stdio.h>

int main()

{

    int year, leap;

    printf("输入年份:");

    scanf("%d", &year);

    if (year % 4 == 0)

    {

        if (year % 100 == 0)

        {

             if (year % 400 == 0)

                 leap = 1;

             else leap = 0;

        }

        else leap = 1;

    }

    else leap = 0;

    if (leap) printf("%d是闰年.\n", year);

    else printf("%d不是闰年.\n", year);

4.9

#include<stdio.h>

#include<math.h>

int main() {

    int a, b, c;

    printf("请输入:");

    scanf("%d%d%d", &a, &b, &c);

    double delta, x1, x2, m, n;

    delta = b * b - 4 * a * c;

    if (delta > 0) {

        x1 = (-b + sqrt(delta)) / (2 * a);

        x2 = (-b - sqrt(delta)) / (2 * a);

        printf("原方程的的解为x1=%f,x2=%f\n", x1, x2);

    }

    if (delta == 0) {

        x1 = (-b) / (2 * a);

        x2 = x1;

        printf("原方程的的解为x1=%f,x2=%f\n", x1, x2);

    }

    if (delta < 0) {

        m = (-b) / (2 * a);

        n = sqrt(-delta) / (2 * a);

        printf("原方程的的解为:x1=%8.4f+%8.4fi ; x2=%8.4f-%8.4fi\n ", m, n, m, n);

    }

    return 0;

}

4.10

#include<stdio.h>

int main()

{

    int c, s;

    float p, w, d, f;

    printf("请输入单价,重量以及距离\n");

    scanf("%f,%f,%d", &p, &w, &s);

    if (s >= 3000)c = 12;

    else          c = s / 250;

    switch (c)

    {

    case 0:d = 0; break;

    case 1:d = 2; break;

    case 2:

    case 3:d = 5; break;

    case 4:

    case 5:

    case 6:

    case 7:d = 8;  break;

    case 8:

    case 9:

    case 10:

    case 11:d = 10; break;

    case 12:d = 15; break;

    }

    f = p * w * s * (1 - d / 100);

    printf("总运费为%10.2f\n", f);

    return 0;

}

第四章章末

第四题

#include<stdio.h>

int main()

{

    int a, b, c, x, y, d;

    printf("请分别输入三个整数");

    scanf("%d %d %d", &a, &b, &c);

    x = max(a, b);

    y = max(b, c);

    d = max(x, y);

    printf("最大为%d", d);

    return 0;

}

int max(int x, int y)

{

    int m;

    if (x >= y) m = x;

    else m = y;

    return m;

}

第五题

#include<stdio.h>

#include<math.h>

int main()

{

    double x = 0;

    int y = 0, i = 0;

    for(i > 0;;)

    {

        printf("请输入一个小于1000的整数:");

        scanf("%lf", &x);

        if (x > 1000)

        {

             printf("请重新输入");

        }

        else

        {

             y = sqrt(x);

             printf("输入的数字开方为%d", y);

             return 0;

        }

    }

    return 0;

}

第六题

#include<stdio.h>

int main()

{

    double x = 0, y = 0;

    int i;

    printf("请输入x的值:\n");

    scanf("%lf", &x);

    if (x < 1)i = 0;

    else if (x >= 1 && x < 10)i = 1;

    else if (x >= 10)i = 2;

    switch (i)

    {

    case 0:printf("y=%5.2lf", y = (x)); break;

    case 1:printf("y=%5.2lf", y = (2 * x - 1)); break;

    case 2:printf("y=%5.2lf", y = (3 * x - 11)); break;

    }

    return 0;

}

第八题

#include<stdio.h>

int main()

{

    int x = 0, i = 0;

    printf("请输入你的分数");

    scanf("%d", &x);

    if (x >= 90 && x <= 100)i = 1;

    else if (x >= 80 && x <= 89)i = 2;

    else if (x >= 70 && x <= 79)i = 3;

    else if (x >= 60 && x <= 69)i = 4;

    switch (i)

    {

    case 0:printf("等级为E"); break;

    case 1:printf("等级为A"); break;

    case 2:printf("等级为B"); break;

    case 3:printf("等级为C"); break;

    case 4:printf("等级为D"); break;

    }

    return 0;

}

第九题

#include<stdio.h>

#include<math.h>

int main()

{

    int x = 0, y = 0, z = 0, t = 0, m = 0, i = 0;

    printf("请输出最高为五位的数字:");

    scanf("%d", &x);

    z = x;

    t = x;

    do

    {

        z = z / 10;

        i++;

    } while (z != 0);

    switch (i)

    {

    case 1:printf("为个位数\n");

        break;

    case 2:printf("为十位数\n");

        break;

    case 3:printf("为百位数\n");

        break;

    case 4:printf("为千位数\n");

        break;

    case 5:printf("为万位数\n");

        break;

    default:

        printf("你小子是不是输入了大于五位的数?\n");        //算位数

        return 0;

    }

    printf("输入的数字逐个输出为:\n");

    do

    {

        i--;

        m = t / pow(10, i);

        printf("%d\n", m);

        t = t - m * pow(10, i);

    } while (t != 0);

    printf("这个数字逆序输出为:");

    do

    {

        y = x % 10;

        printf("%d", y);

        x = x / 10;

    } while (x != 0);                                      //逆序输出

    return 0;

}

第十题

第一种方法

# include <stdio.h>

int main()

{

    int i, branch;

    double bonus, bon1, bon2, bon4, bon6, bon10;

    bon1 = 100000 * 0.1;

    bon2 = bon1 + 100000 * 0.075;

    bon4 = bon2 + 100000 * 0.05;

    bon6 = bon4 + 100000 * 0.03;

    bon10 = bon6 + 400000 * 0.015;

    printf("请输入当月利润i:");

    scanf("%d", &i);

    printf("i=%d\n", i);

    branch = i / 100000;

    if (branch > 10) {

        branch = 10;

    }

    switch (branch) {

    case 0:bonus = i * 0.1; break;

    case 1:bonus = bon1 + (i - 100000) * 0.075; break;

    case 2:

    case 3:bonus = bon2 + (i - 200000) * 0.05; break;

    case 4:

    case 5:bonus = bon4 + (i - 400000) * 0.03; break;

    case 6:

    case 7:

    case 8:

    case 9:bonus = bon6 + (i - 600000) * 0.015; break;

    case 10:bonus = bon10 + (i - 1000000) * 0.01;

    }

    printf("奖金是%10.2f\n", bonus);

    return 0;

}

第二种方法

#include <stdio.h>

int main()

{

       double I, salary = 0;

       printf("enter performance:");

       scanf_s("%lf", &I);

       if (I < 0) {

              printf("请输入一个正数\n");

              system("pause");

              return -1;

       }

       double salary1 = 100000 * 0.1;//10万的奖金

       double salary2 = (200000 - 100000) * 0.075 + salary1;//20万的奖金

       double salary3 = (400000 - 200000) * 0.05 + salary2;//40万的奖金

       double salary4 = (600000 - 400000) * 0.03 + salary3;//60万的奖金

       double salary5 = (1000000 - 600000) * 0.015 + salary4;//100万的奖金

       if (I <= 100000) {

              salary = I * 0.1;//小于100000按10%提成

       }else if (I > 100000 && I <= 200000) {

              salary = salary1 + (I - 100000) * 0.075;//多出10万的按比例计算,加上10w的奖金

       }else if (I > 200000 && I <= 400000) {

              salary = salary2 + (I - 200000) * 0.05;//多出20万的按比例计算,加上20w的奖金

       }else if (I > 400000 && I <= 600000) {

              salary = salary3 + (I - 400000) * 0.03;//多出40万的按比例计算,加上40w的奖金

       }else if (I > 600000 && I <= 1000000) {

              salary = salary4 + (I - 600000) * 0.015;//多出60万的按比例计算,加上60w的奖金

       }else if (I > 1000000){

              salary = salary5 + (I - 1000000) * 0.01;//多出100万的按比例计算,加上100w的奖金

       }

       printf("salary:%f\n", salary);

       system("pause");

       return 0;

}

第十一题

#include <stdio.h>

int main() {

    int a, b, c, d, t;

    printf("请输入4个整数:");

    scanf("%d%d%d%d", &a, &b, &c, &d);

    if (a > b) {

        t = a;

        a = b;

        b = t;

    }

    if (a > c) {

        t = a;

        a = c;

        c = t;

    }

    if (a > d) {

        t = a;

        a = d;

        d = t;

    }

    if (b > c) {

        t = b;

        b = c;

        c = t;

    }

    if (b > d) {

        t = b;

        b = d;

        d = t;

    }

    if (c > d) {

        t = c;

        c = d;

        d = t;

    }

    printf("将这四个数进行从小到大排序:%d,%d,%d,%d", a, b, c, d);

    return 0;

}

第十二题

#include<stdio.h>

#include<math.h>

int main()

{

    double x, y;

    int i = 0;

    printf("请先后输入坐标的(x,y),中间用空格隔开:");

    scanf("%lf %lf", &x, &y);

    if (

        (pow((x - 2), 2) - pow((y - 2), 2)) <= 1 ||

        (pow((x + 2), 2) - pow((y + 2), 2)) <= 1 ||

        (pow((x + 2), 2) - pow((y - 2), 2)) <= 1 ||

        (pow((x - 2), 2) - pow((y - 2), 2)) <= 1

        )i = 1;

    switch (i)

    {

    case 1:printf("该点高度为10m"); break;

    case 0:printf("该点高度为0m"); break;

    }

    return 0;

}