黑马程序员技术交流社区
标题: C语言如何判断输入的正整数是否是整型? 负数没有实现.... [打印本页]
作者: tabor 时间: 2015-6-28 14:26
标题: C语言如何判断输入的正整数是否是整型? 负数没有实现....
/*
用户从键盘上输入两个整数,然后输出
*/
#include <stdio.h>
#include <math.h>
int main()
{
//定义两个整型变量, 2个跳转符
int num1 = 1, num2 = 1, jump=0, count =0;
long long sum = 0;
//开始输入
for(int j=0; j<2; j++)
{
//iuput char
do
{
//提示范围,并开始输入第n个数据
printf("%lld\n",INT64_MAX);
printf("Please input num%d:",j+1);
//初始化存储区
char a[2][30]=
{
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}
};
rewind(stdin);
count = 0;
jump = 0;
//开始输入数据,并一次将用户放入缓存的数据全部收取
for (int i=0; i<30; i++)
{
scanf("%c",&a[j]);
// a[j]=getchar();
printf("i = %d,a[%d][%d] = %c = \'\\%d\'\n",i,j,i,a[j],a[j]);
//回车后数据固定下来
if(a[j] == 10)
{
//提示输入的数据,是否符合要求
printf("亲,你一共输入了%d位数据!\n",i);
a[j]='0';
count = i;
//数据重置开始
for(int k=0; k<i; k++)
{
//已经输入的数据移到低位
a[j][29-k] = a[j][i-k-1];
//将移出位置0
a[j][i-k-1] = '0';
}
//退出赋值进程
i = 30;
sum = 0;
作者: tabor 时间: 2015-6-28 14:27
//当前字符串
printf("当前输入a[%d] = %s\n",j,a[j]);
//转换成正常的数据
for( int temp=0; temp<30;temp++)
{
a[j][temp] -= 48;
printf("转换后:\n");
printf("i = %d, a[%d][%d] = %d,%lld\n",temp,j,temp,a[j][temp],sum);
sum +=(long long)(a[j][temp]*(long long)pow(10,29-temp));
}
printf("sum = %010lld, count = %d\n",sum,count);
switch(count/10)
{
case 2 :
printf("你现在总共输入%d位,严重超范围!\n",count);
jump = 30;
printf("%d\n",jump-30);
break;
case 1:
printf("你现在总共输入%d位,超范围了!\n",count);
jump = 30;
printf("%d\n", jump - 30);
break;
//只有这时,才是正常的输入,并进行重置
case 0:
//提示是正常的
printf("范围正常,可以得到正常的输出!\n");
break;
default:
jump = 30;
printf("这样是不行的,输入的太大,都%d了!\n",count);
break;
}
}
}
//num大于整型最大同时位数不大于10
}while(sum > INT32_MAX || jump == 30);
if(j==0)
{
num1 = (int)sum;
printf("%010d\n",num1);
}
if(j==1)
{
num2 = (int)sum;
printf("%010d\n", num2);
}
}
//output
printf("num1 = %d, num2 = %d \n",num1,num2);
return 0;
}
作者: tabor 时间: 2015-6-28 14:29
怎么才能整个代码一次贴出来啊
作者: 18678927239 时间: 2015-6-28 15:41
没看懂 = = mark 求大神指教
作者: XiaoBaoMi 时间: 2015-6-28 15:44
楼主说的不错学习了
作者: tabor 时间: 2015-6-28 16:02
9223372036854775807
Please input num1:357493275942759274536546
i = 0,a[0][0] = 3 = '\51'
i = 1,a[0][1] = 5 = '\53'
i = 2,a[0][2] = 7 = '\55'
i = 3,a[0][3] = 4 = '\52'
i = 4,a[0][4] = 9 = '\57'
i = 5,a[0][5] = 3 = '\51'
i = 6,a[0][6] = 2 = '\50'
i = 7,a[0][7] = 7 = '\55'
i = 8,a[0][8] = 5 = '\53'
i = 9,a[0][9] = 9 = '\57'
i = 10,a[0][10] = 4 = '\52'
i = 11,a[0][11] = 2 = '\50'
i = 12,a[0][12] = 7 = '\55'
i = 13,a[0][13] = 5 = '\53'
i = 14,a[0][14] = 9 = '\57'
i = 15,a[0][15] = 2 = '\50'
i = 16,a[0][16] = 7 = '\55'
i = 17,a[0][17] = 4 = '\52'
i = 18,a[0][18] = 5 = '\53'
i = 19,a[0][19] = 3 = '\51'
i = 20,a[0][20] = 6 = '\54'
i = 21,a[0][21] = 5 = '\53'
i = 22,a[0][22] = 4 = '\52'
i = 23,a[0][23] = 6 = '\54'
i = 24,a[0][24] =
= '\10'
亲,你一共输入了24位数据!
当前输入a[0] = 000000357493275942759274536546000000000000000000000000000000
转换后:
i = 0, a[0][0] = 0,0
转换后:
i = 1, a[0][1] = 0,0
转换后:
i = 2, a[0][2] = 0,0
转换后:
i = 3, a[0][3] = 0,0
转换后:
i = 4, a[0][4] = 0,0
转换后:
i = 5, a[0][5] = 0,0
转换后:
i = 6, a[0][6] = 3,0
转换后:
i = 7, a[0][7] = 5,-9223372036854775808
转换后:
i = 8, a[0][8] = 7,0
转换后:
i = 9, a[0][9] = 4,-9223372036854775808
转换后:
i = 10, a[0][10] = 9,-9223372036854775808
转换后:
i = 11, a[0][11] = 3,0
转换后:
i = 12, a[0][12] = 2,3000000000000000000
转换后:
i = 13, a[0][13] = 7,3200000000000000000
转换后:
i = 14, a[0][14] = 5,3270000000000000000
转换后:
i = 15, a[0][15] = 9,3275000000000000000
转换后:
i = 16, a[0][16] = 4,3275900000000000000
转换后:
i = 17, a[0][17] = 2,3275940000000000000
转换后:
i = 18, a[0][18] = 7,3275942000000000000
转换后:
i = 19, a[0][19] = 5,3275942700000000000
转换后:
i = 20, a[0][20] = 9,3275942750000000000
转换后:
i = 21, a[0][21] = 2,3275942759000000000
转换后:
i = 22, a[0][22] = 7,3275942759200000000
转换后:
i = 23, a[0][23] = 4,3275942759270000000
转换后:
i = 24, a[0][24] = 5,3275942759274000000
转换后:
i = 25, a[0][25] = 3,3275942759274500000
转换后:
i = 26, a[0][26] = 6,3275942759274530000
转换后:
i = 27, a[0][27] = 5,3275942759274536000
转换后:
i = 28, a[0][28] = 4,3275942759274536500
转换后:
i = 29, a[0][29] = 6,3275942759274536540
sum = 3275942759274536546, count = 24
你现在总共输入24位,严重超范围!
0
9223372036854775807
Please input num1:345
i = 0,a[0][0] = 3 = '\51'
i = 1,a[0][1] = 4 = '\52'
i = 2,a[0][2] = 5 = '\53'
i = 3,a[0][3] =
= '\10'
亲,你一共输入了3位数据!
当前输入a[0] = 000000000000000000000000000345000000000000000000000000000000
转换后:
i = 0, a[0][0] = 0,0
转换后:
i = 1, a[0][1] = 0,0
转换后:
i = 2, a[0][2] = 0,0
转换后:
i = 3, a[0][3] = 0,0
转换后:
i = 4, a[0][4] = 0,0
转换后:
i = 5, a[0][5] = 0,0
转换后:
i = 6, a[0][6] = 0,0
转换后:
i = 7, a[0][7] = 0,0
转换后:
i = 8, a[0][8] = 0,0
转换后:
i = 9, a[0][9] = 0,0
转换后:
i = 10, a[0][10] = 0,0
转换后:
i = 11, a[0][11] = 0,0
转换后:
i = 12, a[0][12] = 0,0
转换后:
i = 13, a[0][13] = 0,0
转换后:
i = 14, a[0][14] = 0,0
转换后:
i = 15, a[0][15] = 0,0
转换后:
i = 16, a[0][16] = 0,0
转换后:
i = 17, a[0][17] = 0,0
转换后:
i = 18, a[0][18] = 0,0
转换后:
i = 19, a[0][19] = 0,0
转换后:
i = 20, a[0][20] = 0,0
转换后:
i = 21, a[0][21] = 0,0
转换后:
i = 22, a[0][22] = 0,0
转换后:
i = 23, a[0][23] = 0,0
转换后:
i = 24, a[0][24] = 0,0
转换后:
i = 25, a[0][25] = 0,0
转换后:
i = 26, a[0][26] = 0,0
转换后:
i = 27, a[0][27] = 3,0
转换后:
i = 28, a[0][28] = 4,300
转换后:
i = 29, a[0][29] = 5,340
sum = 0000000345, count = 3
范围正常,可以得到正常的输出!
0000000345
9223372036854775807
Please input num2:7304523805794370243254
i = 0,a[1][0] = 7 = '\55'
i = 1,a[1][1] = 3 = '\51'
i = 2,a[1][2] = 0 = '\48'
i = 3,a[1][3] = 4 = '\52'
i = 4,a[1][4] = 5 = '\53'
i = 5,a[1][5] = 2 = '\50'
i = 6,a[1][6] = 3 = '\51'
i = 7,a[1][7] = 8 = '\56'
i = 8,a[1][8] = 0 = '\48'
i = 9,a[1][9] = 5 = '\53'
i = 10,a[1][10] = 7 = '\55'
i = 11,a[1][11] = 9 = '\57'
i = 12,a[1][12] = 4 = '\52'
i = 13,a[1][13] = 3 = '\51'
i = 14,a[1][14] = 7 = '\55'
i = 15,a[1][15] = 0 = '\48'
i = 16,a[1][16] = 2 = '\50'
i = 17,a[1][17] = 4 = '\52'
i = 18,a[1][18] = 3 = '\51'
i = 19,a[1][19] = 2 = '\50'
i = 20,a[1][20] = 5 = '\53'
i = 21,a[1][21] = 4 = '\52'
i = 22,a[1][22] =
= '\10'
亲,你一共输入了22位数据!
当前输入a[1] = 000000007304523805794370243254
转换后:
i = 0, a[1][0] = 0,0
转换后:
i = 1, a[1][1] = 0,0
转换后:
i = 2, a[1][2] = 0,0
转换后:
i = 3, a[1][3] = 0,0
转换后:
i = 4, a[1][4] = 0,0
转换后:
i = 5, a[1][5] = 0,0
转换后:
i = 6, a[1][6] = 0,0
转换后:
i = 7, a[1][7] = 0,0
转换后:
i = 8, a[1][8] = 7,0
转换后:
i = 9, a[1][9] = 3,-9223372036854775808
转换后:
i = 10, a[1][10] = 0,0
转换后:
i = 11, a[1][11] = 4,0
转换后:
i = 12, a[1][12] = 5,4000000000000000000
转换后:
i = 13, a[1][13] = 2,4500000000000000000
转换后:
i = 14, a[1][14] = 3,4520000000000000000
转换后:
i = 15, a[1][15] = 8,4523000000000000000
转换后:
i = 16, a[1][16] = 0,4523800000000000000
转换后:
i = 17, a[1][17] = 5,4523800000000000000
转换后:
i = 18, a[1][18] = 7,4523805000000000000
转换后:
i = 19, a[1][19] = 9,4523805700000000000
转换后:
i = 20, a[1][20] = 4,4523805790000000000
转换后:
i = 21, a[1][21] = 3,4523805794000000000
转换后:
i = 22, a[1][22] = 7,4523805794300000000
转换后:
i = 23, a[1][23] = 0,4523805794370000000
转换后:
i = 24, a[1][24] = 2,4523805794370000000
转换后:
i = 25, a[1][25] = 4,4523805794370200000
转换后:
i = 26, a[1][26] = 3,4523805794370240000
转换后:
i = 27, a[1][27] = 2,4523805794370243000
转换后:
i = 28, a[1][28] = 5,4523805794370243200
转换后:
i = 29, a[1][29] = 4,4523805794370243250
sum = 4523805794370243254, count = 22
你现在总共输入22位,严重超范围!
0
9223372036854775807
Please input num2:456
i = 0,a[1][0] = 4 = '\52'
i = 1,a[1][1] = 5 = '\53'
i = 2,a[1][2] = 6 = '\54'
i = 3,a[1][3] =
= '\10'
亲,你一共输入了3位数据!
当前输入a[1] = 000000000000000000000000000456
转换后:
i = 0, a[1][0] = 0,0
转换后:
i = 1, a[1][1] = 0,0
转换后:
i = 2, a[1][2] = 0,0
转换后:
i = 3, a[1][3] = 0,0
转换后:
i = 4, a[1][4] = 0,0
转换后:
i = 5, a[1][5] = 0,0
转换后:
i = 6, a[1][6] = 0,0
转换后:
i = 7, a[1][7] = 0,0
转换后:
i = 8, a[1][8] = 0,0
转换后:
i = 9, a[1][9] = 0,0
转换后:
i = 10, a[1][10] = 0,0
转换后:
i = 11, a[1][11] = 0,0
转换后:
i = 12, a[1][12] = 0,0
转换后:
i = 13, a[1][13] = 0,0
转换后:
i = 14, a[1][14] = 0,0
转换后:
i = 15, a[1][15] = 0,0
转换后:
i = 16, a[1][16] = 0,0
转换后:
i = 17, a[1][17] = 0,0
转换后:
i = 18, a[1][18] = 0,0
转换后:
i = 19, a[1][19] = 0,0
转换后:
i = 20, a[1][20] = 0,0
转换后:
i = 21, a[1][21] = 0,0
转换后:
i = 22, a[1][22] = 0,0
转换后:
i = 23, a[1][23] = 0,0
转换后:
i = 24, a[1][24] = 0,0
转换后:
i = 25, a[1][25] = 0,0
转换后:
i = 26, a[1][26] = 0,0
转换后:
i = 27, a[1][27] = 4,0
转换后:
i = 28, a[1][28] = 5,400
转换后:
i = 29, a[1][29] = 6,450
sum = 0000000456, count = 3
范围正常,可以得到正常的输出!
0000000456
num1 = 345, num2 = 456
上面是输入信息〜〜
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |