当前位置:网站首页>The C programming language (2nd) -- Notes -- 1.6
The C programming language (2nd) -- Notes -- 1.6
2022-07-27 11:28:00 【YovaVan】
1.6 Array
Programming statistics 、 Blank symbols ( Space character 、 Box drawings and line breaks ) And the number of occurrences of all other characters . The program is of little practical significance , But it can be discussed through this procedure C Many aspects of language .
All inputs ( character ) Can be divided into 12 class , Use an array to store the number of times each number appears than use 10 Independent variables are more convenient . Program version 1 :
#include <stdio.h>
/* count digits, white space, others */
main ()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar ()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'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",
nwhite, nother );
}
When taking the program itself as input , The output is :
digits = 9 3 0 0 0 0 0 0 0 1,white space = 123, other = 345
Declaration statements in the program
int ndigit[10];Declare the variable as 10 An array of integer numbers . stay C in , Array subscripts always start from 0 Start , So... Of the array 10 The elements are ndigit[0]、ndigit[1]、…、ndigit[9], This can be done by initializing and printing two in the array for Circular statements reflect .
The array subscript can be any integer expression , Including integer variables ( Such as i) And integer constants .
The execution of this program depends on the number Characters represent attributes . Such as test statement
if(c >= '0' && c <= '9')Used to judge whether the characters in are numbers . If it is , This number corresponds to
c -'0'Only when '0'、'1'、…、'9' With continuously increasing values , This approach is feasible . Fortunately, , All character sets are like this .
By definition ,char The characters of type are small integers , therefore char Variables and constants of type are equivalent to in arithmetic expressions int Constants and variables of type . This is natural and convenient , for example ,c - '0' Is an integer expression , If stored in c The characters in are '0'~'9', Its value will be 0~9, Therefore, it can act as an array ndigit Legal subscript of .
Judge whether a character is a number 、 Whitespace or other characters can be completed by the following statement sequence :
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite ;
else
++nother;The following methods are often used in programs to express and determine multiple ways :
if( Conditions 1)
sentence 1
else if ( Conditions 2)
sentence 2
…
…
else
sentence nIn this way , The conditions are evaluated from front to back , Until a condition is met , Then execute the corresponding statement part . After the execution of this part of the statement , The execution of the whole statement body ends ( Any of these statements can be several statements locked in curly braces ). If all conditions are not met , The execution is at the last else The following statement ( If you have any ). Similar to the previous word counting program , If there is no last else And corresponding sentences , The body of the statement will not perform any actions . At the first if With the last else Between 0 Or multiple statement sequences in the following forms :
else if( Conditions )
sentence In terms of programming style , Readers are advised to use the indentation format shown above to reflect the hierarchical relationship of the structure , otherwise , If each if Than the previous one else Indent some distance in , Then the long decision sequence may exceed the right boundary of the page .
The first 3 Chapter will discuss switch Statement provides another way to write multi branch programs . It is especially suitable for determining whether an integer or character expression matches an element in a constant set . In the fourth 3.4 Section for switch Statement to write another version of the program .
practice 1-13 Programming , Print a histogram of the length of words in the input . The histogram in the horizontal direction is easier to draw , Histogram in vertical direction is more difficult .
practice 1-14 Programming , Print the histogram of the occurrence frequency of each character in the input .
边栏推荐
- [FPGA tutorial case 40] communication case 10 -- Verilog implementation of a simple OFDM system based on FPGA
- 6 find the smallest letter larger than the target letter
- 9 UAV array
- Stack acwing 3302. Expression evaluation
- Verilog implementation of ECG signal acquisition, storage and transmission system based on FPGA
- Sorry, you guys have something to deal with in the bank recently, which has been delayed
- 15 design movie rental system
- 2022 Niuke multi school (3) j.journey
- Knapsack model acwing 423. Picking herbs
- TensorFlow张量运算函数集
猜你喜欢

49 letter ectopic grouping and 242 effective letter ectopic words

tensorflow运行报错解决方法
![[shader realizes shake random shaking effect _shader effect Chapter 10]](/img/49/99669ebc3ba59a0277bb8bc928f576.png)
[shader realizes shake random shaking effect _shader effect Chapter 10]

最长上升子序列模型 AcWing 1012. 友好城市

博弈论 AcWing 892. 台阶-Nim游戏

Backpack model acwing 1022. Collection of pet elves

FAQs of "relay chain" and "dot" in Poka ecosystem

Instructions for mock platform

Digital triangle model acwing 1018. Minimum toll

求组合数 AcWing 886. 求组合数 II
随机推荐
Internal and external troubles of digital collection NFT "boring ape" bayc
When std:: bind meets this
记忆化搜索 AcWing 901. 滑雪
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
PAT(乙级)2022年夏季考试
49字母异位分组和242有效的字母异位词
Wechat push - template message parameter configuration
WGet warning: unable to verify
9 UAV array
深析C语言的灵魂 -- 指针
Yum source installation
最长上升子序列模型 AcWing 1014. 登山
ACM warm-up Exercise 1 in 2022 summer vacation (summary)
First experience of three.js: simulating the growth of a small branch
数字三角形模型 AcWing 1015. 摘花生
14 check whether integers and their multiples exist
Description and feelings
Maximized array sum after 13 K negations
2022牛客多校 (3)J.Journey
高斯消元 AcWing 883. 高斯消元解线性方程组