当前位置:网站首页>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 .
边栏推荐
- What is the mystery of the gate of the meta universe?
- Today's code farmer girl summarized her notes about NPM package management and URL module
- (4) Operator
- "My" bug collection (Reprinted)
- Force buckle - 10. Regular expression matching
- Backpack model acwing 1022. Collection of pet elves
- Lazy loading of lists and pictures
- 最长上升子序列模型 AcWing 482. 合唱队形
- 最长上升子序列模型 AcWing 1010. 拦截导弹
- Students, don't copy all my code, remember to change it, or we both want G
猜你喜欢

C# 自定义集合

The longest ascending subsequence model acwing 1017. The glider wing of the strange thief Kidd

状态压缩DP AcWing 91. 最短Hamilton路径

A deep analysis of the soul of C language -- pointer

Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?

Vscode establishes automatic search of header files under non engineering directories

JVM judges that the object is dead, and practices verify GC recycling

Chinese remainder theorem acwing 204. strange way of expressing integers

2022 Niuke multi school (3) j.journey
![[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. 友好城市
Error while unzipping file in win10: unable to create symbolic link. You may need to run WinRAR with administrator privileges. The client does not have the required privileges
properties文件
Longest ascending subsequence model acwing 1014. mountaineering
Memory search acwing 901. Skiing
Inclusion exclusion principle acwing 890. divisible numbers
What is the issuing price of NFT (Interpretation of NFT and establishment of NFT world outlook)
The longest ascending subsequence model acwing 1017. The glider wing of the strange thief Kidd
FAQs of "relay chain" and "dot" in Poka ecosystem
Tree DP acwing 285. dance without boss
6 find the smallest letter larger than the target letter
洛谷P1896 互不侵犯
(9) Shell I / O redirection
什么是私域流量?
Force buckle - 10. Regular expression matching
求组合数 AcWing 889. 满足条件的01序列
(4) Operator
高斯消元 AcWing 884. 高斯消元解异或线性方程组
WGet warning: unable to verify
Knapsack problem acwing 9. grouping knapsack problem