Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with the problems of students' scoring, calculating time and minimum coins in C language?

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The editor of this article introduces in detail "how C language deals with students' grading, calculating time, and minimum coin problem". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how C language deals with students' scoring, calculating time, and minimum coins" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

Question 1: the problem of minimum coins (simple version)

Suppose there are three denominations of coins, which are 10, 5 and 1 respectively. Receive an integer as the amount and calculate the minimum number of coins of each denomination to reach that amount.

The output shows:

Reference answer:

# include typedef struct StructrueMoneyBox {int N10; int N5; int N1;} MoneyBox; int main (void) {MoneyBox change = {0,0,0}; int face_value [4] = {10,5,1}; int money = 0; int I = 0; printf ("Please enter amount:"); scanf ("% d", & money); do {if (money)

< face_value[i]) { i++; } switch (i) { case 0: change.n10 = money / face_value[i]; break; case 1: change.n5 = money / face_value[i]; break; case 2: change.n1 = money / face_value[i]; break; } money = money % face_value[i]; } while (money >

0); printf ("10:% d\ n", change.n10); printf ("5:% d\ n", change.n5); printf ("1:% d\ n", change.n1); return 0;}

Running result:

Question 2: how long has it been?

Write a stopwatch program to calculate how long it has been.

Implemented using the following structures:

The following two variables are required to solve the problem:

When the program is running, take the current time and save it to the previtimePtr variable.

After that, the program will accept commands indefinitely until the user enters "quit" the program will stop.

If the user enters "check", the difference between the last time stored and the current time is output.

(no action is taken by other commands)

Tip: the method of taking the current time

# include # include int main (void) {time_t timer; struct tm * t; timer = time (NULL); t = localtime (& timer); return 0;}

The output shows:

Reference answer:

# include # include typedef struct {int hours; int minutes; int seconds;} Time; int main (void) {Time* prevTimePtr; Time currTime; time_t timer; struct tm * t; int i = 1, a, b, c; char buf [80]; timer = time (NULL); t = localtime (& timer) PrevTimePtr = (Time*) malloc (sizeof (Time)); prevTimePtr- > hours = t-> tm_hour; prevTimePtr- > minutes = t-> tm_min; prevTimePtr- > seconds = t-> tm_sec; while (1) {printf ("system >"); fgets (buf, 80, stdin); if (! strcmp (buf, "quit\ n") break If (! strcmp (buf, "check\ n") {timer = time (NULL); t = localtime (& timer); currTime.hours = t-> tm_hour; currTime.minutes = t-> tm_min; currTime.seconds = t-> tm_sec Printf ("\ n\ tCheck Point # d\ n", iTunes +); printf ("\ tCurrent Time = d: d: d\ n", currTime.hours, currTime.minutes, currTime.seconds) Printf ("\ tPrevious Time = d: d: d\ n", prevTimePtr- > hours, prevTimePtr- > minutes, prevTimePtr- > seconds); c = currTime.seconds-prevTimePtr- > seconds; b = currTime.minutes-prevTimePtr- > minutes A = currTime.hours-prevTimePtr- > hours; if (c < 0) {cymbidium 60; bmurm-60;} if (b < 0) {baked-60; a-muri- } printf ("\ tElasped Time = d: d: d\ n\ n", a prevTimePtr bjournal c); * prevTimePtr = currTime;}} return 0;}

Running result:

Question 3: automatic grading of students' grades

Write a Mini Program that manages student scores and has the function of calculating student scores.

Use the following structure to store student information:

For example, the value of a student in the above structure is as follows:

Grades student1 = {2601053, "Natalie Lewis", {30.5,65.9,69.6}}

When the program starts, the information is read in a text file ("input.txt"). (please create the file manually here! )

The contents of the document are as follows:

twenty-six

2601053 / Natalie Lewis / 30.5 / 65.9 / 69.6

2026018 / Chloe Christian / 54.1 / 43.4 / 35.4

2016317 / Emolys Evans / 31.6 / 20.4 / 26.3

2961329 / Sophia Allen / 63.5 / 37.9 / 56.8

2016787 / Robert Melton / 44.1 / 54.2 / 32.7

2011661 / Siaomos Barton / 92.2 / 15.4 / 33.7

8166559 / Sebastian Henderson / 55.1 / 38.2 / 0.9

2061185 / Richard Cerney / 63.6 / 98.6 / 97.0

2017454 / Elizabeth Calvin / 7.4 / 0.0 / 26.4

2026196 / Madison Frost / 49.3 / 64.3 / 73.3

2015758 / Emily Stevgenson / 51.6 / 89.2 / 34.0

2016776 / Navy Taylor / 8.6 / 36.4 / 52.3

2062103 / Vincent Newton / 38.7 / 79.0 / 24.0

2501762 / Owen Ingram / 40.6 / 85.1 / 91.1

2042142 / Olyen Massey / 72.1 / 62.0 / 97.3

2027039 / Emma Thomas / 30.1 / 25.1 / 27.3

2051341 / Peter Pitts / 47.9 / 71.8 / 74.9

2071368 / Xygret Latimer / 67.2 / 9.7 / 79.1

2023762 / Michael Grahan / 6.4 / 29.0 / 17.5

2024545 / Mandon Austin / 98.8 / 28.5 / 48.9

2091176 / Violet Smith / 4.8 / 93.9 / 3.9

2802841 / Samuel Davis / 95.6 / 54.2 / 31.8

2027062 / Sam Mackey / 83.6 / 26.1 / 97.5

2061555 / Miguel Parr / 77.2 / 88.1 / 99.5

2028836 / Oscar Hamersley / 67.7 / 43.4 / 91.7

2026582 / MeanSon King / 97.2 / 88.3 / 100.0

The first line of the text shows the number of students n

Starting from the second line, the student's information is given in the order of "id/name/mid/final/proj".

Determine a student's final score by calculating a ranking based on the scores of all students:

The judging rule of ① total score: total_point = mid * 0.3 + final * 0.3 + prog * 0

The evaluation rules of ② score: the first 30% is A, the first 70% is B, the rest is C.

Students whose total ③ score is less than 30 (total_point < 30) ignore the ranking to F.

The range of grades entered is limited to 0,100 and the length of student names is limited to 40 characters.

Only students in a given text file are graded, and the location of the output file is arbitrary.

Demonstration of output effect:

Reference answer:

# include # include # include typedef struct {float mid; float final; float proj;} Points; typedef struct {int studentId; char name [40]; Points subject; char grade [3]; float total_point;} Grades; int main (void) {int cnt, I, j, A_temp, Breadtemp; float a_grade, bounded grade; char name_temp [30]; Grades* grade; Grades gtemp FILE * fp = fopen ("input.txt", "r"); if (fp = = NULL) {printf ("File not found! \ n "); return 1;} fscanf (fp,"% d ", & cnt); A_temp = cnt * 0.3; B_temp = cnt * 0.7; grade = (Grades*) malloc (sizeof (Grades) * cnt); for (I = 0; I < cnt) Fscanf +) {fscanf (fp, "% d /% s% s /% f /% f /% f\ n", & grade.studentId, grade.name, name_temp, & (grade.subject) .mid), & (grade.subject) .final, & (grade.subject) .proj); strcat (grade.name, "") Strcat (grade.name, name_temp); grade.total _ point = (grade.subject). Mid * 0.3 + (grade.subject) .final * 0.3 + (grade.subject) .proj * 0.4;} for (I = 0; I < cnt-1; ifold +) {for (j = 0; j < cnt-I-1) Point +) {if (grade.total _ point < grade [j + 1] .total _ point) {gtemp = grade [j]; grade [j] = grade [j + 1]; grade [j + 1] = gtemp;}} a_grade = grade [A _ temp] .total _ point B_grade = grade [B _ temp] .total _ point; printf ("Cut off for A grade:% f (rank% d)\ n", a_grade, A_temp); printf ("Cut off for B grade:% f (rank% d)\ n", b_grade, B_temp); for (I = 0; I < cnt) ) {if (grade.total _ point < 30) {strcpy (grade.grade, "F");} else if (a_grade

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report