第六章 C控制语句:循环练习

2016年8月20日-第六章 C控制语句:循环
for(初始化、判断、更新 )

/*for_cube.c -- 使用一个for循环产生一个立方表*/
#include<stdio.h>
int main(void)
{
    int n = 0;
    int count = 0;
    printf(" n    n  cubed\n");
    for (n = 1; n<= 6; n++) 
        printf("%d %5d\n", n, n*n*n);
        getchar();
        getchar();
        return 0;
}

/*rows1.c -- 使用嵌套循环*/
#include<stdio.h>
#define ROWS 6
#define CHARS 10
int main(void)
{
    int row;
    char ch;
    for (row = 0; row < ROWS; row++) {
        for (ch = 'A'; ch < ('A' + CHARS); ch++) 
            printf("%c", ch);
            printf("\n");
    }
        getchar();
        getchar();
        return 0;
}

ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ

---
/*rows1.c -- 使用嵌套循环*/
#include<stdio.h>
#define ROWS 6
#define CHARS 6
int main(void)
{
    int row;
    char ch;
    for (row = 0; row < ROWS; row++) {
        for (ch = ('A'+row); ch < ('A' + CHARS); ch++) 
            printf("%c", ch);
            printf("\n");
    }
        getchar();
        getchar();
        return 0;
}
内部循环依赖外部循环

ABCDEF
BCDEF
CDEF
DEF
EF
F
---

/*rows1.c -- 使用嵌套循环*/
#include<stdio.h>
#define SIZE 4
#define PAR 72 
int main(void) {
    int index, score[SIZE];
    int sum = 0;
    float average;
    printf("Enter %d golf scores: \n", SIZE);
    for (index = 0; index < SIZE; index++)
        scanf("%d", &score[index]); // 读入分数
    printf("The scores read in are as follows: \n");
    for (index = 0; index < SIZE; index++)
        printf("%5d", score[index]); // 验证输入
    printf("\n");
    for (index = 0; index < SIZE; index++)
        sum += score[index]; // 求和
    average = (float) sum / index;
    printf("Sum of scores = %d, average = %.2f\n", sum, average);
    printf("That's a handicap of %.0f.\n", average - PAR);
        getchar();
        getchar();
        return 0;
}
---
Enter 4 golf scores:
113 112 116 118
The scores read in are as follows:
  113  112  116  118
Sum of scores = 459, average = 114.75
That's a handicap of 43.
数组边界,输出数据,平均数(float)

/*power.c -- 计算数值的整数次幂*/
#include<stdio.h>
double power(double n, int p);// 函数原型
int main(void) {
    double x, xpow;
    int exp;
    printf("Enter a number and the positive integer power");
    printf("to which\nthe number will be raised. Enter q");
    printf(" to quit.\n");
    while (scanf("%lf%d",&x,&exp)==2)
    {
        xpow = power(x, exp); // 函数调用
        printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
        printf("Enter next pair of numbers or q to quit.\n");
    }
    printf("Hope you enjoyed this power trip -- bye!\n");
    getchar();
    return 0;
}
double power(double n, int p)// 函数定义
{
    double pow = 1;
    int i;
    for (i = 1; i <= p; i++)
        pow *= n;
    return pow;    // 返回pow值
}


定义函数变量n pow *= n; i<=p 运算次数的条件
Enter a number and the positive integer powerto which
the number will be raised. Enter q to quit.
1.2 12
1.2 to the power 12 is 8.9161
Enter next pair of numbers or q to quit.
3 10
3 to the power 10 is 59049
Enter next pair of numbers or q to quit.
2 2
2 to the power 2 is 4
Enter next pair of numbers or q to quit.
2 10
2 to the power 10 is 1024
Enter next pair of numbers or q to quit.
2 5
2 to the power 5 is 32
Enter next pair of numbers or q to quit.


### 编程练习

/* -- 打印小写字母*/
#include<stdio.h>
#define SIZE 26
int main(void) {
    char character[SIZE];
    int i = 0;
    for (i = 0; i < SIZE;  i++)
        printf("%c ", character[i] = 'a' + i);
    printf("\n");
    getchar();
    return 0;
}


a b c d e f g h i j k l m n o p q r s t u v w x y z

/* -- 嵌套循环输出*/
#include<stdio.h>
int main(void) {
    int col = 0, row = 0;
    for (row = 1; row <= 5; row++) {
        for (col = 0; col < row; col++)
        printf("$");
    printf("\n");
    }
    getchar();
    return 0;
}

$
$$
$$$
$$$$
$$$$$

/* -- 嵌套循环输出*/
#include<stdio.h>
int main(void) {
    char alp = 'F';
    int col = 0, row = 0;
    for (row = 1; row <= 6; row++) {
        for (col = 0; col<row; col++)
        printf("%c",alp-col);
    printf("\n");
    }
    getchar();
    return 0;
}

F
FE
FED
FEDC
FEDCB
FEDCBA

4. 

#include<stdio.h>
int main(void)
{
    int i, j, k;
    char letter;
    printf("Please input a capital:");
    scanf("%c", &letter);
    k = letter - 'A' + 1;
    for (i = 0; i<k; i++)
    {
        for (j = 0; j<k - i; j++) printf(" "); // 打印空格
        for (j = 0; j <= i; j++) printf("%c", 'A' + j); // 打印A到i的字母
        for (j = i - 1; j >= 0; j--) printf("%c", 'A' + j); // 打印i之后的字母
        printf("\n");
    }
    getchar();
    getchar();
    return(0);
}


Please input a capital:E
     A
    ABA
   ABCBA
  ABCDCBA
 ABCDEDCBA



#include<stdio.h>
#define BASE 'A'
int main(void)
{
    int lines,space,i,j,k;
    char ch,c;

    printf("Please enter a character(Captial) to start the pyraid");

    scanf("%c",&ch);
    lines=ch-BASE+1; 

    printf("There will be %d layers of pyramid.\n", lines);
    printf("The chosen letter is %c.\n", ch);

    for(i=0;i<lines;i++)    // outer loop controls the total lines
    {
        for(space=lines;space>i;space--)
            printf(" ");
        for(c=0;c<=i;c++)
            printf("%c",c+BASE);
        for(c=i-1;c>=0;c--)
            printf("%c",c+BASE);
            printf("\n");
    }
    return 0;
}

The chosen letter is E.
     A
    ABA
   ABCBA
  ABCDCBA
 ABCDEDCBA

观察字母变化,依次(计算)输出字母
#include<stdio.h>
int main(void)
{
    int i=0,min=0,max=0;
    printf("Please input min and max:");
    scanf("%d %d", &min,&max);
    printf("%s%8s%8s\n","num","square","cube");
    for (i = min; i <= max; i++) {
        printf("%d%8d%8d\n", i, i*i, i*i*i);
    }
      return(0);
}

Please input min and max:2 5
num  square    cube
2       4       8
3       9      27
4      16      64
5      25     125


#include<stdio.h>
#include<string.h>
#define SIZE 20
int main(void)
{
    char word[SIZE];
    int i = 0;
    printf("Please input a word:");
    scanf("%s", &word);
    for (i = strlen(word) - 1; i >=0; i--)
        printf("%c",word[i]);
    printf("\n");
    getchar();
    getchar();
    return(0);
}

Please input a word:hello
olleh


7.

#include<stdio.h>
#include<string.h>
int main(void)
{
    float a = 0, b = 0;
    printf("Please input two floats number:");
    scanf("%f %f", &a, &b);
    do
    {
        printf("(%.2f-%.2f)-(%.2f*%.2f)=%.2f",a,b,a,b, (a - b) / (a*b));
    } while (scanf("%f %f",&a,&b)==2);
    getchar();
    getchar();
    return(0);
}

Please input two floats number:2.5 2.3
(2.50-2.30)-(2.50*2.30)=0.03

题目要求将公式输出,且注意数据处理%.2f


8.

#include<stdio.h>
float compute(float,float);
int main(void)
{
    float result = 0, a = 0, b = 0;
    printf("Please input two floats number:");
    while (scanf("%f %f", &a, &b)==2)
    {
    result=compute(a, b);
        printf("(%.2f-%.2f)-(%.2f*%.2f)=%.2f\n", a, b, a, b, result);
    printf("Please input two floats number:");
    }
    getchar();
    getchar();
    return(0);
}
    float compute(float x, float y) {
return (x - y) / (x*y);
    }  

Please input two floats number:2.5 2.3
(2.50-2.30)-(2.50*2.30)=0.03
Please input two floats number:

9.

#include<stdio.h>
int main(void)
{
    int lower = 0,upper= 0, sum= 0,i=0;
    printf("Enter lower and upper integer limits:");
    scanf("%d %d", &lower, &upper);
    while(lower<upper)
    {
        for (i = lower,sum=0; i <= upper; i++) 
        //for (int i = lower,sum=0; i <= upper; i++)  // 加上类型会被初始化为0
            sum += i*i;
        printf("The sum of the square from %d to %d is %d\n", lower*lower, upper*upper, sum);
        printf("Enter next of limits:");
    scanf("%d %d", &lower, &upper);
    }
    printf("Done\n");
    getchar();
    getchar();
    return(0);
}

Enter lower and upper integer limits:5 9
The sum of the square from 25 to 81 is 255
Enter next of limits:3 25
The sum of the square from 9 to 625 is 5520
Enter next of limits:10 10
Done


#include<stdio.h>
int main(void)
{
    int i=0, num[8];
    printf("Please input eight integer number:\n");
    for (i = 0; i < 8; i++)
    scanf("%d", &num[i]);
    for (i = 7; i >= 0; i--)
        printf("%2d", num[i]);
    getchar();
    getchar();
    return(0);
}

Please input eight integer number:
1 2 3 4 5 6 7 8
87654321

#include<stdio.h>
int main(void)
{
    double i = 0,sum1=0,sum2=0; 
    int x = 0,sign=0; // 项数
    printf("Please input you want to sum of n item ");
    scanf("%d",&x);
    for (i = 1.0, sign = 1, sum1 = 0, sum2 = 0; i <= x; i++, sign =-sign) { // there is sign =- sign ***
        sum1 += 1.0 / i;
        sum2 += sign*1.0 / i;
    }
    printf("1.0+1.0/2.0+1.0/3.0+1.0/4.0+...(%d terms)=%lf\n", x, sum1);
    printf("1.0-1.0/2.0+1.0/3.0-1.0/4.0+...(%d terms)=%lf\n", x, sum2);
    getchar();
    getchar();
    return(0);
}


Please input you want to sum of n item 2
1.0+1.0/2.0+1.0/3.0+1.0/4.0+...(2 terms)=1.500000
1.0-1.0/2.0+1.0/3.0-1.0/4.0+...(2 terms)=0.500000

#include<stdio.h>
#include<math.h>
int main(void)
{
    int i=0, num[8];
        for (i = 0; i < 8; i++)
            num[i] = pow(2,i); // pow 幂函数
        i = 0;
        do
            printf("%d\t",num[i]);
         while (++i<8);
        printf("\n");
    getchar();
    getchar();
    return(0);
}

1       2       4       8       16      32      64      128


两个数组的使用,第二个数组和第一个数组元素个数的关系,求和
#include<stdio.h>
#include<math.h>
int main(void)
{
    double num1[8], num2[8],sum=0;
    int i = 0, j = 0;
    printf("Please input eghit numbers:");
    for (i = 0; i < 8; i++) {  
        scanf("%lf", &num1[i]);
        for (j = 0, num2[i] = 0; j <= i; j++)
            num2[i] += num1[j];
    }
    for(i=0;i<8;i++)
    printf("%8.3lf\t", num1[i]);
    printf("\n");
    for(i=0;i<8;i++)
    printf("%8.3lf\t", num2[i]);
    getchar();
    getchar();
    return(0);
}

Please input eghit numbers:1 2 3 4 5 6 7 8
   1.000           2.000           3.000           4.000           5.000           6.000           7.000           8.000
   1.000           3.000           6.000          10.000          15.000          21.000          28.000          36.000



#include<stdio.h>
#define LENGTH 5
int main(void)
{
    char a[255];
    int i = 0;
    printf("Enter a line word:");
    for (i = 0; i <=LENGTH; i++)
        scanf("%c", &a[i]);
    for (i = LENGTH-1; i > 0; i--)
        printf("%c", a[i]);
    printf("\n");
    getchar();
    getchar();
    return(0);
}

Enter a line word:adldl
ldld

#include<stdio.h>
#define DOLLARS 100
const double DA = 0.1;
const double DE = 0.05;
int main(void) {
    int years = 0;
    int i = 0;
    double sum_da=0, sum_de=0; // 分别表示当前金额
    while (sum_da <= sum_de) {
        sum_da += DOLLARS*DA*years;
        sum_de = DOLLARS;
        sum_de += sum_de*DE*years;
        years++;
    }
    printf("After %d years,Deirdre's investment is %lf,Daphne's investment is %lf\n", years, sum_da, sum_de);
    getchar();
    getchar();
    return(0);
}

After 6 years,Deirdre's investment is 150.000000,Daphne's investment is 125.000000



#include<stdio.h>
#define USE 10
const double D = 0.08;
int main(void) {
    int years = 0;
    double blance=100; // 表示当前金额
    while ( blance >= 0) {
        blance += blance*D;
        blance -=  USE;
        years++;
    }
    printf("After %d years,blance is 0\n", years);
    getchar();
    getchar();
    return(0);
}

After 21 years,blance is 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值