鱼C论坛

 找回密码
 立即注册
查看: 1236|回复: 18

[已解决]如何只用C语言做这个题目?

[复制链接]
发表于 2018-12-9 13:23:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
有一个喜欢装逼的学长,英语词汇量没到20个词还不会语法,却总想着拽两句英文,问个a+b都不会好好问,非用英文问。
这位学长特有的语法是,按位直接说英文,例如123他会念成one two three,123+456他会念one two three add four five six,结果是five seven nine
而1+2他会念one add two,结果是three
这位学长为了照顾英文不好的同学,他教大家学英语
0 zero
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine


输入
输入由1行字符串组成,形如题面描述(保证0<=a,b < 1000)


输出
按题面描述形式输出a+b的结果


样例输入
one two three add four five six
one add two
zero add one


样例输出
five seven nine
three
one


提示
能多组输入

原题网址:http://120.78.162.102/problem.php?cid=1412&pid=3
最佳答案
2018-12-9 22:41:18
彭尼玛 发表于 2018-12-9 17:47
是的……我那大妹子被要求必须用C语言…………
欸老哥,我临时这边有个问题想不通。。。
就是不明白, ...
  1. #include <stdio.h>
  2. #include <string.h>

  3. static const char *g_table[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
  4. static const size_t g_table_size = sizeof(g_table) / sizeof(g_table[0]);

  5. static size_t WordToNumber(const char *word)
  6. {
  7.         for(size_t i = 0; i < g_table_size; ++i)
  8.         {
  9.                 if(!strcmp(word, g_table[i]))
  10.                         return i;
  11.         }
  12.         return -1;
  13. }

  14. static const char *NumberToString(char *dest, size_t number)
  15. {
  16.         char buf[512];
  17.         sprintf(buf, "%d", number);

  18.         char *separator = "";
  19.         dest[0] = '\0';
  20.         for(size_t i = 0; buf[i]; ++i)
  21.         {
  22.                 strcat(dest, separator);
  23.                 strcat(dest, g_table[buf[i] - '0']);
  24.                 separator = " ";
  25.         }
  26.         return dest;
  27. }

  28. size_t GetWord(char *dest, const char *string)
  29. {
  30.         size_t count = 0;
  31.         while(string[count] && string[count] != ' ')
  32.                 ++count;

  33.         strncpy(dest, string, count);
  34.         dest[count] = '\0';
  35.         return count;
  36. }

  37. const char *GetResult(char *dest, const char *expression)
  38. {
  39.         size_t a = 0, b = 0;
  40.         char word[32];
  41.         size_t offset = 0;
  42.         size_t size;
  43.         while((size = GetWord(word, expression + offset)) && (offset += size + 1) && strcmp(word, "add"))
  44.         {
  45.                 a = a * 10 + WordToNumber(word);
  46.         }

  47.         while((size = GetWord(word, expression + offset)) && (offset += size + 1))
  48.         {
  49.                 b = b * 10 + WordToNumber(word);
  50.         }

  51.         return NumberToString(dest, a + b);
  52. }

  53. int main()
  54. {
  55.         char expression[512][512];
  56.         size_t size = 0;

  57.         while(fgets(expression[size], 512, stdin))
  58.         {
  59.                 expression[size][strlen(expression[size]) - 1] = '\0';        // 去掉'\n'
  60.                 ++size;
  61.         }

  62.         char dest[512];
  63.         for(size_t i = 0; i < size; ++i)
  64.         {
  65.                 puts(GetResult(dest, expression[i]));
  66.         }

  67.         return 0;
  68. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-12-9 14:16:15 | 显示全部楼层
必须是C语言?
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>

  5. static const std::vector<std::string>g_table = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

  6. static size_t WordToNumber(const std::string &word)
  7. {
  8.         for(auto iter = g_table.begin(); iter != g_table.end(); ++iter)
  9.         {
  10.                 if(*iter == word)
  11.                         return iter - g_table.begin();
  12.         }

  13.         return -1;
  14. }

  15. static const std::string NumberToString(size_t number)
  16. {
  17.         std::stringstream ss;
  18.         ss << number;

  19.         char ch;
  20.         std::string result;
  21.         std::string separator;
  22.         while(ss >> ch)
  23.         {
  24.                 result += separator + g_table[ch - '0'];
  25.                 separator = " ";
  26.         }

  27.         return result;
  28. }

  29. const std::string GetResult(const std::string &expression)
  30. {
  31.         std::stringstream ss;
  32.         ss << expression;

  33.         size_t a = 0, b = 0;
  34.         std::string word;
  35.         while(ss >> word && word != "add")
  36.         {
  37.                 a = a * 10 + WordToNumber(word);
  38.         }

  39.         while(ss >> word)
  40.         {
  41.                 b = b * 10 + WordToNumber(word);
  42.         }

  43.         return NumberToString(a + b);
  44. }

  45. int main()
  46. {
  47.         std::vector<std::string> expression;

  48.         std::string line;
  49.         while(std::getline(std::cin, line))
  50.                 expression.push_back(line);

  51.         for(auto const &i: expression)
  52.                 std::cout << GetResult(i) << std::endl;

  53.         return 0;
  54. }
复制代码

  1. one two three add four five six
  2. one add two
  3. zero add one
  4. ^Z
  5. five seven nine
  6. three
  7. one
  8. 请按任意键继续. . .
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-9 17:47:09 | 显示全部楼层

是的……我那大妹子被要求必须用C语言…………
欸老哥,我临时这边有个问题想不通。。。
就是不明白,为什么IEEE 754标准里的 阶码的偏移量是127。。。我看计算机组成原理的时候,前面说的是 移码=补码的符号位取反   或者用  真值+2^n次方 来表示
为什么一到IEEE 754这里就变成了  真值+2^n次方-1   这个-1是哪里来的??
还有就是
隐含位的问题,这是IEEE754规定的是吧,是为了让尾数的精度能提高一些吗?

我百度了很多,但是没有看到什么特别好的回答QAQ
求大佬帮帮孩子吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-9 22:41:18 | 显示全部楼层    本楼为最佳答案   
彭尼玛 发表于 2018-12-9 17:47
是的……我那大妹子被要求必须用C语言…………
欸老哥,我临时这边有个问题想不通。。。
就是不明白, ...
  1. #include <stdio.h>
  2. #include <string.h>

  3. static const char *g_table[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
  4. static const size_t g_table_size = sizeof(g_table) / sizeof(g_table[0]);

  5. static size_t WordToNumber(const char *word)
  6. {
  7.         for(size_t i = 0; i < g_table_size; ++i)
  8.         {
  9.                 if(!strcmp(word, g_table[i]))
  10.                         return i;
  11.         }
  12.         return -1;
  13. }

  14. static const char *NumberToString(char *dest, size_t number)
  15. {
  16.         char buf[512];
  17.         sprintf(buf, "%d", number);

  18.         char *separator = "";
  19.         dest[0] = '\0';
  20.         for(size_t i = 0; buf[i]; ++i)
  21.         {
  22.                 strcat(dest, separator);
  23.                 strcat(dest, g_table[buf[i] - '0']);
  24.                 separator = " ";
  25.         }
  26.         return dest;
  27. }

  28. size_t GetWord(char *dest, const char *string)
  29. {
  30.         size_t count = 0;
  31.         while(string[count] && string[count] != ' ')
  32.                 ++count;

  33.         strncpy(dest, string, count);
  34.         dest[count] = '\0';
  35.         return count;
  36. }

  37. const char *GetResult(char *dest, const char *expression)
  38. {
  39.         size_t a = 0, b = 0;
  40.         char word[32];
  41.         size_t offset = 0;
  42.         size_t size;
  43.         while((size = GetWord(word, expression + offset)) && (offset += size + 1) && strcmp(word, "add"))
  44.         {
  45.                 a = a * 10 + WordToNumber(word);
  46.         }

  47.         while((size = GetWord(word, expression + offset)) && (offset += size + 1))
  48.         {
  49.                 b = b * 10 + WordToNumber(word);
  50.         }

  51.         return NumberToString(dest, a + b);
  52. }

  53. int main()
  54. {
  55.         char expression[512][512];
  56.         size_t size = 0;

  57.         while(fgets(expression[size], 512, stdin))
  58.         {
  59.                 expression[size][strlen(expression[size]) - 1] = '\0';        // 去掉'\n'
  60.                 ++size;
  61.         }

  62.         char dest[512];
  63.         for(size_t i = 0; i < size; ++i)
  64.         {
  65.                 puts(GetResult(dest, expression[i]));
  66.         }

  67.         return 0;
  68. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-9 23:10:47 | 显示全部楼层
彭尼玛 发表于 2018-12-9 17:47
是的……我那大妹子被要求必须用C语言…………
欸老哥,我临时这边有个问题想不通。。。
就是不明白, ...

浮点数部分我需要研究研究
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-10 10:34:48 | 显示全部楼层

感动,谢谢,替我大妹子感谢你QAQ
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-15 15:03:48 | 显示全部楼层
彭尼玛 发表于 2018-12-9 17:47
是的……我那大妹子被要求必须用C语言…………
欸老哥,我临时这边有个问题想不通。。。
就是不明白, ...

目前我已经研究了差不多一半了,我还需要一些时间研究另外差不多一半的内容
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-16 11:04:21 | 显示全部楼层
人造人 发表于 2018-12-15 15:03
目前我已经研究了差不多一半了,我还需要一些时间研究另外差不多一半的内容

计算机组成原理,好多看得不是很懂的,现在开始学数据结构和算法QAQ
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-16 12:41:57 | 显示全部楼层
彭尼玛 发表于 2018-12-16 11:04
计算机组成原理,好多看得不是很懂的,现在开始学数据结构和算法QAQ

C语言学完了是吧?
建议先学汇编语言
有了C语言和汇编语言之后,你就可以任意选择了
如果愿意,再加上C++
^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-17 21:12:53 | 显示全部楼层
人造人 发表于 2018-12-16 12:41
C语言学完了是吧?
建议先学汇编语言
有了C语言和汇编语言之后,你就可以任意选择了

看完了C和C++ ,汇编准备先学完数据结构在那个
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-22 12:36:46 | 显示全部楼层
彭尼玛 发表于 2018-12-9 17:47
是的……我那大妹子被要求必须用C语言…………
欸老哥,我临时这边有个问题想不通。。。
就是不明白, ...

两个星期了,我已经失去耐心了,估计下面这两个程序再也不会被完善了,最多就是重写这两个程序了^_^
这两个程序的设计上面的问题限制了继续对其完善,也许还有办法,但是我选择放弃这两个程序

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-22 12:38:54 | 显示全部楼层
解析单精度浮点数的存储格式
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <iomanip>

  5. #define BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE
  6. #include <boost/multiprecision/cpp_int.hpp>

  7. static const std::string NumberToDecimalString(uint64_t num)
  8. {
  9.         std::stringstream ss;
  10.         ss << std::setprecision(0) << std::fixed << num;
  11.         return ss.str();
  12. }

  13. static size_t DecimalStringToNumber(const std::string &string)
  14. {
  15.         std::stringstream ss;
  16.         ss << string;

  17.         size_t result;
  18.         ss >> result;
  19.         return result;
  20. }

  21. static const std::string NumberToBinaryString(uint32_t num)
  22. {
  23.         std::string result;
  24.         for(uint32_t mask = 0x80000000; mask; mask >>= 1)
  25.         {
  26.                 if(mask & num)
  27.                         result += "1";
  28.                 else
  29.                         result += "0";
  30.         }
  31.         return result;
  32. }

  33. static size_t BinaryStringToNumber(std::string string)
  34. {
  35.         size_t result = 0;
  36.         size_t size = string.size();
  37.         for(auto iter = string.begin(); iter != string.end(); ++iter)
  38.         {
  39.                 if(*iter == '1')
  40.                 {
  41.                         size_t index = size - (iter - string.begin()) - 1;
  42.                         result += size_t(pow(2, index));
  43.                 }
  44.         }
  45.         return result;
  46. }

  47. static const std::string GetIntegerPart(const std::string binary_string)
  48. {
  49.         return NumberToDecimalString(BinaryStringToNumber(binary_string));
  50. }

  51. static const std::string GetDecimalPart(const std::string binary_string)
  52. {
  53.         using bigint = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<>>;

  54.         bigint value = 1;        // 随便一个数字,这里使用数字1;为了最后的字符串前面有0,例如0625
  55.         for(auto iter = binary_string.begin(); iter != binary_string.end(); ++iter)
  56.         {
  57.                 value *= 10;
  58.                 if(*iter == '1')
  59.                 {
  60.                         int index = iter - binary_string.begin();
  61.                         value += uint64_t(pow(2, -(index + 1)) * pow(10, index + 1));
  62.                 }
  63.         }

  64.         std::string num = value.str();
  65.         num.erase(num.begin());                // 去掉上面的那个'1'
  66.         while(!num.empty() && (*(num.end() - 1) == '0'))        // 删除末尾的'0'
  67.                 num.erase(num.end() - 1);

  68.         if(num == "")                // 不能全删掉
  69.                 num = "00";

  70.         return num;
  71. }

  72. const std::string ParseFloat(float num)
  73. {
  74.         std::string binary_string = NumberToBinaryString(*(uint32_t *)&num);
  75.         char sign = binary_string[0];
  76.         std::string exponent = std::string(binary_string.begin() + 1, binary_string.begin() + 9);
  77.         std::string fraction = std::string(binary_string.begin() + 9, binary_string.end());


  78.         int32_t e = BinaryStringToNumber(exponent);
  79.         if(e == 0xFF)        // INF or NaN
  80.         {
  81.                 if(BinaryStringToNumber(fraction))
  82.                         return "NaN";
  83.                 return "INF";
  84.         }
  85.         if(e == 0)        // 非正规化
  86.         {
  87.                 fraction.insert(fraction.begin(), '0');
  88.         }
  89.         else
  90.         {
  91.                 fraction.insert(fraction.begin(), '1');
  92.                 e -= 127;
  93.         }

  94.         size_t pos;
  95.         if(e >= 0)
  96.         {
  97.                 pos = e + 1;
  98.         }
  99.         else
  100.         {
  101.                 while(e++ < 0)
  102.                         fraction.insert(fraction.begin(), '0');
  103.                 pos = 1;
  104.         }

  105.         return GetIntegerPart(std::string(fraction.begin(), fraction.begin() + pos))
  106.                 + "." + GetDecimalPart(std::string(fraction.begin() + pos, fraction.end()));
  107. }

  108. int main()
  109. {
  110.         float num;

  111.         num = 3.1415926F;
  112.         std::cout << "0x" << std::hex << std::uppercase << *(uint32_t *)&num << ": " << ParseFloat(num) << std::endl;

  113.         num = 0.0F;
  114.         std::cout << "0x" << std::hex << std::uppercase << *(uint32_t *)&num << ": " << ParseFloat(num) << std::endl;

  115.         num = 0.005F;
  116.         std::cout << "0x" << std::hex << std::uppercase << *(uint32_t *)&num << ": " << ParseFloat(num) << std::endl;

  117.         num = 1234;
  118.         std::cout << "0x" << std::hex << std::uppercase << *(uint32_t *)&num << ": " << ParseFloat(num) << std::endl;

  119.         return 0;
  120. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-22 12:41:23 | 显示全部楼层
构造单精度浮点数的存储格式,这个代码还有问题,我知道为什么,但是却没办法修改它,这个代码太乱了,我实在是不想继续在其上面完善了
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <iomanip>
  5. #include <cmath>

  6. #define BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE
  7. #include <boost/multiprecision/cpp_int.hpp>

  8. using BigInteger = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<>>;

  9. static const std::string IntegerPartToBinaryString(const std::string &string, size_t bits)
  10. {
  11.         BigInteger num(string);
  12.         std::string result;

  13.         while(num)
  14.         {
  15.                 if(num % 2)
  16.                         result += "1";
  17.                 else
  18.                         result += "0";
  19.                 num /= 2;
  20.         }

  21.         std::reverse(result.begin(), result.end());
  22.         if(bits)
  23.         {
  24.                 while(result.size() != bits)
  25.                         result.insert(result.begin(), '0');
  26.         }
  27.         if(result.size() == 0)
  28.                 result += "0";
  29.         return result;
  30. }

  31. static const std::string RemovePrefixZero(const std::string &string)
  32. {
  33.         auto iter = string.begin();
  34.         for(; iter != string.end(); ++iter)
  35.         {
  36.                 if(*iter != '0')
  37.                         break;
  38.         }
  39.         if(iter == string.end())
  40.                 return std::string("0");
  41.         return std::string(iter, string.end());
  42. }

  43. static void BinaryCarry(std::string &string)
  44. {
  45.         for(auto iter = string.rbegin(); iter != string.rend(); ++iter)
  46.         {
  47.                 if(*iter == '0')
  48.                 {
  49.                         *iter = '1';
  50.                         break;
  51.                 }
  52.                 *iter = '0';
  53.         }
  54. }

  55. static const std::string DecimalPartToBinaryString(const std::string &string, size_t bits)
  56. {
  57.         BigInteger num = BigInteger(pow(10, string.size()));
  58.         num += BigInteger(RemovePrefixZero(string));

  59.         std::string result;
  60.         for(size_t i = 0; i < bits + 1; ++i)
  61.         {
  62.                 num *= 2;
  63.                 num -= BigInteger(pow(10, string.size()));

  64.                 BigInteger remainder = (num / BigInteger(pow(10, string.size()))) % 10;
  65.                 if(remainder > 1)
  66.                 {
  67.                         num -= BigInteger(pow(10, string.size()));
  68.                         result += "1";
  69.                 }
  70.                 else
  71.                 {
  72.                         result += "0";
  73.                 }
  74.         }

  75.         char ch = *(result.end() - 1);
  76.         result.erase(result.end() - 1);
  77.         if(ch == '1')
  78.         {
  79.                 BinaryCarry(result);
  80.         }

  81.         return result;
  82. }

  83. static const std::string GetFraction(const std::string &binary_integer_part, const std::string &binary_decimal_part)
  84. {
  85.         std::string fraction = binary_integer_part + binary_decimal_part;
  86.         fraction.erase(fraction.begin());
  87.         return fraction;
  88. }

  89. static const std::string GetExponent(size_t exponent, bool normalization)
  90. {
  91.         if(!normalization)
  92.                 return std::string("00000000");
  93.         BigInteger num = exponent + 127;
  94.         return IntegerPartToBinaryString(num.str(), 8);
  95. }

  96. static void SetBit(uint32_t &data, bool bit, size_t pos)
  97. {
  98.         data &= ~(1 << pos);
  99.         data |= bit << pos;
  100. }


  101. static uint32_t MakeFloat(bool sign, const std::string &exponent, const std::string &fraction)
  102. {
  103.         uint32_t num = 0;
  104.         SetBit(num, sign, 31);
  105.         for(int i = 30; i != 22; --i)
  106.                 SetBit(num, exponent[30 - i] == '1', i);
  107.         for(int i = 22; i >= 0; --i)
  108.                 SetBit(num, fraction[22 - i] == '1', i);
  109.         return num;
  110. }

  111. size_t ParseFloat(std::string num)
  112. {
  113.         std::string integer_part;
  114.         std::string decimal_part;

  115.         size_t pos = num.find('.');
  116.         if(pos == std::string::npos)
  117.         {
  118.                 integer_part = num;
  119.                 decimal_part = "0";
  120.         }
  121.         else
  122.         {
  123.                 integer_part = std::string(num.begin(), num.begin() + pos);
  124.                 decimal_part = std::string(num.begin() + pos + 1, num.end());
  125.         }

  126.         std::string binary_integer_part = IntegerPartToBinaryString(integer_part, 0);
  127.         std::string binary_decimal_part = DecimalPartToBinaryString(decimal_part, 23 - (binary_integer_part.size() - 1));

  128.         std::string exponent;
  129.         if(binary_integer_part == "0")
  130.                 exponent = GetExponent(binary_integer_part.size() - 1, false);        // 非正规化
  131.         else
  132.                 exponent = GetExponent(binary_integer_part.size() - 1, true);        // 正规化
  133.         std::string fraction = GetFraction(binary_integer_part, binary_decimal_part);
  134.         return MakeFloat(num[0] == '-', exponent, fraction);
  135. }

  136. int main()
  137. {
  138.         float num;

  139.         std::cout << std::hex << std::uppercase;

  140.         num = 3.14F;
  141.         std::cout << *(uint32_t *)&num << " -> " << ParseFloat("3.14") << std::endl;

  142.         num = 1234.0F;
  143.         std::cout << *(uint32_t *)&num << " -> " << ParseFloat("1234") << std::endl;

  144.         num = 3.1415926F;
  145.         std::cout << *(uint32_t *)&num << " -> " << ParseFloat("3.1415926") << std::endl;

  146.         num = 0.0F;
  147.         std::cout << *(uint32_t *)&num << " -> " << ParseFloat("0") << std::endl;

  148.         num = 0.005F;
  149.         std::cout << *(uint32_t *)&num << " -> " << ParseFloat("0.005") << std::endl;

  150.         return 0;
  151. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-22 12:47:04 | 显示全部楼层
本帖最后由 人造人 于 2018-12-22 13:35 编辑

https://www.jianshu.com/p/e5d72d764f2f
为什么一到IEEE 754这里就变成了  真值+2^n次方-1   这个-1是哪里来的??
因为要把 0000 0000 和 1111 1111 留出来给 “非正规化”
加127正好把这个数(正数)向上偏移了 “一半”,如果是负数,那就向下偏移
从中间向两边偏移,把两个极限值 0000 0000 和 1111 1111 留出来用于“非正规化”表示


更正:0000 0000用于“非正规化”,1111 1111用于Inf和NaN

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-22 12:50:11 | 显示全部楼层
隐含位的问题,这是IEEE754规定的是吧,是为了让尾数的精度能提高一些吗?
可以这么说,因为这个隐含位要么恒为1,要么恒为0,没有必要存储这一位,至于是恒为1,还是恒为0,取决于是“正规化”还是“非正规化”
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-22 12:59:53 | 显示全部楼层
差点忘了这个
这个是写上面那两个程序时的中间数据,也许有参考价值
  1. 0.14 * 2 = 0.28                0
  2. 0.28 * 2 = 0.56                0
  3. 0.56 * 2 = 1.12                1
  4. 0.12 * 2 = 0.24                0
  5. 0.24 * 2 = 0.48                0
  6. 0.48 * 2 = 0.96                0
  7. 0.96 * 2 = 1.92                1
  8. 0.92 * 2 = 1.84                1
  9. 0.84 * 2 = 1.68                1
  10. 0.68 * 2 = 1.36                1
  11. 0.36 * 2 = 0.72                0
  12. 0.72 * 2 = 1.44                1
  13. 0.44 * 2 = 0.88                0
  14. 0.88 * 2 = 1.76                1
  15. 0.76 * 2 = 1.52                1
  16. 0.52 * 2 = 1.04                1
  17. 0.04 * 2 = 0.08                0
  18. 0.08 * 2 = 0.16                0
  19. 0.16 * 2 = 0.32                0



  20. #include <stdio.h>

  21. int main()
  22. {
  23.         double num = 0.14;

  24.         for(size_t i = 0; i < 32; ++i)
  25.         {
  26.                 printf("%.4f * 2 = %.4f\t%d\n", num, num * 2, (num * 2 > 1) ? 1 : 0);
  27.                 num *= 2;
  28.                 if(num > 1)
  29.                         num -= 1;
  30.         }

  31.         return 0;
  32. }

  33. 0.1400 * 2 = 0.2800     0
  34. 0.2800 * 2 = 0.5600     0
  35. 0.5600 * 2 = 1.1200     1
  36. 0.1200 * 2 = 0.2400     0
  37. 0.2400 * 2 = 0.4800     0
  38. 0.4800 * 2 = 0.9600     0
  39. 0.9600 * 2 = 1.9200     1
  40. 0.9200 * 2 = 1.8400     1
  41. 0.8400 * 2 = 1.6800     1
  42. 0.6800 * 2 = 1.3600     1
  43. 0.3600 * 2 = 0.7200     0
  44. 0.7200 * 2 = 1.4400     1
  45. 0.4400 * 2 = 0.8800     0
  46. 0.8800 * 2 = 1.7600     1
  47. 0.7600 * 2 = 1.5200     1
  48. 0.5200 * 2 = 1.0400     1
  49. 0.0400 * 2 = 0.0800     0
  50. 0.0800 * 2 = 0.1600     0
  51. 0.1600 * 2 = 0.3200     0
  52. 0.3200 * 2 = 0.6400     0
  53. 0.6400 * 2 = 1.2800     1
  54. 0.2800 * 2 = 0.5600     0
  55. 0.5600 * 2 = 1.1200     1
  56. 0.1200 * 2 = 0.2400     0
  57. 0.2400 * 2 = 0.4800     0
  58. 0.4800 * 2 = 0.9600     0
  59. 0.9600 * 2 = 1.9200     1
  60. 0.9200 * 2 = 1.8400     1
  61. 0.8400 * 2 = 1.6800     1
  62. 0.6800 * 2 = 1.3600     1
  63. 0.3600 * 2 = 0.7200     0
  64. 0.7200 * 2 = 1.4400     1
  65. 请按任意键继续. . .


  66. 1 0 -1  -2  -3  -4  -5  -6  -7  -8  -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22
  67. 1 1. 0   0   1   0   0   0   1   1   1   1   0   1   0   1   1   1   0   0   0   0   1   1

  68. 2^-3 + 2^-7 + 2^-8 + 2^-9 + 2^-10 + 2^-12 + 2^-14 + 2^-15 + 2^-16 + 2^-21 + 2^-22

  69. 2^-1 = 0.5
  70. 2^-2 = 0.25
  71. 2^-3 = 0.125
  72. 2^-4 = 0.0625
  73. 2^-5 = 0.03125
  74. 2^-6 = 0.015625



  75. #include <stdio.h>
  76. #include <math.h>

  77. int main()
  78. {
  79.         char format[64];
  80.         for(int i = 1; i <= 32; ++i)
  81.         {
  82.                 sprintf(format, "2^-%d = %%.%dlf\n", i, i);
  83.                 printf(format, pow(2, -i));
  84.         }

  85.         return 0;
  86. }


  87. 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
  88. 0  0  1  0  0  0  1  1  1  1  0  1  0  1  1  1  0  0  0  0  1  1

  89. 2^-1 = 0.5
  90. 2^-2 = 0.25
  91. 2^-3 = 0.125
  92. 2^-4 = 0.0625
  93. 2^-5 = 0.03125
  94. 2^-6 = 0.015625
  95. 2^-7 = 0.0078125
  96. 2^-8 = 0.00390625
  97. 2^-9 = 0.001953125
  98. 2^-10 = 0.0009765625
  99. 2^-11 = 0.00048828125
  100. 2^-12 = 0.000244140625
  101. 2^-13 = 0.0001220703125
  102. 2^-14 = 0.00006103515625
  103. 2^-15 = 0.000030517578125
  104. 2^-16 = 0.0000152587890625
  105. 2^-17 = 0.00000762939453125
  106. 2^-18 = 0.000003814697265625
  107. 2^-19 = 0.0000019073486328125
  108. 2^-20 = 0.00000095367431640625
  109. 2^-21 = 0.000000476837158203125
  110. 2^-22 = 0.0000002384185791015625
  111. 2^-23 = 0.00000011920928955078125
  112. 2^-24 = 0.000000059604644775390625
  113. 2^-25 = 0.0000000298023223876953125
  114. 2^-26 = 0.00000001490116119384765625
  115. 2^-27 = 0.000000007450580596923828125
  116. 2^-28 = 0.0000000037252902984619140625
  117. 2^-29 = 0.00000000186264514923095703125
  118. 2^-30 = 0.000000000931322574615478515625
  119. 2^-31 = 0.0000000004656612873077392578125
  120. 2^-32 = 0.00000000023283064365386962890625
  121. 请按任意键继续. . .








  122. 0.0000000101000111101011100001010





  123. *************************************************************


  124. 0.005
  125. 005

  126. 1005




复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-28 11:05:29 | 显示全部楼层
人造人 发表于 2018-12-22 12:47
https://www.jianshu.com/p/e5d72d764f2f
为什么一到IEEE 754这里就变成了  真值+2^n次方-1   这个-1是哪 ...

谢谢,超感动QAQ,没想到仁兄还记得这个QAQ,哇,最近比较少登入鱼C,一上来就好感动,QAQ
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-28 12:17:05 | 显示全部楼层
彭尼玛 发表于 2018-12-28 11:05
谢谢,超感动QAQ,没想到仁兄还记得这个QAQ,哇,最近比较少登入鱼C,一上来就好感动,QAQ

当然记得
^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-28 19:07:50 | 显示全部楼层
刚入门中
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 22:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表