FAstril 2021-09-20 14:34 采纳率: 100%
浏览 19
已结题

为什么需要写成temp[index++]=numberToChar(num%7);,直接写temp[index++]=num%7;为什么不行呢?


#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();

}
  • 写回答

2条回答 默认 最新

  • 闭目鸽 2021-09-20 14:37
    关注

    temp[index++]的类型是Char
    num%7的类型是int
    两种不同的数据类型不能直接用等号

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月28日
  • 已采纳回答 9月20日
  • 创建了问题 9月20日

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试