0% found this document useful (0 votes)
24 views44 pages

程式語言-Week 9 2

Uploaded by

caabset
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views44 pages

程式語言-Week 9 2

Uploaded by

caabset
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

2022/10/26

國立臺灣大學 生工系
111學年度 第一學期

計算機應用及程式語言
Week 9

2022.10.31

1
2022/10/26

1. Every team should demonstrate their program


during the oral presentation and provide a
paper report with source codes in the end of
the semester.

2. Contributions of each person should be


mentioned in both the oral presentation and
the paper reports. Scores will be given by the
contributions from each person.

1. The scores for your final projects will be based


on two parts: 80% for the oral presentation
and 20% for the reports.

2. You will have to include the following details in


your oral presentation:
▫ Concepts of your program (30%)
▫ Explanation of the source codes (30%)
▫ Demonstration of your final
results/achievement (30%)
▫ cooperation among the team members (10%)

2
2022/10/26

3
2022/10/26

4
2022/10/26

int a=90;
printf(“%p %d”, &a, a);
a (0022A) 90

int *ptra; ptra (0023A) 0022A


ptra=&a;
printf(“%d”, *ptra);

printf(“%p”, ptra);

printf(“%p”, &ptra);

5
2022/10/26

int a=90;
int *ptra;

ptra=&a;
printf(“%d”, *ptra);
a (0022A) 100
a=100;
printf(“%d”, *ptra); ptra (0023A) 0022A

printf(“%p %p”, ptra, &ptra);

char s[]=“NTU BSE is awesome”;


char *ptr=s[0];

do{
printf(“%c”, *ptr);
ptr++;
} while (*ptr!= ‘\0’);

printf(“\n”);

6
2022/10/26

#include <stdio.h>
void setNum(int n);

int main(){
int a = 2;
printf("before setNum(), a: %d\n", a);
setNum(a);
printf("after setNum(), a: %d\n", a);
}

void setNum(int n){


printf("before setting, n: %d\n", n);
n = 5;
printf("after sett ing, n: %d\n", n);
}

#include <stdio.h>
void setNum(int *n);

int main(){
int a = 2;
printf("before setNum(), a: %d\n", a);
setNum(&a);
printf("after setNum(), a: %d\n", a);
}

void setNum(int *n){


printf("before setting, n: %d\n", *n);
*n = 5;
printf("after setting, n: %d\n", *n);
}

7
2022/10/26

int array1[3][3]={{0,1,2},{3,4,5},{6,7,8}};
0 1 2
int *ptr=array1[0];
3 4 5
int array2[2][2]; 6 7 8

for(i=0;i<2;i++){
for(j=0;j<2;j++){
array2[i][j]=*ptr;
ptr++;
}}

int var=20;
int *ptr1=&var;
int **ptr2=&ptr1;

printf(“%d, %f, %f”, **ptr2, *ptr2, ptr2)

var (0022A) 20

ptr1 (0023A) 0022A

ptr2 (0024A) 0023A

8
2022/10/26

int array1[3][3]={{0,1,2},{3,4,5},{6,7,8}};
int *ptr1[3];
int *ptr2;

ptr1[0]=&a[0];
ptr1[1]=&a[1];
ptr1[2]=&a[2];

ptr2=&ptr[0];

9
2022/10/26

Reading files

FILE *fopen("filename", "mode");

mode:
r: read; w: write; a: add; r+:read and rewrite (file have
to be existed); w+: read and rewrite (new file can be
created if not existed); a+ read and add (new file can
be created if not existed).

fclose(*pointer);

10
2022/10/26

FILE *fp;

fp = fopen("/tmp/test.txt", "w+");

fclose(fp);

• fgetc(): get a character from the file.


• fgets(): get a string from the file.

11
2022/10/26

int main() {
FILE *fp;
int c;
fp=fopen("test.txt", "r");
if(fp==NULL){
printf("file is not found");
return 0;
}
while((c=fgetc(fp))!=EOF){
printf("%d \n", c);
}
return 0;
}

int main() {
FILE *fp;
int c;
fp=fopen("test.txt", "r");
if(fp==NULL){
printf("file is not found");
return 0;
}
while((c=fgetc(fp))!=EOF){
if(c!=10){
printf("%c \n",(char)c);
}
}
return 0;
}

12
2022/10/26

int main() {
FILE *fp;
int c;
fp=fopen("test.txt", "r");
if(fp==NULL){
printf("file is not found");
return 0;
}
int line=0;
while((c=fgetc(fp))!=EOF){
if(c==10){
line++;
}
}
printf(“There are totally %d lines in the file", line);
return 0;
}

• fgets(): get a string from the file.

• the “fgets()” function read a string each time and


stores all the characters as a string array. It
returns ‘NULL’ when the read is failed.

• Ex: fgets(array name, array numbers, pointer)

13
2022/10/26

int main() {
FILE *fp;
fp=fopen("test.txt", "r");
if(fp==NULL){
printf("file is not found");
return 0;
}
char str[10];
while((fgets(str, 10, fp))!=NULL){
printf(“%s”, str);
}
return 0;
}

int main() {
FILE *fp;
fp=fopen("test.txt", "r");
if(fp==NULL){
printf("file is not found");
return 0;
}
char str[10];
while((fgets(str, 10, fp))!=NULL){
printf(“%s”, str);
}
return 0;
}

14
2022/10/26

• fscanf(): read data from the file with indicated


formats.

• Ex: fscanf(pointer, data formats, variables)

int main() {
FILE *fp;
fp=fopen("score.txt", "r");
if(fp==NULL){
printf("file is not found");
return 0;
}
char name[15];
int score;
int sum=0;
int num=0;
while(fscanf(fp, "%s %d", name, &score)!=EOF){
printf("%s \t %d \n", name, score);
sum=score+sum;
num++;
}
printf("\n\n The average score is %.1f \n", (float)sum/num);
return 0;
}

15
2022/10/26

FILE *fp;
fp=fopen("score.txt", "r");
if(fp==NULL){
printf("file is not found");
return 0;
}
int line=0;
int c;
while((c=fgetc(fp))!=EOF){
if(c==10){
line++;
}
}
rewind(fp);
char name[15];
int temp;
int score[line];
line=0;
while(fscanf(fp, "%s %d", name, &temp)!=EOF){
score[line]=temp;
line++;
}
int i;
for(i=0; i<line; i++){
printf("%d \n", score[i]);
}
return 0;

FILE *fp;
fp=fopen("score1.txt", "r");
if(fp==NULL){
printf("file is not found");
return 0;
}
char name[15];
int chinese, english, math;
printf("Name\t\tChinese\t\tEnglish\t\tMath\n");
printf("--------------------------------------------------
-----------\n");
while(fscanf(fp, "%[^,],%d,%d,%d", name,
&chinese, &english, &math)!=EOF){
printf("%s\t\t%d\t\t%d\t\t%d",name,chinese,en
glish,math);
}
printf("\n\n");
return 0;

16
2022/10/26

FILE *fp;
fp=fopen("info.txt", "r");
if(fp==NULL){
printf("file is not found");
return 0;
}
char name[15], mail[30],phone[10];
printf("Name\tE-mail\t\t\tPhone\n");
printf("---------------------------------------\n");
while(fscanf(fp, "%[^,],%[^,],%s", name, mail,
phone)!=EOF){
printf("%s\t%s\t\t%s",name,mail,phone);
}
return 0;

Writing files

17
2022/10/26

fputc(): write a character into the opened file.

Ex. fputc(char character, *pointer)

FILE *fp;
fp=fopen("test.txt", "a+");
if(fp==NULL){
printf("file is not found");
return 0;}

char name[20], choice[1];


int s=10;
int i;
do{
printf("Please enter the name:");
gets(name);
for(i=0;i<strlen(name);i++){
fputc(name[i], fp);}
fputc((char)s, fp);
printf("Enter the next name? (y/n)");
gets(choice);
}while(choice[0]=='y');
fclose(fp);
return 0;

18
2022/10/26

fputs(): write a string into the opened file.

Ex. fputs(char string, *pointer)

FILE *fp;
fp=fopen("test.txt", "a+");
char email[20], choice[1];
do{
printf("Please enter the email:");
gets(email);
fputs(email, fp);
fputs("\n", fp);
printf("Enter the next email? (y/n)");
gets(choice);
}while(choice[0]=='y');
fclose(fp);
return 0;

19
2022/10/26

fprintf(): write data into the opened file with selected


formats.

Ex. fprintf(*pointer, formats, data)

FILE *fp;
fp=fopen("test.txt", "a+");
float a;
char choice[1];
while(choice[0]!='n'){
printf("Please enter a value:");
scanf("%f", &a);
fprintf(fp, "%.2f \n", a);
printf("Enter the next value? (y/n)");
scanf("%s",choice);
}
fclose(fp);
return 0;

20
2022/10/26

ftell(): Returns the current value of the position


indicator of the stream.

ftell(pointer)

fseek(): Sets the position indicator associated with the


stream to a new position.

fseek(pointer, number of positions, starting position)

FILE *fp;
fp = fopen ( "example.txt" , "w+" );
fputs ( "This is an apple." , fp);
fseek ( fp, 9 , SEEK_SET );
fputs ( " sam" , fp);
fclose (fp);
return 0;

21
2022/10/26

#include <ctype.h>

isalnum(): Check if character is alphanumeric.

isalpha(): Check if character is alphabetic.

isdigit(): Check if character is decimal digit.

isspace(): Check if character is a white-space.

islower(): Check if character is lowercase letter.

isupper(): Check if character is uppercase letter.

FILE *fp;
fp=fopen("test.txt", "a+");
char name[20];
int i,a;
do{
a=0;
printf("Please enter the name:");
gets(name);
for(i=0;i<strlen(name);i++){
a=a+isalnum(name[i]);
}
if(a!=0){
fputs(name, fp);
fputs("\n", fp);
printf("You just entered %s \n", name);
}
}while(a!=0);
fclose(fp);
return 0;

22
2022/10/26

FILE *fp;
fp=fopen("test.txt", "a+");
char phone[10];
int i,a;
do{
a=0;
printf("Please enter a phone number:");
scanf("%s", phone);
for(i=0;i<strlen(phone);i++){
a=a+isdigit(phone[i]);
}
if(a==10){
fprintf(fp, "%s \n", phone);
printf("You just entered %s \n", phone);
}
}while(a==10);
fclose(fp);
return 0;

if(a==10){
printf("You just entered:\t");
for(i=0;i<10;i++){
if(i==4||i==7){
printf("-%c",phone[i]);
}
else if(i!=4||i!=7){
printf("%c", phone[i]);
}
}
printf("\n");
for(i=0;i<10;i++){
if(i==4||i==7){
fprintf(fp,"-%c",phone[i]);
}
else if(i!=4||i!=7){
fprintf(fp,"%c", phone[i]);
}
}
fprintf(fp,"\n");
}

23
2022/10/26

int main () int main ()


{ {
float pi=3.14; float pi=3.14;
printf(“%f”, pi*1*1); for (i=1; i<4; i++)
printf(“%f”, pi*2*2); {
printf(“%f”, pi*3*3); printf(“%f”, pi*i*i);
} }
}

24
2022/10/26

Examples of using "user defined


functions" for previous mentioned
math problems

int main () void area(int);


{ int main ()
int a; {
float pi=3.14; int a;
scanf(“%d”, &a);
scanf(“%d”, &a); area(a);
for (i=1; i<a; i++) }
{
printf(“%f”, pi*i*i); void area(int num)
{
} for (i=1; i<num; i++)
} {
printf(“%f”, 3.14*i*i);
}
}

25
2022/10/26

Practice
• Make a program with a "user defined function"
to calculate the circumference of a circle with a
radius given by users

void starline(char cha)


{ void linear(){
int a=0;
while(a<80) int i;
{ for(i=10; i>=1; i--){
printf(“%c”, cha);
a++; for(j=1; j<i; j++){
} printf(“ “);
printf(“\n”);
} }
printf(“\* \n”);
int main ()
{ }}
printf(…);
printf(…);
printf(…); int main(){
…..
starline(‘#’);
linear();
…… }
}

26
2022/10/26

void starline(char cha)


{ void linear(){
int a=0;
while(a<80) int i;
{ for(i=10; i>=1; i--){
printf(“%c”, cha);
a++; for(j=1; j<i; j++){
} printf(“ “);
printf(“\n”);
} }
printf(“\* \n”);
int main ()
{ }}
printf(…);
printf(…);
printf(…); int main(){
…..
starline(‘#’);
linear();
…… }
}

void linear(int a){


int i, j;
for(i=10; i>=1; i--){
for(j=1; j<i; j=j+a){
printf(“ “);
}
printf(“\* \n”);
}}

int main(){
linear(2);
}

27
2022/10/26

void linear(float a){


int i;
float j;
for(i=10; i>=1; i--){
printf("│");
for(j=1; j<i; j=j+a){
printf(" ");
}
printf("* \n");
}
printf("└────────────────────────");
}

int main(){
float b;
printf("enter a slope:");
scanf("%f", &b);
printf("\n\n");
linear(b);
}

void area(int); #define area 3.14*r*r;

int main () int main ()


{ {
int a; int r, i;
scanf(“%d”, &a); scanf(“%d”, &r);
area(a);
} for(i=1, i<r, i++)
{
void area(int num) printf(“%f”, r, area);
{ }
for (i=1; i<num; i++) }
{
printf(“%f”, 3.14*i*i);
}
}

28
2022/10/26

Example
• determine whether a quadratic equation has two
roots, double root, or no root.

&& AND
int main () {
|| OR int a;
! Negation for (i=1, i<=20, i++) {
!= Not equal
?: True or False
for (j=1, j<=20, j++){
if (j%2=0 && i%2=0)
{ …. }
else if (j%2=0 || i%2=0)
{ …. }
j%2!=0 || i%2!=0 else if (!(j%2=0 && i%2=0))
{ …. }
a=(j%2=0)? … : …;
}}}

29
2022/10/26

#include <math.h>
#define FUNC(a,b,c) b*b-4*a*c

int main () {
float a,b,c;
printf("Please enter the coefficients of a quadratic equation (a b c):");
scanf("%f %f %f", &a, &b, &c);
if (FUNC(a, b, c)>0)
{printf("The answers are %.2f and %.2f", (-
b+sqrt(FUNC(a,b,c)))/(2*a),(-b-sqrt(FUNC(a,b,c)))/(2*a));}
else if (FUNC(a, b, c)==0)
{printf("The answers are both %.2f", (-b/(2*a)));}
else
{printf("The answers are %.2f + %.2fi and %.2f - %.2fi",-b/(2* a),
sqrt(-(FUNC(a,b,c))), -b/(2* a), sqrt(-(FUNC(a,b,c))));}
}

#include <math.h>
#define FUNC(a,b,c) b*b-4*a*c
#define realroot(func) func>0
#define doubleroot(func) func==0
#define imaginaryroot(func) func<0

int main () {
float a,b,c,f;
printf("Please enter the coefficients of a quadratic equation (a b c):");
scanf("%f %f %f", &a, &b, &c);
f=FUNC(a,b,c);
if (realroot(f))
{printf("The answers are %.2f and %.2f", (-
b+sqrt(FUNC(a,b,c)))/(2*a),(-b-sqrt(FUNC(a,b,c)))/(2*a));}
else if (doubleroot(f))
{printf("The answers are both %.2f", (-b/(2*a)));}
else if (imaginaryroot(f))
{printf("The answers are %.2f + %.2fi and %.2f - %.2fi",-b/(2* a),
sqrt(-(FUNC(a,b,c))), -b/(2* a), sqrt(-(FUNC(a,b,c))));}
}

30
2022/10/26


T if (answer <1)
F r {
a u
l e printf(“%d is smaller than %d”, a, b);
s
e }
T else if (answer >1)
F r {
a u
l e printf(“%d is larger than %d”, a, b); T
s r
e } u
T else if (answer ==1) T
e
r
F r { u
a u
l e printf(“%d and %d are equal”, a, b); T e
s
e
} r
u
e

31
2022/10/26

switch
switch(expression){
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */

/* you can have any number of case statements */


default : /* Optional */
statement(s);
}

int main() { int main() {


…. ….
switch(score / 10) { if(score / 10>9) {
case 10: case 9: printf("得 A");
printf("得 A"); }
break;
case 8: else if(score / 10==8){
printf("得 B" ); printf("得 B" );
break; }
case 7: else if(score / 10==7){
printf("得 C" ); printf("得 C" );
break; break;
case 6: else if(score / 10==6){
printf("得 D" ); printf("得 D" );
break; }
default: else{
printf("不及格" ); printf("不及格" );
break; }
}

32
2022/10/26

switch(ch1) {
case 'A':
printf("This A is part of outer switch" );
switch(ch2) {
case 'A':
printf("This A is part of inner switch" );
break;
case 'B': /* case code */
}
break;
case 'B': /* case code */
}

if(ch1 == A) {
printf("This A is part of outer switch" );
if(ch2 == A) {
printf("This A is part of inner switch" );
}
else if(ch2 ==B){
codes
}
}
else if(ch1 ==B){
codes
}

33
2022/10/26

struct function can be used to define/combine


many variables as one group

Ex:
struct name{
type variable;


}group name;

34
2022/10/26

PCR isopycnic centrifugation Partitioning DNA with


different density gradients

struct func{ struct func{


char site[10]; char site[10];
char season; char season;
float bacteria[10]; float bacteria[10];
}; } mangrove[12];

int main{ int main{


struct func mangrove[12]; …
mangrove[0].season;
… mangrove[3].bacteria[2];
mangrove[0].season;
mangrove[3].bacteria[2];

35
2022/10/26

struct func{
char site[10];
char season;
float bacteria[10];
}man[12];

int main{

while(feof(fp)==0){
fscanf(fp, "%[^,], %[^,],
%f,%f",&man[i].name,&func[i].season,&func[i].ba
cteria[0]&func[i].bacteria[1]);
i++;
}

More on programming
• I know C already. What is the next ?

• Learning algorithm
▫ Discrete math
▫ Algorithm
▫ Linear algebra
▫ Engineering mathematics
▫ Statistical methods
▫ Many many others

36
2022/10/26

Scientific tools

• Numerical analysis
• Matrix computation
• Optimization tools
• Statistical tools
• Graphical tools
• …

• Google

• Netlib

• GSL - GNU Scientific Library

37
2022/10/26

http://www.gnu.org/software/gsl/

38
2022/10/26

39
2022/10/26

將下載的GSL資料複製到
C:\cygwin64\home\admin

40
2022/10/26

依序執行以下指令
cd ~
tar -xzvf gsl-latest.tar.gz
cd gsl-2.6
./configure
make -j10
make install
make check

1 2

41
2022/10/26

1
2

再將課程提供之ZIP檔案解壓縮到指定目錄

42
2022/10/26

43
2022/10/26

44

You might also like