C Primer Plus 全书源代码(三)
上一篇 / 下一篇 2012-03-19 09:28:09 / 个人分类:学习日志
// scores_in.c -- uses loops for array processing
/*
#include <stdio.h>
#define SIZE 10
#define PAR 72
int main(void)
{
int index, score[SIZE];
int sum = 0;
float average;
/*
#include <stdio.h>
#define SIZE 10
#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]); // read in the ten scores
printf("The scores read in are as follows:\n");
for (index = 0; index < SIZE; index++)
printf("%5d", score[index]); // verify input
printf("\n");
for (index = 0; index < SIZE; index++)
sum += score[index]; // add them up
average = (float) sum / SIZE; // time-honored method
printf("Sum of scores = %d, average = %.2f\n", sum, average);
printf("That's a handicap of %.0f.\n", average - PAR);
for (index = 0; index < SIZE; index++)
scanf("%d", &score[index]); // read in the ten scores
printf("The scores read in are as follows:\n");
for (index = 0; index < SIZE; index++)
printf("%5d", score[index]); // verify input
printf("\n");
for (index = 0; index < SIZE; index++)
sum += score[index]; // add them up
average = (float) sum / SIZE; // time-honored method
printf("Sum of scores = %d, average = %.2f\n", sum, average);
printf("That's a handicap of %.0f.\n", average - PAR);
return 0;
}
*/
///////////////////////////////////////////////
// an answer to the question 16 at page 151
/*
#include<stdio.h>
}
*/
///////////////////////////////////////////////
// an answer to the question 16 at page 151
/*
#include<stdio.h>
void main()
{
int year=0;
double f;
for(f=100;f>0;f=f-10)
{
printf("at the year %d , the money you have is %g \n",year,f);
f=f+f*0.08;
year++;
}
{
int year=0;
double f;
for(f=100;f>0;f=f-10)
{
printf("at the year %d , the money you have is %g \n",year,f);
f=f+f*0.08;
year++;
}
printf("at the year %d\n",year);
printf("you have no money now!\n");
}
printf("you have no money now!\n");
}
*/
///////////////////////////////////////////////
///////////////////////////////////////////////
//chapter 07 C控制语句:分支与跳转
// colddays.c -- finds percentage of days below freezing
/*
#include <stdio.h>
int main(void)
{
const int FREEZING = 0;
float temperature;
int cold_days = 0;
int all_days = 0;
///////////////////////////////////////////////
///////////////////////////////////////////////
//chapter 07 C控制语句:分支与跳转
// colddays.c -- finds percentage of days below freezing
/*
#include <stdio.h>
int main(void)
{
const int FREEZING = 0;
float temperature;
int cold_days = 0;
int all_days = 0;
printf("Enter the list of daily low temperatures.\n");
printf("Use Celsius, and enter q to quit.\n");
while (scanf("%f", &temperature) == 1)
{
all_days++;
if (temperature < FREEZING)
cold_days++;
}
if (all_days != 0)
printf("%d days total: %.1f%% were below freezing.\n",
all_days, 100.0 * (float) cold_days / all_days);
if (all_days == 0)
printf("No data entered!\n");
printf("Use Celsius, and enter q to quit.\n");
while (scanf("%f", &temperature) == 1)
{
all_days++;
if (temperature < FREEZING)
cold_days++;
}
if (all_days != 0)
printf("%d days total: %.1f%% were below freezing.\n",
all_days, 100.0 * (float) cold_days / all_days);
if (all_days == 0)
printf("No data entered!\n");
return 0;
}
*/
///////////////////////////////////////////////
/* cypher1.c -- alters input, preserving spaces */
/*
#include <stdio.h>
#define SPACE ' ' // that's quote-space-quote
int main(void)
{
char ch;
}
*/
///////////////////////////////////////////////
/* cypher1.c -- alters input, preserving spaces */
/*
#include <stdio.h>
#define SPACE ' ' // that's quote-space-quote
int main(void)
{
char ch;
ch = getchar();
while (ch != '\n') //* while not end of line
{
if (ch == SPACE) //* leave the space
putchar(ch); //* character unchanged
else
putchar(ch + 1); //* change other characters
ch = getchar();
}
putchar(ch);
return 0;
}
*/
///////////////////////////////////////////////
while (ch != '\n') //* while not end of line
{
if (ch == SPACE) //* leave the space
putchar(ch); //* character unchanged
else
putchar(ch + 1); //* change other characters
ch = getchar();
}
putchar(ch);
return 0;
}
*/
///////////////////////////////////////////////
// cypher2.c -- alters input, preserving non-letters
/*
#include <stdio.h>
#include <ctype.h> // for isalpha()
int main(void)
{
char ch;
while ((ch = getchar()) != '\n')
{
if (isalpha(ch)) // if a letter,
putchar(ch + 1); // change it
else // otherwise,
putchar(ch); // print as is
}
putchar(ch); // print the newline
/*
#include <stdio.h>
#include <ctype.h> // for isalpha()
int main(void)
{
char ch;
while ((ch = getchar()) != '\n')
{
if (isalpha(ch)) // if a letter,
putchar(ch + 1); // change it
else // otherwise,
putchar(ch); // print as is
}
putchar(ch); // print the newline
return 0;
}
*/
///////////////////////////////////////////////
// chcount.c -- use the logical AND operator
/*
#include <stdio.h>
#define PERIOD '.'
int main(void)
{
int ch;
int charcount = 0;
while ((ch = getchar()) != PERIOD)
{
if (ch != '"' && ch != '\'')
charcount++;
}
printf("There are %d non-quote characters.\n", charcount);
return 0;
}
*/
//////////////////////////////////////////////
// wordcnt.c -- counts characters, words, lines
/*
#include <stdio.h>
#include <ctype.h> // for isspace()
#define STOP '|'
#define false 0
#define true 1
int main(void)
{
char c; // read in character
char prev; // previous character read
long n_chars = 0L; // number of characters
int n_lines = 0; // number of lines
int n_words = 0; // number of words
int p_lines = 0; // number of partial lines
int inword = false; // == true if c is in a word
}
*/
///////////////////////////////////////////////
// chcount.c -- use the logical AND operator
/*
#include <stdio.h>
#define PERIOD '.'
int main(void)
{
int ch;
int charcount = 0;
while ((ch = getchar()) != PERIOD)
{
if (ch != '"' && ch != '\'')
charcount++;
}
printf("There are %d non-quote characters.\n", charcount);
return 0;
}
*/
//////////////////////////////////////////////
// wordcnt.c -- counts characters, words, lines
/*
#include <stdio.h>
#include <ctype.h> // for isspace()
#define STOP '|'
#define false 0
#define true 1
int main(void)
{
char c; // read in character
char prev; // previous character read
long n_chars = 0L; // number of characters
int n_lines = 0; // number of lines
int n_words = 0; // number of words
int p_lines = 0; // number of partial lines
int inword = false; // == true if c is in a word
printf("Enter text to be analyzed (| to terminate):\n");
prev = '\n'; // used to identify complete lines
while ((c = getchar()) != STOP)
{
n_chars++; // count characters
if (c == '\n')
n_lines++; // count lines
if (!isspace(c) && !inword)
{
inword = true; // starting a new word
n_words++; // count word
}
if (isspace(c) && inword)
inword = false; // reached end of word
prev = c; // save character value
}
if (prev != '\n')
p_lines = 1;
printf("characters = %ld, words = %d, lines = %d, ",
n_chars, n_words, n_lines);
printf("partial lines = %d\n", p_lines);
prev = '\n'; // used to identify complete lines
while ((c = getchar()) != STOP)
{
n_chars++; // count characters
if (c == '\n')
n_lines++; // count lines
if (!isspace(c) && !inword)
{
inword = true; // starting a new word
n_words++; // count word
}
if (isspace(c) && inword)
inword = false; // reached end of word
prev = c; // save character value
}
if (prev != '\n')
p_lines = 1;
printf("characters = %ld, words = %d, lines = %d, ",
n_chars, n_words, n_lines);
printf("partial lines = %d\n", p_lines);
return 0;
}
*/
/////////////////////////////////////////////////////
/* paint.c -- uses conditional operator */
/*
#include <stdio.h>
#define COVERAGE 200 // square feet per paint can
int main(void)
{
int sq_feet;
int cans;
printf("Enter number of square feet to be painted:\n");
while (scanf("%d", &sq_feet) == 1)
{
cans = sq_feet / COVERAGE;
cans += (sq_feet % COVERAGE == 0) ? 0 : 1;
printf("You need %d %s of paint.\n", cans,
cans == 1 ? "can" : "cans");
printf("Enter next value (q to quit):\n");
}
}
*/
/////////////////////////////////////////////////////
/* paint.c -- uses conditional operator */
/*
#include <stdio.h>
#define COVERAGE 200 // square feet per paint can
int main(void)
{
int sq_feet;
int cans;
printf("Enter number of square feet to be painted:\n");
while (scanf("%d", &sq_feet) == 1)
{
cans = sq_feet / COVERAGE;
cans += (sq_feet % COVERAGE == 0) ? 0 : 1;
printf("You need %d %s of paint.\n", cans,
cans == 1 ? "can" : "cans");
printf("Enter next value (q to quit):\n");
}
return 0;
}
*/
//////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//chapter 08 字符输入/输出和输入确认
}
*/
//////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//chapter 08 字符输入/输出和输入确认
/* echo.c -- repeats input */
/*
#include <stdio.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '#')
putchar(ch);
printf("\n");
return 0;
}
*/
//////////////////////////////////////////////////////
/* echo_eof.c -- repeats input to end of file */
/*
#include <stdio.h>
int main(void)
{
int ch;
while ((ch = getchar()) != EOF) //now you know how to end the program
putchar(ch);
return 0;
}
*/
//////////////////////////////////////////////////////
// file_eof.c --open a file and
/*
#include <stdio.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '#')
putchar(ch);
printf("\n");
return 0;
}
*/
//////////////////////////////////////////////////////
/* echo_eof.c -- repeats input to end of file */
/*
#include <stdio.h>
int main(void)
{
int ch;
while ((ch = getchar()) != EOF) //now you know how to end the program
putchar(ch);
return 0;
}
*/
//////////////////////////////////////////////////////
// file_eof.c --open a file and
TAG:
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 36043
- 日志数: 31
- 文件数: 6
- 建立时间: 2012-02-02
- 更新时间: 2014-04-02