Programming

Just another WordPress.com weblog

ข้อสอบ CT212 final เรื่องไฟล์

แสดงความเห็นโดย จั่น บน ธันวาคม 6, 2008

โจทย์ ให้เขียน program หยิบข้อมูล Input จากแฟ้มข้อมูล

(StudentScore.txt) ซึ่งประกอบไปด้วยข้อมูล

รหัสนักศึกษา, ชื่อนักศึกษา, คะแนนเก็บ, คะแนน midterm ,คะแนน final

ดังนี้

5102026     Sakon        15    40    40
5102027     Damrong      10    25    26
5102028     Lanna        5     10    18
5102029     Kwee         10    10    15
2102030     Udon         5     30    29
2102031     Tee          20    35    40

ให้แสดงผล output ออกทางหน้าจอ ตามนี้

นักศึกษาที่ได้เกรด G ได้แก่
5102026 Sakon 95
2102031 Tee 95

นักศึกษาที่ได้เกรด P ได้แก่
5102027 Damrong 61
2102030 Udon 64

นักศึกษาที่ได้เกรด F ได้แก่
5102028 Lanna 33
5102029 Kwee 35

code ด้านล่างจะรันได้ก็ต่อเมื่อมี file StudentScore.txt อยู่แล้ว โดยสร้างตามโจทย์

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

#define sourceFile “StudentScore.txt”

using namespace std;

int ProcessScore(int);
void PrintOutput(const string, const string, const int);

void main(){

cout << “Student Grade G ” << endl;
ProcessScore(1);

cout << endl;

cout << “Student Grade P ” << endl;
ProcessScore(2);

cout << endl;

cout << “Student Grade F ” << endl;
ProcessScore(3);

}

int ProcessScore(int type){

ifstream ifs;
ifs.open(sourceFile);

if(ifs.fail()){

cerr << “Error Cannot Open ” << sourceFile << endl;
return EXIT_FAILURE;

}

string studentcode, studentname;
int homework, midterm, finale;
int totalscore;

ifs >> studentcode >> studentname >> homework >> midterm >> finale;

while(!ifs.eof()){

totalscore = homework + midterm + finale;

if(type == 1){

if(totalscore >= 80){

PrintOutput(studentcode, studentname, totalscore);

}

}else if(type == 2){

if(totalscore >= 60 && totalscore <= 79 ){

PrintOutput(studentcode, studentname, totalscore);

}

}else if(type == 3){

if(totalscore < 60){

PrintOutput(studentcode, studentname, totalscore);

}

}

ifs >> studentcode >> studentname >> homework >> midterm >> finale;

}

ifs.close();
return 0;

}

void PrintOutput(const string studentcode, const string studentname, const int totalscore){

cout << studentcode << ” ” << studentname << ” ” << totalscore << endl;

}

มีการตอบกลับหนึ่งครั้ง ถึง “ข้อสอบ CT212 final เรื่องไฟล์”

  1. Spore กล่าว

    LU Factorize เน้อ พี่น้อง
    CT317 ม่ายยากอย่างที่คิดเน้อ

    import java.util.Scanner;
    class LU{
    public static void main(String[] args){
    CT317_LU t=new CT317_LU();
    t.LU_Factorize();
    }
    }

    class CT317_LU{
    private int i,j,k,n,size=15;
    private double[][] A=new double[size][size];
    private double[][] L=new double[size][size];
    private double[][] U=new double[size][size];
    private double[] B=new double[size];
    private double[] X=new double[size];
    private double[] Y=new double[size];
    double sum=0;
    Scanner sc=new Scanner(System.in);
    public void LU_Factorize(){
    System.out.print(“Dimension of matrix A (n) = “);
    n=sc.nextInt();

    //get matrix A from user.
    System.out.println(“\nEnter elements of matrix A (coefficient of x) :”);
    for (i = 0; i < n; i++){
    for (j = 0; j < n; j++){
    System.out.print(“A” + i + j + ” = “);
    A[i][j]=sc.nextDouble();
    } //end for j.
    } //end for i.
    //get matrix B from user.
    System.out.println(“\nEnter elements of matrix B (result of the right hand side) :”);
    for (i = 0; i < n; i++)
    {
    System.out.print(“B” + i + ” = “);
    B[i]=sc.nextDouble();
    } //end for i.

    //matrix A = LU.
    //set known elements of matrix L,U.
    for (i = 0; i < n; i++)
    {
    for (j = 0; j j)
    U[i][j] = 0;
    else
    {
    if (i < j)
    L[i][j] = 0;
    else //case i = j
    L[i][j] = 1;
    } //end if.
    } //end for j.
    } //end for i.

    //compute other elements of matrix L,U.
    for (i = 0; i < n; i++)
    {
    for (j = 0; j < n; j++)
    {
    if (j < i)
    {
    if (U[j][j] == 0)
    {
    System.out.println(“\nCannot find solutions becuase cannot devide by 0.” );
    return;
    } //end if.
    sum = 0.0;
    for (k = 0; k = i.
    {
    sum = 0.0;
    for (k = 0; k < i; k++)
    sum = sum + (L[i][k]*U[k][j]);
    U[i][j] = A[i][j] – sum;
    } //end else.
    } //end for j.
    } //end for i.

    //display matrix L.
    System.out.println(“\nMatrix L :”);
    for (i = 0; i < n; i++)
    {
    for (j = 0; j < n; j++)
    System.out.print(L[i][j]+”\t”);
    System.out.print(“\n”);
    }
    //display matrix U.
    System.out.println(“\nMatrix U :” );
    for (i = 0; i < n; i++)
    {
    for (j = 0; j < n; j++)
    System.out.print(U[i][j]+”\t”);
    System.out.print(“\n”);
    }
    //matrix AX = B
    //matrix (LU)X = B
    //matrix L(UX) = B
    //matrix L(Y) = B
    //compute y[i] from L(Y)= B by forward substitution.
    Y[0] = B[0];
    for (i = 1; i < n; i++)
    {
    sum = 0.0;
    for (j = 0; j = 0; i–)
    {
    sum = 0.0;
    for (j = i; j < n; j++)
    sum = sum + (U[i][j]*X[j]);
    X[i] = (Y[i] – sum)/U[i][i];
    }

    //display result(y).
    System.out.println(“\nResult(y) :”);
    for (i = 0; i < n; i++)
    System.out.println(“Y” + i + ” = ” + Y[i]);

    //display solutions(x).
    System.out.println(“\nSolution(x) :”);
    for (i = 0; i < n; i++)
    System.out.println(“X” + i + ” = ” + X[i]);
    }

    }

ใส่ความเห็น

XHTML: คุณสามารถใช้แท็กเหล่านี้ได้: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>