//import java.lang.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
//import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Overflow {
public static void main(String[] args)throws FileNotFoundException {
PrintStream ps=new PrintStream(new FileOutputStream("output.txt"));
System.setOut(ps);
System.out.println("Java");
factByte();
factShort();
factInt();
factLong();
factFloat();
factDouble();
writeCsv();
}
static ArrayList<String> type = new ArrayList<>();
static ArrayList<Integer> nn = new ArrayList<>();
// byte类型
static void factByte(){
int n = 1, max;
byte ans = 1; // n!
while (true) {
byte fact = 1;
for (int i = n; i > 1; i--) {
fact *= i;
}
if (Byte.MAX_VALUE / ans < n) {
max = n - 1;
break;
}
ans = fact;
//System.out.println(n+"!="+fact);
++n;
}
System.out.println("byte\t"+max+"!="+ans);
type.add("byte");
nn.add(max);
}
// short类型
static void factShort(){
int n = 1, max;
short ans = 1; // n!
while (true) {
short fact = 1;
for (int i = n; i > 1; i--) {
fact *= i;
}
if (Short.MAX_VALUE / ans < n) {
max = n - 1;
break;
}
ans = fact;
//System.out.println(n+"!="+fact);
++n;
}
System.out.println("short\t"+max+"!="+ans);
type.add("short");
nn.add(max);
}
// int类型
static void factInt(){
int n = 1, max;
int ans = 1; // n!
while (true) {
int fact = 1;
for (int i = n; i > 1; i--) {
fact *= i;
}
if (Integer.MAX_VALUE / ans < n) {
max = n - 1;
break;
}
ans = fact;
//System.out.println(n+"!="+fact);
++n;
}
System.out.println("int\t"+max+"!="+ans);
type.add("int");
nn.add(max);
}
// long类型
static void factLong(){
int n = 1, max;
long ans = 1; // n!
while (true) {
long fact = 1;
for (int i = n; i > 1; i--) {
fact *= i;
}
if (Long.MAX_VALUE / ans < n) {
max = n - 1;
break;
}
ans = fact;
//System.out.println(n+"!="+fact);
++n;
}
System.out.println("long\t"+max+"!="+ans);
type.add("long");
nn.add(max);
}
// float类型
static void factFloat(){
int n = 1, max;
float ans = 1; // n!
while (true) {
float fact = 1;
for (int i = n; i > 1; i--) {
fact *= i;
}
if (Float.MAX_VALUE / ans < n) {
max = n - 1;
break;
}
ans = fact;
//System.out.println(n+"!="+fact);
++n;
}
System.out.println("float\t"+max+"!="+ans);
type.add("float");
nn.add(max);
}
// double类型
static void factDouble(){
int n = 1, max;
double ans = 1; // n!
while (true) {
double fact = 1;
for (int i = n; i > 1; i--) {
fact *= i;
}
if (Double.MAX_VALUE / ans < n) {
max = n - 1;
break;
}
ans = fact;
//System.out.println(n+"!="+fact);
++n;
}
System.out.println("double\t"+max+"!="+ans);
type.add("double");
nn.add(max);
}
static void writeCsv()throws FileNotFoundException{
PrintWriter pw = new PrintWriter("table.csv");
StringBuilder sb = new StringBuilder();
int count = type.size();
sb.append("type");
sb.append(',');
sb.append("max n\n");
for(int i = 0; i < count; i++){
sb.append(type.get(i));
sb.append(',');
sb.append(nn.get(i));
sb.append('\n');
}
pw.write(sb.toString());
pw.close();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
设计一个实验,通过 n!的运算,了解不同数据类型在内部存储上的差别。用不同数据类型进行结果值的存放,分别获得不溢出的n 的最大值。 考虑: 1. C/C++ 的数值类型 int float double,修饰符 short long unsigned 带来的影响 2. Java 的 数值类型 整型 和 浮点 计算过程中注意类型不能 混用 3. 关注在不同的程序语言环境下,关键语句的差异。
资源详情
资源评论
资源推荐
收起资源包目录
Java和CC++对比实验报告.rar (3个子文件)
01-C.cpp 4KB
02-Java.java 5KB
对比实验报告.docx 203KB
共 3 条
- 1
春杪无蜩
- 粉丝: 17
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0