100 sets of grade II C operation questions in the national computer grade examination question bank (the 25th set)

Posted by Kookedoh on Fri, 04 Mar 2022 12:25:28 +0100

Set 25:

The record of personnel consists of number and year, month and day of birth. The data of N personnel has been stored in the structure array std in the main function, and the number is unique. The function fun is to find out the data of the person with the specified number, return it as the function value, and output it by the main function. If the specified number does not exist, the number in the returned data is an empty string. Please fill in the correct content in the underline of the program and delete the underline to make the program get the correct result.
Note: the source program is stored in blank1 under the examinee folder In C.
Do not add or delete lines, nor change the structure of the program!
Given source program:

#include <stdio.h> 
#include <string.h> 
#define N 8 
typedef struct 
{ char num[10]; 
int year,month,day ; 
}STU; 
___1___ fun(STU *std, char *num) 
{ int i; STU a={"",9999,99,99}; 
for (i=0; i<N; i++) 
if( strcmp(___2___,num)==0 ) 
return (___3___); 
return a; 
} 
main() 
{    STU std[N]={ {"111111",1984,2,15},{"222222",1983,9,21},{"333333",1984,9,1}, 
{"444444",1983,7,15},{"555555",1984,9,28},{"666666",1983,11,15}, 
{"777777",1983,6,22},{"888888",1984,8,19}}; 
STU p; char n[10]="666666"; 
p=fun(std,n); 
if(p.num[0]==0) 
printf("\nNot found !\n"); 
else 
{ printf("\nSucceed !\n "); 
printf("%s %d-%d-%d\n",p.num,p.year,p.month,p.day); 
} 
} 

Problem solving ideas:
This question is to find out the record data with the same number from the given personnel data.
First: from the perspective of return value, it returns a structural value, so it should be filled in: STU.
Second: judge whether the number num in the structural variable is equal, so it should be filled in: stu [i] num.
The third place: return the record value with the same number, so it should be filled in: std[i].

Given program modi1 The function fun in C is to find out the number of substrings that are the same as the string indicated by t from the string indicated by s and return it as the function value.
For example, when the content of the string indicated by s is "abcdabfab", the content of the string indicated by t is:
"ab", the function returns the integer 3.
Please correct the mistakes in the program so that it can get the correct results.
Note: do not change the main function, do not add or delete lines, and do not change the structure of the program!
Given source program:

#include <stdio.h> 
#include <string.h> 
int fun (char *s, char *t) 
{ 
int n; char *p , *r; 
n = 0; 
while ( *s ) 
{ p = s; r = t; 
while ( *r ) 
if ( *r == *p ) { 
r++; p++ 
} 
else break; 
if ( r == '\0' ) 
n++; 
s++; 
} 
return n; 
} 
main() 
{ 
char s[100], t[100]; int m; 
printf("\nPlease enter string S:"); scanf("%s", s); 
printf("\nPlease enter substring t:"); scanf("%s", t); 
m = fun( s, t); 
printf("\nThe result is: m = %d\n", m); 
} 

Problem solving ideas:
First place: the semicolon is missing after the statement.
Second: judge whether the current character of R is a string terminator, so it should be changed to: if(*r==0).

The function fun is to delete the characters with even ASCII value in the string referred to by s and the remaining words in the string
Character to form a new string and put it in the array referred to by t.
For example, if the content in the string referred to by s is "ABCDEFG12345", where the ASCII code value of character B
If the ASCII code value of character 2 is even number,..., the ASCII code value of character 2 is even number,... Should be deleted, and so on. Finally, t refers to the contents of the array should be: "ACEG135".
Note: some source programs have the file prog1 In C.
Do not change anything in the main function and other functions. Only fill in the curly brackets of the function fun with some statements you have written.
Given source program:

#include <stdio.h> 
#include <string.h> 
void fun(char *s, char t[]) 
{ 
} 
main() 
{ 
char s[100], t[100]; 
printf("\nPlease enter string S:"); scanf("%s", s); 
fun(s, t); 
printf("\nThe result is: %s\n", t); 
NONO(); 
} 

Problem solving ideas:
This problem is to generate another new string from one string as required. We use the for loop statement to solve this problem.
Reference answer:

void fun(char *s, char t[]) 
{ 
int i, j = 0 ; 
for(i = 0 ; i < strlen(s); i++) if(s[i] % 2) t[j++] = s[i] ; 
t[j] = 0 ; 
}