Chapter05

5.7

5.7.1

int CalculateY(int x) {

    if (x < -5) 
    {
        return -3 * x + 10;
    }
    else if(x >= -5 && x < 0)
    {
        return x;
    }
    else if(x == 0)
    {
        return 0;
    }
    else if(x > 0 && x <= 5)
    {
        return 2 * 2;
    }
    else
    {
        return 4 * x - 10;
    }
}

5.7.2

#include<stdio.h>
void BubbleSort(int* arr, int length);
void SelectionSort(int* arr, int length);
int main() {
	int nums[3];
	printf("请输入三个数:");
	for (int i = 0; i < 3; i++)
	{
		scanf_s("%d", &nums[i]);
	}

	BubbleSort(nums, 3);
	//SelectionSort(nums, 3);
	printf("降序排列的数字为:\n");
	for (int i = 2; i >=0; i--)
	{
		printf("%d  ", nums[i]);
	}
	printf("\n");

	return 0;
}

void BubbleSort(int* arr, int length)
{

	for (int i = 0; i < length - 1; i++)
	{
		for (int j = 0; j < length - (i + 1); j++) 
		{
			if (arr[j] > arr[j + 1]) {
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}

void SelectionSort(int* arr, int length)
{

	int i, j, min_index;
	for (int i = 0; i < length - 1; i++)
	{
		min_index = i;
		for (int j = i + 1; j < length; j++)
		{
			if (arr[j] < arr[min_index]) 
			{
				min_index = j;//更新最小值的索引
			}
		}

		if (min_index != i)
		{
			int temp = arr[i];
			arr[i] = arr[min_index];
			arr[min_index] = temp;
		}
	}

	
}

5.7.3

#include<stdio.h>
struct Student
{
	double score;
};
int main() {

	Student stu[20];

	//输入20个学生的成绩
	for (int i = 0; i < 20; i++)
	{
		scanf_s("%f", &stu[i].score);
	}

	int score1 = 0, score2 = 0, score3 = 0, score4 = 0, score5 = 0;
	for (int i = 0; i < 20; i++)
	{
		if (stu[i].score >= 90)
		{
			score1++;
		}
		else if(stu[i].score >= 80 && stu[i].score < 90)
		{
			score2++;
		}
		else if(stu[i].score >= 70 && stu[i].score < 80)
		{
			score3++;
		}
		else if(stu[i].score >= 60 && stu[i].score < 70)
		{
			score4++;
		}
		else
		{
			score5++;
		}

	}


	printf("90以上共有:%d人, 80~89共有:%d人,70~79共有:%d人, 60~69共有:%d人, 60以下共有:%d人", score1, score2, score3, score4, score5);

}

5.7.4

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAX_SIZE 1024
int main() {
	char input[MAX_SIZE];
	int letters = 0, digits = 0, others = 0;
	printf("请输入一串字符串:");
	fgets(input, sizeof(input), stdin);
	input[strcspn(input, "\n")] = 0;

	for (int i = 0; i < strlen(input); i++)
	{
		if (isalpha(input[i])) 
		{
			letters++;
		}
		else if(isdigit(input[i]))
		{
			digits++;
		}
		else
		{
			others++;
		}
	}

	printf("该字符串中,字母共有:%d个, 数字共有:%d个, 其他字符共有:%d个", letters, digits, others);
	return 0;
}

5.7.5/ 7.6/ 7.7省略(简单使用if-else语句即可)

5.7.8

#include <stdio.h>  

int areCollinear(int x1, int y1, int x2, int y2, int x3, int y3);

int main() {
    int x1, y1, x2, y2, x3, y3;

    // 输入三个点的坐标  
    printf("请输入第一个点的坐标 (x1, y1): ");
    scanf_s("%d %d", &x1, &y1);
    printf("请输入第二个点的坐标 (x2, y2): ");
    scanf_s("%d %d", &x2, &y2);
    printf("请输入第三个点的坐标 (x3, y3): ");
    scanf_s("%d %d", &x3, &y3);

    // 判断是否共线  
    if (areCollinear(x1, y1, x2, y2, x3, y3)) {
        printf("这三个点在一条直线上。\n");
    }
    else {
        printf("这三个点不在一条直线上。\n");
    }

    return 0;
}

/// <summary>
/// // 判断三个点是否共线
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <param name="x3"></param>
/// <param name="y3"></param>
/// <returns></returns>
int areCollinear(int x1, int y1, int x2, int y2, int x3, int y3) {
    // 检查分母是否为零以避免除以零错误  
    if ((x2 - x1) == 0) {
        // 如果x1 == x2,则检查y1是否等于y2和y3  
        if (x1 == x2 && x2 == x3) {
            return (y1 == y2 && y2 == y3);
        }
        else {
            return 0; // 不共线  
        }
    }

    if ((x3 - x2) == 0) {
        // 如果x2 == x3,则检查y2是否等于y1和y3(实际上只需检查y2 == y3因为x2 == x3)  
        return (x2 == x3 && y2 == y3); // 这里隐含y2 == y3,因为x2 == x3已经确认  
    }

    // 计算斜率并比较  
    if ((y2 - y1) * (x3 - x2) == (y3 - y2) * (x2 - x1)) {
        return 1; // 共线  
    }
    else {
        return 0; // 不共线  
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值