| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1462 人关注过本帖, 1 人收藏
标题:发一道新加坡国大的编程作业
只看楼主 加入收藏
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:3 
程序代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;

class RandomMatrix {
    int rows;
    int cols;
    int matrix[20][20];
    ofstream MatrixA;
    ofstream OutputMatrix;
public:
    RandomMatrix(int r, int c) :
        rows(r), cols(c),
        MatrixA("MatrixA.txt"),
        OutputMatrix("OutputMatrix.txt")
    {
        random();
    }
    bool isPrime(int) const;
    void random();
    void output1();
    void output2();
};

bool RandomMatrix::isPrime(int num) const
{
    if (num % 2 == 0)
        return false;
    for (int i = 2; i < num; i++)
        if (num % i == 0)
            return false;
    return true;
}

void RandomMatrix::random()
{
    srand(time(0));
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            matrix[i][j] = rand() % 100;
}

void RandomMatrix::output1()
{
    MatrixA << rows << endl;
    MatrixA << cols << endl;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            MatrixA << matrix[i][j] << " ";
        MatrixA << endl;
    }
}

void RandomMatrix::output2()
{
    OutputMatrix << rows << endl;
    OutputMatrix << rows << endl;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            if (isPrime(matrix[i][j]))
                OutputMatrix << 0 << " ";
            else
                OutputMatrix << matrix[i][j] << " ";
        OutputMatrix << endl;
    }
}

int main()
{
    int r = 0, c = 0;
    cout << "Enter rows and columns between 10 to 20:" << endl;
failed:
    cin >> r >> c;
    if (r < 10 || r > 20 || c < 10 || c > 20) {
        cout << "Error, rows and columns must between 10 to 20." << endl;
        cout << "Try again!" << endl;
        goto failed;
    }
    RandomMatrix rm(r, c);
    rm.output1();
    rm.output2();
}

My life is brilliant
2011-10-03 20:46
c821101017
Rank: 2
等 级:论坛游民
帖 子:33
专家分:10
注 册:2011-9-21
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
    int M,N,i,j;
    srand(time(0));
    M=rand()%5+3;
    N=rand()%7+2;
    printf("The row and the column of the Matrix are %d,%d.\n",M,N);
    int **a;
    FILE *fp;
    FILE *fp1;
    a=(int **)calloc(M+1,sizeof(int *));
    for(i=0;i<M;i++)
        a[i]=(int *)calloc(N+1,sizeof(int ));
    fp=fopen("a.txt","w");
    printf("The elements of the Matrix are: \n");
    for(i=0;i<M;i++)
    {
        for(j=0;j<N;j++)
        {
            a[i][j]=rand()%3+4;
            printf("%d ",a[i][j]);
            fprintf(fp,"%d ",a[i][j]);
        }
        printf("\n");
        fprintf(fp,"\n");
    }
    fclose(fp);
    fp1=fopen("b.txt","w");
    for(i=0;i<M;i++)
        for(j=0;j<N;j++)
        {
            if(a[i][j]==2||a[i][j]%2==1)
                a[i][j]=0;
        }
    printf("The Matrix that we transform from the front is: \n");
    for(i=0;i<M;i++)
    {
        for(j=0;j<N;j++)
        {
            printf("%d ",a[i][j]);
            fprintf(fp,"%d ",a[i][j]);
        }
        printf("\n");
        fprintf(fp1,"\n");
    }
    fclose(fp1);
}
2011-10-03 20:48
fantasysonic
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-9-29
收藏
得分:0 
公布我自己写的答案,

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <malloc.h>
 
 
void main(){
 
FILE *myFile;
FILE *myFile2;
FILE *fp;
int i,j,n=1,N,a[20][20],num;
int temp;
int k=0,count=0;
 
 
printf("Lab-II A: File reading and writing \n");
 
printf("*******Programed By Xu Jian********\n\n");
 
printf("Enter the value for N\n");
scanf("%d", &N);
 
 
 
  myFile = fopen("MatixA.txt", "w");
if ( myFile )
{
 
for(i=0;i<=N;i++)
  for(j=0;j<N;j++)
   a[i][j]=rand()%100;
  for(i=0;i<N;i++)
  {
for(j=0;j<N;j++)
{
printf("%5d", a[i][j]);
fprintf(myFile,"%5d", a[i][j]);
 
}
printf("\n");
fprintf(myFile, "\n");
 
}
printf("\============ \n");
printf("\ MatrixA and OutputMatrix from MatrixA.txt and OutputMatrix.txt files \n\n");
 
fclose(myFile);
 
 
fp=fopen("MatixA.txt","r");
 
if(fp==NULL)
{
printf("Open File error!");
}
else
{
 
num=a[0][0];
temp=2;
fscanf(fp,"%d", &temp);
while(temp<=num/2)
{
if(num%temp==0)
break;
temp++;
}
if (temp>=num/2,temp=0)
printf("The number is prime");
 
else
printf("The number is not prime");;
 
getch();
}
fclose(fp);
myFile2 = fopen("OutputMatrix.txt", "w");
    if ( myFile2 )
  {
 
fprintf(myFile2,"%5d",temp);
 
 
}
fclose(myFile2);
 
}
 
 
}}



[ 本帖最后由 fantasysonic 于 2011-10-26 23:03 编辑 ]
2011-10-26 22:51
fantasysonic
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-9-29
收藏
得分:0 
谁会第二题啊。哈哈,我第二题还有很多问题

[ 本帖最后由 fantasysonic 于 2011-10-26 23:03 编辑 ]
2011-10-26 22:52
fantasysonic
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-9-29
收藏
得分:0 
再发一道题目,,谁来挑战啊。。。 天天作业做到要吐血了。

LABORATORY EXERCISE (Lab-III)
Lab-III: Database Creation & Search
In this Project you will create a database using structures and write several
functions to search different types of data on this database.
The application data is text data comprising all the clinical information about
patients and drugs. In other words, you are going to create a library that is
useful for a medical practitioner!!
Create a global database for at least 15 patients– array of structures with
the following information.
- Name of the patient (format: Firstname X. lastname);
- Patient’s ID (random generated 4 digit number with the first two
letters as the first two characters of first and middle or last names);
- Separate structure identifying patient’s contact information;
- Doctor’s name who is attending this patient name (format: Firstname
X. lastname)
- Age of the patient (dd/mm/yyyy format)
- Separate structure to identify patient’s weight during the last three
consultations;
- Blood Group
Special structure capturing the following two items: - Allergic drugs
listing, regular drugs list and any Major disease(s) treated recently in
the last three years;
- Nature of complaint during the last visit to the hospital;
- Last visited date to the hospital;
Create a separate Structure for recording the current visit details of a
patient: Date of visit; weight of the patient, temperature; nature of the
complaint for this current visit; decision made (string input by the
doctor); drugs administered; next appointment date; Any other special
comments
2
With the above attributes, implement the following search functions. In
all these functions, a NIL must be displayed if no result is found.
(I) (a) A simple function to search by using Patient’s name; Display
the full record of the patient as stored; (b) Update the patient’s
record and display the particulars of the current visit;
(II) A simple function to search by disease and list the names of the
patients; Then allow the user to choose one patient from the
displayed list and display that patient’s record;
(III) A simple function to list all the patients attended by a specific
doctor;
Allow the user to interact with the system by looping continuously.
He/She will see his/her search results and then must be allowed to search
further again. Finally, he/she must be asked to confirm an “exit” to stop
interacting with the system.
You may choose to read in all the data to create the database from a text
file or manually enter.
2011-10-26 22:53
快速回复:发一道新加坡国大的编程作业
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021893 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved