C语言程序设计:现代设计方法习题笔记《chapter5》上篇

第一题

题目分析:程序判断一个数的位数可以通过循环除以10求余,通过计算第一次与10求余为0的次数计算位数,由此可得示例1代码,另一种思路根据提示,可得示例2代码。

代码示例1:

cpp 复制代码
#include<stdio.h>

int main()
{
	printf("Enter a number: ");
	int number,temp;
	scanf_s("%d", &number);
	temp = number;
	int digit = 0;
	while (temp % 10 != 0)
	{
		digit += 1;
		temp = temp / 10;
	};
	printf("The number %d has %d digits", number, digit);
	return 0;
}

输出:

​​​​​​​ ​​​​​​​

代码示例2:

cpp 复制代码
#include<stdio.h>

int main()
{
	/*printf("Enter a number: ");
	int number,temp;
	scanf_s("%d", &number);
	temp = number;
	int digit = 0;
	while (temp % 10 != 0)
	{
		digit += 1;
		temp = temp / 10;
	};
	printf("The number %d has %d digits", number, digit);
	return 0;*/
	int number;
	printf("Enter a number: ");
	scanf_s("%d", &number);
	if (number >= 0 && number <= 9)
	{
		printf("The number %d has 1 digits", number);
	}
	else if (number >= 10 && number <= 99)
	{
		printf("The number %d has 2 digits", number);
	}
	else if (number >= 100 && number <= 999)
	{
		printf("The number %d has 3 digits", number);
	}
	else if (number >= 1000 && number <= 9999)
	{
		printf("The number %d has 4 digits", number);
	}
	else
	{
		printf("The number %d 输入位数多于4位", number);
	}
		return 0;
}

输出

​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​

第二题

题目分析:输入24进制小时制的时间,显示12小时的格式如果大于12时,减去12即可,如果小于12,原样显示,由此给出如下代码。

示例代码

cpp 复制代码
#include<stdio.h>

int main()
{
	printf("Enter a 24-hour time: ");
	int h, m;
	scanf_s("%d:%d", &h, &m);
	if (h>24)
	{
		printf("输入数据%d:%d不符合要求", h, m);
	}
	else if (h>12&&h<24)
	{
		printf("Equivalent 12-hour time: %d:%d PM", h - 12, m);
	}
	else if (h<0)
	{
		printf("输入数据%d:%d不符合要求", h, m);
	}
	else if (h==24)
	{
		printf("Equivalent 12-hour time: %d:%d AM", h - 24, m);
	}
	else
	{
		printf("Equivalent 12-hour time: %d:%d AM", h, m);
	}
	return 0;
}

输出

​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​

第三题

题目分析:这个题我的理解是基础33美元,然后每股加3美分或者2美分,将美分换算成美元进行计算,得到下列代码。

示例代码

cpp 复制代码
#include<stdio.h>

int main()
{
	float commission, value, per_price;
	int num;
	printf("Enter value of trade: ");
	scanf_s("%f%d", &per_price, &num);
	value = per_price * num;
	if (value<2500.00f)
	{
		commission = 30.00f + .017f * value;
	}
	else if (value<6250.00f)
	{
		commission = 56.00f + .0066f * value;
	}
	else if (value<20000.00f)
	{
		commission = 76.00f + .0034f * value;
	}
	else if (value<50000.00f)
	{
		commission = 100.00f + .0022f * value;
	}
	else if (value<500000.00f)
	{
		commission = 155.00f + .0011f * value;
	}
	else
	{
		commission = 255.00f + .0009f * value;
	}
	if (commission<39.00f)
	{
		commission = 39.00f;
	}
	printf("Commission: $%.2f\n", commission);

	//竞争对手
	float j_money;
	if (num < 20000)
	{
		j_money = 33.00f + 3.00f*num/100.00f;
	}
	else
	{
		j_money = 33.00f + 2.00f*num/100.00f;
	}
	printf("j_money: $%.2f\n", j_money);
	return 0;
}

输出

​​​​​​​ ​​​​​​​ ​​​​​​​

第四题

题目分析:简单的数值范围判断问题

示例代码

cpp 复制代码
#include<stdio.h>

int main()
{
	float wind_speed;
	printf("Eter the wind speed: ");
	scanf_s("%f", &wind_speed);
	if (wind_speed<1)
	{
		printf("Calm(无风)");
	}
	else if (wind_speed>=1&&wind_speed<=3)
	{
		printf("Light air(轻风)");
	}
	else if (wind_speed>=4&&wind_speed<=27)
	{
		printf("Breeze(微风)");
	}
	else if (wind_speed>=28&&wind_speed<=47)
	{
		printf("Gale(大风)");
	}
	else if (wind_speed>=48&&wind_speed<=63)
	{
		printf("Strom(暴风)");
	}
	else if (wind_speed > 63)
	{
		printf("Hurricane(飓风)");
	}
	return 0;

}

输出

​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​

第五题

题目分析:这个题主要是注意有印刷错误。

示例代码

cpp 复制代码
#include<stdio.h>

int main()
{
	printf("输入收入:");
	float salary, tax;
	scanf_s("%f", &salary);
	if (salary<=750)
	{
		tax = salary * .01;
	}
	else if (salary<=2250)
	{
		tax = 7.5 + (salary - 750) * .02;
	}
	else if (salary<=3750)
	{
		tax = 37.5 + (salary - 2250) * .03;
	}
	else if (salary<=5250)
	{
		tax = 82.50 + (salary - 3750) * .04;
	}
	else if (salary<=7000)
	{
		tax = 142.50 + (salary - 5250) * .05;
	}
	else
	{
		tax = 230.00 + (salary - 7000) * .06;
	}
	printf("应该交税$%.2f", tax);
	return 0;
}

输出

​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​ ​​​​​​​

相关推荐
Kagol11 小时前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
冬奇Lab12 小时前
OpenClaw 源码精读(2):Channel & Routing——一条消息如何找到它的 Agent?
人工智能·开源·源码阅读
冬奇Lab12 小时前
一天一个开源项目(第38篇):Claude Code Telegram - 用 Telegram 远程用 Claude Code,随时随地聊项目
人工智能·开源·资讯
sunny86513 小时前
Claude Code 跨会话上下文恢复:从 8 次纠正到 0 次的工程实践
人工智能·开源·github
strayCat232551 天前
Clawdbot 源码解读 7: 扩展机制
人工智能·开源
Moment1 天前
OpenClaw 从能聊到能干差的是这 50 个 Skills 😍😍😍
前端·后端·开源
草梅友仁1 天前
墨梅博客 1.7.0 发布与 AI 开发实践 | 2026 年第 9 周草梅周报
开源·github·ai编程
ursazoo2 天前
写了一份 7000字指南,让 AI 帮我消化每天的信息流
人工智能·开源·github
冬奇Lab2 天前
一天一个开源项目(第37篇):awesome-selfhosted - 自托管软件资源集合
开源·自动化运维·资讯
冬奇Lab2 天前
一天一个开源项目(第36篇):EverMemOS - 跨 LLM 与平台的长时记忆 OS,让 Agent 会记忆更会推理
人工智能·开源·资讯