C Primer Plus 第十二章程序清单……2015.5.10
来源:互联网 发布:汕头淘宝村地址 编辑:程序博客网 时间:2024/05/18 00:52
C Primer Plus
第五版
第十二章 程序清单
#include<stdio.h>int main()
{
int x=30;
printf("x in outer block:%d\n",x);
{
int x=77;
printf("x in inner block:%d\n",x);
}
printf("x in outer block:%d\n",x);
while(x++<33)
{
int x=100;
x++;
printf("x in while loop:%d\n",x);
}
printf("x in outer loop:%d\n",x);
return 0;
}
2、
#include <stdio.h>
int main ()
{
int n = 10;
printf ("Initially, n = %d \n", n);
for (int n = 1; n < 3; n++)
printf ("loop 1: n = %d \n",n);
printf ("After loop 1, n = %d \n", n);
for (int n = 1; n < 3; n++)
{
printf ("loop 2 index n = %d \n", n);
int n = 30;
printf ("loop 2: n = %d \n",n);
n++;
}
printf ("After loop 2, n = %d \n",n);
return 0;
}
程序清单12.3
#include<stdio.h>
void trysat(void);
int main(void)
{
int count;
for(count=1;count<=3;count++)
{
printf("Here comes lterration%d:
\n",count);
trysat();
}
return 0;
}
void trysat(void)
{
int fade=1;
static int stay=1;
printf("fade=%d and stay=%d\n",fade++,stay++);
}
程序清单12.4
#include<stdio.h>
int units=0;
void critic(void);
int main(void)
{
extern int units;
printf("how many pounds to a firkin of butter?
\n");
scanf("%d",&units);
while(units!=56)
critic();
printf("You must have looked it up! %d
\n",units);
return 0;
}
void critic(void)
{
printf("Noluck");
scanf("%d",&units);
}
#include<stdio.h>
void report_count();
void accumulate(int k);
int count=0;
int main(void)
{
int value;
register int i;
printf("Enter a positive integer:");
while(scanf("%d",&value)==1&&value>0)
{
++count;
for(i=value;i>=0;i--)
accumulate(i);
printf("Enter a positive integer ");
}
report_count();
return 0;
}
void report_count()
{
printf("Loop executed %d times\n",count);
}
#include<stdio.h>
extern int count;
static int total=0;
void accumulate(int k);
void accumulate(int k)
{
static int subtotal=0;
if(k<=0)
{
printf("loop cycle:%d\n",count);
printf("subtotal:%d; total:%d
\n",subtotal,total);
subtotal=0;
}
else
{
subtotal+=k;
total+=k;
}
}
程序清单12.7
static unsigned long int next=1;
int rand0(void)
{
next=next*1103515245+12345;
return (unsigned int)(next/65536)%32768;
}
程序清单12.8
#include<stdio.h>
extern int rand0(void);
int main(void)
{
int count;
for(count=0;count<5;count++)
printf("%hd\n",rand0());
return 0;
}
程序清单12.9
#include<stdio.h>
extern void srand1(unsigned int x);
extern int rand1(void);
int main(void)
{
int count;
unsigned seed;
printf("Please enter your choice for seed.\n");
while(scanf("%u",&seed)==1)
{
srand1(seed);
for(count=0;count<5;count++)
printf("%hd\n",rand1());
printf("Please enter next seed\n");
}
printf("Done\n");
return 0;
}
程序清单12.10
static unsigned long int next=1;
int rand1(void)
{
next=next*1103515245+12345;
return (unsigned int)(next/65536)%32768;
}
void srand1(unsigned int seed)
{
next=seed;
}
程序清单9.11
#include"diceroll.h"
#include<stdio.h>
#include<stdlib.h>
int roll_count=0;
static int rollem(int sides)
{
int roll;
roll=rand()%sides+1;
++roll_count;
return roll;
}
int roll_n_dice(int dice,int sides)
{
int d;
int total=0;
if(sides<2)
{
printf("Need at least 2 sides.\n");
return -2;
}
if(dice<1)
{
printf("Need at least 1 die.\n");
return -1;
}
for(d=0;d<dice;d++)
total+=rollem(sides);
return total;
}
程序清单9.12
extern int roll_count;
int roll_n_dice(int dice,int sides);
程序清单9.13
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"diceroll.h"
int main(void)
{
int dice,roll;
int sides;
srand((unsigned int)time(0));
printf("Enter the number of sides per die.0 to
stop.\n");
while(scanf("%d",&sides)==1&&sides>0)
{
printf("How many dice?\n");
scanf("%d",&dice);
roll=roll_n_dice(dice,sides);
printf("You have rolled a %d using %d %d
sided dice.\n",roll,dice,sides);
printf("How many sides?\n");
}
printf("The rollem %d times.\n",roll_count);
printf("GOOD");
return 0;
}
程序清单12.14
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
double *ptd;
int max;
int number;
int i=0;
puts("What is the ?");
scanf("%d",&max);
ptd=(double*)malloc(max*sizeof(double));
if(ptd==NULL)
{
puts("Memory allocation faild\n");
exit(EXIT_FAILURE);
}
puts("Enter the values");
while(i<max&&scanf("%lf",&ptd[i])==1)
++i;
printf("Here are your %d entries\n",number=i);
for(i=0;i<number;i++)
{
printf("%7.2f",ptd[i]);
if(i%7==6)
putchar('\n');
}
if(i%7!=0)
putchar('\n');
puts("Done.");
free(ptd);
return 0;
}
1 0
- C Primer Plus 第十二章程序清单……2015.5.10
- C Primer Plus 第十章程序清单……2015.5.3
- C Primer Plus 第十一章程序清单……2015.5.7
- C Primer Plus 第十三章程序清单……2015.5.15
- C Primer Plus 第七章程序清单……2014.4.24
- C Primer Plus 第九章程序清单……2015.4.28
- C Primer Plus 第五版 前四章程序清单
- C Primer Plus 第六章程序清单——2015.4.22
- C Primer Plus 第十二章课后习题……2015.5.10
- C Primer Plus 第十二章学习总结……2015.5.5
- C Primer Plus 第八章 程序清单……2015.4.27
- C primer plus 第十章 例题疑问(清单10.18):
- C Primer Plus 程序清单及注释解释
- C primer plus(第五版)编程练习第十二章
- C Primer Plus 第十二章 编程练习 1-8题
- C++Primer Plus(第六版) 第十二章 第一题
- C++Primer Plus(第六版) 第十二章 第二题
- C ++ Primer Plus 第六版 第十二章编程练习答案
- PHP中的魔术方法详解
- Ubuntu14.10中安装eclipse后打开无菜单栏解决办法
- java 重入锁死
- 黑马程序员------------JAVA基础----单例设计模式
- C# 基础进阶 HTML语言一 标签、表格
- C Primer Plus 第十二章程序清单……2015.5.10
- Spring mvc 中处理模型数据 ModelAndView
- SQL Server 表和索引存储结构
- Tomcat容器的四个等级
- 迷宫
- 小白也刷题---leetcode去除数组中的重复元素
- cf-467B. Fedor and New Game
- Centos通过phpize安装php扩展
- MYSQL explain详解