如何通过强制转换在C语言中进行总结?

时间:2019-03-15 13:10:51

标签: c

include <stdio.h>
include <math.h>

int main() 
{
  int inches;
  float feet, cm, yard, meter;

  printf("Enter value <Unit: inches>:");
  scanf("%d", &inches);

  printf("Convert into *\n");

  feet = inches/12;
  cm = inches * 2.54;
  yard = inches/36.0;
  meter = cm/100;

  printf("====================\n");
  printf("feet\t:%6.2f\n", feet);
  printf("cm\t\t:%6.2f\n", cm);
  printf("yard\t:%6.2f\n", yard);
  printf("====================\n \n");
  printf("총 %dinches are %dm and %dcm.", inches, (int) round (cm/100), (int) cm%100);
}

对不起,我不知道如何正确输入代码。我想将最后一个((int)cm%100)部分向上舍入,因为C转换将其转换为整数并向下舍入。我将如何总结呢?它没有显示在帖子中,但我确实包含了math.h和stdio.h

1 个答案:

答案 0 :(得分:4)

表达式 private void button7_Click(object sender, EventArgs e) { EncryptFile(); } public void EncryptFile() { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Office Files|*.doc;*.xls;*.ppt"; dialog.InitialDirectory = @"C:\"; dialog.Title = "Please select an office file to encrypt."; string fileName = ""; if (dialog.ShowDialog() == DialogResult.OK) { fileName = dialog.FileName; /// doing something } } 被分组为(int) cm % 100,它会截断((int) cm) % 100cm的编译将失败,因为cm % 100需要C语言中的整数参数。

使用%提取浮点模数,然后fmod(cm, 100)提取结果。这两个函数都位于round中,即

math.h

是解决办法。

但是,话虽如此,您可能会发现(int)round(fmod(cm, 100)) (int)round(cm) / 100在无法再使用(int)round(cm) % 100的情况下返回更明智的输出。

最后,检查负面输入的行为,我个人不允许这样做。