C++文件转10进制
  • 板块灌水区
  • 楼主bo_rui2008
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/5/23 11:31
  • 上次更新2023/11/4 22:50:39
查看原帖
C++文件转10进制
383416
bo_rui2008楼主2021/5/23 11:31

输入格式:

从in.in文件读取两个整数,第一个是要转换的数,第二个是进制数

输出格式:

将结果输出到out.out里面去,(在一行内)从前到后是转换后各个位数上的数字(用空格分开)

输入样例:

13 2

输出样例:

1 1 0 1

我曾经尝试过,但是不成功,还请各位蒟蒻多多请教

代码如下:

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	FILE *fp,*tp; int a,b,c;
	if ( fp=fopen("in.in","r") ) {
	  if ( tp=fopen("out.out","w+") ) {
	    while ( !feof(fp) ) {
	      if ( fscanf(fp,"%d",&a)==1 ) { 
	         c=1; 
			 b=0; 
			 while(a){
			 b+=a%10*c; a/=10; c*=2; 
			 } 
			 fprintf(tp,"%d ",b);
	      }
	    }
	    fclose(tp);
	  }  printf("Can't Out out.out'\n");
	  fclose(fp);
	} 
	else printf("Can't Open in.in'\n");
	return 0;
}

输出结果是:

5 2

呜呜呜,快来人帮帮我撒

2021/5/23 11:31
加载中...