Topic + source code analysis (81 examples)

Posted by Pavel_Nedved on Sat, 19 Feb 2022 10:43:34 +0100

[Title 81]

Title: a company uses a public telephone to transmit data. The data is a four digit integer and is encrypted in the transmission process. The encryption rules are as follows: add 5 to each number, then replace the number with the remainder of sum divided by 10, and then exchange the first and fourth bits, and the second and third bits.
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"
 
int main()
{
    int a,i,aa[4],t;
    printf("Please enter four digits: ");
    scanf("%d",&a);
    aa[0]=a%10;
    aa[1]=a%100/10;
    aa[2]=a%1000/100;
    aa[3]=a/1000;
    for(i=0;i<=3;i++)
    {
        aa[i]+=5;
        aa[i]%=10;
    }
    for(i=0;i<=3/2;i++)
    {
        t=aa[i];
        aa[i]=aa[3-i];
        aa[3-i]=t;
    }
    printf("Encrypted number: ");
    for(i=3;i>=0;i--)
        printf("%d",aa[i]);
    printf("\n");
    return 0;
}

[Title 82]

Title: calculate the number of occurrences of substrings in a string.
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"
#include "stdlib.h"
#include "string.h" 

int main()
{
    int i,j,k,TLen,PLen,count=0;
    char T[50],P[10];
    printf("Please enter two strings,Separated by carriage return,The bus string comes first,Substring after:\n");
    gets(T);
    gets(P);
    TLen=strlen(T);
    PLen=strlen(P);
    for(i=0;i<=TLen-PLen;i++)
    {
        for(j=0,k=i;j<PLen&&P[j]==T[k];j++,k++)
            ;
        if(j==PLen)count++;
    }
    printf("%d\n",count);
    system("pause");
    
    return 0;
}

[Title 83]

Title: input some characters from the keyboard and send them to the disk one by one until one # is entered.
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"
#include "stdlib.h"

int main()
{
    FILE *fp=NULL;
    char filename[25],ch;
    printf("Enter the name of the file you want to save to: \n");
    gets(filename);
    if((fp=fopen(filename,"w"))==NULL)
    {
        printf("error: cannot open file!\n");
        exit(0);
    }
    printf("Now you can enter some characters you want to save,with#End: \ n "");
    getchar();
    while((ch=getchar())!='#')
	{
        fputc(ch,fp);
    }
    fclose(fp);
    system("pause");
    return 0;
}

[Title 84]

Title: input a string from the keyboard, convert all lowercase letters into uppercase letters, and then output it to a disk file "test" to save. The input string is in! end.
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main()
{
    FILE *fp=NULL;
    char str[50];
    int i,len;
    printf("Enter a string: \n");
    gets(str);
    len=strlen(str);
    for(i=0;i<len;i++)
    {
        if(str[i]<='z'&&str[i]>='a')
            str[i]-=32;
    }
    if((fp=fopen("test","w"))==NULL)
    {
        printf("error: cannot open file!\n");
        exit(0);
    }
    fprintf(fp,"%s",str);
    fclose(fp);
    
    system("pause");
    return 0;
}

[Title 85]

Title: there are two disk files A and B, each storing A line of letters. It is required to combine the information in these two files (in alphabetical order) and output them to A new file C.
1. Topic analysis: premise: A.txt and B.txt need to be established.
2. The source code of the title is as follows:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main()
{
    FILE*fa,*fb,*fc;
    int i,j,k;
    char str[100],str1[100];
    char tem;
    if((fa=fopen("A.txt","r"))==NULL)		//A.txt file needs to exist
    {
        printf("error: cannot open A file!\n");
        exit(0);
    }
    fgets(str,99,fa);
    fclose(fa);
    if((fb=fopen("B.txt","r"))==NULL)		//B.txt file needs to exist
    {
        printf("error: cannot open B file!\n");
        exit(0);
    }
    fgets(str1,100,fb);
    fclose(fb);
    strcat(str,str1);
    for(i=strlen(str)-1;i>1;i--)
        for(j=0;j<i;j++)
            if(str[j]>str[j+1])
            {
                tem=str[j];
                str[j]=str[j+1];
                str[j+1]=tem;
            }
    
    if((fc=fopen("C.txt","w"))==NULL)  // Merge into C.txt
    {
        printf("error: cannot open C file!\n");
        exit(0);
    }
    fputs(str,fc);
    fclose(fc);
    system("pause");
    return 0;
}

[Title 86]

Title: there are five students. Each student has the scores of three courses. Input the above data from the keyboard (including student number, name and scores of three courses) to calculate the average score. The original data and the calculated average score are stored in the disk file "study".
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"
#include "stdlib.h"

typedef struct{
    int ID;
    int math;
    int English;
    int C;
    int avargrade;
    char name[20];
}Stu;
int main()
{
    FILE*fp;
    Stu stu[5];
    int i,avargrade=0;
    printf("Please enter the information of 5 students: student number, name and 3 grades:\n");
    for(i=0;i<5;i++)
    {
        scanf("%d %s %d %d %d",&(stu[i].ID),stu[i].name,&(stu[i].math),&(stu[i].English),&(stu[i].C));
        stu[i].avargrade=(stu[i].math+stu[i].English+stu[i].C)/3;
    }
    
    if((fp=fopen("stud","w"))==NULL)
    {
        printf("error :cannot open file!\n");
        exit(0);
    }
    for(i=0;i<5;i++)
        fprintf(fp,"%d %s %d %d %d %d\n",stu[i].ID,stu[i].name,stu[i].math,stu[i].English,
                stu[i].C,stu[i].avargrade);
    
    fclose(fp);		// ==system("pause");
    return 0;
}

[Title 87]

Title: Chinese ancient mathematicians have a question in the "Suanjing": "chicken Weng 1 is worth five; chicken mother 1 is worth three; chicken chick 3 is worth one. How many chickens are chicken Weng, mother and chick when you buy 100 chickens for 100 yuan?" it means: 5 yuan for each Rooster, 3 for each hen and 1 yuan for each chick. How many roosters, hens and chicks are there when you buy 100 chickens for 100 yuan? "
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"

int main()
{
	int x,y,z;			//Three variables are defined to represent the number of cocks, hens and chicks respectively 
	for(x=0;x<=20;x++)
	{
		for(y=0;y<=33;y++)
		{
			z=100-x-y;
			if(z%3==0 && x*5+y*3+z/3==100)			//There are 100 chickens of three breeds 
			printf("cock: %d,hen: %d,chick: %d \n",x,y,z);
		}
	}
	getchar();
	return 0;	
} 

[Title 88]

Title: Fibonacci sequence is also called "rabbit sequence" because mathematician Leonardo Fibonacci took rabbit breeding as an example. Generally speaking, rabbits have the ability to reproduce two months after birth. A pair of rabbits can give birth to a pair of little rabbits every month. If all rabbits do not die, how many pairs of rabbits can they breed in a year?
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"
#define NUM 13

int main()
{
	int i;
	long fib[NUM]={1,1};
	for(i=2;i<NUM;i++)
	{
		fib[i]=fib[i-1]+fib[i-2];	
	}		
	for(i=0;i<NUM;i++)
	{
		printf("The first%d Total number of rabbits per month:%d only \n",i,fib[i]);
	}
	getchar();
	return 0;
}

[Title 89]

Title: the mother prepared a deposit for her son's four-year college life. The way is to deposit in whole and withdraw in zero. It is stipulated that little Sum will withdraw the living expenses of the next month at the end of each month. Now suppose that the annual interest rate of the bank is 1.71%, please write a program to calculate how much money the mother needs to deposit at least?
(when I graduated from Sun University, I had to take 1000 yuan with interest and principal)
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"
#define FETCH 1000
#define RATE 0.0171

int main()
{
	double corpus[49];
	int i;
	corpus[48]=(double)FETCH;
	for(i=47;i>0;i--)
	{
		corpus[i]=(corpus[i+1]+FETCH)/(1+RATE/12);
	}
	for(i=48;i>0;i--)
	{
		printf("%d Total principal and interest at the end of the month:%.2f \n",i,corpus[i]);
	}
	getchar();
	return 0;
}

[Title 90]

Title: suppose there is a lottery of 7 out of 29, each note is composed of 7 numbers from 1 to 29, and these 7 numbers cannot be the same, write a program to generate all number combinations.
1. Topic analysis:
2. The source code of the title is as follows:

#include "stdio.h"
#define MAXN 7 		// Set the number of digits for each lottery ticket
#define NUM 29 		// Set the numbers that make up the lottery

int num[NUM];
int lottery[MAXN];
void combine(int n,int m)
{
	int i,j;
	for(i=n;i>=m;i--)
	{
		lottery[m-1]=num[i-1];		//Save one digit 
		if(m>1)
		combine(i-1,m-1);
		else				//If m=1, a note number will be output 
		{
			for(j=MAXN-1;j>=0;j--)
			{
				printf("%3d",lottery[j]);
			}
			getchar();
			printf("\n");
		}	
	}	
}
int main()
{
	int i,j;
	for(i=0;i<NUM;i++)		//Set the single digit number of the lottery ticket 
	num[i]=i+1;
	for(i=0;i<MAXN;i++)
	{
		lottery[i]=0;
	}
	combine(NUM,MAXN);
	getchar();
	return 0;
}

Topics: C Algorithm data structure