C语言程序设计-英文版例题0305

Aarays:

#include <stdio.h>
/*count digits, white space, others*/
//main()
//{
// int c, i, nwhite, nother,nm;
// int ndigit[10];
//
// nwhite = nother = nm = 0;
// for (i = 0; i < 10; ++i)
// ndigit[i] = 0;
//
// while ((c = getchar()) != EOF)
// if (c >= '0'&&c <= '9')
// {
// ++ndigit[c - '0'], ++nm; /*ndigit[10]是一个数组{0000000000},
// 如果第i位出现数字就在数组相应的位置ndigit[i]+1,变为ndigit[i]=1
// 重复的数字,还是叠加,例如2出现两次,ndigit[2]=0*/
// }
// else if (c == ' ' || c == '\n' || c == '\t')
// ++nwhite;
// else
// ++nother;
// printf("digits=");
// for (i = 0; i < 10; ++i)
// printf("%d", ndigit[i]); /*将数组中的数按照顺序一个一个打印出来*/
// printf(", white space= %d, other=%d\n, nm=%d\n",
// nwhite, nother, nm);
//}
//举例输入下面的值,得到相应结果
/*123 ab 12 a
^Z
digits = 0221000000, white space = 6, other = 3
, nm = 5

C:\Users\zhoujia4\Desktop\visual studio\ChatRoom_1\ChatRoom\Debug\Chapter1.exe(进程 4404)已退出,返回代码为: 0。
若要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口...*/

word counting

#include <stdio.h>

#define IN 1 /*inside a word*/
#define OUT 0 /*outside a word*/

/* count lines, words, and characters in input */
//main()
//{
// int a, nl, nw, nc, state;
//
// state = OUT;
// nl = nw = nc = 0;
// while ( (a = getchar() ) != EOF) {
// ++nc;
// if (a == '\n')
// ++nl;
// if (a == ' ' || a == '\n' || a == '\t')
// state = OUT;
// else if (state == OUT) {
// state = IN; /*象征字符的应用是精髓,state控制word的取值
// state*/
// ++nw;
// }
// }
// printf("nl:%d nw:%d nc:%d\n", nl, nw, nc);
//}

line couting

#include <stdio.h>
/*count lines in input*/

//int main()
//{
// int c, nl;
// nl = 0;
//
// while ((c = getchar()) != EOF)
// if (c == '\n')
// ++nl;
// printf("%d\n", nl);
//}

character counting

#include <stdio.h>
/*count characters in input; 1st version*/
//main()
//{
// long nc;
// nc = 0;
// while (getchar() != EOF)
// ++nc;
// printf("% 1d\n", nc); /*%1d tells printf that the correspon
// ding argurement is a long integer
// 长整型32bits*/
//}
#include <stdio.h>
/*count characters in input; 2st version*/
//main()
//{
// double nc;
// for (nc = 0; getchar() != EOF; ++nc)
// ; /*a null state 用于满足for循环的要求*/
// printf("%.0f\n", nc); /*%f适合float 和double型,%.0f不输出小数点,
// 小数部分为0*/

temperature change

/* first version */

#include <stdio.h>
//main()
//{
// int fahr, celsius;
// int lower, upper, step;
// int a, b, c;
// scanf_s("%d%d%d", &a, &b, &c);
// lower = a; /*scanf_s("%d%d%d", &a, &b, &c);*/
// upper = b;
// step = c;
// fahr = lower;
// while (fahr < upper) {
// celsius = 5*(fahr - 32) / 9;
// printf("%3d\t%6d\n", fahr, celsius);
// fahr = fahr + step;
//
// }
//}
/*second vesion:floating-point*/
#include <stdio.h>
//main()
//{
// float fahr, celsius;
// float lower, upper, step;
// float a, b, c;
// scanf_s("%f%f%f", &a, &b, &c);
// lower = a; /*scanf_s("%d%d%d", &a, &b, &c);*/
// upper = b;
// step = c;
// fahr = lower;
// while (fahr < upper) {
// celsius = 5.0*(fahr - 32.0) / 9.0;
// printf("%3.0f\t%6.1f\n", fahr, celsius);
// fahr = fahr + step;
//
// }
//}
/*third vesion:for loop*/
//#include <stdio.h>
//main()
//{
// int fahr;
// for (fahr = 300; fahr >= 0; fahr=fahr- 20)
// printf("%3d\t%6.1f\n", fahr, (5.0 / 9.0)*(fahr - 32));
//
//
//}
/*fourth vesion:symbolic constant*/
#include <stdio.h>
//#define lower 0
//#define upper 300
//#define step 20
//
//
//main()
//{
// int fahr;
// for (fahr = lower; fahr <= upper; fahr=fahr+ step)
// printf("%3d\t%6.1f\n", fahr, (5.0 / 9.0)*(fahr - 32));
//
//
//}

 

posted @ 2021-03-05 18:19  曙光中远航  阅读(196)  评论(0)    收藏  举报