C#学生成绩单生成程序
简介
生成学生成绩单的方法有很多。今天我们将学习其中一种方法。在本文中,我们将学习一个使用C#生成学生成绩单的程序。让我们先学习算法。
算法
让我们讨论一下生成学生成绩单程序的算法。学生需要向系统输入数据,然后生成成绩单。
步骤1 − 首先,我们声明整型变量来存储不同的数据,例如学生的学号和各科成绩。
步骤2 − 接下来,我们声明一个浮点型变量来存储学生的百分比。
步骤3 − 接下来,我们声明一个字符串变量来存储学生的姓名。
步骤4 − 现在我们从用户那里获取输入,即学生的学号、姓名和各科成绩。
步骤5 − 然后我们对各科成绩进行求和,计算学生的百分比。我们使用Convert.ToInt32()进行显式转换,将输入转换为32位有符号整数。
步骤6 − 现在,我们将百分比与等级表进行比较,确定学生的等级,然后关闭程序。
现在,是时候看看算法的代码了。
示例
// C# program to generate the mark sheet of the student
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class tpt{
static void Main(string[] args) {
// Variables declaration for collection of different data
int rollnum, marksub1, marksub2, marksub3, marksub4, marksub5, totalmark;
// Declaration of percentage variable
float stud_percent;
// Declaration of string variable to store student name
string name;
// Getting the student’s roll number
Console.WriteLine("Enter the roll number of the student :");
rollnum = Convert.ToInt32(Console.ReadLine());
// Getting the student’s name
Console.WriteLine("Enter the name of the student :");
name = Console.ReadLine();
// Getting the student’s first subject marks
Console.WriteLine("Enter the marks of first subject : ");
marksub1 = Convert.ToInt32(Console.ReadLine());
//Getting the student’s second subject marks
Console.WriteLine("Enter the marks of second subject : ");
marksub2 = Convert.ToInt32(Console.ReadLine());
// Getting the student’s third subject marks
Console.WriteLine("Enter the marks of third subject : ");
marksub3 = Convert.ToInt32(Console.ReadLine());
// Getting the student’s fourth subject marks
Console.WriteLine("Enter the marks of fourth subject : ");
marksub4 = Convert.ToInt32(Console.ReadLine());
// Getting the student’s fifth subject marks
Console.WriteLine("Enter the marks of fifth subject : ");
marksub5 = Convert.ToInt32(Console.ReadLine());
// Adding the total marks of the student
totalmark = marksub1 + marksub2 + marksub3 + marksub4 + marksub5;
// Calculating the percentage of the student
stud_percent = totalmark / 5.0f;
// Displaying the total marks and percentage of the student
Console.WriteLine("Final result of {0} is : ", name);
Console.WriteLine("Total Marks : " + totalmark);
Console.WriteLine("Percentage : " + stud_percent);
// Block to calculate the grades of the student
if (stud_percent <= 35) {
Console.WriteLine("Grade : F");
}
else if (stud_percent >= 34 && stud_percent <= 39) {
Console.WriteLine("Grade : D");
}
else if (stud_percent >= 40 && stud_percent <= 59) {
Console.WriteLine("Grade : C");
}
else if (stud_percent >= 60 && stud_percent <= 69) {
Console.WriteLine("Grade : B");
}
else if (stud_percent >= 70 && stud_percent <= 79) {
Console.WriteLine("Grade : B+");
}
else if (stud_percent >= 80 && stud_percent <= 90) {
Console.WriteLine("Grade : A");
}
else if (stud_percent >= 91) {
Console.WriteLine("Grade : A+");
}
}
}
输出
Enter the roll number of the student : 7 Enter the name of the student : Leo Enter the marks of first subject : 10 Enter the marks of second subject : 30 Enter the marks of third subject : 21 Enter the marks of fourth subject : 7 Enter the marks of fifth subject : 11 Final result of Leo is : Total Marks : 79 Percentage : 15.8 Grade : F
如果科目数量不固定,并且每个学生的科目数量都不同,那么我们可以使用数组的概念。
示例
// C# program to generate the mark sheet of the student
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class tpt{
static void Main(string[] args) {
// Variables declaration for collection of different data
int rollnum, subnum, totalmark=0;
int[] marksub=new int[25];
// Declaration of percentage variable
float stud_percent;
// Declaration of string variable to store student name
string name;
// Getting the student’s roll number
Console.WriteLine("Enter the roll number of the student :");
rollnum = Convert.ToInt32(Console.ReadLine());
// Getting the student’s name
Console.WriteLine("Enter the name of the student :");
name = Console.ReadLine();
// Getting the number of subjects
Console.WriteLine("Enter the number of subjects : ");
subnum = Convert.ToInt32(Console.ReadLine());
//Getting the student’s subject marks
for(int i=1; i<=subnum; i++) {
Console.WriteLine("Enter the marks of {0} subject : ",+i);
marksub[i] = Convert.ToInt32(Console.ReadLine());
totalmark=totalmark+marksub[i];
}
// Calculating the percentage of the student
stud_percent = totalmark / subnum;
// Displaying the total marks and percentage of the student
Console.WriteLine("Final result of {0} is : ", name);
Console.WriteLine("Total Marks : " + totalmark);
Console.WriteLine("Percentage : " + stud_percent);
// Block to calculate the grades of the student
if (stud_percent <= 35) {
Console.WriteLine("Grade : F");
}
else if (stud_percent >= 34 && stud_percent <= 39) {
Console.WriteLine("Grade : D");
}
else if (stud_percent >= 40 && stud_percent <= 59) {
Console.WriteLine("Grade : C");
}
else if (stud_percent >= 60 && stud_percent <= 69) {
Console.WriteLine("Grade : B");
}
else if (stud_percent >= 70 && stud_percent <= 79) {
Console.WriteLine("Grade : B+");
}
else if (stud_percent >= 80 && stud_percent <= 90) {
Console.WriteLine("Grade : A");
}
else if (stud_percent >= 91) {
Console.WriteLine("Grade : A+");
}
}
}
输出
Enter the roll number of the student : 7 Enter the name of the student : Cris Enter the number of subjects : 2 Enter the marks of 1 subject : 89 Enter the marks of 2 subject : 77 Final result of Cris is : Total Marks : 166 Percentage : 83 Grade : A
时间复杂度
在科目数量已知的第一个代码中,没有循环,数据大小是固定的,所以时间复杂度是O(1)。
第二个代码使用一个数组来存储科目数量。因此,在输入科目数量后,循环会运行以获取科目成绩并将它们加到用户输入的科目数量中,即n次。所以时间复杂度是O(N)。现在,让我们总结一下这篇文章。
结论
在这篇文章中,我们学习了两种不同的方法来编写C#程序生成学生成绩单。一种是已知科目数量的方法,另一种是未知科目数量的方法。通过这篇文章,希望能够提升您对C#的理解。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP