C language: the core steps of array programming (part)

The following share personal findings and ability improvement after doing programming questions.

This sharing:

  1. Code display
  2. Code analysis (focus on the core part);

Example 1:
Translate Arabic numerals into Roman numerals;

The original answer to this programming question was made with pointers, and then the teacher changed it to a three-dimensional array to solve it:
code show

#include<stdio.h>
#include<string.h>
int main()
{
    
    
	 char a[][10][10]={
    
    "","I","II","III","IV","V","VI","VII","VIII","IX",
						"","X","XX","XXX","XL","L","LX","LXX","LXXX","XCC",
						"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"
						};
	int n,t,i,m;
	printf("Please input number: ");
	scanf("%d",&n);
	printf("Output:\n");
	if(n<=0 || n>=1000)
	{
    
    
		printf("Input error!\n");
		return 0;
	}
	printf("%d=",n);
	for(m=0,i=1000;m<3;m++,i/=10)
	{
    
    
		t=(n%i)/(i/10);//核心。
		printf("%s",a[2-m][t]);
	}                          
	putchar(10);
	return 0;
}

A three-dimensional array is needed, and then the hundreds, tens, and digits are extracted, and then one-to-one mapping to the three-dimensional array can be converted.

Core algorithm part:

	for(m=0,i=1000;m<3;m++,i/=10)
	{
    
    
		t=(n%i)/(i/10);
		printf("%s",a[2-m][t]);
	}                          

m=0;m<3; using 1000 to mod can realize the separation of singles, tens and hundreds

putchar(10);
The output of putchar(10) here is'\0', which is output as a newline, which is similar to printf("\n");

From this (n%i)/(i/10) expression,
it is worth noting that the value of n%m (n<m) is 1. It is not 0 (because it was always considered to be 0 in the past)...

Example 2:
Count the number of letters in a word;

Code display:

#include <stdio.h>
#include <string.h>

int main()
{
    
    
        char str[1000];                // 用来读取整个字符串
        char *pStr;                        // 一个字符指针,用来存储以空格和点号分割的字符串
	    int iCount = 0;        // 记录已经有多少数字输出了,由于每个数字间用一个空格分开,因而需要记录数字的输出数目
		printf("please input str:");
        gets(str);
		
                pStr = strtok(str, " .");        // 先用空格和点号作一次切割
              				printf("Output:\n");
                while(pStr)
				{
    
    
                        if(iCount++)
						{
    
    
                                putchar(' ');        // 数字间使用一个空格分割
                        }
                        printf("%d", strlen(pStr));        // 输出单词的长度
                        pStr = strtok(NULL, " .");        // 做下一次分割
                }

                putchar('\n');                        // 别忘了换行
        

        return 0;
}


Since the use of pointers is involved here, I have to dabble in advance (the winter vacation was expected to start)...

Code analysis is reflected in the code one by one,
char *pStr strtok(str," .");
pStr = strtok(NULL, "."); These involve pointer parts, learn this part first to solve similar problems.

Here is a special description of NULL in the code;
*p=NULL, the change of the pointer is that p points to a memory whose address is not 0, which reminds that the pointer cannot be referenced. NULL is actually 0x0. A
null pointer refers to a pointer that does not locate memory, and the value is NULL (0).

Guess you like

Origin blog.csdn.net/yooppa/article/details/112315281