하루종일 보고있다는-_-;

누나들도 이쁘고...;ㅂ; 은근은근 중독성있네요 이거
정각되면 남자들이 나오네여 ㅋㅋ
Creative Commons License
이올린에 북마크하기(0) 이올린에 추천하기(0)
2007/12/03 03:36 2007/12/03 03:36
Tag //

Q1) <첨부파일1> 에서 그 내용을 읽어 각 학생의 점수와 합을 구하여 새로운 파일에 내용과 합을 출력하는 프로그램을 작성하시오. <첨부파일1>은 학생의 이름과 두 과목의 성적으로 구성되어 있음
 
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
 FILE* in,*out;
 int i=0,j=0;
 struct SCORE {
  int num;
  char name[10];
  float score1;
  float score2;
  float sum;
 } score[6];
 
 if(argc!=3)
 {
  printf("Usage %s <input> <output>\n",argv[0]);
  exit(1);
 }

 if((in=fopen(argv[1],"r"))==NULL)
 {
  printf("Cannot open file %s",argv[1]);
  exit(1);
 }

 while(1)
 {
  if((fscanf(in,"%d %s %f %f",&score[i].num,&score[i].name,&score[i].score1,&score[i].score2))==EOF) break;
  score[i].sum=score[i].score1 + score[i].score2;
  i++;
 }
 
 if((out=fopen(argv[2],"w"))==NULL)
 {
  printf("Cannot open file %s",argv[2]);
  exit(1);
 }
 for(j=0;j<i;j++)
 {
  fprintf(out,"%d %s %.1f %.1f %.1f\n",score[j].num,score[j].name,score[j].score1,score[j].score2,score[j].sum);
  printf("%d %s %.1f %.1f %.1f\n",score[j].num,score[j].name,score[j].score1,score[j].score2,score[j].sum);
 }
 
 fclose(in);
 fclose(out);

 return 0;
}

Q2) Commad Line에서 <srcfile>과 <string> 을 파라미터로 입력받는 findString 이라는 프로그램을 작성하시오. > findString <srcfile> <string>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
 FILE* fp;
 int i=0;
 int count=1;
 char string[20];

 if(argc!=3)
 {
  printf("Usage %s <srcfile> <string>\n",argv[0]);
  exit(1);
 }

 if((fp=fopen(argv[1],"r"))==NULL)
 {
  printf("Cannot open file %s",argv[1]);
  exit(1);
 }

 while(1)
 {
  if((fgets(string, 20, fp))==NULL) break;
  if(strstr(string,argv[2])!=NULL) // strstr, 문자열 string에서 문자열 argv[2]가 있는지 검사
  {
   printf("%s(%d) ",argv[1],count);
   printf("%s",string);  
  }
  count++;
 }
 
 fclose(fp);

 return 0;
}

2번문제의 핵심은 strstr!

Creative Commons License
이올린에 북마크하기(0) 이올린에 추천하기(0)
2007/11/29 01:18 2007/11/29 01:18