# 第七课条件运算与选择结构 by yurtgc548

VIEWS: 8 PAGES: 29

• pg 1
```									Control Flow
—— repetition

Hu Junfeng
2010/09/17
//第1题：输出5个数的和
#include <stdio.h>
int main()
{
int a,b,c,d,e;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("%d",a+b+c+d+e);
return 0;
}

2
Repetition Statements
 repeat statement(s) a controlled number of
times; can be infinite

 controlled by a test expression, typically a
relational expression, which must be
enclosed in parentheses ( )

 control a single statement or a compound
statement (i.e. a block)

3
Repetition Statements (cont.)

4
for statement
 good for counter-controlled or indexed loops

 uses for keyword

 index variable or loop counter (typically an
integer variable), controls the loop
 condition is tested at the top of the loop (i.e.
before executing the loop statements)
 has initialization and update expressions for
manipulating the loop counter

5
initializing;

testing;

altering

6
/*一个程序，接受5个整数输入，求和，1000010438胡修涵*/

#include <stdio.h>
int main()
{
int a;
int i, ans = 0;
for (i=1; i<=5; i++)
{
scanf("%d",&a);
ans = ans + a;
}
printf("%d",ans);
return 0;
}

7
scanf()： return value
/*一个程序，接受N个整数输入，求和*/

#include <stdio.h>
int main()
{
int a;
int i, ans = 0;
for ( ; scanf("%d",&a);   )
ans += a;
printf("%d",ans );
return 0;
}

8

 排序，去中间那个数。
 按快速排序的思路，思考。
 逆序数等于K/2的数。

9
10
while statement
 good for flag or sentinel value conditions

 uses while keyword

 condition is tested at the top of the loop
(i.e. before executing the loop statements)

11
while statement (cont.)

12
do while statement
 good for flag or sentinel value conditions, when
you need to execute the loop statements at least
once

 uses do and while keywords (as a pair)

 condition is tested at the bottom of the loop (i.e.
after executing the loop statements)

 requires a semicolon ; at the end

13
Enter at least once, often used
in verification

14
Exp3_3:
/*poj 3255; Description

Input

Output

Sample Input
3
13
46
265

Sample Output
21
114
1121
*/

#include <stdio.h>
#define base 6

15
break and continue statements

 break statement, used for early
termination of a loop

 continue statement, used for early
continuation of a loop

16
break;

17
A bouncing ball

Each time rebounds ¾ of original height
Impact speed = sqrt (2 * g * height)
Impact speed first three times?
1

3/4

18
Exp 4:
#include <stdio.h>
#include <math.h>
void main() //a bouncing ball
{
int i = 0;
double hgt = 2.0, spd, h, g;
printf("Enter the gravitational constant.\n");
printf("Enter 9.81 for earth or 1.67 for the moon: ");
scanf("%lf", &g);
h = hgt;
while(1)
{
spd = sqrt(2.0 * g * h);
h = h* 3.0/4.0;
printf("The impact speed of bounce %d is %f m/s.\n", ++i, spd);
printf("The rebound height after bounce %i is %f m.\n", i, h);
if (h <= 1E-6)
break;
}
}

19
continue;

20
Exp5: sum up
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *stream ;
int i, sum = 0;
if((stream = freopen("infile.txt", "r", stdin)) == NULL)
exit(-1);

while(scanf("%d",&i) != EOF)
{
if ( i % 2 != 0)
continue;
else
sum += i;
}
printf("sum = %d\n", sum);
stream = freopen("CON", "r", stdin); //return
scanf("%d",&i); printf("i=%d",i);

return 0;
}

21
Nested loop
 #define NUM 4
 int i;j;
 for ( i = NUM; i > 0; i--)
{
for ( j = 0; j<i; j++)
printf(“*”);
printf(“\n”);
}

22
/* No:2 一个程序，接受5个整数输入，排降序，输出。by 陈云帆 1000010012*/

#include <stdio.h>
int swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return 0;
}
int main()
{
int a[5];
int i,j;
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
for(j=0;j<4;j++)
if(a[j]<a[j+1])
swap(&a[j],&a[j+1]);
for(i=0;i<4;i++)
printf("%d ",a[i]);
printf("%d\n",a[i]);
return 0;
}                                                                    23
Exp 6.8 evaluate expressions
#include <stdio.h>
int main()
{
float value1, value2;
char oper;

Loop:
printf("input expressions,seperate by blank space:\n");

scanf("%f %c %f", &value1, &oper, &value2);
switch(oper)
{
case '+':
printf("%f", value1 + value2);   break;
case '-':
printf("%f", value1 - value2);   break;
case '*':
printf("%f", value1 * value2);   break;
case '/':
printf("%f", value1 / value2);   break;
default:
printf("input error");

24
25
Expression with brackets

26
Newton-Raphson Method
—— P131
 Step1: set a guess as ‘1’
 Step2: if |guess2 – x| < ε, process step 4
 Step3: set the value of guess to (x/guess + guess)/2
return step2

 The guess is almost the toot

27
Exp:
#include <stdio.h>
#include <math.h>
double absdouble (   double a, double b )
{
if ( a >= b   )
return   ( a - b );
else
return   ( b - a );
}
int main ()
{
double num,root,root0;
int i;

while ( scanf ( "%lf",&num ) != EOF )
{
root0 = 1.0;
root = ( root0 + num / root0 ) / 2;
i = 1;
while ( absdouble( root, root0 ) >= 1e-6 )
{
root0 = root;
root = ( root0 + num / root0 ) / 2;
i++;

28
Home work
 1、有括号的四则运算正整数表达式的求值。

 2、两个半径为5的小球，已知初始位置（x1,y1），（x2,y2）;参数都
是0-400之间的浮点数。
 另外输入参数a1,a2;b1,b2;
 小球1的运动方程为：x=a1t;y=a2t;
 小球2的运动方程为： x=b1t;y=b2t;
 问，两球是否会发生碰撞。
 有兴趣的同学可以给坐标系加个（0，0）到（800，600）框做这道
题试试看。（完成者可以发到teacherhu@gmail.com)
 两题任选一题。
 提交到0917文件夹。

29

```
To top