#include "Septinary.h"
#include<fstream>
Septinary::Septinary(int num) {
this->data = new char[100];
this->createSeptinary(num);
}
Septinary::~Septinary() {
if(data!= NULL)
delete[] data;
}
void Septinary::createSeptinary(int num) {
//********333********
int index=0;
char temp[100];
while(num){
temp[index++]=numberToChar(num%7); //这行为什么不能直接写成temp[index++]=num%7;
num/=7;
}
data[index]='\0';
int i=0;
while(index)
{
data[--index]=temp[i++];
}
//********666********
}
char Septinary::numberToChar(int num) { //numberToChar函数
if (num < 0 || num >7) {
cout << "error number" <<endl;
return 0;
}
return (char) (num + (char)'0'); //这行什么意思,没看懂,这是要返回一个什么呀?
}
void Septinary::show() {
cout << data <<endl;
}
int main() {
Septinary s(129);
s.show();
writeToFile("");
system("pause");
return 0;
}
void writeToFile(const char *path)
{
char filename[30];
strcpy(filename,path);
strcat(filename,"out.dat");
ofstream fout(filename);
Septinary s(129);
fout<<s.getData()<<endl;
Septinary s1(5);
fout<<s1.getData()<<endl;
fout.close();
}
temp[index++]的类型是Char
num%7的类型是int
两种不同的数据类型不能直接用等号
因为temp[index++]实际要存储的应该是数值的ASCII码,否则保存到文件或者打印时看不到你想要的结果