Chapter03

习题3.4

c 复制代码
#include<stdio.h>
#include<limits.h>
int main() {
	
	int score[10] = { 0 }, min_score = INT_MAX, max_score = INT_MIN, sum_score = 0;
	double avg = 0.0;
	printf("请输入十个得分:\n");
	for (int i = 0; i < 10; i++)
	{
		scanf_s("%d", &score[i]);
		if (score[i] > min_score) {
			min_score = score[i];
		}
		if (score[i] < max_score) {
			max_score = score[i];
		}
		sum_score += score[i];//求和
	}
	 
	avg = (sum_score - min_score - max_score) / 8;


	printf("去掉最高分和最低分后平均分为: %f", avg);

	return 0;
}

习题3.5

c 复制代码
#include<stdio.h>
int main() {
	int num[20];//假设数字序列不超过20个
	int count = 0;
	int ch;
	printf("请输入数字序列: \n");
	while ((ch = fgetc(stdin)) != '\n') {
		scanf_s("%d", &num[count++]);
	}
	
	for (int  i = 1; i < count; i++)
	{
		printf("%d  ", num[i] - num[i - 1]);
	}

	return 0;
}

3.6

c 复制代码
#include <stdio.h>  
#include <string.h>  

#define MAX_SIZE 1024  

int main() {
    char input[MAX_SIZE];
    char max_char = '\0'; // 初始化为一个不可能在输入中出现的字符,或者简单地初始化为'\0'  

    printf("请输入一串字符串: ");
    if (fgets(input, sizeof(input), stdin) != NULL) {
        // 遍历字符串,找到ASCII值最大的字符  
        for (int i = 0; input[i] != '\0'; i++) {
            if (input[i] > max_char) {
                max_char = input[i];//更新最大字符
            }
        }

        // 打印ASCII值最大的字符  
        printf("ASCII值最大的字符是: %c\n", max_char);
    }
    else {
        printf("读取输入时出错。\n");
    }

    return 0;
}

3.7

c 复制代码
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define MAX_MAXSIZE 26

int main() {

	char str[26], times_uppercase = 0, times_lowercase = 0;

	printf("请输入一串字符串:\n");
	fgets(str, sizeof(str), stdin);
	str[strcspn(str, "\n")] = 0;//去除末尾多余换行符

	for (int i = 0; i < sizeof(str); i++)
	{
		if (isupper(str[i]))
		{
			times_uppercase++;
		}
		else if(islower(str[i]))
		{
			times_lowercase++;
		}
	}

	printf("大写字母出现的次数为:%d, 小写字母出现的次数为:%d", times_uppercase, times_lowercase);


	return 0;
}

3.8

c 复制代码
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#define MAX_MAXSIZE 1024 //假设输入的字符串长度不超过1024
bool isEmptyString(const char* str);

int main() {
	char input[MAX_MAXSIZE];
	printf("请输入一串字符串: \n");
	fgets(input, sizeof(input), stdin);
	input[strcspn(input, "\n")] = 0;//去除末尾的换行符

	

	if (isEmptyString(input)) {
		printf("你输入的是空串!");
	}
	else
	{
		printf("你输入的是:%s", input);
	}

	return 0;
}

bool isEmptyString(const char* str) {
	// 检查指针是否为NULL或者第一个字符是否为'\0'  
	return str == NULL || *str == '\0';
}

3.9

c 复制代码
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 1024
int main() {

	FILE* fp;
	char buffer[MAX_SIZE];
	fp = fopen("score.txt", "r");//以读的形式打开
	if (fp == NULL) {
		perror("文件score.txt打开失败!");
		return EXIT_FAILURE;
	}

	while (fgets(buffer, sizeof(buffer), fp) != NULL) 
	{
		fputs(buffer, fp);
	}
	fclose(fp);
	return EXIT_SUCCESS;
}

3.10

c 复制代码
#include<stdio.h>
#include<stdlib.h>
int main() {

	char ch;
	FILE* fp = fopen("test.txt", "w");//以写的方式打开
	if (fp == NULL) {
		perror("文件test.txt打开失败!");
		return EXIT_FAILURE;
	}
	
	while ((ch = fgetc(stdin)) != '\n') {
		fputc(ch, fp);
		fputc('\n', fp);//写入换行
	}

	fclose(fp);

	return EXIT_SUCCESS;
}
相关推荐
ACP广源盛139246256733 小时前
(ACP广源盛)GSV6172---MIPI/LVDS 信号转换为 Type-C/DisplayPort 1.4/HDMI 2.0 并集成嵌入式 MCU
c语言·开发语言·单片机·嵌入式硬件·音视频
im_AMBER4 小时前
算法笔记 09
c语言·数据结构·c++·笔记·学习·算法·排序算法
是苏浙7 小时前
零基础入门C语言之C语言内存函数
c语言·开发语言
闭着眼睛学算法9 小时前
【双机位A卷】华为OD笔试之【排序】双机位A-银行插队【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·javascript·c++·python·算法·华为od
AI柠檬10 小时前
C语言基于MPI并行计算矩阵的乘法
c语言·c++·算法
czy878747512 小时前
C语言实现观察者模式
c语言·观察者模式
代码雕刻家13 小时前
1.4.课设实验-数据结构-单链表-文教文化用品品牌2.0
c语言·数据结构
侯小啾13 小时前
【22】C语言 - 二维数组详解
c语言·数据结构·算法
qq_4798754313 小时前
Linux time function in C/C++【2】
linux·c语言·c++
yuuki23323314 小时前
【数据结构】双向链表的实现
c语言·数据结构·后端