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