c语言troubc int类型占几个字节,程序设计基础(C)第06讲例程

1summing.c

/* summing.c -- 根据用户键入的整数求和 */

#include

int main(void)

{

long num;

long sum = 0L; /* 把sum 初始化为0 */

int status;

printf("Please enter an integer to be summed ");

printf("(q to quit): ");

status = scanf("%ld", &num);

while (status == 1) /* == means "is equal to" */

{

sum = sum + num;

printf("Please enter next integer (q to quit): ");

status = scanf("%ld", &num);

}

printf("Those integers sum to %ld.\n", sum);

return 0;

}

2 when.c

// when.c -- 何时退出循环

#include

int main(void)

{

int n = 5;

while (n < 7) // line 7

{

printf("n = %d\n", n);

n++; // line 10

printf("Now n = %d\n", n); // line 11

}

printf("The loop has finished.\n");

return 0;

}

3 while1.c

/* while1.c -- 注意花括号的使用 */

/* 糟糕的代码创建了一个死循环 */

#include

int main(void)

{

int n = 0;

while (n < 3)

printf("n is %d\n", n);

n++;

printf("That's all this program does\n");

return 0;

}

4 while2.c

/* while2.c -- 注意分号的位置 */

#include

int main(void)

{

int n = 0;

while (n++ < 3); /* line 7 */

printf("n is %d\n", n); /* line 8 */

printf("That's all this program does.\n");

return 0;

}

5 cmpflt.c

// cmpflt.c -- 浮点数比较

#include

#include

int main(void)

{

const double ANSWER = 3.14159;

double response;

printf("What is the value of pi?\n");

scanf("%lf", &response);

while (fabs(response - ANSWER) > 0.0001)

{

printf("Try again!\n");

scanf("%lf", &response);

}

printf("Close enough!\n");

return 0;

}

6 t_and_f.c

/* t_and_f.c -- C中的真和假的值 */

#include

int main(void)

{

int true_val, false_val;

true_val = (10 > 2); // value of a true relationship

false_val = (10 == 2); // value of a false relationship

printf("true = %d; false = %d \n", true_val, false_val);

return 0;

}

7 truth.c

// truth.c -- 哪些值为真

#include

int main(void)

{

int n = 3;

while (n)

printf("%2d is true\n", n--);

printf("%2d is false\n", n);

n = -3;

while (n)

printf("%2d is true\n", n++);

printf("%2d is false\n", n);

return 0;

}

8 trouble.c

// trouble.c -- 这里误用了=导致了死循环

#include

int main(void)

{

long num;

long sum = 0L;

int status;

printf("Please enter an integer to be summed ");

printf("(q to quit): ");

status = scanf("%ld", &num);

while (status = 1)

{

sum = sum + num;

printf("Please enter next integer (q to quit): ");

status = scanf("%ld", &num);

}

printf("Those integers sum to %ld.\n", sum);

return 0;

}

9 boolean.c

// boolean.c -- 使用 _Bool类型的变量

#include

int main(void)

{

long num;

long sum = 0L;

_Bool input_is_good;

printf("Please enter an integer to be summed ");

printf("(q to quit): ");

input_is_good = (scanf("%ld", &num) == 1);

while (input_is_good)

{

sum = sum + num;

printf("Please enter next integer (q to quit): ");

input_is_good = (scanf("%ld", &num) == 1);

}

printf("Those integers sum to %ld.\n", sum);

return 0;

}

10 sweetie1.c

// sweetie1.c -- 一个计数循环

#include

int main(void)

{

const int NUMBER = 22;

int count = 1; // i初始化

while (count <= NUMBER) // 测试

{

printf("Be my Valentine!\n"); // 行为

count++; // 更新计数

}

return 0;

}

11 sweetie2.c

// sweetie2.c -- 使用for循环的计数循环

#include

int main(void)

{

const int NUMBER = 22;

int count;

for (count = 1; count <= NUMBER; count++)

printf("Be my Valentine!\n");

return 0;

}

12 for_cube.c

/* for_cube.c -- 使用for循环创建一个立方表 */

#include

int main(void)

{

int num;

printf(" n n cubed\n");

for (num = 1; num <= 6; num++)

printf("%5d %5d\n", num, num*num*num);

return 0;

}

13 postage.c

// postage.c --邮资

#include

int main(void)

{

const int FIRST_OZ = 46; // 2013 邮资

const int NEXT_OZ = 20; // 2013 邮资

int ounces, cost;

printf(" ounces cost\n");

for (ounces=1, cost=FIRST_OZ; ounces <= 16; ounces++,cost += NEXT_OZ)

printf("%5d $%4.2f\n", ounces, cost/100.0);

return 0;

}

14 zeno.c

/* zeno.c -- 求序列的和*/

#include

int main(void)

{

int t_ct; // 项计数

double time, power_of_2;

int limit;

printf("Enter the number of terms you want: ");

scanf("%d", &limit);

for (time=0, power_of_2=1, t_ct=1; t_ct <= limit;t_ct++, power_of_2 *= 2.0)

{

time += 1.0/power_of_2;

printf("time = %f when terms = %d.\n", time, t_ct);

}

return 0;

}

15 do_while.c

/* do_while.c -- 出口条件循环 */

#include

int main(void)

{

const int secret_code = 13;

int code_entered;

do

{

printf("To enter the triskaidekaphobia therapy club,\n");

printf("please enter the secret code number: ");

scanf("%d", &code_entered);

} while (code_entered != secret_code);

printf("Congratulations! You are cured!\n");

return 0;

}

16 entry.c

/* entry.c -- 出口条件循环 */

#include

int main(void)

{

const int secret_code = 13;

int code_entered;

printf("To enter the triskaidekaphobia therapy club,\n");

printf("please enter the secret code number: ");

scanf("%d", &code_entered);

while (code_entered != secret_code)

{

printf("To enter the triskaidekaphobia therapy club,\n");

printf("please enter the secret code number: ");

scanf("%d", &code_entered);

}

printf("Congratulations! You are cured!\n");

return 0;

}

17 rows1.c

/* rows1.c -- u使用嵌套循环 */

#include

#define ROWS 6

#define CHARS 10

int main(void)

{

int row;

char ch;

for (row = 0; row < ROWS; row++) /* line 10 */

{

for (ch = 'A'; ch < ('A' + CHARS); ch++) /* line 12 */

printf("%c", ch);

printf("\n");

}

return 0;

}

18 rows2.c

// rows2.c -- 依赖外部循环的嵌套循环

#include

int main(void)

{

const int ROWS = 6;

const int CHARS = 6;

int row;

char ch;

for (row = 0; row < ROWS; row++)

{

for (ch = ('A' + row); ch < ('A' + CHARS); ch++)

printf("%c", ch);

printf("\n");

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值