65,187
社区成员




#include<iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main(void)
{
long double sum = 0;
int h = 0;
ifstream in("a.txt");
if (in.good()) cout << "文件打开成功\n";
for (string s; getline(in, s);)
{
h++;
long double sum = 0;
if (s == "-1")
break;
int k = s.length();
//char a[s.length()];//好像vs2013不支持c99 可变长数组 不支持!!!!
char *a = (char *)malloc(k);
//strcpy(a, s);//error C2664: “char *strcpy(char *,const char *)”: 无法将参数 2 从“std::string”转换为“const char *”
cout <<"第"<<h<<"行的二进制位数为: " <<s.length() << endl;
cout << "数据如下 :\n";
cout << s << endl;
cout << "转换成的十进制数为: ";
const char *ss = s.c_str();
strcpy(a, ss);//已经将ss中的字符串赋值给了数组a[]
//cout << a[5]<< endl;
//*下面进行二进制转换*/
for (int i = s.length() - 1,j = 0; i > 0;--i,j++)
{
int b = a[i];
sum = sum + (b-48) * pow(2, j);
}
cout << sum << endl;
}
in.close();
}
不支持,但是c++的模板支持呀