第六章 编程练习
6-1
#include <stdio.h> #define SIZE 26 int main(int argc, char const *argv[]) { char lcase[SIZE]; int i; for (i = 0; i < SIZE; i++){ lcase[i] = (char)((int)'a' + i); //可以直接lcase[i] = 'a'+i } for (i = 0; i < SIZE; i++) printf("%c", lcase[i]); return 0; }
6-2
#include <stdio.h> int main(int argc, char const *argv[]) { int i,j; for (i = 1; i <= 5; i++){ for (j = 1; j <= i; j++) { printf("$"); } printf("\n"); } return 0; } /* $ $$ $$$ $$$$ $$$$$ */
6-3
#include <stdio.h> int main(int argc, char const *argv[]) { int i,j; char c; for (i = 1; i <= 6; i++){ for (j = 1, c = 'F'; j <= i; j++, c--) { printf("%c", c); } printf("\n"); } return 0; } /* F FE FED FEDC FEDCB FEDCBA */
6-4
#include <stdio.h> int main(int argc, char const *argv[]) { int i,j; char c = 'A'; for (i = 1; i <= 6; i++){ for (j = 1; j <= i; j++, c++) { printf("%c", c); } printf("\n"); } return 0; } /* A BC DEF GHIJ KLMNO PQRSTU */
6-5
#include <stdio.h> int main(int argc, char const *argv[]) { int i,j,n; char c; printf("Enter a capital letter(like E):"); scanf("%c", &c); n = c - 'A' + 1; for (i = 1; i <= n; i++){ for (j = 1; j <= ((2*n-1)-(2*i-1))/2; j++) printf(" "); for (j = 0, c = 'A'; j < i; c++, j++) printf("%c", c); for (j = 0, c-=2; j < (i-1); c--, j++) printf("%c", c); printf("\n"); } return 0; } /* Enter a capital letter(like E):E A ABA ABCBA ABCDCBA ABCDEDCBA */
6-6
#include <stdio.h> int main(int argc, char const *argv[]) { int top,bot; int i; printf("Enter a top num:"); scanf("%d", &top); printf("Enter a bottom num:"); scanf("%d", &bot); printf("%5s %5s %5s\n", "n", "square", "cube"); for (i = bot; i <= top; i++){ printf("%5d %5d %5d\n", i, i*i, i*i*i); } return 0; } /* Enter a top num:5 Enter a bottom num:1 n square cube 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 */
6-7
#include <stdio.h> #include <string.h> int main(int argc, char const *argv[]) { char word[20]; int i; printf("Enter a word:"); scanf("%s", word); for (i = strlen(word)-1; i >= 0; i--){ printf("%c", word[i]); } return 0; } /* Enter a word:god dog */
6-8
#include <stdio.h> int main(int argc, char const *argv[]) { float a, b; printf("Enter two different float number:"); while (scanf("%f %f", &a, &b) == 2){ //printf()中float和double都用 %f;scanf()中float用 %f,double用 %lf printf("(%.3g-%.3g)/(%.3g*%.3g) = %.3g\n", a, b, a, b, (a-b)/(a*b)); //%.3g表示小数点后面三位有效数字 printf("Enter two different float number:"); } return 0; } /* Enter two different float number:3 2 (3-2)/(3*2) = 0.167 Enter two different float number:6.2 8.9 (6.2-8.9)/(6.2*8.9) = -0.0489 Enter two different float number:q */
6-9
#include <stdio.h> float calc(float m, float n); int main(int argc, char const *argv[]) { float a, b; printf("Enter two different float number:"); while (scanf("%f %f", &a, &b) == 2){ //printf()中float和double都用 %f;scanf()中float用 %f,double用 %lf printf("(%.3g-%.3g)/(%.3g*%.3g) = %.3g\n", a, b, a, b, calc(a, b)); //%.3g表示小数点后面三位有效数字 printf("Enter two different float number:"); } return 0; } float calc(float m, float n){ return (m-n)/(m*n); } /* Enter two different float number:3 2 (3-2)/(3*2) = 0.167 Enter two different float number:6.2 8.9 (6.2-8.9)/(6.2*8.9) = -0.0489 Enter two different float number:q */
6-10
#include <stdio.h> int main(int argc, char const *argv[]) { int a, b, sum, i; printf("Enter a lower integer and a upper integer(like 5 9):"); scanf("%d %d", &a, &b); while (a < b){ for (sum = 0, i = a; i <= b; i++) { sum += i*i; } printf("The sum of the squares form %d to %d is %d\n", a*a, b*b, sum); printf("Enter a lower integer and a upper integer(like 5 9):"); scanf("%d %d", &a, &b); } printf("Done!"); return 0; } /* Enter a lower integer and a upper integer(like 5 9):5 9 The sum of the squares form 25 to 81 is 255 Enter a lower integer and a upper integer(like 5 9):3 25 The sum of the squares form 9 to 625 is 5520 Enter a lower integer and a upper integer(like 5 9):5 5 Done! */
6-11
#include <stdio.h> #define SIZE 8 int main(int argc, char const *argv[]) { int num[SIZE]; int i; printf("Enter %d integer:\n", SIZE); for (i = 0; i < SIZE; i++) { scanf("%d", &num[i]); } printf("The revese sequence of your input number is:\n"); for (i = SIZE - 1; i >= 0; i--) { printf("%d ", num[i]); } printf("\n"); return 0; } /* Enter 8 integer: 1 2 33 4 55 66 7 88 The revese sequence of your input number is: 88 7 66 55 4 33 2 1 */
6-12
#include <stdio.h> #include <math.h> int main(int argc, char const *argv[]) { int n; int i, j; float sum; printf("Enter how many element you want to be summary(n):"); scanf("%d", &n); while (n > 0){ for (i = 1, sum = 0; i <= n; sum += 1.0/i, i++); printf("when n = %d, 1.0 + 1.0/2.0 + 1.0/3.0 + ... = %f\n", n, sum); for (i = 1, j = 1, sum = 0; i <= n; sum += 1.0/i*j, i++, j*=-1); printf("when n = %d, 1.0 - 1.0/2.0 + 1.0/3.0 - ... = %f\n", n, sum); printf("Enter how many element you want to be summary(n):"); scanf("%d", &n); } printf("Done!"); return 0; } /* Enter how many element you want to be summary(n):100 when n = 100, 1.0 + 1.0/2.0 + 1.0/3.0 + ... = 5.187378 when n = 100, 1.0 - 1.0/2.0 + 1.0/3.0 - ... = 0.688172 Enter how many element you want to be summary(n):1000 when n = 1000, 1.0 + 1.0/2.0 + 1.0/3.0 + ... = 7.485478 when n = 1000, 1.0 - 1.0/2.0 + 1.0/3.0 - ... = 0.692646 Enter how many element you want to be summary(n):10000 when n = 10000, 1.0 + 1.0/2.0 + 1.0/3.0 + ... = 9.787613 when n = 10000, 1.0 - 1.0/2.0 + 1.0/3.0 - ... = 0.693091 Enter how many element you want to be summary(n):10000000 when n = 10000000, 1.0 + 1.0/2.0 + 1.0/3.0 + ... = 15.403683 when n = 10000000, 1.0 - 1.0/2.0 + 1.0/3.0 - ... = 0.693137 Enter how many element you want to be summary(n):-1 Done! */
6-13
#include <stdio.h> #include <math.h> #define SIZE 8 int main(int argc, char const *argv[]) { int num[SIZE]; int i; for (i = 0; i < SIZE; i++) { num[i] = pow(2, i+1); } printf("The element in array are:\n"); i = 0; do{ printf("%d ", num[i]); i++; }while(i < SIZE); return 0; } /* The element in array are: 2 4 8 16 32 64 128 256 */
6-14
#include <stdio.h> #define SIZE 8 int main(int argc, char const *argv[]) { double a[SIZE], b[SIZE]; int i; double sum; printf("Enter %d number:\n", SIZE); for (i = 0, sum = 0; i < SIZE; i++) { scanf("%lf", &a[i]); sum += a[i]; b[i] = sum; } printf("Two array would be listeda as follows:\n"); for (i = 0; i < SIZE; i++) { printf("%6.3g", a[i]); } printf("\n"); for (i = 0; i < SIZE; i++) { printf("%6.3g", b[i]); } return 0; } /* Enter 8 number: 1.1 2.1 3.1 4.4 5.5 6.6 7 8 Two array would be listeda as follows: 1.1 2.1 3.1 4.4 5.5 6.6 7 8 1.1 3.2 6.3 10.7 16.2 22.8 29.8 37.8 */
6-15-6-18略

浙公网安备 33010602011771号