当前位置:网站首页>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 .
边栏推荐
- Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
- 数字三角形模型 AcWing 1027. 方格取数
- XXX packages are looking for funding run 'NPM fund' for details solutions
- Solutions to errors in tensorflow operation
- Vscode establishes automatic search of header files under non engineering directories
- 349两个数组的交集和01两数之和
- The longest ascending subsequence model acwing 1017. The glider wing of the strange thief Kidd
- 12 is at least twice the maximum number of other numbers
- SQL Server2000 database error
- 求组合数 AcWing 888. 求组合数 IV
猜你喜欢

Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?

Redis simple to use
![Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.](/img/7f/f42f9267cdff35522c260ef073bab9.png)
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.

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

Moveit2 - 4. robot model and robot state

Game theory acwing 893. Set Nim game

Digital triangle model acwing 1015. Picking flowers

Quantitative industry knowledge summary

Remember an experience of using canvas to make the banner streamer effect of Tencent cloud homepage

区间问题 AcWing 906. 区间分组
随机推荐
Find the combination number acwing 886. find the combination number II
数字三角形模型 AcWing 1018. 最低通行费
Pat (Grade B) 2022 summer exam
Digital triangle model acwing 275. pass note
Digital triangle model acwing 1027. Grid retrieval
10 complete half of the questions
MySQL installation (RPM package)
求组合数 AcWing 889. 满足条件的01序列
Smart pointer (shared_ptr, unique_ptr, weak_ptr)
FAQs of "relay chain" and "dot" in Poka ecosystem
When std:: bind meets this
15 design movie rental system
Longest ascending subsequence model acwing 1010. Interceptor missile
6 find the smallest letter larger than the target letter
Gaussian elimination acwing 883. solving linear equations with Gaussian elimination
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
11 wrong set
Find the combination number acwing 885. find the combination number I
2022 Niuke multi school (3) j.journey
数字三角形模型 AcWing 1027. 方格取数