1. Preamble
In this post we explain2 months to get computer level 2 C language--True Question 9
2. Procedural fill-in-the-blank questions
2.1 Title requirements
2.2 Code provided
#include <>
double f1(double x)
{
return x * x;
}
double f2(double x, double y)
{
return x * y;
}
/**********found**********/
__1__ fun(int i, double x, double y)
{
if (i == 1)
/**********found**********/
return __2__(x);
else
/**********found**********/
return __3__(x, y);
}
main()
{
double x1 = 5, x2 = 3, r;
r = fun(1, x1, x2);
r += fun(2, x1, x2);
printf("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n\n", x1, x2, r);
getchar();
}
2.3 Problem solving ideas
Fill in the blank at (1):
The question asks for a floating-point output, so the return value can be used as amain
Types used in functionsdouble
。
double fun(int i, double x, double y)
Fill in the blank at (2):
The only parameters filled in here arex
So is the function that callsf1
。
return f1(x);
Fill in the blank at (3):
parameters arex,y
which corresponds to the functionf2
。
return f2(x, y);
2.4 Code Implementation
Fill in the complete code:
#include <>
double f1(double x)
{
return x * x;
}
double f2(double x, double y)
{
return x * y;
}
/**********found**********/
double fun(int i, double x, double y)
{
if (i == 1)
/**********found**********/
return f1(x);
else
/**********found**********/
return f2(x, y);
}
main()
{
double x1 = 5, x2 = 3, r;
r = fun(1, x1, x2);
r += fun(2, x1, x2);
printf("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n\n", x1, x2, r);
getchar();
}
Tip: To ensure that the code works correctly, please test and run it in the corresponding topic of the question bank programming environment.
3. Program modification questions
3.1 Title requirements
3.2 Code provided
#include <>
void fun(int n)
{
int j, b, c, m, flag = 0;
for (b = 1; b <= n / 2; b++) {
/**********found**********/
n = m;
c = b;
while (m != 0 && m >= c) {
/**********found**********/
m = m - c; c++
}
/**********found**********/
if (m != 0)
{
printf("%d=", n);
for (j = b; j < c - 1; j++) printf("%d+", j);
printf("%d\n", j);
flag = 1;
}
}
if (flag == 0)
printf("decomposable\n");
}
main()
{
int n;
printf("Please enter an integer : "); scanf("%d", &n);
fun(n);
getchar();
}
3.3 Problem solving ideas
Revise (1):
The original program wasn = m;
Becausem
This variable was not initialized at the time of its definition, and what it stores internally may be garbage values assigned to then
after that will result inn
Instead of storing the incoming 100, the garbage value is stored, which makes no practical sense and causes the program to run away.
So here's what needs to be madem
initialize ton
inwhile (m != 0 && m >= c)
This loop is constantly moving fromm
minusc
(the value of the current sequence), and letc
increment to account for the next positive integer.
m = n;
Amend (2):
Each statement in C starts with;
As a termination, in the statementc++
It's missing in the back.;
The following is added to the list of items to be included in the list.
m = m - c; c++;
Amend (3):
If there is a change in thewhile (m != 0 && m >= c)
At the end of the loop, the variablem
has a value of 0, indicating that by continually subtracting the current positive integerc
Them
to 0. We previously reduced then
Assigns a value to them
、b
Assigns a value to thec
, which means that from theb
The sum of consecutive positive integers starting at exactly equal ton
。
So ifm
is equal to 0, which means that the sum is foundn
of a set of consecutive positive integers, the corresponding output is performed.
if (m == 0)
3.4 Code Implementation
Modified code:
#include <>
void fun(int n)
{
int j, b, c, m, flag = 0;
for (b = 1; b <= n / 2; b++) {
/**********found**********/
m = n;
c = b;
while (m != 0 && m >= c) {
/**********found**********/
m = m - c; c++;
}
/**********found**********/
if (m == 0)
{
printf("%d=", n);
for (j = b; j < c - 1; j++) printf("%d+", j);
printf("%d\n", j);
flag = 1;
}
}
if (flag == 0)
printf("decomposable\n");
}
main()
{
int n;
printf("Please enter an integer : "); scanf("%d", &n);
fun(n);
getchar();
}
Tip: To ensure that the code works correctly, please test and run it in the corresponding topic of the question bank programming environment.
4. Programming questions
4.1 Title requirements
4.2 Code provided
#include <>
#include <>
void NONO();
int fun(char* t)
{
}
main()
{
char s[26];
printf("Please enter a string consisting of letters : "); gets(s);
if (fun(s)) printf("%s is a string of consecutive letters. \n", s); else printf("%s is not a string of consecutive letters.
else printf("%s is not a string of consecutive letters! \n", s); else printf("%s is not a string of consecutive letters!
NONO();
getchar();
}
void NONO()
{/* This function is used to open a file, input data, call a function, output data, and close the file. */
FILE* fp, * wf.
int i;
char s[26], * p;
fp = fopen("", "r");
wf = fopen("", "w");
for (i = 0; i < 10; i++) {
fgets(s, 26, fp);
p = strchr(s, '\n');
if (p) *p = 0;
if (fun(s)) fprintf(wf, "%s\n", s + 2);
else fprintf(wf, "%s\n", strrev(s));
}
fclose(fp); fclose(wf); else
fclose(wf); }
}
4.3 Problem Solving Ideas
I give two solutions, the second at the end of the article, so let's look at the first one first.
For this question, we first need to knowt
What is the first letter of the string referred to, i.e., thet[0]
value, here I use thechar c = t[0]
to store it, and after the value is known it can be stored according to thet
size traverses it while performing eachc++;
As a comparison of sequences of consecutively incrementing letters, it is sufficient to find once in the loop thet[i]
not equal ≠c
The value of thet
If the string is discontinuous, return 0. When all the elements are equal, the string is a continuous sequence of increasing letters, and 1 is returned.
hypothesist
The string referred to isacd
, for a demonstration of the discontinuous string program:
i c t[i]
1. 0 a a---- > equal to proceed next time
2. 1 b c---- > unequal returns 0
Instead, consecutive strings will be executedfor
Returns 1 after the loop.
4.4 Code Implementation
Fill in the complete code:
#include <>
#include <>
void NONO();
int fun(char* t)
{
char c = t[0];
int i = 0;
for (i = 0; i < strlen(t); i++)
{
if (t[i] ! = c) // when one inequality is detected, it means it's not continuously incrementing
{
return 0; // then return 0
}
c++.
}
return 1; // Iterate through the string and return 1 if no inequality is found.
}
main()
{
char s[26]; printf("Please enter a string of letters : "); gets(s)
printf("Please enter a string consisting of letters : "); gets(s);
if (fun(s)) printf("%s is a string of consecutive letters. \n", s); else printf("%s is not a string of consecutive letters.
else printf("%s is not a string of consecutive letters! \n", s); else printf("%s is not a string of consecutive letters!
NONO();
getchar();
}
void NONO()
{/* This function is used to open a file, input data, call a function, output data, and close the file. */
FILE* fp, * wf.
int i;
char s[26], * p;
fp = fopen("", "r");
wf = fopen("", "w");
for (i = 0; i < 10; i++) {
fgets(s, 26, fp);
p = strchr(s, '\n');
if (p) *p = 0;
if (fun(s)) fprintf(wf, "%s\n", s + 2);
else fprintf(wf, "%s\n", strrev(s));
}
fclose(fp); fclose(wf); else
fclose(wf); }
}
Tip: To ensure that the code works correctly, please test and run it in the corresponding topic of the question bank programming environment.
Another method is to first generate a continuous sequence of incremental letters based on the first element and the length, using the functionstrcmp()
expense or outlayt
Comparing with it, you can also determine whether it is continuous or not, the code is given below for your reference:
int fun(char* t)
{
char c[26] = { 0 };
int i = 0;
// First produce a sequence of consecutively increasing letters based on the value of t[0], and its size
c[0] = t[0]; for (i = 0; i &l)
for (i = 0; i < strlen(t); i++)
{
c[i] = c[0] + i;
}
// Use the strcmp() function to determine if the two strings are equal
if (strcmp(c, t) == 0) // if strcmp() returns 0 then the strings are equal
{
return 1; // then 1 is returned
}
else {
return 0; // if they are not equal, return 0
}
5. Postscript
This is the end of this blog, if you have questions or suggestions you are welcome to leave them in the comments section.