程式語言-Week 9 2
程式語言-Week 9 2
國立臺灣大學 生工系
111學年度 第一學期
計算機應用及程式語言
Week 9
2022.10.31
1
2022/10/26
2
2022/10/26
3
2022/10/26
4
2022/10/26
int a=90;
printf(“%p %d”, &a, a);
a (0022A) 90
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
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);
}
#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);
}
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;
var (0022A) 20
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
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);
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;
}
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
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
FILE *fp;
fp=fopen("test.txt", "a+");
if(fp==NULL){
printf("file is not found");
return 0;}
18
2022/10/26
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
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(pointer)
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>
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
24
2022/10/26
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
26
2022/10/26
int main(){
linear(2);
}
27
2022/10/26
int main(){
float b;
printf("enter a slope:");
scanf("%f", &b);
printf("\n\n");
linear(b);
}
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 */
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
Ex:
struct name{
type variable;
…
…
}group name;
34
2022/10/26
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
• …
• Netlib
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