为什么我的代码不能在float中打印答案?

时间:2017-08-11 09:14:48

标签: c

#include <stdio.h>
float div ( int a,int b, int c, float x );
int main() 
{ 
    int a,b,c,x;
    a=250;
    b=85;
    c=25;

    x=div (a,b,c,x);


   printf("%d ", x);

}
// your code goes here
float div (int a,int b, int c, float x) 
{
    x=((a-b)/c);
    return(x);

}

3 个答案:

答案 0 :(得分:0)

因为x被声明为int而不是float

您的代码:

int x;
...
printf("%d ", x);

您需要的是:

float x;
...
printf("%f ", x);

您的div功能也是错误的:

float div (int a,int b, int c, float x) 
{
    x=((a-b)/c);  // << this will perform an integer division
    return(x);    //    and therefore x will be truncated, even if it is a float
}

你需要这个:

float div (int a,int b, int c, float x) 
{
    x = ( ((float)(a-b) ) / c);
    return x;   
}

(float)之前的(a-b)被称为演员,并确保(a-b)float分割期间被c号作为x号。< / p>

BTW函数可以简化,你不需要float div (int a,int b, int c) { float x = ( ((float)(a-b) ) / c); return x; } 参数:

float div (int a,int b, int c) 
{
    return ((float)(a-b) ) / c;
}

甚至更简单:

using Microsoft.office.interop.word.

 var application = new Word.Application();
        string path = Path.GetDirectoryName(Path.GetFullPath("YourDocsName.doc"));
        object path_YourDocsName = path + @"\folder\YourDocsName.doc";

        object o = Missing.Value;
        object oFalse = false;
        object oTrue = true;

        Word._Application app = null;
        Word.Documents docs = null;
        Word.Document doc = null;

        try
        {
            app = new Word.Application();
            app.Visible = false;
            app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

            docs = app.Documents;
            doc = docs.Open(ref path_YourDocsName, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
            doc.Activate();           

            foreach (Word.Range range in doc.StoryRanges)
            {

                Word.Find find = range.Find;
                object findText = "[Todays date]";
                //  object findText = { "[Todays date]","[]" };
                object replacText = todaysdate;   //gets todays date and time to doc
                object replace = Word.WdReplace.wdReplaceAll;
                object findWrap = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref replaceThis, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap, ref o, ref replaceThisWith,
                    ref replace, ref o, ref o, ref o, ref o);

                Word.Find find1 = range.Find;
                object findText1 = "[doc content]";
                object replacText1 = Name;
                object replace1 = Word.WdReplace.wdReplaceAll;
                object findWrap1 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText1, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap1, ref o, ref replacText1,
                    ref replace1, ref o, ref o, ref o, ref o);

                Word.Find find2 = range.Find;
                object findText2 = "[doc content]";
                object replacText2 = Somestringyouneed;
                object replace2 = Word.WdReplace.wdReplaceAll;
                object findWrap2 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText2, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap2, ref o, ref replacText2,
                    ref replace2, ref o, ref o, ref o, ref o);

                Word.Find find3 = range.Find;
                object findText3 = "[Doc content]";
                object replacText3 = somesecondstringyouneed;
                object replace3 = Word.WdReplace.wdReplaceAll;
                object findWrap3 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText3, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap3, ref o, ref replacText3,
                    ref replace3, ref o, ref o, ref o, ref o);

                Word.Find find4 = range.Find;
                object findText4 = "[doc content]";
                object replacText4 = somesecondstringyouneed;
                object replace4 = Word.WdReplace.wdReplaceAll;
                object findWrap4 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText4, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap4, ref o, ref replacText4,
                    ref replace4, ref o, ref o, ref o, ref o);

                Word.Find find5 = range.Find;
                object findText5 = "[doc content]";
                object replacText5 = somesecondstringyouneed;
                object replace5 = Word.WdReplace.wdReplaceAll;
                object findWrap5 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText5, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap5, ref o, ref replacText5,
                    ref replace5, ref o, ref o, ref o, ref o);

                Word.Find find6 = range.Find;
                object findText6 = "[doc content]";
                object replacText6 = somesecondstringyouneed;
                object replace6 = Word.WdReplace.wdReplaceAll;
                object findWrap6 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText6, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap6, ref o, ref replacText6,
                    ref replace6, ref o, ref o, ref o, ref o);

                Word.Find find7 = range.Find;
                object findText7 = "[doc content]";
                object replacText7 = somesecondstringyouneed;
                object replace7 = Word.WdReplace.wdReplaceAll;
                object findWrap7 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText7, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap7, ref o, ref replacText7,
                    ref replace7, ref o, ref o, ref o, ref o);

                Word.Find find8 = range.Find;
                object findText8 = "[doc content]";
                object replacText8 = somesecondstringyouneed;
                object replace8 = Word.WdReplace.wdReplaceAll;
                object findWrap8 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText8, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap8, ref o, ref replacText8,
                    ref replace8, ref o, ref o, ref o, ref o);

                Word.Find find9 = range.Find;
                object findText9 = "[doc content]";
                object replacText9 = somesecondstringyouneed;
                object replace9 = Word.WdReplace.wdReplaceAll;
                object findWrap9 = Word.WdFindWrap.wdFindContinue;
                find.Execute(ref findText9, ref o, ref o, ref o, ref oFalse, ref o,
                    ref o, ref findWrap9, ref o, ref replacText9,
                    ref replace9, ref o, ref o, ref o, ref o);

                Marshal.FinalReleaseComObject(find);
                Marshal.FinalReleaseComObject(find1);
                Marshal.FinalReleaseComObject(find2);
                Marshal.FinalReleaseComObject(find3);
                Marshal.FinalReleaseComObject(find4);
                Marshal.FinalReleaseComObject(find5);
                Marshal.FinalReleaseComObject(find6);
                Marshal.FinalReleaseComObject(find7);
                Marshal.FinalReleaseComObject(find8);
                Marshal.FinalReleaseComObject(find9);
                Marshal.FinalReleaseComObject(range);
            }
            var path_YourDocsName = path + @"\folder\YourDocsName.doc";
            Console.WriteLine(path_YourDocsName);
            doc.SaveAs(path_YourDocsName);
            ((Word._Document)doc).Close(ref o, ref o, ref o);
            doc.Close();
            app.Quit(ref o, ref o, ref o);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.StackTrace);
        }
        finally
        {
            if (doc != null) Marshal.FinalReleaseComObject(doc);

            if (docs != null) Marshal.FinalReleaseComObject(docs);

            if (app != null) Marshal.FinalReleaseComObject(app);
        }

答案 1 :(得分:0)

仔细阅读声明

   x=((a-b)/c);

注意操作数,它们都是整数,因此执行整数除法,然后在赋值时将结果转换为浮点类型。你不希望这样。

如果你想要一个浮点除法,你需要确保其中一个操作数是float / double类型。

您可以使用强制转型,例如

  x=(((float)a-b)/c);

强制浮点运算。

也就是说,在main()中,您必须使用float%f的正确转化说明符。

答案 2 :(得分:-1)

打印出一个整数:

    printf("%f", x);

打印出一个浮点数:

X1.7_RC02.20170811110948

x也必须来自float