#include <stdio.h>
#include <math.h>
int main(void)
{
// input value
int num, lower, upper;
double squareroot;
int square;
int cube;
printf("enter your number:\n");
scanf_s("%d", &num);
do
{
printf("the lower value limit is ");
scanf_s("%d", &lower);
} while (lower < 0 || lower > 50);
do
{
printf("the upper value limit is ");
scanf_s("%d", &upper);
} while (upper < 0 || upper > 50);
// the formular to find the squareroot, square, cube
squareroot = sqrt(num);
square = num * num;
cube = num * num * num;
//a for loop
for (num = 0; num <= upper; num ) {
printf("*base number* || *square root* || *square* || *cube*\n");
printf("*%d* || *%f* || *%ld* || *%ld*\n",
lower, squareroot, square, cube);
}
return 0;
}
我嘗試制作一個表格來顯示基數、平方根、平方和立方,并為表格設定一個限制。例如,如果我輸入小數是 1,大數是 5,那么表格將停在 5,然后顯示平方根、平方和立方
uj5u.com熱心網友回復:
至少這個問題:
不匹配的說明符/型別
int square;
int cube;
...
printf("*%d* || *%f* || *%ld* || *%ld*\n",
lower, squareroot, square, cube);
"%d"與 一起使用int,而不是"%ld"。
移動作業
以下分配需要在回圈內。
for (num = 0; num <= upper; num ) {
square = num * num;
cube = num * num * num;
省時間。啟用所有編譯器警告
uj5u.com熱心網友回復:
鎖定6小時。對此答案的評論已被禁用,但它仍在接受其他互動。了解更多。嘗試下面的代碼進行所有更改我添加了評論以澄清我為什么做
#include <stdio.h>
#include <math.h>
int main(void)
{
// input value
int lower, upper;
double squareroot;
int square;
int cube;
//Read the Limits First
printf("Enter the Lower Limit: ");
scanf("%d", &lower);
printf("Enter the Upper Limit: ");
scanf("%d", &upper);
//Instead of Declaring an Entire Loop You Can Just Use an If Statement this Reduces Code Complexity
//You can also Set Your Limits
if(upper > 0 && upper < 50 && lower > 0 && lower < 50)
{
//THen Enter Actual Code
//Also Dont Set Lower to 0 It will Change Your Actual Value Instead Take another Loop Var
for (int i = lower; i <= upper; i )
{
//Then Perform all Functions on i
squareroot = sqrt(i);
square = i * i;
cube = i * i * i;
printf("*base number* || *square root* || *square* || *cube*\n");
printf("*%d* || *%f* || *%d* || *%d*\n", i, squareroot, square, cube);
}
}
return 0;
}
uj5u.com熱心網友回復:
幾個問題:
- 你去請求的麻煩
lower,但你不使用它來控制你的回圈 - 你的回圈應該是
換句話說,如果你總是打算讓你的回圈從零開始,那么你根本不需要for( int num = lower; num <= upper; num ) { ... }lower。 num您需要為回圈的每次迭代計算平方根、平方和立方。相反,您在回圈之前執行一次,然后一遍又一遍地列印相同的值;- 您只需要在回圈體之外 列印一次表頭;
printf作為陳述句的一部分,您可以計算平方根、平方和立方:
printf( "%d %f %d %d\n", num, sqrt((double) num), num * num, num * num * num );
欄位寬度說明符是您的朋友——您可以printf準確地知道每列的寬度。例子:
printf( "m.2fdd", num, sqrt((double) num), num * num, num * num * num );
這意味著 for 的列num是 6 個字符寬,平方根的列是 12 個字符寬,其中 3 個字符保留用于小數點和后面的兩個數字,square 和 cube 的列是 12 個字符寬。例子:
printf( "%6ssss\n", "base", "root", "square", "cube" );
printf( "%6ssss\n", "----", "----", "------", "----" );
for (int i = lower; i <= upper; i )
printf( "m.2fdd\n", i, sqrt( (double) i ), i*i, i*i*i );
它給出了這樣的輸出(lower == 1,upper == 10):
base root square cube
---- ---- ------ ----
1 1.00 1 1
2 1.41 4 8
3 1.73 9 27
4 2.00 16 64
5 2.24 25 125
6 2.45 36 216
7 2.65 49 343
8 2.83 64 512
9 3.00 81 729
10 3.16 100 1000
完整示例:
#include <stdio.h>
#include <math.h>
int main( void )
{
int lower = 0, upper = 0;
printf( "Gimme a lower value: " );
while ( scanf( "%d", &lower ) != 1 || (lower < 0 || lower > 50 ))
{
/**
* Clear any non-numeric characters from the input stream
*/
while ( getchar() != '\n' )
; // empty loop
printf( "Nope, try again: " );
}
printf( "Gimme an upper value: " );
while ( scanf( "%d", &upper ) != 1 || (upper < lower || upper > 50 ))
{
while( getchar() != '\n' )
; // empty loop
printf( "Nope, try again: " );
}
printf( "%6ssss\n", "base", "root", "square", "cube" );
printf( "%6ssss\n", "----", "----", "------", "----" );
for (int i = lower; i <= upper; i )
printf( "m.2fdd\n", i, sqrt( (double) i ), i*i, i*i*i );
return 0;
}
我已經撰寫了輸入部分,這樣它就會拒絕像fooor之類的輸入a123。它不會正確處理類似的輸入12w4,但這會使示例比它需要的更復雜(您不是在詢問輸入驗證,而是在詢問計算和格式化)。示例運行:
$ ./table
Gimme a lower value: foo
Nope, try again: a123
Nope, try again: 123
Nope, try again: 1
Gimme an upper value: 100
Nope, try again: 50
base root square cube
---- ---- ------ ----
1 1.00 1 1
2 1.41 4 8
3 1.73 9 27
4 2.00 16 64
5 2.24 25 125
6 2.45 36 216
7 2.65 49 343
8 2.83 64 512
9 3.00 81 729
10 3.16 100 1000
11 3.32 121 1331
12 3.46 144 1728
13 3.61 169 2197
14 3.74 196 2744
15 3.87 225 3375
16 4.00 256 4096
17 4.12 289 4913
18 4.24 324 5832
19 4.36 361 6859
20 4.47 400 8000
21 4.58 441 9261
22 4.69 484 10648
23 4.80 529 12167
24 4.90 576 13824
25 5.00 625 15625
26 5.10 676 17576
27 5.20 729 19683
28 5.29 784 21952
29 5.39 841 24389
30 5.48 900 27000
31 5.57 961 29791
32 5.66 1024 32768
33 5.74 1089 35937
34 5.83 1156 39304
35 5.92 1225 42875
36 6.00 1296 46656
37 6.08 1369 50653
38 6.16 1444 54872
39 6.24 1521 59319
40 6.32 1600 64000
41 6.40 1681 68921
42 6.48 1764 74088
43 6.56 1849 79507
44 6.63 1936 85184
45 6.71 2025 91125
46 6.78 2116 97336
47 6.86 2209 103823
48 6.93 2304 110592
49 7.00 2401 117649
50 7.07 2500 125000
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/510432.html
標籤:C视窗
