当对输入的数据进行大小排序有浮点数和整数,判断是浮点数还是整型数,按原型输出

本文介绍了一个使用C语言实现的简单程序,该程序通过冒泡排序法对用户输入的五个浮点数进行排序,并能区分并正确输出整数和非整数的浮点数。文章详细展示了如何进行数值的比较与转换,并讨论了如何优化输出过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>


int main()
{
int j = 0;
int i = 0;
int b = 0;
float c = 0;
float temp = 0;
float str[5] = {0};

printf("please input 5 numbers:\n");
for(i = 0; i <= 4; i++)
{
scanf("%f",&str[i]);
}
for(i = 1; i < 5; i++) //从第i轮进行排序
{
for(j = 0; j < 5 - i; j++) //从第一个数开始比较
{
if(str[j]>str[j + 1])
{
temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp; //将两个数进行交换
}
}
}

for(i = 0; i < 5; i++)
{
b = (int)str[i]; //将str[i]数转换为整型数
c = (float)b; //将转换后的整型数再转换为浮点数
//这里这样转换是因为浮点数转换为整型数的时候去掉了小数部分;再转换为浮点数的时候与原来的数是不相等的;

if(c == str[i]) //将整型转换为浮点数再与原来的数进行判断是否相等
{
printf("%d\t",(int)str[i]); //若是整数就转换为整数输出
}
else
{
printf("%1.1f\t",str[i]); //是浮点数就还是浮点数输出
}
}

printf("\n");

return 0;

}

自己觉得最后那个整数还是浮点数输出有点复杂化了,但是不懂该如何简化,希望有懂得人能够讲解一下

C#简单变色龙游戏 /// /// 创建一副52张的新牌 /// /// public static System.Collections.ArrayList Creat() { System.Collections.ArrayList list = new System.Collections.ArrayList(); for (int i = 3; i <= 6; i++) { for (int j = 1; j <= 13; j++) { Poker p = new Poker(); switch (j) { case 1: p = new Poker((PokerStyle)i, "A", j, 11); break; case 2: p = new Poker((PokerStyle)i, j.ToString(), j, 10); break; case 11: p = new Poker((PokerStyle)i, "J", j, 11); break; case 12: p = new Poker((PokerStyle)i, "Q", j, 2); break; case 13: p = new Poker((PokerStyle)i, "K", j, 3); break; default: p = new Poker((PokerStyle)i, j.ToString(), j, j); break; } list.Add(p); } } return list; } /// /// 洗牌 /// /// /// public static System.Collections.ArrayList shuffle(System.Collections.ArrayList list)//洗牌 { System.Random r = new Random(); for (int i = 0; i <= 7; i++) { int rightOrRight = r.Next(1, 3); Poker[] leftPoker = new Poker[list.Count / 2]; Poker[] rightPoker = new Poker[list.Count % 2 == 0 ? list.Count / 2 : list.Count / 2 + 1]; list.CopyTo(0, leftPoker, 0, list.Count / 2); list.CopyTo(list.Count / 2, rightPoker, 0, list.Count % 2 == 0 ? list.Count / 2 : list.Count / 2 + 1); if (rightOrRight == 1)//左边插右边 { list.Clear(); for (int j = 0; j <= leftPoker.Length - 1; j++) { list.Add(rightPoker[j]); list.Add(leftPoker[j]); } if (leftPoker.Length != rightPoker.Length) { list.Add(rightPoker[rightPoker.Length - 1]); } } else { list.Clear(); for (int j = 0; j <= leftPoker.Length - 1; j++) { list.Add(leftPoker[j]); list.Add(rightPoker[j]); } if (leftPoker.Length != rightPoker.Length) { list.Add(rightPoker[rightPoker.Length - 1]); } } } return list; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值