免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 發帖

[糟糕物] c++練習題2

1.       
Write a program that prompts for and reads a floating-point value.
The program prints the whole part on one line and the decimal (fraction) part on a second line.
For example, if the input value is 123.456, it would print the output:
  1. the input value is 123.456
  2. the whole part is 123
  3. the decimal(fraction) part is 0.456
複製代碼
  1. #include<stdio.h>
  2. int main(){

  3. double a,b,c;

  4. printf("input a floating-point number");
  5. scanf(" %lf", &a);       
  6. b = (int)a;
  7. c = a-b;
  8. printf("\nthe input value is %lf",a);
  9. printf("\nthe whole part is %lf",b);
  10. printf("\nthe decimal(fraction) part is %lf",c);       
  11.        
  12. }
複製代碼
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

2.
Write a program that asks the user to enter two numbers,
obtains the two numbers from the user and prints the sum, product, difference, and quotient of the two numbers.

               Number1       Number2
Case I           int               int
Case II          float            float
Case III        double          double
Case VI         int               float

Consider and discuss the results.
  1. #include<stdio.h>
  2. int main(){

  3. int a,b;
  4. double c,d,e,f;

  5. printf("case1: input two numbers");
  6. printf("\nNo.1:");
  7. scanf(" %d",&a);
  8. printf("\nNo.2:");
  9. scanf(" %d",&b);

  10. c = a+b;
  11. d = a*b;
  12. e = a-b;
  13. f = a/b;

  14. printf("\nthe sum is %lf",c);
  15. printf("\nthe product is %lf",d);
  16. printf("\nthe difference is %lf",e);
  17. printf("\nthe quotient is %lf",f);

  18. float h,i;

  19. printf("\n\ncase2: input two numbers are float");
  20. printf("\nNo.1:");
  21. scanf(" %f",&h);
  22. printf("\nNo.2:");
  23. scanf(" %f",&i);
  24. c = h+i;
  25. d = h*i;
  26. e = h-i;
  27. f = h/i;

  28. printf("\nthe sum is %lf",c);
  29. printf("\nthe product is %lf",d);
  30. printf("\nthe difference is %lf",e);
  31. printf("\nthe quotient is %lf",f);

  32. double j,k;

  33. printf("\n\ncase2: input two numbers are double");
  34. printf("\nNo.1:");
  35. scanf(" %lf",&j);
  36. printf("\nNo.2:");
  37. scanf(" %lf",&k);
  38. c = j+k;
  39. d = j*k;
  40. e = j-k;
  41. f = j/k;

  42. printf("\nthe sum is %lf",c);
  43. printf("\nthe product is %lf",d);
  44. printf("\nthe difference is %lf",e);
  45. printf("\nthe quotient is %lf",f);

  46. int m;
  47. float m;

  48. printf("\n\ncase2: input two numbers are double");
  49. printf("\nNo.1:");
  50. scanf(" %d",&m);
  51. printf("\nNo.2:");
  52. scanf(" %f",&n);
  53. c = m+n;
  54. d = m*n;
  55. e = m-n;
  56. f = m/n;

  57. printf("\nthe sum is %lf",c);
  58. printf("\nthe product is %lf",d);
  59. printf("\nthe difference is %lf",e);
  60. printf("\nthe quotient is %lf",f);
  61. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

3.
Write a program that accepts an integer between 7 and 9 digits long.
a. Extracts and prints the third-rightmost digit of the input data.
b. Writes the integer with commas between every third digit starting from the right.
Example:
Input: 12345678   
Output:
The third-rightmost digit of the input data is 6
The input data with commas between every third digit is 12,345,678
  1. #include<stdio.h>
  2. int main(){

  3. int a,b,c,d,e,f;
  4. printf("input an integer between 7 and 9 digits long: ");
  5. scanf(" %d", &a);
  6. b = a%1000000;
  7. c = a%1000;
  8. f = c/100;
  9. d = a/1000000;
  10. e = b/1000;
  11. printf("\nThe third-rightmost digit of the input data is %d",f);
  12. printf("\nThe input data with commas between every third digit is %d,%d,%d",d,e,c);

  13. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

4.
Write a program to calculate the diameter, the circumference, and the area of a circle with a radius of 6.75.
Assign the radius of float variable, and then output the radius with an appropriate message.
Declare a named const PI with the value 3.14159.
The program should output the diameter, the circumference, and the area, each on a separate line.
Print each value to five decimal places within a total field width(欄位) of 10.

Note1:      
when the compiler reads this specifier: %10.5f
i.The compiler prepares 10 columns to output this real number with the five right-most columns for the fraction part.
ii.If the real number has less than five digits in the fraction part, the compilers pads the remaining columns with zero.
iii.The 6th column from the right is the decimal point.
iv.The remaining four columns are the integer part.
   If the real number has less than four digits in the integer part, the output is padded with blank on the left.

%10.5f 的意思:
這個format對應的data其輸出格式如下:
留十個欄位來輸出實數,其中後五個欄位為小數點後面的位數,不足五位則在後方補「0」,後面數來第六位數是小數點,剩下四位放整數,整數不足四位則在前方插入空白。

Note2: Be sure to include appropriate comments in your program, choose meaningful identifier.
  1. #include<stdio.h>
  2. int main(){

  3. float rad=6.75;
  4. double a,b,c,d;

  5. a=2*rad;
  6. b=a*3.14159;
  7. c=rad*rad*3.14159;

  8. printf("\nthe radius is %f",rad);         
  9. printf("\nthe diameter is%10.5f",a);     //直徑
  10. printf("\nthe circumference is%10.5f",b);//圓周長
  11. printf("\nthe area is%10.5f",c);         //圓面積


  12. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

5.
The effective resistance of a parallel circuit with five parallel resistances
is given by:

R = 1 / ( (1/R1)+(1/R2)+(1/R3)+(1/R4)+(1/R5) )

Read these five resistances from the keyboard and calculate the effective
resistance R.

  1. #include<stdio.h>
  2. int main(){

  3. double r1,r2,r3,r4,r5,a,b;

  4. printf("input five resistance");
  5. printf("\nr1 :");
  6. scanf(" %lf",&r1);
  7. printf("\nr2 :");
  8. scanf(" %lf",&r2);
  9. printf("\nr3 :");
  10. scanf(" %lf",&r3);
  11. printf("\nr4 :");
  12. scanf(" %lf",&r4);
  13. printf("\nr5 :");
  14. scanf(" %lf",&r5);

  15. r1 = 1/r1;
  16. r2 = 1/r2;
  17. r3 = 1/r3;
  18. r4 = 1/r4;
  19. r5 = 1/r5;

  20. a = r1+r2+r3+r4+r5;
  21. a = 1/a;

  22. printf("the effective resistance R is %lf",a);        
  23. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

6.
Solve a set of simultaneous equations:
         ax + by = c
         dx + ey = f
  
Input data: six real numbers.
Formulas for the solution:

         x = (ce-bf)/(ae-bd)

         y = (af-cd)/(ae-bd)

Output all the input values a, b, c, d, e, and f and the computed values for
x and y.
  1. #include<stdio.h>
  2. int main(){
  3.        
  4. double a,b,c,d,e,f,x,y;
  5. printf("input a :");
  6. scanf(" %lf",&a);
  7. printf("input b :");
  8. scanf(" %lf",&b);
  9. printf("input c :");
  10. scanf(" %lf",&c);
  11. printf("input d :");
  12. scanf(" %lf",&d);
  13. printf("input e :");
  14. scanf(" %lf",&e);
  15. printf("input f :");
  16. scanf(" %lf",&f);

  17. x = (c*e-b*f)/(a*e-b*d);
  18. y = (a*f-c*d)/(a*e-b*d);

  19. printf("\na is %lf",a);
  20. printf("\nb is %lf",b);
  21. printf("\nc is %lf",c);
  22. printf("\nd is %lf",d);
  23. printf("\ne is %lf",e);
  24. printf("\nf is %lf",f);
  25. printf("\nx is %lf",x);
  26. printf("\ny is %lf",y);
  27. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

返回列表