| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1462 人关注过本帖, 1 人收藏
标题:发一道新加坡国大的编程作业
取消只看楼主 加入收藏
fantasysonic
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-9-29
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:5 
发一道新加坡国大的编程作业
发一道新加坡国大的编程作业,挑战的,请进来。
LABORATORY EXERCISE (Lab-II)1
Lab-II A: File reading and writing
Problem Statement: Consider a N x N matrix of random
numbers in the range 0 to 99. Write a random number
generator to generate a matrix of N x N (you can fix N in
the range 10 to 20) and write the matrix into a TEXT file –
call this matrix as MatrixA.txt. Read MatrixA.txt from your
program and replace all prime numbers with 0. Write your
output to another file OutputMatrix.txt. You should display
the following output:
============
MatrixA and OutputMatrix from MatrixA.txt and
OutputMatrix.txt files. (Lab Demonstrators will look for
these two files generated by your program automatically).
Number of Prime numbers: 23
=================
NOTE: Following information may be useful. Parts of the codes to read and
write to a file are given below. This is only one way and there are many
ways you may wish to write.
Input file looks like this:
First create files “MatA.txt” and MatB.txt”. Enter the values for a M=4, N=5
(4 x 5) matrix. Follow the style of arranging the numbers given in the
following sampleMatrix.txt file below.
sampleMatrix.txt:
Note - First two lines indicate the number of rows and number of
columns
=======
4
5
2 2 3 4 6
1 2 5 7 3
3 6 1 8 0
10 1 3 7 2
======
Code for reading a single (first) character from a file:
/*Declare a file pointer to sampleMatrix.txt*/
FILE *myFile;
/*number to be input*/
int ip;
/*Open the file for reading*/
myFile = fopen("sampleMatrix.txt","r");
/*Read the first integer, 4, to ip*/
fscanf(myFile,"%d", &ip);
/*close the file stream*/
fclose(myFile);
Code for writing a single character to a file:
/*Declare a file pointer*/
FILE *myFile;
/*Open the file for writing*/
myFile = fopen("output.txt","w");
/*Write a number*/
fprintf(myFile, "%d", 4);
/*close the file stream*/
fclose(myFile);

搜索更多相关主题的帖子: 新加坡 编程 generate reading another 
2011-09-29 22:06
fantasysonic
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-9-29
收藏
得分:0 
我只会做出随机数 RAMDOM NUMBER 的矩阵,输入,和输出文件,试过很多次都失败。以下是我自己的CODE

#include <stdio.h>
void main()
{     
    int a[50][50];
    int i,j,k,p,m,n;
 
    p=1;              
    while(p==1)
{
        printf("Please Enter N for N x N matrix.(N must be between 10 to 20)\n");
        scanf("%d",&n);
        if((n!=0)&&(10<=n)&&(20>=n))
  {
   printf("The Matrix you entered is :%d\n",n);
   p=0;
  }
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                a[i][j]=0;
}
 
    j=n/2+1;
    a[1][j]=1;
    for(k=2;k<=99;k++)
{
        i=i-1;
        j=j+1;
        if((i<1)&&(j>n))
  {
            i=i+2;
            j=j-1;
  }
        else
  {
            if(i<1)    i=n;
            if(j>n)    j=1;
  }
        if(a[i][j]==0)
            a[i][j]=k;
        else
  {
            i=i+2;
            j=j-1;
            a[i][j]=k;
  }
}
 
for(i=1;i<=n;i++)
{
        for(j=1;j<=n;j++)
            printf("%5d",a[i][j]);
  printf("\n");
}
}
 
2011-09-30 20:44
fantasysonic
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-9-29
收藏
得分:0 
有谁能做出输出到TXT文件啊。。。 我试了好多例子方法都失败
2011-10-03 19:45
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.013812 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved