在c中打印相同的行

时间:2014-10-28 08:48:28

标签: c printf

以下代码:

void day(int d);
void  amountOwed(double i);

#include <stdio.h>

int main(void){
    int x = 1;
    day(1);
    double amount = .01;
    amountOwed(amount);
}


void day(int d){
    printf("Day                  Amount Owed                    Total Paid");
    printf("\n---     ------------------------      ------------------------");
}

void amountOwed(double i){
   int a;
   float d;
   for (d = 1; d < 64; d++){
        printf("\n%2f",d);
    }
   for (a = 0; a < 64; a++){
       i *= 2;
       printf("\n  %30.2f", i);
    }
    printf("\n");
}

输出以下内容:

Day                  Amount Owed                    Total Paid
---     ------------------------      ------------------------
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000
11.000000
12.000000
13.000000
14.000000
15.000000
16.000000
17.000000
18.000000
19.000000
20.000000
21.000000
22.000000
23.000000
24.000000
25.000000
26.000000
27.000000
28.000000
29.000000
30.000000
31.000000
32.000000
33.000000
34.000000
35.000000
36.000000
37.000000
38.000000
39.000000
40.000000
41.000000
42.000000
43.000000
44.000000
45.000000
46.000000
47.000000
48.000000
49.000000
50.000000
51.000000
52.000000
53.000000
54.000000
55.000000
56.000000
57.000000
58.000000
59.000000
60.000000
61.000000
62.000000
63.000000
                            0.02
                            0.04
                            0.08
                            0.16
                            0.32
                            0.64
                            1.28
                            2.56
                            5.12
                           10.24
                           20.48
                           40.96
                           81.92
                          163.84
                          327.68
                          655.36
                         1310.72
                         2621.44
                         5242.88
                        10485.76
                        20971.52
                        41943.04
                        83886.08
                       167772.16
                       335544.32
                       671088.64
                      1342177.28
                      2684354.56
                      5368709.12
                     10737418.24
                     21474836.48
                     42949672.96
                     85899345.92
                    171798691.84
                    343597383.68
                    687194767.36
                   1374389534.72
                   2748779069.44
                   5497558138.88
                  10995116277.76
                  21990232555.52
                  43980465111.04
                  87960930222.08
                 175921860444.16
                 351843720888.32
                 703687441776.64
                1407374883553.28
                2814749767106.56
                5629499534213.12
               11258999068426.24
               22517998136852.48
               45035996273704.96
               90071992547409.92
              180143985094819.84
              360287970189639.69
              720575940379279.38
             1441151880758558.75
             2882303761517117.50
             5764607523034235.00
            11529215046068470.00
            23058430092136940.00
            46116860184273880.00
            92233720368547760.00
           184467440737095520.00

我有什么方法可以在同一行上打印这个声明吗?我试图在c中找到一种创造性的方法。无论如何我还要格式化下面的双日,所以它不打印为1.0000等等

2 个答案:

答案 0 :(得分:0)

除非我误解了这个问题,否则你只需要从格式字符串中省略\n

答案 1 :(得分:0)

这个怎么样?

for (d = 1; d < 64; d++){
        i *= 2;
        printf("\n%2f  %30.2f",d,i);
    }

也许它并不完美,因为你的for循环不会打印相同数量的行,但我认为你可以解决这个小小的差异。