Current location - Training Enrollment Network - Mathematics courses - C language level 2 100 set of questions
C language level 2 100 set of questions
I'll send you a part below and send it to your mailbox.

Nankai problem set type:

First, replace the characters. ( 1、3、5、7、9、27、30、73、79、9 1、93、95、97、99)

Second, the left and right sorting and comparison of strings. (2、4、6、 12、26、29、7 1、72、74、84、90、96、98、 100)

Third, sort the positive integers (including divisor, finding the maximum and minimum) to get the average value. (8、3 1、33、35、37、39、42、

44、46、47、48、49、50、5 1、52、53、54、56、58、60、6 1、63、65、67、69、92、94、)

Fourthly, the comparative arrangement of the five elements of products is a structural operational problem. ( 1 1、 13、 15、 17、 19、8 1、83、85、87、89 )

5. Prime numbers. ( 14、22、28、5 1、80 )

Sixth, number sorting. ( 10、 18、)

Seven, other mathematical calculations. ( 16、20、36、43、55、57、59、64、66、78、86、88 )

Eight, the calculation of numbers or characters after shifting. (2 1、23、4 1、45、77 )

Nine, student performance, structural problems. (24、76 )

X. inversion and deletion of strings (words). (25、75 )

XI。 The distribution of votes. (32、68、70、82 )

Twelve, out of the circle. (34 )

Thirteen, decimal conversion. (38、40、62 )

1 function ReadDat () realizes reading an English article from ENG file. And store it in a string array xx; Please compile the function encryptChar () to replace all the characters in the array xx according to the given replacement relation, and still store them in the corresponding position of the array xx. Finally, call the function WriteDat () to output the result xx to the file PS10.dat.

Replacement relation: f(p) = p *11mod256 (p is the ASCII value of a character in the array, and f (p) is the ASCII value of the new character after calculation). If the ASCII value of the original character is even or the calculated f(p) value is less than or equal to 32, the character remains unchanged, otherwise F (p) corresponds.

Some source programs are given. The format of the original data file is: the width of each line is less than 80 characters.

Do not change the content of the main function (), read the data function ReadDat () and the output data function WriteDat ().

# include & ltstdio.h & gt

# include & ltstring.h & gt

# include & ltconio.h & gt

# include & ltctype.h & gt

Unsigned character xx [50] [80];

int maxline = 0; /* Total number of articles */

int ReadDat(void)

void WriteDat(void)

void encryptChar()

{

}

void main()

{

clr SCR();

if(ReadDat()){

Printf ("data file ENG. Unable to open! \ n \ 007 ");

Return;

}

encrypt char();

write dat();

}

int ReadDat(void)

{

FILE * fp

int I = 0;

Unsigned char * p;;

If((fp=fopen("eng.in ","r ")= = NULL) returns1;

while(fgets(xx[i],80,fp)! =NULL){

p=strchr(xx[i],' \ n ');

if(p)* p = 0;

i++;

}

maxline = I;

fclose(FP);

Returns 0;

}

void WriteDat(void)

{

FILE * fp

int I;

fp=fopen("ps 10.dat "," w ");

for(I = 0; I & ltmaxlinei++){

printf("%s\n ",xx[I]);

fprintf(fp," %s\n ",xx[I]);

}

fclose(FP);

}

________________________________________

Note: In the ReadDat () function, the fgets () function did not read the string terminator' \0' when reading data, because

Instead, at the end of each row of the xx array, use the while () loop to replace the newline' \n' with the terminator' \0'.

The function written is as follows: the basic algorithm of this function is-let the character pointer pf point to the beginning of each line, and then go one by one.

After moving, transform as needed during moving. *pf%2==0 is used to judge whether it is an even number. If () condition

Sentences are used to control irreplaceable characters.

Solution 1:

void encryptChar()

{

int I;

char * pf

for(I = 0; I & ltmaxlineI++) /* Line loop */

{ pf = xx[I]; /* Number of characters per line */

And (*pf! =0)

{ if(* pf % 2 = = 0 | | * pf * 1 1% 256 & lt; 32)

{ pf++; Continue; }

* pf = * pf * 1 1% 256;

pf++;

}

}

}

Solution 2:

void encryptChar()

{

int i,j,t;

for(I = 0; I & ltmaxlinei++)

{

for(j = 0; j & ltstrlen(xx[I]); j++)

{

t = xx[I][j]* 1 1% 256;

if(t & lt; =32 || xx[i][j]%2==0) Continue;

xx[I][j]= t;

}

}

}

The 2-question function ReadDat () reads 20 lines of data from the file in.dat and stores them in the string array xx (the length of the first line of string is less than 80). Please compile the function jsSort (), which is used to sort strings according to the given conditions, and the sorted results are still stored in the string array xx line by line. Finally, call the function WriteDat () to output the result xx to the file out.dat

Condition: the string is divided into two parts, and the left part is sorted in ascending order by ASCII values of characters. After sorting, the left half is exchanged with the right half. If the length of the original string is odd, the middle character does not participate in the processing, and the characters are still placed in the original position.

For example: location 0 1 2 3 4 5 6 7 8

Source string d c b a h g f e

4 3 2 1 9 8 7 6 5

Then process the string h g f e a b c d.

8 7 6 5 9 1 2 3 4

Some source programs are given.

Do not change the contents of the main function (), read the function ReadDat () and write the function WriteDat ().

# include & ltstdio.h & gt

# include & ltstring.h & gt

# include & ltconio.h & gt

char xx[20][80];

void jsSort()

{

}

void main()

{

read dat();

jsSort();

write dat();

}

readDat()

{

File * in

int I = 0;

char * p;

in=fopen("in.dat "," r ");

While (I & lt20 & fgets (xx [I], 80, in)! =NULL){

p=strchr(xx[i],' \ n ');

if(p)* p = 0;

i++;

}

fclose(in);

}

writeDat()

{

FILE * out();

int I;

clr SCR();

out=fopen("out.dat "," w ");

for(I = 0; I & lt20; i++){

printf(\"%s\n ",xx[I]);

fprintf(out," %s\n ",xx[I]);

}

fclose(out);

}

________________________________________

Note: First, the left part is sorted in ascending order by bubbling method, and then the sorted left and right parts are sorted according to the corresponding positions.

Exchange.

void jsSort()

{

int i,strl,half,j,k;

char ch

for(I = 0; I & lt20; I++) /* Line loop */

{ strl = strlen(xx[I]); /* Length of each line */

half = strl/2;

for(j = 0; J< semi-1; J++) /* J position of each line */

for(k = j+ 1; K< half; k++)

if(xx[I][j]& gt; xx[i][k])

{ ch = xx[I][j]; /* Assign the minimum number to xx[i][j]*/

xx[I][j]= xx[I][k];

xx[I][k]= ch;

}

for(j=half- 1,k = strl- 1; j & gt=0; j -,k -)

{ ch = xx[I][j];

xx[I][j]= xx[I][k];

xx[I][k]= ch;

}

}

}

void jsSort()

{

int i,j,k,strl

char ch

for(I = 0; I & lt20; i++)

{

strl = strlen(xx[I]);

for(j = 0; j & ltstrl/2; j++)

for(k = j+ 1; k & ltstrl/2; k++)

if(xx[I][j]& gt; xx[i][k])

{

ch = xx[I][j];

xx[I][j]= xx[I][k];

xx[I][k]= ch;

}

for(j = 0; j & ltstrl/2; j++)

{

ch = xx[I][j];

xx[I][j]= xx[I][(strl+ 1)/2+j];

xx[I][(strl+ 1)/2+j]= ch;

}

}

}

The three-question function ReadDat () realizes reading an English article from the ENG file. And store it in a string array xx; Please compile the function encryptChar () to replace all the characters in the array xx according to the given replacement relation, and still store them in the corresponding position of the array xx. Finally, call the function WriteDat () to output the result xx to the file PS8.DAT

Replacement relation: f(p) = p *11mod256 (p is the ASCII value of one character in the array, and f (p) is the ASCII value of a new character after calculation). If the original character is lowercase or the calculated value of f(p) is less than or equal to 32, the character remains unchanged, otherwise the character corresponding to f(p) will change.

Some source programs are given. The format of the original data file is: the width of each line is less than 80 characters.

Do not change the content of the main function (), read the data function ReadDat () and the output data function WriteDat ().

# include & ltstdio.h & gt

# include & ltstring.h & gt

# include & ltconio.h & gt

# include & ltctype.h & gt

Unsigned character xx [50] [80];

int maxline = 0; /* Total number of articles */

int ReadDat(void)

void WriteDat(void)

void encryptChar()

{

}

void main()

{

clr SCR();

if(ReadDat()){

Printf ("data file ENG. Unable to open! \ n \ 007 ");

Return;

}

encrypt char();

write dat();

}

int ReadDat(void)

{

FILE * fp

int I = 0;

Unsigned char * p;;

If((fp=fopen("eng.in ","r ")= = NULL) returns1;

while(fgets(xx[i],80,fp)! =NULL){

p=strchr(xx[i],' \ n ');

if(p)* p = 0;

i++;

}

maxline = I;

fclose(FP);

Returns 0;

}

void WriteDat(void)

{

FILE * fp

int I;

fp=fopen("ps8.dat "," w ");

for(I = 0; I & ltmaxlinei++){

printf("%s\n ",xx[I]);

fprintf(fp," %s\n ",xx[I]);

}

fclose(FP);

}

________________________________________

Note: Similar problems 1.

void encryptchar()

{

int I;

char * pf

for(I = 0; I & ltmaxlinei++)

{ pf = xx[I];

And (*pf! =0)

{ if((* pf & gt; = ' a ' & amp& amp*pf<。 = ' z ')| | * pf * 1 1% 256 & lt; 32)

{ pf++; Continue; }

* pf = * pf * 1 1% 256;

pf++;

}

}

}

void encryptChar()

{

int i,j,t;

for(I = 0; I & ltmaxlinei++)

{

for(j = 0; j & ltstrlen(xx[I]); j++)

{

t = xx[I][j]* 1 1% 256;

if(t & lt; = 32 | |(xx[I][j]& gt; = ' a ' & amp& ampxx[I][j]& lt; ='z')) Continue;

xx[I][j]= t;

}

}

}

The 4-question function ReadDat () reads 20 lines of data from the file in.dat and stores them in the string array xx (the length of the first line of string is less than 80). Please compile the function jsSort (), which is used to sort strings according to the given conditions, and the sorted results are still stored in the string array xx line by line. Finally, call the function WriteDat () to output the result xx to the file out.dat

Condition: the string is divided into two parts, and the left part is arranged in descending order by ASCII values of characters. After sorting, the left half is exchanged with the right half. If the length of the original string is odd, the middle character does not participate in the processing, and the characters are still placed in the original position.

For example: location 0 1 2 3 4 5 6 7 8

Source string a b c d h g f e

1 2 3 4 9 8 7 6 5

Then the processed string h g f e d c b a

8 7 6 5 9 4 3 2 1

Some source programs are given.

Do not change the content of the main function (), read the data function ReadDat () and the output data function WriteDat ().

# include & ltstdio.h & gt

# include & ltstring.h & gt

# include & ltconio.h & gt

char xx[20][80];

void jsSort()

{

}

void main()

{

read dat();

jsSort();

write dat();

}

readDat()

{

File * in

int I = 0;

char * p;

in=fopen("in.dat "," r ");

While (I & lt20 & fgets (xx [I], 80, in)! =NULL){

p=strchr(xx[i],' \ n ');

if(p)* p = 0;

i++;

}

fclose(in);

}

writeDat()

{

FILE * out();

int I;

clr SCR();

out=fopen("out.dat "," w ");

for(I = 0; I & lt20; i++){

printf("%s\n ",xx[I]);

fprintf(out," %s\n ",xx[I]);

}

fclose(out);

}

________________________________________

Note: This question is arranged in descending order of insertion method, and others are similar to question 2.

void jsSort()

{

int i,strl,half,j,k;

char ch

for(I = 0; I & lt20; i++)

{ strl = strlen(xx[I]);

half = strl/2;

for(j = 1; J< half; j++)

{ ch = x[I][j];

k = j- 1;

while((k & gt; = 0)& amp; & amp(ch & gtxx[i][k])

{ xx[I][k+ 1]= xx[I][k];

k-;

}

xx[I][k+ 1]= ch;

}

for(j=half- 1,k = strl- 1; j & gt=0; j -,k -)

{ ch = xx[I][j];

xx[I][j]= xx[I][k];

xx[I][k]= ch;

}

}

}

void jsSort()

{

int i,j,k,strl

char ch

for(I = 0; I & lt20; i++)

{

strl = strlen(xx[I]);

for(j = 0; j & ltstrl/2; j++)

for(k = j+ 1; k & ltstrl/2; k++)

if(xx[I][j]& lt; xx[i][k])

{

ch = xx[I][j];

xx[I][j]= xx[I][k];

xx[I][k]= ch;