# C Programming Language (202001-A1)
{%youtube youtubeid %}
## :blue_book: Class09 (2020.02.03)
```c=
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int ABC[6]={11, 48, 30, 17, 62, 37};
int i, max, min;
min=max=0;
for (i=0; i<6; i++)
{
if (*(ABC+i)>*(ABC+max))
{
max=i;
}
if (*(ABC+i)<*(ABC+min))
{
min=i;
}
}
printf("The index of maximum value in array ABC is %d\n", max);
printf("The index of minimum value in array ABC is %d\n", min);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int ONE[5]={31, 17, 33, 22, 16};
int *ptr=ONE;
int i;
printf("ONE[5]={");
for (i=0; i<5; i++)
{
if (i!=4)
{
printf("%d ", *(ptr+i));
}
else
{
printf("%d}\n", *(ptr+i));
}
}
printf("ONE[5]={");
for (i=0; i<5; i++)
{
if (i!=4)
{
printf("%d ", *(ptr+i)+2);
}
else
{
printf("%d}\n", *(ptr+i)+2);
}
}
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *ptr="We are best friends.";
int i, cont=0;
for (i=0; *(ptr+i)!='\0'; i++)
{
if (*(ptr+i)>=97&&*(ptr+i)<=122)
{
cont=cont+1;
}
}
printf("Number of lowercase in *ptr is %d\n", cont);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int a, b;
a=35;
b=a&7;
printf("a & 7 = %d\n", b);
b&=7;
printf("b & 7 = %d\n", b);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
unsigned char cht, ch1, ch2;
ch1=41;
ch2=11;
cht=ch1&ch2;
printf("ch1 and ch2 = %d\n", cht);
cht=ch1|ch2;
printf("ch1 or ch2 = %d\n", cht);
cht=ch1^ch2;
printf("ch1 xor ch2 = %d\n", cht);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
unsigned char ch=53;
unsigned char ix=5;
ch=ch<<1;
printf("ch=%x\n", ch);
ch=ch<<1;
printf("ch=%x\n", ch);
ch=ch<<1;
printf("ch=%x\n", ch);
ch=ch>>1;
printf("ch=%x\n", ch);
ch=ch>>1;
printf("ch=%x\n", ch);
ix=ix<<5;
printf("ix=%x\n", ix);
ix=ix>>3;
printf("ix=%x\n", ix);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *JJ="Hello";
char *KK="World";
char *tmp;
printf("JJ=%s KK=%s\n", JJ, KK);
tmp=JJ;
JJ=KK;
KK=tmp;
printf("JJ=%s KK=%s\n", JJ, KK);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n=20, *p, **pp;
p=&n;
pp=&p;
printf("n=%d, &n=%p, *p=%d, p=%p, &p=%p\n", n, &n, *p, p, &p);
printf("**pp=%d, *pp=%p, pp=%p, &pp=%p\n", **pp, *pp, pp, &pp);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int num[3][4];
printf("num=%p\n", num);
printf("&num=%p\n", &num);
printf("*num=%p\n", *num);
printf("num[0]=%p\n", num[0]);
printf("num[1]=%p\n", num[1]);
printf("num[2]=%p\n", num[2]);
printf("&num[0]=%p\n", &num[0]);
printf("&num[1]=%p\n", &num[1]);
printf("&num[2]=%p\n", &num[2]);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int num[3][5]={{12, 23, 34, 45, 56},
{54, 43, 32, 21, 10},
{55, 66, 22, 77, 88}};
int m, n;
for (m=0; m<3; m++)
{
for (n=0; n<5; n++)
{
printf("num[%d][%d]=%d, address=%p\n", m, n, *(*(num+m)+n), *(num+m)+n);
}
}
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
struct data
{
char name[20];
int math;
};
struct data student;
printf("Please type in your name: ");
gets(student.name);
printf("Please input a score: ");
scanf("%d", &student.math);
printf("Name: %s\n", student.name);
printf("Score: %d\n", student.math);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
struct data
{
char name[20];
int math;
};
struct data student={"Mary Wang", 75};
printf("Name: %s\n", student.name);
printf("Score: %d\n", student.math);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
struct data
{
char name[15];
int eng;
};
struct data std;
printf("sizeof(std)=%d\n", sizeof(std));
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
struct data
{
char name[15];
int sex;
int age;
}student_1={"John Lee", 1, 19}, student_2={"Mary Wang", 0, 20};
printf("%s\n", student_1.name);
printf("Gender: ");
if (student_1.sex==1)
{
printf("Male");
}
else
{
printf("Female");
}
printf("\n");
printf("age: %2d\n", student_1.age);
printf("%s\n", student_2.name);
printf("Gender: ");
if (student_2.sex==1)
{
printf("Male");
}
else
{
printf("Female");
}
printf("\n");
printf("age: %2d\n", student_2.age);
system("pause");
return 0;
}
/* prog10_13, 指標常數的值與位址 1430*/
//a是一個指向陣列位址的指標常數, 儲存陣列第一個元素的位置
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i,a[5]={32,16,35,65,52};
printf("a=%p\n",a); /* 印出指標常數a的值 */
printf("&a=%p\n",&a); /* 印出指標常數a的位址 */
//陣列名稱本身就是存放陣列位址的變數
//陣列名稱本身是一個存放位址的指標常數, 他指向陣列的位址(陣列巧妙設計)
//連續的記憶體空間配置給陣列元素存放
for(i=0;i<5;i++)
printf("&a[%d]=%p\n",i,&a[i]); /* 印出陣列a每一個元素的位址 */
system("pause");
return 0;
}
/* prog10_14, 利用指標常數來存取陣列的內容 指標的算數運算*/ //1505
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a[3]={5,7,9};
printf("a[0]=%d, *(a+0)=%d\n",a[0],*(a+0));
//int 每個元素佔了4個位元組, a+i代表a[i]的位址, 運用指標的算數運算,
//可控制陣列的各個元素
printf("a[1]=%d, *(a+1)=%d\n",a[1],*(a+1));
//所指向的資料型態大小來處理
printf("a[2]=%d, *(a+2)=%d\n",a[2],*(a+2));
system("pause");
return 0;
}
/* prog10_15, 利用指標求陣列元素和 */ //1515
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a[3]={5,7,9};
int i,sum=0;
for(i=0;i<3;i++)
sum+=*(a+i); /* 加總陣列元素的總和 sum=sum+a[i] */
printf("sum=%d\n",sum);
system("pause");
return 0;
}
/* prog10_16, 利用指標求陣列元素和 */ //1523
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a[3]={5,7,9};
int i,sum=0;
int *ptr=a; /* 設定指標ptr指向陣列元素a[0] */
for(i=0;i<3;i++)
sum+=*(ptr++); /* 計算陣列元素值的累加 */
printf("sum=%d\n",sum);
system("pause");
return 0;
}
//位元運算子 0830
/* ch19_4.c */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b;
a = 35; //10 0011
b = a & 7; //00 0111
printf("a & b (10 進位) = %d \n",b);
b &= 7; //00 0111
printf("a & b (10 進位) = %d \n",b);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
unsigned char cht,ch1,ch2;
ch1=41; //101001
ch2=11; //001011
cht=ch1&ch2;
printf("101001 and 001011=%2d\n",cht);
cht=ch1|ch2;
printf("101001 or 001011=%2d\n",cht);
cht=ch1^ch2; //exclusive KK[ɪkˋsklusɪv] or
printf("101001 xor 001011=%2d\n",cht);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(void) {
unsigned char ch=53;
unsigned char ix=5;
ch=ch<<1;
printf("ch=%x\n",ch);
ch=ch<<1;
printf("ch=%x\n",ch);
ch=ch<<1;
printf("ch=%x\n",ch);
ch=ch>>1;
printf("ch=%x\n",ch);
ch=ch>>1;
printf("ch=%x\n",ch);
ix=ix<<5;
printf("ix<<5=%x\n",ix);
ix=ix>>3;
printf("ix>>3=%x\n",ix);
system("pause"); return 0;
}
/* ch19_1.c */
#include <stdlib.h>
#include <stdio.h>
//int decimalToBin(int);
int BIN(int);
int main()
{
int x;
printf("Please input an integer: ");
scanf("%d", &x);
printf("DEC %d , BIN %d\n", x, BIN(x));
system("pause");
return 0;
}
//int decimalToBin(int n)
int BIN(int x)
{
int binary = 0; /* 紀錄 2 進位數字 */
int times = 1; /* 每一次增加 10 倍*/
int rem; /* 餘數 */
int i = 1; /* 求餘數迴圈次數 */
while (x != 0)
{
rem = x % 2; /* 計算 2 的餘數 */
printf("%d: %3d, %d\n",i++ ,x ,rem);
//printf("loop %d: %3d/2, %d\n",i++ ,x ,rem);
//printf("loop %d: %3d/2, 餘=%d, 商=%d\n",i++ ,n ,rem ,n/2);
x /= 2; /* 商 */
binary += rem*times; /* 儲存 10 進位 */
times *= 10; /* 往左至下一筆 */
}
return binary;
}
#include <stdio.h> //0900
#include <stdlib.h>
#define ROW 2 // 定義陣列的列個數
#define COL 3 // 定義陣列的行個數
int main(void)
{
int i,j;
int A[ROW][COL]={{3,2,4},
{6,1,5}}; // 宣告陣列 A 並設定初值
int B[ROW][COL]={{2,-1,3},
{3,-4,-2}}; // 宣告陣列 B 並設定初值
int C[ROW][COL]={0}; // 宣告陣列 C 並設定初值為 0
int *piA, *piB, *piC;
for( i = 0 ; i < ROW ; i++ ) {
piA = A[i]; piB = B[i]; piC = C[i];
for( j = 0 ; j < COL ; j++ )
*(piC+j) = *(piA+j) + *(piB+j);
}
printf("A + B = \n");
for(i=0;i<ROW;i++) {
piC = C[i];
for(j=0;j<COL;j++)
printf("%3d",*(piC+j)); // 輸出陣列 C 的內容
printf("\n");
}
system("pause"); return 0;
}
/* 自訂一個函式mymean(),傳入一個大小為5的整數陣列,傳回元素的平均值 */
#include <stdio.h>
#include <stdlib.h>
#define LEN 5
float mymean(int *, int); /* 函數原型宣告 */
int main() {
int ABC[]={11, 33, 55, 77, 99}; /* 整數陣列 */
printf("ABC: ");
int i;
for(i=0; i<5; i++)
{
printf("%d ", ABC[i]);
}
float m=mymean(ABC, 5); /* 呼叫函式傳回陣列平均值 */
printf("\navg=%.1f\n", m); /* 輸出結果 */
system("pause");
return 0;
}
float mymean(int *ABC, int len) { /* 傳入陣列與長度 */
int i, sum=0;
for (i=0; i<len; i++) { /* 掃描陣列 */
sum=sum+*(ABC+i); /* 累加求元素總和 */
}
return sum/len; /* 傳回平均值 */
}
/* 指標存放變數的位置 透過位置可存取變數的內容 雙重指標 指標指向另一個指標變數 指向指標的指標
存放某個指標的位址*/
#include<stdio.h> //0920
#include<stdlib.h>
int main(void)
{
int p=20;
int *ptr1,**ptr2;
ptr1=&p;
ptr2=&ptr1;
printf("p=%d, &p=%p, *ptr1=%d, ptr1=%p, &ptr1=%p\n",p,&p,*ptr1,ptr1,&ptr1);
printf("**ptr2=%d, *ptr2=%p, ptr2=%p, &ptr2=%p\n",**ptr2,*ptr2,ptr2,&ptr2);
system("pause");
return 0;
}
/* 陣列名稱是一個指向陣列位址的指標常數 指標常數的位址等於指標常數的內容
num是雙重指標常數 他指向指標常數陣列的起始位址 */
#include<stdio.h> //0943
#include<stdlib.h>
int main(void)
{
int num[3][4];
printf("num=%p\n",num);
printf("&num=%p\n",&num);
printf("*num=%p\n",*num);
printf("num[0]=%p\n",num[0]);
printf("num[1]=%p\n",num[1]);
printf("num[2]=%p\n",num[2]);
printf("&num[0]=%p\n",&num[0]);
printf("&num[1]=%p\n",&num[1]);
printf("&num[2]=%p\n",&num[2]);
system("pause");
return 0;
}
#include<stdio.h> //1023
#include<stdlib.h>
int main(void)
{
int num[3][4]={{12,23,42,18},
{43,22,16,14},
{31,13,19,28}};
int m,n;
for(m=0;m<3;m++)
{
for(n=0;n<4;n++)
{
printf("num[%d][%d]=%d, address=%p\n",m,n,*(*(num+m)+n),*(num+m)+n);
}
}
system("pause");
return 0;
}
#include<stdio.h> //1045
#include<stdlib.h>
int main(void)
{
struct data
{
char name[20];
int math;
}student;
printf("Please type in your name:");
gets(student.name);
printf("Please input a score:");
scanf("%d",&student.math);
printf("Name:%s\n",student.name);
printf("Score:%d\n",student.math);
system("pause");
return 0;
}
/* 編譯器以結構中資料型態最大者為基本單位 */
#include<stdio.h> //1057
#include<stdlib.h>
int main(void)
{
struct data
{
char name[15];
int math;
}student;
printf("sizeof(student)=%d\n",sizeof(student));
system("pause");
return 0;
}
#include<stdio.h> //1104
#include<stdlib.h>
int main(void)
{
struct data
{
char name[15];
int math;
};
struct data student={"Mary Wang",74};
printf("Name:%s\n",student.name);
printf("Score:%d\n",student.math);
system("pause");
return 0;
}
#include<stdio.h> //1113
#include<stdlib.h>
int main(void)
{
struct data
{
char name[15];
int sex;
int age;
}stu1={"John",1,18},stu2={"Mary",0,19};
printf("%s gender: ",stu1.name);
if(stu1.sex==1)
{
printf("Male");
}
else
{
printf("Female");
}
printf("\nage:%2d\n",stu1.age);
printf("%s gender: ",stu2.name);
if(stu2.sex==1)
{
printf("Male");
}
else
{
printf("Female");
}
printf("\nage:%2d\n",stu2.age);
system("pause");
return 0;
}
//1128
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
struct data
{
char name[15];
int math;
}stu1={"Lily Chen",83};
struct data stu2;
stu2=stu1;
printf("stu2.name=%s, stu2.math=%d\n",stu2.name,stu2.math);
system("pause");
return 0;
}
#include<stdio.h> //1133
#include<stdlib.h>
int main(void)
{
struct data
{
char name[10];
int math;
}student[10];
printf("sizeof(student[3])=%d\n",sizeof(student[3]));
printf("sizeof(student)=%d\n",sizeof(student));
system("pause");
return 0;
}
#include<stdio.h> //1138
#include<stdlib.h>
int main(void)
{
struct date
{
int month;
int day;
};
struct data
{
char name[15];
int math;
struct date birthday;
}stu1={"Mary Wang",74,{10,2}};
printf("Name: %s\n",stu1.name);
printf("Birthday: %d %d\n",stu1.birthday.month,stu1.birthday.day);
system("pause");
return 0;
}
#include<stdio.h> //1320
#include<stdlib.h>
#define MAX 2
int main(void)
{
int i;
struct data
{
char name[15];
int math;
}student[MAX];
for(i=0;i<MAX;i++)
{
printf("Name: ");
gets(student[i].name);
printf("Score: ");
scanf("%d",&student[i].math);
fflush(stdin);
}
for(i=0;i<MAX;i++)
{
printf("%s Score=%d\n",student[i].name,student[i].math);
}
system("pause");
return 0;
}
#include<stdio.h> //1335
#include<stdlib.h>
int main(void)
{
struct data
{
char name[15];
int math;
int eng;
}student, *ptr;
ptr=&student;
printf("Name: ");
gets(ptr->name);
printf("Math: ");
scanf("%d",&ptr->math);
printf("English: ");
scanf("%d",&ptr->eng);
printf("Math=%d\n",ptr->math);
printf("English=%d\n",ptr->eng);
printf("Average=%.2f\n",(ptr->math+ptr->eng)/2.0);
system("pause");
return 0;
}
#include<stdio.h> //1350
#include<stdlib.h>
struct data
{
char name[15];
int math;
};
void display(struct data);
int main(void)
{
struct data s1={"Jenny",94};
display(s1);
system("pause");
return 0;
}
void display(struct data st)
{
printf("Name: %s\n",st.name);
printf("Math: %d\n",st.math);
}
/* ch13_15.c */
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct date /* 內層結構 */
{
int year; /* 出生年 */
int month; /* 出生月 */
int day; /* 出生日 */
};
struct data /* 外層結構 */
{
char name[12]; /* 名字 */
int id; /* 學號 */
char gender; /* 性別 */
struct date birth; /* 出生日其結構 */
};
struct data student[3] = {{"John",220501,'M',{2001,8,20}},
{"Kevin",220502,'M',{2001,3,19}},
{"Christy",220503,'F',{2001,5,6}}};
int i;
for (i = 0; i < 3; i++)
{
printf("Name : %s\n",student[i].name);
printf("ID : %d\n",student[i].id);
printf("Gender : %c\n",student[i].gender);
printf("Birthday : %d\\%d\\%d\n",student[i].birth.year,\
student[i].birth.month,\
student[i].birth.day);
printf("=====\n");
}
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
union Paid
{
char creditCard[21];
char bankAccount[17];
int iCash;
};
#include<stdio.h>
#include<stdlib.h>
struct data
{
char name[15];
int eng;
};
void student(struct data *, struct data *);
int main(void)
{
struct data stud1={"Mary",74};
struct data stud2={"Terasa",88};
student(&stud1, &stud2);
printf("After call student() function: \n");
printf("student1 name: %s, student1 Score: %d\n",stud1.name ,stud1.eng);
printf("student2 name: %s, student2 Score: %d\n",stud2.name ,stud2.eng);
system("pause");
return 0;
}
void student(struct data *stud1, struct data *stud2)
{
struct data tmp;
tmp=*stud1;
*stud1=*stud2;
*stud2=tmp;
}
/* ch13_12.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Books
{
char title[20];
char author[20];
int price;
};
void show(struct Books *);
int main()
{
struct Books book;
strcpy(book.title, "programming C");
strcpy(book.author, "施威銘");
book.price = 700;
show(&book);
system("pause");
return 0;
}
void show(struct Books *book)
{
printf("Book : %s\n", book->title);
printf("Author: %s\n", book->author);
printf("Price : %d\n", book->price);
}
int main(void)
{
union Paid money;
printf("sizeof(union paid)=%2d\n",sizeof(union paid));
printf("Please enter a bank account number: ");
scanf("%s",&money.bankAccount);
printf("Bank account:%s\n",money.bankAccount);
printf("Please enter a credit card number: ");
scanf("%s",&money.creditCard);
printf("Card number:%s\n",money.creditCard);
printf("Please enter a cash amount: ");
scanf("%d",&money.iCash);
printf("Cash:%4d\n",money.iCash);
printf("Card number:%s\n",money.creditCard);
system("pause");
return 0;
}
#include<stdio.h> //1420
#include<stdlib.h>
#include<string.h>
union Paid
{
char creditCard[17];
int cash;
}money;
int main(void)
{
int Amt=1500,Opt;
printf("Amounts payable: %d\n",Amt);
do
{
printf("Choose payment method (1) credit card (2) cash :");
scanf("%d",&Opt);
fflush(stdin);
if(Opt==1)
{
printf("Please enter your card number: ");
gets(money.creditCard);
if(strlen(money.creditCard)!=16)
{
printf("Card number error! Please re-operate!\n");
Opt=0;
}
else
{
printf("Payment completed!\n");
}
}
else if(Opt==2)
{
printf("Please enter the amount of cash: ");
scanf("%d",&money.cash);
if(money.cash<Amt)
{
printf("Insufficient amount! Please try again!\n");
Opt=0;
}
else
{
printf("give change: %3d\n",money.cash-Amt);
}
}
else
{
printf("Please enter the correct payment mothod \n");
}
}while(Opt!=1&&Opt!=2);
system("pause");
return 0;
}
prog9_21, 列舉型態的使用 列舉一群有意義的名稱已取代整數值*/
#include <stdio.h>
#include <stdlib.h>
enum Fruit{APPLE,ORANGE,GRAPE};
// 自動編號
enum RGBColor{RED = 10,GREEN, BLUE};
// 設定第一個成員的編號
enum Animal{LION = 1, TIGER=11, BEAR=22};
// 設定每一個成員的編號
int main(void) {
printf("APPLE=%d ORANGE=%d GRAPE=%d\n",APPLE, ORANGE, GRAPE);
printf("RED=%d GREEN=%d BLUE=%d\n",RED, GREEN, BLUE);
printf("LION=%d TIGER=%d BEAR=%d\n",LION, TIGER, BEAR);
system("pause"); return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
enum color
{
red,
green,
blue
};
enum color shirt;
printf("sizeof(shirt)=%d\n",sizeof(shirt));
printf("red=%d\n",red);
printf("green=%d\n",green);
printf("blue=%d\n",blue);
shirt=green;
if(shirt==green)
{
printf("Chosen the green clothes");
}
else
{
printf("Chosen the non-green chlthes");
}
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char key;
enum color
{
red=114,
green=103,
blue=98
};
enum color shirt;
do
{
printf("Please input (r,g,b):");
scanf("%c",&key);
fflush(stdin);
}while((key!=red)&&(key!=green)&&(key!=blue));
shirt=key;
switch(shirt)
{
case red:
printf("You selected red\n");
break;
case green:
printf("You selected green\n");
break;
case blue:
printf("You selected blue\n");
break;
}
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
enum Week
{
SUN,
MON,
TUE,
WED,
THU,
FRI,
SAT
}theday;
int main(void)
{
char cChiName[7][10] = {"星期日", "星期一","星期二",
"星期三","星期四","星期五","星期六",};
char cEngName[7][10] = {"Sunday","Monday","Tuesday",
"Wednesday","Thursday","Friday","Saturday"};
printf("英文 中文 \n");
for (theday = SUN ; theday <= SAT ; theday++) // 輸出一週七天的中英文名稱
printf("%-10s %s \n",cEngName[theday], cChiName[theday]);
system("pause"); return(0);
}
#include <stdio.h>
#include <stdlib.h>
enum Identity
{
teacher=1,
student
};
struct Teacher {
int iTID; // 教師編號
int iYears; // 服務年資
};
struct Student {
int iSID; // 學號
int iDegree; // 年級
};
struct PersonalInfo {
char cName[10]; // 姓名
enum Identity iType; // 身份別, 1 表示老師, 2 代表學生
union {
struct Teacher tea;
struct Student stu;
};
} person;
int main(void)
{
int iOpt;
do {
printf("Please select status - 1 (teacher) 2 (student) : ");
scanf("%d",&iOpt);
fflush(stdin);
} while( iOpt != 1 && iOpt != 2 );
//struct PersonalInfo *ptr=&person;
person.iType = iOpt;
if ( iOpt == 1 ) { // 身份為老師
printf("Please enter the teacher's name: "); gets(person.cName); //scanf("%s",person.cName);
printf("Enter the teacher's number: "); scanf("%d",&person.tea.iTID); fflush(stdin);
printf("Please enter years of service: "); scanf("%d",&person.tea.iYears); fflush(stdin);
}
else { // 身份為學生
printf("Please enter the student's name: "); gets(person.cName); //scanf("%s",person.cName);
printf("Please enter student ID: "); scanf("%d",&person.stu.iSID); fflush(stdin);
printf("Please enter the student's grade: "); scanf("%d",&person.stu.iDegree); fflush(stdin);
}
if( person.iType == teacher )
printf("%s teacher's number: %6d years of service: %2d\n",
person.cName,person.tea.iTID,person.tea.iYears);
else
printf("student %s student ID: %6d %2d grade\n",
person.cName,person.stu.iSID,person.stu.iDegree);
system("pause"); return(0);
}
```
## :blue_book: Class08 (2020.01.31)
```c=
//10503_02 0835
//check max's index
#include <stdlib.h>
#include <stdio.h>
int func(int b[],int);
int i;
int main(void)
{
int a[10]={ 1, 3, 9, 2, 5, 8, 4, 9, 6, 7 };
printf("%d\n", func(a, 10));
system("pause");
return 0;
}
int func (int b[], int n)
{
int index = 0;
for (i=1; i<=n-1; i=i+1)
{
if (b[i] >= b[index])
{
index = i;
}
}
return index;
}
//10503_04 0900
//51+52+53+...+60
#include <stdlib.h>
#include <stdio.h>
int i;
int main()
{
int a[100];
int b[100];
for (i=1; i<100; i=i+1)
{
b[i] = i;
}
a[0] = 0;
for (i=1; i<100; i=i+1)
{
a[i] = b[i] + a[i-1];
}
printf ("%d\n", a[60]-a[50]);
system("pause");
return 0;
}
//1105
#include <stdio.h>
#include <stdlib.h>
void Update(int [], int); // 原型宣告, 引數2 通常會傳遞陣列的大小
void Update(int Arr[], int n) {
int i;
for( i = 0 ; i < n ; i++ ) {
if( Arr[i]%2 ) Arr[i]++; // 奇數,內容加1
else Arr[i]--; // 偶數數,內容減1
}
}
int main(void)
{
int i;
int iNum[6] = {23,46,37,51,48,9};
printf("Before Update: ");
for( i = 0 ; i < 6 ; i++ ) printf("%2d ",iNum[i]);
printf("\n");
Update(iNum, 6);
printf("After Update: ");
for( i = 0 ; i < 6 ; i++ ) printf("%2d ",iNum[i]);
printf("\n");
system("pause"); return(0);
}
/* prog9_24, 字串陣列的複製 1132 */
#include <stdio.h>
#include <stdlib.h>
#define MAX 3
#define LENGTH 10
int main(void)
{
char arr1[MAX][LENGTH]={"Tom","Lily","James Lee"};
char arr2[MAX][LENGTH];
int i,j;
for(i=0;i<MAX;i++) /* 將arr1的內容複製到arr2中 */
{
for(j=0;j<LENGTH;j++)
{
if(arr1[i][j]=='\0') /* 如果遇到「\0」,代表讀到字串結束 */
{
//printf("arr2[%d]=%c\n",i,arr2[i][j]);
break;
}
/* 此行的break敘述會跳到第19行執行 */
else
{
arr2[i][j]=arr1[i][j];
//printf("arr2[%d]=%c\n",i,arr2[i][j]);
}
}
arr2[i][j]='\0';
}
for(i=0;i<MAX;i++)
{
printf("arr2[%d]=%s\n",i,arr2[i]); /* 印出陣列arr2的內容 */
//printf("SIZE: %d\n",sizeof(arr2[i]));
}
system("pause"); return 0;
}
/* prog10_1, 印出變數於記憶體內的位址 提供程式快速存取資料 指向變數所使用記憶體的位址 1325*/
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a=5,b=10;
double c=3.14;
printf("a=%4d, sizeof(a)=%d, address:%d\n",a,sizeof(a),&a);
printf("b=%4d, sizeof(b)=%d, address:%d\n",b,sizeof(b),&b);
printf("c=%4.2lf, sizeof(c)=%d, address:%d\n",c,sizeof(c),&c);
system("pause"); return 0;
}
指標,其實也只是一個變數,只是這個變數的意義是:指向某個儲存位址。
int *p1 = &a; 我們看成:p1 is a pointer points to integer variable a,即:p1是一個指標,指向整數變數a。每個變數會存在記憶體的某個位置,每個記憶體的位置會被付與一個位址,可以使用位址運算子 & 存取這個位址 。
/* prog10_2, 指標變數的宣告 1342*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *ptr,num=20; /* 宣告變數num與指標變數ptr */
/* 將num的位址設給指標變數ptr存放 */
ptr=# /*指標變數ptr指向變數num */
printf("num=%d, &num=%p\n",num,&num); /*%p輸出位址格式碼 */
printf("*ptr=%d, ptr=%p, &ptr=%p\n",*ptr,ptr,&ptr);
system("pause");
return 0;
}
/* prog10_3, 指標變數的使用 重新設定指向另一個相同型態的變數 1358*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a=5,b=3;
int *ptr; /* 宣告指標變數ptr */
ptr=&a; /* 將a的位址設給指標ptr存放 */
printf("&a=%x, &ptr=%x, ptr=%x, *ptr=%d\n",&a,&ptr,ptr,*ptr);
ptr=&b; /* 將b的位址設給指標ptr存放 */
printf("&b=%x, &ptr=%x, ptr=%x, *ptr=%d\n",&b,&ptr,ptr,*ptr);
system("pause");
return 0;
}
/* prog10_4, 指標變數的大小 1415*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *ptri; /* 宣告指向整數的指標ptri */
char *ptrc; /* 宣告指向字元的指標ptrc */
printf("sizeof(ptri)=%d\n",sizeof(ptri));
//64位元作業系統.指標變數存放記憶體的位址, 佔了8個位元組
printf("sizeof(ptrc)=%d\n",sizeof(ptrc));
printf("sizeof(*ptri)=%d\n",sizeof(*ptri));
//指向整數型態的指標, *ptri代表ptri所指向的整數, 所以*ptri佔了4個位元組
printf("sizeof(*ptrc)=%d\n",sizeof(*ptrc));
//指向字元型態的指標, *ptrc代表ptrc所指向的字元, 所以*ptrc佔了1個位元組
system("pause");
return 0;
}
/* prog10_5, 指標的操作練習 1427 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a=5,b=10;
int *ptr1,*ptr2;
ptr1=&a; /* 將ptr1設為a的位址 */
ptr2=&b; /* 將ptr2設為b的位址 */
printf("a=%p, b=%p, \nptr1=%p, ptr2=%p\n",&a,&b,ptr1,ptr2);
*ptr1=7; /* 將ptr1指向的內容設為7 */
*ptr2=32; /* 將ptr2指向的內容設為32 */
a=17; /* 設定a為17 */
ptr1=ptr2; /* 設定ptr1=ptr2 */
*ptr1=9; /* 將ptr1指向的內容設為9 */
ptr1=&a; /* 將ptr1設為a的位址 */
a=64; /* 設定a為64 */
*ptr2=*ptr1+5; /* 將ptr2指向的內容設為*ptr1+5*/
ptr2=&a; /* 將ptr2設為a的位址 */
printf("a=%2d, b=%2d, *ptr1=%2d, *ptr2=%2d\n",a,b,*ptr1,*ptr2);
printf("ptr1=%p, ptr2=%p\n",ptr1,ptr2);
system("pause"); return 0;
}
/* prog10_6, 錯誤的指標型態 賦予指標指向的資料型態 */
//型態不合, 造成指向的變數內容不正確
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a1=100, *ptri;
float a2=3.2f, *ptrf;
ptri=&a2; /* 錯誤,將int型態的指標指向float型態的變數 */
ptrf=&a1; /* 錯誤,將float型態的指標指向int型態的變數 */
printf("sizeof(a1)=%d\n",sizeof(a1));
printf("sizeof(a2)=%d\n",sizeof(a2));
printf("a1=%d,*ptri=%d\n",a1,*ptri);
printf("a2=%.1f,*ptrf=%.1f\n",a2,*ptrf);
system("pause");
return 0;
}
/* 10 prog10_7, 傳遞指標到函數裡 */
#include <stdio.h>
#include <stdlib.h>
void address(int *); /* 宣告address()函數的原型 指向整數的指標 */
int main(void)
{
int a=12; /* 設定變數a的值為12 */
int *ptr=&a; /* 將指標ptr指向變數a */
address(&a); /* 將a的位址傳入address()函數中 */
address(ptr); /* 將ptr傳入address()函數中 */
system("pause");
return 0;
} //可接收指向整數型態的指標(存放整數變數的位址)
void address(int *p1) /* 將指向整數變數a的指標變數ptr 傳入address()函數中 */
{
printf("Address: %p,Variable content: %d\n",p1,*p1);
}
/* prog10_8, 傳遞指標的應用 */
#include <stdio.h>
#include <stdlib.h>
void add10(int *); /* add10()函數的原型 */
int main(void)
{
int a=5;
printf("Before call add10(),a=%d\n",a);
add10(&a); /* 呼叫add10()函數 */
printf("After call add10(),a=%d\n",a);
system("pause");
return 0;
}
void add10(int *p1)
{
*p1=*p1+10;
}
/* prog10_9, 將a與b值互換(錯誤示範) */
#include <stdio.h>
#include <stdlib.h>
void swap(int,int); /* swap()函數的原型 */
int main(void) {
int a=5,b=20;
printf("Before swap... ");
printf("a=%d,b=%d\n",a,b);
swap(a,b); /* 呼叫swap()函數,將a和b兩個變數的值互換 */
printf("After swap... ");
printf("a=%d,b=%d\n",a,b);
system("pause");
return 0;
}
void swap(int x,int y) { /* 定義swap()函數 */
int tmp=x;
x=y;
y=tmp;
}
/* prog10_10, 將a與b值互換(正確範例) */
#include <stdio.h>
#include <stdlib.h>
void swap(int *,int *); /* 函數swap()原型的宣告 */
int main(void) {
int a=5,b=20;
printf("Before swap...");
printf("a=%d,b=%d\n",a, b);
swap(&a, &b); /* 呼叫swap()函數,並傳入a與b的位址 */
printf("After swap...");
printf("a=%d,b=%d\n",a, b);
system("pause");
return 0;
}
void swap(int *p1,int *p2) /* swap()函數的定義 */
{
int tmp=*p1;
*p1=*p2;
*p2=tmp;
}
void sum(int, int *); // 原型宣告,引數為指標變數必須同時保留 * 號 1352
int main(void)
{
int n, total;
printf("Please input the integer: "); scanf("%d",&n);
sum(n, &total); // 呼叫 Sum, &total 將左值傳遞過去
printf("1+2+...+%d = %d\n",n,total);
system("pause"); return 0;
}
void sum(int n, int *result)
{
int i, m=0;
for( i = 1 ; i <= n ; i++ )
{
m += i;
}
*result = m; // 將計算結果儲存到 result 中
}
/* prog10_11, 傳回多個數值的函數 更改傳入的引數值 */
#include <stdio.h>
#include <stdlib.h>
void rect(int,int,int *,int *); /* 函數rect()的原型 */
int main(void)
{
int a=5,b=8;
int area,peri;
rect(a, b, &area, &peri); /* 呼叫rect(),計算面積及周長 */
printf("area=%d,total length=%d\n",area ,peri);
system("pause");
return 0;
}
void rect(int x,int y,int *p1, int *p2)
{
*p1=x*y;
*p2=2*(x+y);
}
/* hw10_13, 「*」與「++」運算子優先次序的比較 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num[] = {14, 23, 32, 62, 19};
int *p1, *p2;
p1 = p2 = num;
*p1++;
printf("*p1 = %d\n", *p1);
(*p2)++;
printf("*p2 = %d\n", *p2);
system("pause");
return 0;
}
/* hw10_18b */
#include <stdio.h>
#include <stdlib.h>
int main(void){
char *ptr = "We are best friends.";
int i, cont = 0;
for(i = 0; *(ptr + i) != '\0'; i++)
if(*(ptr + i) >= 97 && *(ptr + i) <= 122)
cont += 1;
printf("Number of lowercases in *ptr is %d\n",cont);
system("pause");
return 0;
}
```
## :blue_book: Class07 (2020.01.31)
```c=
/* prog8_14, 區域變數的範例7 */
#include <stdio.h>
#include <stdlib.h>
void func(void);
int main(void)
{
int a=100; /* 宣告main()函數裡的區域變數a */
printf("Before call func(),a=%d\n",a); /* 印出main()中a的值 */
func(); /* 呼叫自訂的函數 */
printf("After call func(),a=%d\n",a); /* 印出a的值 */
system("pause");
return 0;
}
void func(void) /* 函數func() */
{
int a=300; /* 宣告func()函數裡的區域變數a */
printf("In func() function,a=%d\n",a); /* 印出func函數中a的值 */
}
/* prog8_15, 全域變數的範例(一) 8 */
#include <stdio.h>
#include <stdlib.h>
void func(void); /* 函數func()的原型 */
int a; /* 宣告全域變數a */
int main(void) {
a=100; /* 設定全域變數a的值為100 */
printf("Before call func(),a=%d\n",a);
func(); /* 呼叫自訂的函數 */
printf("After call func(),a=%d\n",a);
system("pause");
return 0;
}
void func(void) /* 自訂的函數func() */
{
a=300; /* 設定全域變數a的值為300 */
printf("In func(),a=%d\n",a);
}
/* prog8_16, 全域變數的範例 */
#include <stdio.h>
#include <stdlib.h>
void func(void);
int a=50; /* 定義全域變數a */
int main(void) {
int a=100; /* 定義區域變數a */
printf("Before call func(),a=%d\n",a);
func(); /* 呼叫自訂的函數 */
printf("After call func(),a=%d\n",a);
system("pause");
return 0;
}
void func(void)
{
a=a+300; /* 這是全域變數a */
printf("In func(),a=%d\n",a);
}
/* prog8_17, 全域變數的使用範例 0948*/
#include <stdio.h>
#include <stdlib.h>
const double pi=3.14; /* 宣告全域變數pi */
void peri(double);
void area(double);
int main(void) {
double r=1.0;
printf("pi=%.2f\n",pi); /* 於main()裡使用全域變數 pi*/
printf("radius=%.2f\n",r);
peri(r); /* 呼叫自訂的函數 */
area(r);
system("pause"); return 0;
}
void peri(double r) /* 自訂的函數peri(),印出圓周 */
{
printf("Circumference=%.2f\n",2*pi*r); /* 於peri()裡使用全域變數pi */
}
void area(double r) /* 自訂的函數area(),印出圓面積 */
{
printf("Circular area=%.2f\n",pi*r*r); /* 於area()裡使用全域變數pi */
}
/* prog8_18, 區域靜態變數使用的範例 1025*/
#include<stdio.h>
#include<stdlib.h>
void func(void);
int main(void)
{
func();
func();
func();
func();
system("pause");
return 0;
}
void func(void)
{
static int a=200; //編譯時已配置固定記憶體空間 值可以保存下來
printf("In func(), a=%d\n",a);
a+=200;
}
#include <stdlib.h>
#include <stdio.h>
int func(int [], int);
int i;
int main(void)
{
int a[10]={1, 3, 9, 2, 5, 8, 4, 9, 6, 7};
printf("%d\n", func(a, 10));
system("pause");
return 0;
}
int func(int a[], int n)
{
int index=0;
for (i=1; i<=n-1; i++)
{
if (a[i]>=a[index])
{
index=i;
}
}
return index;
}
#include <stdlib.h>
#include <stdio.h>
int weather(void);
void comment_weather(int);
int main(void)
{
comment_weather(weather());
system("pause");
return 0;
}
int weather()
{
int temperature;
printf("Please enter the current temperature: ");
scanf("%d", &temperature);
fflush(stdin);
return temperature;
}
void comment_weather(int temperature)
{
if (temperature>=26)
{
printf("It's hot now\n");
}
else if (temperature>15)
{
printf("This is a comfortable temperature\n");
}
else if (temperature>5)
{
printf("It's a little cold\n");
}
else
{
printf("Cold weather\n");
}
}
/* prog8_19, 函數的傳值機制 1045 */
#include <stdio.h>
#include <stdlib.h>
void add20(int,int); /* add10()的原型 */
int main(void)
{
int a=33, b=55; /* 宣告區域變數a與b */
printf("Before call add10() function: ");
printf("a=%d, b=%d\n",a,b); /* 印出a、b的值 */
add10(a,b);
printf("After call add10() function: ");
printf("a=%d, b=%d\n",a,b); /* 印出a、b的值 */
system("pause");
return 0;
}
void add10(int a,int b)
{
a=a+20; /* 將變數a的值加10之後,設回給a */
b=b+20; /* 將變數b的值加10之後,設回給b */
}
#include <stdlib.h>
#include <stdio.h>
#define MAX 3
#define LEN 10
int main(void)
{
char arr1[MAX][LEN]={"Tom", "Jerry", "James"};
char arr2[MAX][LEN];
int i, j;
for (i=0; i<MAX; i++)
{
for (j=0; j<LEN; j++)
{
if (arr1[i][j]=='\0')
{
break;
}
else
{
arr2[i][j]=arr1[i][j];
}
}
arr2[i][j]='\0';
}
for (i=0; i<MAX; i++)
{
printf("arr2[%d]= %s\n", i, arr2[i]);
}
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int fac(int);
int main(void)
{
printf("fac(4)=%d\n", fac(4));
system("pause");
return 0;
}
int fac(int n)
{
if (n>0)
{
return (n*fac(n-1));
}
else
{
return 1;
}
}
#include <stdlib.h>
#include <stdio.h>
#define LEN 8
void update(int [], int);
int main(void)
{
int i;
int iNum[LEN]={22, 47, 37, 51, 48, 9, 33, 62};
printf("Before Update: ");
for (i=0; i<LEN; i++)
{
printf("%2d ", iNum[i]);
}
printf("\n");
update(iNum, LEN);
printf("After Update: ");
for (i=0; i<LEN; i++)
{
printf("%2d ", iNum[i]);
}
system("pause");
return 0;
}
void update(int iNum[], int n)
{
int i;
for (i=0; i<n; i++)
{
if (iNum[i]%2==1)
{
iNum[i]++;
}
}
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[50];
int i;
scanf("%s", &str);
fflush(stdin);
if (str[0]=='+')
{
for (i=1; i<strlen(str); i++)
{
str[i]+=2;
if (str[i]>'Z')
{
str[i]-=26;
}
}
}
if (str[0]=='-')
{
for (i=1; i<strlen(str); i++)
{
str[i]-=2;
if (str[i]<'A')
{
str[i]+=26;
}
}
}
printf("%s\n", str);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int a=5, b=10;
double c=6.28;
printf("a=%4d, sizeof(a)= %d, address: %d\n", a, sizeof(a), &a);
printf("b=%4d, sizeof(b)= %d, address: %d\n", b, sizeof(b), &b);
printf("c=%4.2f, sizeof(c)= %d, address: %d\n", c, sizeof(c), &c);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int *ptr;
int num=20;
ptr=#
printf("num= %d, &num= %p,\n", num, &num);
printf("*ptr= %d, ptr= %p, &ptr= %p\n", *ptr, ptr, &ptr);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int a=5, b=3;
int *ptr;
ptr=&a;
printf("&a= %p, &ptr= %p, ptr= %p, *ptr= %d\n", &a, &ptr, ptr, *ptr);
ptr=&b;
printf("&b= %p, &ptr= %p, ptr= %p, *ptr= %d\n", &b, &ptr, ptr, *ptr);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int *ptri;
char *ptrc;
printf("sizeof(ptri)= %d\n", sizeof(ptri));
printf("sizeof(ptrc)= %d\n", sizeof(ptrc));
printf("sizeof(*ptri)= %d\n", sizeof(*ptri));
printf("sizeof(*ptrc)= %d\n", sizeof(*ptrc));
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int tom=5, jerry=10;
int *ptr1, *ptr2;
ptr1=&tom;
ptr2=&jerry;
printf("tom= %p, jerry= %p, \nptr1= %p, ptr2= %p\n", &tom, &jerry, ptr1, ptr2);
*ptr1=7;
*ptr2=32;
tom=17;
ptr1=ptr2;
*ptr1=9;
ptr1=&tom;
tom=64;
*ptr2=*ptr1+5;
ptr2=&tom;
printf("a=%2d, b=%2d, *ptr1=%2d, *ptr2=%2d\n", tom, jerry, *ptr1, *ptr2);
printf("ptr1=%p, ptr2=%p\n", ptr1, ptr2);
system("pause");
return 0;
}
```
## :blue_book: Class06 (2020.01.30) test
```c=
#include <stdio.h>
#include <stdlib.h>
float get_score(void);
char level(float, float, float);
int main(void)
{
float s1, s2, s3;
char grade;
s1=get_score();
s2=get_score();
s3=get_score();
grade=level(s1, s2, s3);
printf("\nYour grade is %c \n", grade);
system("pause");
return 0;
}
float get_score(void)
{
float score;
printf("Please input your score: \n");
scanf("%f", &score);
fflush(stdin);
return score;
}
char level(float s1, float s2, float s3)
{
float avg;
printf("\n Score: %.1f %.1f %.1f\n", s1, s2, s3);
avg=(s1+s2+s3)/3;
printf("Avgrage: %4.1f\n", avg);
if (avg>=90)
{
return 'A';
}
else if (avg>=75)
{
return 'B';
}
else if (avg>=60)
{
return 'c';
}
else
{
return 'D';
}
}
#include<stdio.h>
#include<stdlib.h>
#define NUM 70
int guess_num(int,int,int);
int main(void)
{
int min=1, max=100, keyin=0, count=0;
while(keyin!=NUM)
{
count=count+ 1;
printf("guess a number: (%d - %d)\n",min ,max);
scanf("%d", &keyin);
guess_num(keyin, min, max);
printf("You have guessed %d times: \n",count);
}
printf("game over\n");
system("pause");
return 0;
}
int guess_num(int keyin, int min, int max)
{
if ((keyin >= min) && (keyin <= max))
{
if (keyin == NUM)
{
printf("Bingo! Guess it right, the answer is: %d \n",keyin);
// printf("you guessed %d times: \n",count);
}
else if (keyin > NUM)
{
//max = keyin;
printf("smaller\n");
}
else if (keyin < NUM)
{
//min = keyin;
printf("bigger\n");
}
}
else
{
printf("Please enter a number within the suggested range: (min - max)\n");
}
}
/* ch3 value2.c*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int scanf_value;
int i = 10;
int j = 20;
int k = 30;
printf("Input three decimal value :\n");
scanf_value = scanf("%d %d %d", &i, &j, &k);
printf("\nReturn value is %d.\n", scanf_value);
printf(" i = %d\n", i);
printf(" j = %d\n", j);
printf(" k = %d\n", k);
system("PAUSE");
return 0;
}
/* Ch7 ret1.c */
#include<stdio.h>
#include <stdlib.h>
int get_int(void);
int find_max(int,int);
int main()
{
int a,b;
int max;
a = get_int();
b = get_int();
max = find_max(a,b);
printf("\nMAX(%d,%d) is %d.\n",a,b,max);
system("PAUSE");
return 0;
}
int get_int(void)
{
int num;
printf("Input a valid integer : ");
while (scanf("%d",&num) != 1)
{
while (getchar () !='\n')
continue;
printf("Input error! Please input again : ");
}
return(num);
}
int find_max(int x, int y)
{
if (x > y)
return(x);
else
return(y);
}
#include <stdio.h>
#include <stdlib.h>
void showChar(char, int);
int main(void)
{
int i, n;
printf("Please input the layer: ");
scanf("%d", &n);
fflush(stdin);
for (i=0; i<n; i++)
{
showChar(32, n-i-1);
showChar(65+i, i*2+1);
printf("\n");
}
system("pause");
return 0;
}
void showChar(char ch, int n)
{
int i;
for (i=0; i<n; i++)
{
printf("%c", ch);
}
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char text[]="hello";
int i, length=sizeof(text)/sizeof(text[0]);
for (i=0; i<length; i++)
{
if (text[i]=='\0')
{
puts("\nEND");
}
else
{
printf("%c", text[i]);
}
}
printf("\n");
printf("length= %d\n", length);
system("pause");
return 0;
}
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(void)
{
char str1[]="xyz";
char str2[]="abcdef";
printf("str1=%s\n",str1);
strcat(str1, str2);
printf("str1=%s\n",str1);
system("pause");
return 0;
}
/* 程式範例: Ch9_5_3.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* 主程式 */
int main() {
char str[20] = "This is a pen."; /* 宣告字元陣列str */
char str1[20], str2[20]; /* 宣告字元陣列str1和str2 */
char str3[20] = "Hi! "; /* 宣告字元陣列str */
printf("str字串內容: \"%s\"\n", str);
printf("str字串的長度: %d\n", strlen(str));
strcpy(str1, "This is a book."); /* 複製到str1字串 */
strcpy(str2, str); /* 複製到str2字串 */
printf("str1字串內容: \"%s\"\n", str1);
printf("str2字串內容: \"%s\"\n", str2);
strcat(str3, str1); /* 連接字串 */
printf("str3字串內容: \"%s\"\n", str3);
/*
if ( strcmp(str1, str2) > 0 ) // 字串比較
printf("str1比較大...");
else
printf("str2比較大...");
*/
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
int a[100];
int b[100];
for (i=1; i<100; i++)
{
b[i]=i;
}
a[0]=0;
for (i=1; i<100; i++)
{
a[i]=b[i]+a[i-1];
}
printf("%d\n", a[60]-a[50]);
system("pause");
return 0;
}
#include<stdlib.h>
#include<stdio.h>
#define SIZE 4
void show(int []);
int main(void)
{
int CC[SIZE]={5, 6, 7, 8};
printf("The contents of the array: ");
show(CC);
system("pause");
return 0;
}
void show(int CC[])
{
int i;
for(i=0; i<SIZE; i++)
{
printf("%d ",CC[i]);
}
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
void func(void);
int main(void)
{
int a=100;
printf("Before call func(), a= %d\n", a);
func();
printf("After call func(), a= %d\n", a);
system("pause");
return 0;
}
void func(void)
{
int a=300;
printf("In func() function, a= %d\n", a);
}
#include <stdio.h>
#include <stdlib.h>
void func(void);
int a=50;
int main(void)
{
int a=100;
printf("Before call func(), a= %d\n", a);
func();
printf("After call func(), a= %d\n", a);
system("pause");
return 0;
}
void func(void)
{
a=a+300;
printf("In func() function, a= %d\n", a);
}
```
## :blue_book: Class05 (2020.01.15)
```c=
/* 函數是C語言的基本模組 可以簡化主程式的結構
prog8_1, 簡單的函數範例 節省撰寫相同的程式碼*/
#include <stdio.h> //1105
#include <stdlib.h>
void dot(void); /* dot()函數的原型*/
int main(void)
{
dot();
printf("Welcome to the C language\n");
dot();
system("pause");
return 0;
}
void dot(void)
{
printf(".........................\n");
}
/* prog8_2, 使用add()函數 */ //1118
#include <stdio.h>
#include <stdlib.h>
int add(int,int); /*add()函數的原型prototype 包含函數名稱 傳入的引數型態 傳回值型態
告知編譯器將使用這麼一個函數 函數的定義也須依宣告的格式撰寫
宣告於main()的外面 可被相同檔案內的其他函數呼叫 */
int main(void) {
int sum, a=5, b=3;
sum=add(a,b); /* 呼叫add()函數,並把傳回值設給sum */
printf("%d+%d=%d\n",a,b,sum);
system("pause");
return 0;
}
int add(int num1, int num2) /* add()函數的定義 */
{
int a; /* 於add()函數裡宣告變數a */
a=num1+num2;
return a; /* 傳回num1+num2 的值 */
}
/* prog8_4, display()的練習 */ //1132
#include <stdio.h>
#include <stdlib.h>
void display(char,int); /* display()函數的原型 */
int main(void) {
int n;
char ch;
printf("Please input a character:");
scanf("%c",&ch);
fflush(stdin);
printf("How many characters do you want to print:");
scanf("%d",&n);
fflush(stdin);
display(ch,n); /* 呼叫自訂的函數,印出n個ch字元 */
system("pause");
return 0;
}
void display(char ch,int n) /* 自訂的函數display() */
{
int i;
for(i=1;i<=n;i++) /* for迴圈,可印出n個ch字元 */
printf("%c",ch); /* 印出ch字元 */
printf("\n"); return 0;
}
/* prog8_5, 求絕對值函數abs() */ //1325
#include <stdio.h>
#include <stdlib.h>
int abso(int); /* 宣告函數abs()的原型 */
int main(void) {
int i;
printf("Please input an integer:"); /* 輸入整數 */
scanf("%d",&i);
fflush(stdin);
printf("abs(%d)=%d\n",i,abso(i)); /* 印出絕對值 */
system("pause"); return 0;
}
int abso(int i) /* 自訂的函數abs(),傳回絕對值 */
{
if (i<0)
return -i;
else
return i;
}
/* prog8_6, 計算 x的n次方 1340 */
#include <stdio.h>
#include <stdlib.h>
double power(double, int); /* 宣告函數power()的原型 */
int main(void) {
double x; /* x為底數 */
int n; /* n是次方 */
printf("Please input (base,exponent):");
scanf("%lf,%d",&x,&n); /* 要打逗點 輸入底數與次方 */
fflush(stdin);
printf("%.2lf to the power of %d =%.2lf\n",x,n,power(x,n));
system("pause");
return 0;
}
/* power()函數的定義 */
double power(double base, int n) /* power()函數的定義 */
{
int i;
double pow=1.0;
for(i=1;i<=n;i++) /* for() 迴圈,用來將底數連乘n次 */
pow=pow*base;
return pow;
}
/* 6 prog8_7, 質數的找尋 除了1和自己本身之外,沒有其他整數可以整除他的數
質數最小為2, 唯一的偶數*/ //1325
#include <stdio.h>
#include <stdlib.h>
int is_prime(int); /* 宣告函數is_prime()的原型 */
int main(void)
{
int i;
for(i=2;i<=30;i++) /* 找出小於30的所有質數 */
if(is_prime(i)) /* 呼叫is_prime()函數 */
printf("%3d",i); /* 如果是質數,便把此數印出來 */
printf("\n");
system("pause");
return 0;
}
int is_prime(int num) /* is_prime()函數,可測試num是否為質數 */
{
int i;
for(i=2;i<=num/2;i++)
if(num%i==0)
return 0;
return 1; //若是質數, 回應1
}
世界衛生組織計算標準體重之方法 :
男性: (身高cm-80)×70﹪=標準體重
女性: (身高cm-70)×60﹪=標準體重
/* prog7_9, 求理想體重 */ //1422
#include <stdio.h>
#include <stdlib.h>
float sweight(float,int); //原型定義,第一個引數為身高
int main(void)
{
int sex,height; // sex 是性別選項變數, height 是身高
float weight; // 計算結果的體重值
printf("Gender (0) Female (1) Male:");
scanf("%d",&sex); //這裡沒有檢查輸入值,預設輸入不是0就是1
fflush(stdin);
printf("Please enter height (cm): ");
scanf("%d",&height);
fflush(stdin);
weight = sweight(height,sex); //sweight(height,sex)
printf("Your ideal body weight is %.1f kg\n",weight);
system("pause"); return 0;
}
float sweight(float h,int s) // 理想體重的計算函式
{
float ret;
if (s == 0)
{
ret = (h - 70) * 0.6;
}
else
{
ret = (h - 80) * 0.7;
}
return ret;
}
/* prog8_8, 同時呼叫多個函數 */ //1408
#include <stdio.h>
#include <stdlib.h>
void sum(int), fac(int); /* 定義函數的原型 兩個傳回值型態相同 */
int main(void)
{
fac(4);
sum(4);
fac(6);
sum(6);
fac(7);
sum(7);
fac(9);
sum(9);
system("pause");
return 0;
}
void fac(int a) /* 自訂階乘函數fac(),計算a! factorial*/
{
int i,total=1;
for(i=1;i<=a;i++)
{
total=total*i;
}
printf("1x2x...x%d=%d\n",a,total);
}
void sum(int a) /* 自訂函數sum(),計算1+2+...+a的結果*/
{
int i,total=0;
for(i=1;i<=b;i++)
{
total=total+i;
}
printf("1+2+...+%d=%d\n",b,total);
}
#include<stdio.h>
#include<stdlib.h>
float get_score(void);
char level(float, float, float);
int main(void)
{
float s1, s2, s3;
char grade;
s1=get_score();
s2=get_score();
s3=get_score();
grade=level(s1, s2, s3);
printf("\n Your grade is %c \n",grade);
system("pause");
return 0;
}
float get_score(void)
{
float score;
printf("Please input your score; \n");
scanf("%f",&score);
fflush(stdin);
return score;
}
char level(float s1, float s2, float s3)
{
float avg;
printf("\n Score: %.1f %.1f %.1f\n",s1 ,s2 ,s3);
avg=(s1+s2+s3)/3;
printf(" Average: %4.1f\n",avg);
if(avg>=90)
{
return 'A';
}
else if(avg>=80)
{
return 'B';
}
else if(avg>=70)
{
return 'C';
}
else if(avg>=60)
{
return 'D';
}
else
{
return 'E';
}
}
#include<stdio.h> //1443
#include<stdlib.h>
#define HEIGHT 30
void showChar(char,int);
int main(void)
{
int i;
for(i=0;i<7;i++)
{
showChar(32,7-i-1);
showChar(65+i,i*2+1);
printf("\n");
}
system("pause");
return 0;
}
void showChar(char ch,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%c",ch);
}
}
/* ch9_15_1.c */
#include <stdio.h>
#include <stdlib.h>
int weather();
void comment_weather(int);
int main()
{
comment_weather(weather());
system("pause");
return 0;
}
int weather()
{
int temperature;
printf("Please enter the current temperature : ");
scanf("%d",&temperature);
return temperature;
}
void comment_weather(int t)
{
if (t >= 27)
printf("It's hot now\n");
else if (t > 15)
printf("This is a comfortable temperature\n");
else if (t > 5)
printf("it's a little cold\n");
else
printf("cold weather\n");
}
/* prog8_10, 遞迴函數,計算階乘 ==函數本身呼叫自己*/
#include <stdio.h>
#include <stdlib.h>
int fac(int); /* 遞迴函數fac()的原型 */
int main(void)
{
printf("fac(4)=%d\n", fac(4)); /* 呼叫遞迴函數fac() */
system("pause");
return 0;
}
/* 自訂階乘函數fac(),計算n! */
int fac(int n) /* 自訂函數fac(),計算n! */
{
if(n>0)
return (n*fac(n-1));
else
return 1;
}
```
## :blue_book: Class04 (2020.01.15)
```c=
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int main(void)
{
int score[MAX];
int i=0;
int num;
float sum=0.0f;
printf("Please input the score, input 0 to end:\n");
do
{
if(i==MAX)
{
printf("Array space has been used up!\n");
i++;
break;
}
printf("Please input the score:");
scanf("%d",&score[i]);
}while(score[i++]>0);
num=i-1;
for(i=0;i<num;i++)
{
sum=sum+score[i];
}
printf("average score %4.2f\n",sum/num);
system("pause"); return 0;
}
1130
/* prog9_8, 陣列的搜尋 */
#include <stdio.h>
#include <stdlib.h>
#define SIZE 6 /* 定義SIZE為6 */
int main(void)
{
int i,num,flag=0;
int A[SIZE]={33,75,69,41,33,19};
printf("The value of an array element:");
for(i=0;i<SIZE;i++)
printf("%d ",A[i]); /* 印出陣列的內容 */
printf("\nPlease input an integer: ");
scanf("%d",&num); /* 輸入欲搜尋的整數 */
for(i=0;i<SIZE;i++)
if(A[i]==num) /* 判斷陣列元素是否與輸入值相同 */
{
printf("found it! A[%d]=%d\n",i,A[i]);
flag=1; /* 設flag為1,代表有找到相同的數值 */
}
if(flag==0)
printf("Not found!!\n");
system("pause");
return 0;
}
1320
/* prog9_9, 二維陣列的輸入輸出 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i,j,sale[2][4],sum=0;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
{
printf("sales%d %d quarter results:",i+1,j+1);
scanf("%d",&sale[i][j]); /* 輸入銷售量 */
}
printf("***Output***");
for(i=0;i<2;i++) /* 輸出銷售量並計算總銷售量 */
{
printf("\nsales%d quarter results:",i+1);
for(j=0;j<4;j++)
{
printf("%d ",sale[i][j]);
sum+=sale[i][j];
}
}
printf("\n2018 results for the total sales volume of %d cars \n",sum);
system("pause");
return 0;
}
1350
#include<stdio.h>
#include<stdlib.h>
//#define ROW 5
//#define COLUMN 10
int main(void) {
int i,j;
int maze[5][10]; //maze[ROW][COLUMN];
for(i = 0; i <5; i++)
{
for(j = 0; j < 10; j++)
{
maze[i][j] = (i+1) * (j+1);
}
}
for(i = 0; i < 5; i++)
{
for(j = 0; j < 10; j++)
{
printf("%5d", maze[i][j]);
}
printf("\n");
}
system("pause");
return 0;
}
/* 程式範例: Ch9_3_2.c , 2層巢狀迴圈存取2維陣列 */
#include <stdio.h>
#include <stdlib.h>
#define LEN 9
int main(void) { /* 主程式 */
int i, j; /* 宣告變數 */
int tables[LEN][LEN]; /*宣告int型態的二維陣列tables[][] */
for ( i=0; i < LEN; i++)
{ /* 指定二維陣列的元素值 */
for ( j=0; j < LEN; j++)
{
tables[i][j] = (i+1)*(j+1);
}
}
for ( i=0; i < LEN; i++)
{ /* 顯示二維陣列的元素值 */
for ( j=0; j < LEN; j++)
{
printf("%d*%d=%2d ",(i+1),(j+1),tables[i][j]);
}
printf("\n");
}
system("pause"); return 0;
}
/* prog9_11, 三維陣列與初值的設定 */
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
int A[2][4][3]={{{21,32,65},
{78,95,76},
{79,44,65},
{89,54,73}},
{{32,56,89},
{43,23,32},
{32,56,78},
{94,78,45}}};
int i,j,k,max=A[0][0][0];
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<3;k++)
{
if(max<A[i][j][k])
{
max=A[i][j][k];
}
}
}
}
printf("max=%d\n",max);
system("pause");
return 0;
}
/* prog9_10, 矩陣相加 的法則是在相同位置的元素做加法計算*/ 0845
#include <stdio.h>
#include <stdlib.h>
#define ROW 2 /* 定義ROW為2 */
#define COL 3 /* 定義COL為3 */
int main(void) {
int i,j;
int A[ROW][COL]={{1,2,3},{5,6,8}}; /* 宣告陣列A並設定初值 */
int B[ROW][COL]={{3,0,2},{3,5,7}}; /* 宣告陣列B並設定初值 */
printf("Matrix A+B=\n");
for(i=0;i<ROW;i++) /* 外層迴圈,用來控制列數 */
{
for(j=0;j<COL;j++) /* 內層迴圈,用來控制行數 */
printf("%3d",A[i][j]+B[i][j]); /* 計算二陣列相加 */
printf("\n");
}
system("pause"); return 0;
}
/* 6 程式範例: Ch9_5_2.c */ //0905
#include <stdio.h>
#include <stdlib.h>
int main(void){
char str1[15]; /* 字元陣列宣告 */
char str2[15] = {'H','e','l','l','o','!',' ',
'w','o','r','l','d','\n','\0'};
char str[15] = "Hello! world\n";
/* 指定字元陣列初值 */
str1[0] = 'H'; str1[1] = 'e'; str1[2] = 'l';
str1[3] = 'l'; str1[4] = 'o'; str1[5] = '!';
str1[6] = ' '; str1[7] = 'w'; str1[8] = 'o';
str1[9] = 'r'; str1[10] = 'l'; str1[11] = 'd';
str1[12] = '\n'; str1[13] = '\0';
printf("str = %s", str); /* 顯示字串內容 */
printf("str1 = %s", str1);
printf("str2 = %s", str2);
system("pause");
return 0;
}
/* prog9_20, 印出字元及字串的長度 */ //0924
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch='c'; /* 宣告字元變數ch */
char str1[]="c"; /* 宣告字串變數str1 */
char str2[]="We are family"; /* 宣告字串變數str2 */
printf("ch:%4dbyte\n",sizeof(ch));
printf("str1:%2dbyte\n",sizeof(str1));
printf("str2:%2dbyte\n",sizeof(str2));
system("pause");
return 0;
}
/* ch3 scanfs.c 0936 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num1, num2;
double po;
char str[20];
printf("Input two number :\n");
scanf("%d %d", &num1, &num2);
printf(" ===> %d + %d = %d\n\n", num1, num2, num1+num2);
printf("Input a floating point :\n");
scanf("%lf", &po);
printf(" ===> %f is %e\n\n", po, po);
printf("Input a string :\n");
scanf("%s", str);
printf(" ===> %s \n\n", str);
printf("Input at most 10 chars :\n");
scanf("%10s", str);
printf(" ===> %s\n", str);
system("PAUSE");
return 0;
}
/* prog9_21, 輸入及印出字串 0945 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char name[15]; /* 宣告字元陣列name */
puts("What's your name?");
gets(name); /* 利用gets()讀入字串,並寫入字元陣列name裡 */
puts("Hi!");
puts(name); /* 印出字元陣列name的內容 */
puts("How are you?");
system("pause");
return 0;
}
//1025
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i=0;
char str[50];
printf("Please input a string:");
gets(str);
while(str[i]!='\0')
{
if(str[i]>=65&&str[i]<=90)
{
str[i]+=32;
}
i++;
}
printf("Uppercase converted to lowercase:%s\n",str);
system("pause");
return 0;
}
/* ch3 cut.c */
#include <stdio.h> //1045
#include <stdlib.h>
int main()
{
int num = 321;
printf("Decimal : %d\n", num); //1 1 1
printf("Character : %c\n", num); //256 128 64 32 16 8 4 2 1
system("PAUSE");
return 0;
}
/* prog9_23,字串陣列 */
//1055
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char S[3][10]= {"Tom","Lily","Bruce Lee"};
int i;
for(i=0;i<3;i++)
printf("S[%d]=%s\n",i,S[i]); /* 印出字串陣列內容 */
printf("\n");
for(i=0;i<3;i++) // 印出字串陣列元素的位址
{
printf("S[%d]=%p\n",i,S[i]);
printf("address of S[%d][1]=%c %p\n\n",i,S[i][1],&S[i][1]);
}
system("pause");
return 0;
}
```
## :blue_book: Class03 (2020.01.14)
```c=
/* prog7_3, while迴圈的使用 */ //1425
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char guess;
while(guess != 'e')
{
printf("Press a to z:");
scanf("%c",&guess);
fflush(stdin);
if(guess != 'e')
{
printf("You guess wrong, guess again!\n");
}
}
printf("Congratulations! You got it!\n");
system("pause"); return 0;
}
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
int n,i=1,sum=0;
do
{
printf("Please input the value of n(n>0):");
scanf("%d",&n);
fflush(stdin);
}while(n<=0);
do
{
sum+=i;
i++;
}while(i<=n);
printf("1+2+...+%d=%d\n",n,sum);
system("pause");
return 0;
}
/* prog7_12, do while迴圈,將整數反過來列印 */
int main(void)
{
int a,r;
while(1) //Ctrl+C
{
do
{
printf("Input an integer:");
scanf("%d",&a);
fflush(stdin);
} while (a<=0); /* 必須輸入大於0的正整數 */
printf("The reverse is ");
while (a!=0)
{ /* 第二內層將正整數倒過來輸出13579 */
r=a%10; /* 計算a/10的餘數 13579%10=9*/
a/=10; /* 計算a/10,再把結果1357設回給a */
printf("%d",r);
}
printf("\n\n");
}
system("pause"); return 0;
}
/* prog9_200, 密碼讀取流程 */
#include <stdio.h>
#include <stdlib.h>
#define PASSWD 1234
int main(void) {
int passwd;
int flag=0, retry=1;
do {
printf("%d. Enter your password:\n",retry++);
scanf("%d",&passwd);
fflush(stdin);
if(passwd == PASSWD)
flag=1;
}while(!flag && (retry<=3)); //!flag為0, 密碼驗證結束, 離開do…while
if(flag)
printf("Congratulatons!\n");
else
printf("You are rejected!\n");
system("pause"); return 0;
}
/* prog7_13, break敘述的使用 */
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i;
for(i=1;i<=10;i++)
{
if(i%3==0) /* 判斷i%3是否為0 */
break; /* 跳離迴圈 */
printf("i=%d\n",i); /* 印出i的值 */
}
printf("跳離迴圈時, i=%d\n",i);
system("pause");
return 0;
}
/* 16 prog7_14, continue敘述的使用 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
for(i=1;i<=10;i++)
{
if(i%3==0) /* 判斷i%3是否為0 */
continue; /* 回到迴圈的起始處繼續執行 */
printf("i=%d\n",i); /* 印出i的值 */
}
printf("跳離迴圈時, i=%d\n",i);
system("pause");
return 0;
}
#include<stdio.h> //0830
#include<stdlib.h>
#define SQUARE n*n
int main (void)
{
int n;
printf("Please input an integer:");
scanf("%d",&n);
printf("%d*%d=%d\n",n,n,SQUARE); //代換標記
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define ABS(x) x>0?x:-x
int main (void)
{
int i,j=-3;
float x,y=-1.35f;
i=ABS(j);
printf("ABS(%d)=%d\n",j,i);
x=ABS(y);
printf("ABS(%4.2f)=%4.2f\n",y,x);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define SQUARE(x) (x)*(x)
int main (void)
{
int n;
printf("Please input an integer:");
scanf("%d",&n);
printf("%dx%d=%d\n",n+1,n+1,SQUARE(n+1));
system("pause"); return 0;
}
/* prog7_141, continue敘述的使用 */
#include <stdio.h>
#include <stdlib.h>
int main(void )
{
int i;
for (i = 0; i < 10; i++)
{
if (i % 2!=0)
continue; // start next iteration
else if (i == 8)
break; // end loop
printf("%d\n", i); // 0246
}
system("pause");
return 0;
}
/* prog9_1, 一維陣列的基本操作 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i,score[4]; /* 宣告整數變數i與整數陣列score */
score[0]=78; /* 設定陣列的第一個元素為78 */
score[1]=55; /* 設定陣列的第二個元素為55 */
score[2]=92; /* 設定陣列的第三個元素為92 */
score[3]=80; /* 設定陣列的最後一個元素為80 */
for(i=0;i<=3;i++)
printf("score[%d]=%d\n",i,score[i]); /* 印出陣列的內容 */
system("pause"); return 0;
}
/* prog9_2, 一維陣列的基本操作(錯誤的示範) */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i,score[4]; /* 宣告整數變數i與整數陣列score */
score[0]=78;
score[1]=55;
/* score[2]=92; 此行刻意不將score[2]設值 */
score[3]=80;
for(i=0;i<=3;i++)
printf("score[%d]=%d\n",i,score[i]);
system("pause"); return 0;
}
/* 11 prog9_3, 查詢陣列所佔的記憶空間 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double data[4]; /* 宣告有4個元素的double型態陣列 */
printf("Array element :%d\n",sizeof(data[0])); // Array element所佔的位元組
printf("Array :%d\n",sizeof(data)); // 整個Array所佔的位元組
//printf("The number of array elements:%d\n",sizeof(data)/sizeof(double));
system("pause");
return 0;
}
/* prog9_5, 比較陣列元素值的大小 */
int main(void) {
int array[5]={7,48,30,17,62};
int i, min, max;
//min=max=A[0]; /* 將max與min均設為陣列的第一個元素 */
min=array[0];
max=array[0];
for(i=0;i<5;i++)
{
if(array[i]>max) /* 判斷A[i]是否大於max */
max=array[i];
if(array[i]<min) /* 判斷Aarrayi]是否小於min */
min=array[i];
}
printf("The maximum value of the array element :%d\n",max);
printf("The minimum value of the array element :%d\n",min);
system("pause"); return 0;
}
#include<stdlib.h>
#include<stdio.h>
#define SIZE 6
int main(void)
{
int i, num, flag=0;
int array[SIZE]={33, 75, 69, 41, 33, 19};
printf("The value of an array element: \n");
for(i=0; i<SIZE; i++)
{
printf("%d ",array[i]);
}
printf("\nPlease input an integer: ");
scanf("%d",&num);
for(i=0; i<SIZE; i++)
{
if(array[i]==num)
{
printf("Found it! array[%d]=%d\n",i ,array[i]);
flag=1;
}
}
if(flag==0)
{
printf("Not found!\n");
}
system("pause");
return 0;
}
/* prog9_7, 陣列的界限檢查 */
#define MAX 5 /* 定義MAX為5 */
int main(void) {
int score[MAX]; /* 宣告score陣列,可存放MAX個整數 */
int i=0,num;
float sum=0.0f;
printf("Please input the score, input 0 to end:\n");
do
{
if(i==MAX) /* 當i的值為MAX時,表示陣列已滿,即停止輸入 */
{
printf("Array space has been used up!!\n");
i++; /* 此行先將i值加1,因為23行會把i的值減1掉 */
break;
}
printf("Please input the score:");
scanf("%d",&score[i]);
}while(score[i++]>0); /* 輸入成績,輸入0時結束 */
num=i-1;
for(i=0;i<num;i++)
sum+=score[i]; /* 計算平均成績 */
printf("average score %.2f\n",sum);
system("pause"); return 0;
}
```
## :blue_book: Class02 (2020.01.14)
```c=
/* prog4_10, 使用scanf()函數,一次輸入兩個整數 11*/ 1345
#include <stdio.h>
#include <stdlib.h>
int main(void){
int ia,ib;
printf("Please input two integers: ");
scanf("%d %d",&ia,&ib); /* 由鍵盤輸入二個數並設定給變數a、b */
printf("%d+%d=%d\n",ia,ib,ia+ib); /* 計算總和並印出內容 */
system("pause"); return 0;
}
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
char size='M';
int cups=8;
float discount=0.85f;
printf("%c size cup of 35 dollars\n",size);
printf("Buy %d cups, total %d dollars\n",cups,45*cups);
printf("Buy %d cups and get a discount %4.2f\n",cups, discount);
system("pause");
return 0;
}
#include<stdio.h> //0830
#include<stdlib.h>
int main(void)
{
printf("12%%4=%d\n",12%4);
printf("12%%5=%d\n",12%5);
printf("12%%16=%d\n",12%16);
system("pause");
return 0;
}
int main(void)
{
int num=5;
printf("num/2=%.2f\n",num/2);
printf("num/2=%.2f\n",(float)num/2);
printf("num/2=%.2f\n",num/2.0);
system("pause");
return 0;
}
#include<stdio.h> //0840
#include<stdlib.h>
int main(void)
{
int x=65;
char A='A',B='B';
float fx=65.0F;
printf("%d==%c=%d\n",x,A,x==A);
printf("%d>%c=%d\n",x,B,x>B);
printf("%c>%c=%d\n",B,A,B>A);
printf("%.1f==%c=%d\n",fx,A,fx==A);
printf("%.1f>%c=%d\n",fx,B,fx>B);
system("pause"); return 0;
}
/* prog5_4, 關係運算子的練習 16*/ 1429
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
int ia=5;
int ib=7;
int ic=8;
if(ia>2)
{
printf("yes 5>2\n");
}
if(ib>9)
{
printf("no 7>9\n");
}
if(ic==8)
{
printf("yes %d=8\n",ia);
}
system("pause");
return 0;
}
/* prog5_5, 遞增運算子「++」 */ //0855
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a=3, b=3;
printf("a=%d,\n",a);
printf("a++ return value %d",a++); /* 計算a++,並印出其傳回值 */
printf(", a=%d\n",a);
printf("b=%d,\n",b);
printf("++b return value %d",++b); /* 計算++b,並印出其傳回值 */
printf(", b=%d\n",b);
system("pause"); return 0;
}
/*prog5_7, 簡潔運算式 */ //0905
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a=3,b=5,c=6,d=7;
printf("before calculation: a=%d, b=%d\n",a,b);
a+=b; /* 計算a+=b, 即a=a+b */
printf("calculation: a=%d, b=%d\n",a,b);
printf("before calculation: c=%d, d=%d\n",c,d);
c*=d; /* 計算a+=b, 即a=a+b */
printf("calculation: c=%d, d=%d\n",c,d);
system("pause"); return 0;
}
/* prog6_3_3, 條件運算子的練習 */ //1120
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int hour;
printf("Please input the 24-hour system => ");
scanf("%d", &hour);
hour = (hour >= 12) ? hour -12 : hour;
printf("Present time: %d\n", hour);
system("pause");
return 0;
}
/* prog6_9, 條件運算子的練習 */ //0933
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num1,num2,larger;
printf("Please input two integers:");
scanf("%d %d",&num1,&num2);
num1>num2 ? (larger=num1) : (larger=num2); /* 條件運算子 */
printf("%d greater value\n",larger);
system("pause"); return 0;
}
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
int x=6;
if(x>0)
{
printf("Bruce\n");
}
else
{
printf("Jenny\n");
}
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int num;
printf("Please input an integer:");
scanf("%d",&num);
if(num>=0)
{
if(num<=20)
{
printf("number between 0-20\n");
}
}
else
{
printf("number less than 0\n");
}
system("pause");
return 0;
}
/* prog6_5, 巢狀if敘述的練習 */ //0945
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int score;
printf("Please input a score:");
scanf("%d",&score);
if(score<60)
{
if(score>=50)
{
printf("make up exams!\n");
}
else
{
printf("Fail!\n");
}
}
else
{
printf("Pass\n");
}
system("pause");
return 0;
}
/* prog6_7, if-else配對問題(一) */ //1023
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num;
printf("Please input an integer:");
scanf("%d",&num);
if(num>=0)
if(num<=20)
printf("number between 0-20\n");
else
printf("number less than 0\n");
system("pause");
return 0;
}
/* prog6_8, if-else配對問題(二) */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num;
printf("Please input an integer:");
scanf("%d",&num);
if (num>=0)
{
if(num<=20)
printf("number between 0-20\n");
}
else /* 如果第10行的if敘述不成立 */
{
printf("number less than 0\n");
}
system("pause");
return 0;
}
/* prog5_8, 運算式的型態轉換 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch='c';
short int s=-2;
int i=5;
double d=6.28;
//float f=5.3f;
//int sum;
/*
printf("(ch/s)=%d\n",(ch/s));
printf("(d/s)=%f\n",(d/s));
printf("(s+i)=%d\n",(s+i));
*/
printf("(ch/s)-(d/s)-(s+i)=%f\n",(ch/s)-(d/s)-(s+i));
//printf("size=%d\n",sizeof((ch/s)-(d/s)-(s+i)));
system("pause");
return 0;
}
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
int chinese, english, bonus;
printf("Please input the score of chinese: ");
scanf("%d",&chinese);
printf("Please input the score of English: ");
scanf("%d",&english);
if (chinese == 100)
{
if (english == 100)
bonus = 1000;
else
bonus = 500;
}
else
{
if (english == 100)
bonus = 500;
else
bonus = 0;
}
printf("bouns: %d\n",bonus);
system("pause");
return 0;
}
/* prog6_6, if-else-if敘述的應用 */ //1046
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int score;
printf("Your score:");
scanf("%d",&score);
if (score>=80)
printf("%d is A\n",score); /* 印出A */
else if (score>=70)
printf("%d is B\n",score); /* 印出B */
else if (score>=60)
printf("%d is C\n",score); /* 印出C */
else
printf("Failed!!\n"); /* 印出字串"Failed!!" */
system("pause");
return 0;
}
/* prog6_10, switch敘述的使用範例 */ //1105
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
float a,b;
char oper;
printf("Please input the expression:(ex:3+2): "); /* 輸入運算式 */
scanf("%f %c %f",&a,&oper,&b);
switch(oper)
{
case '+':
printf("%.2f+%.2f=%.2f\n",a,b,a+b); /* 印出a+b */
break;
case '-':
printf("%.2f-%.2f=%.2f\n",a,b,a-b); /* 印出a-b */
break;
case '*':
printf("%.2f*%.2f=%.2f\n",a,b,a*b); /* 印出a*b */
break;
case '/':
printf("%.2f/%.2f=%.2f\n",a,b,a/b); /* 印出a%b */
break;
default:
printf("input error!!\n"); /* 印出字串 */
}
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a,b;
char oper;
printf("Please input the expression:(ex:3+2): "); /* 輸入運算式 */
scanf("%d %c %d",&a ,&oper ,&b);
switch(oper)
{
case '+':
printf("%2d+%2d=%3d\n",a,b,a+b); /* 印出a+b */
break;
case '-':
printf("%2d-%2d=%3d\n",a,b,a-b); /* 印出a-b */
break;
case '*':
printf("2d*2d=3d\n",a,b,a*b); /* 印出a*b */
break;
case '/':
printf("%2d/%2d=%3d\n",a,b,a/b); /* 印出a/b */
break;
case '%':
printf("%2d %% %2d =%3d\n",a,b,a%b); /* 印出a%b */
break;
default:
printf("input error!!\n"); /* 印出字串 */
}
system("pause");
return 0;
}
/* prog6_11, switch敘述-以不同的選擇值來處理相同的敘述 */ //1120
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char grade;
printf("Please input a grade: (ex: A or a):");
scanf("%c",&grade);
switch(grade)
{
case 'a': /* 輸入a或A時印出Excellent! */
case 'A':
printf("Excellent!\n");
break;
case 'b': /* 輸入b或B時印出Good! */
case 'B':
printf("Good!\n");
break;
case 'c': /* 輸入c或C時印出Be study hard! */
case 'C':
printf("Be study hard!\n");
break;
default: /* 輸入其他字元時印出Failed! */
printf("Failed!\n");
}
system("pause");
return 0;
}
for 變數=初值 to 終值 <==涵義就相當於 FOR迴圈條件在於幾圈幾圈
while 條件 <==涵義就相當於讓某個事件產生
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i=1, sum=0;
while(i<=10)
{
printf("Hay!!\n");
i++;
}
system("pause");
return 0;
}
/* prog7_3, while迴圈的使用 1要累加到多少時, 才會超過10*/ //1137
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i=1,sum=0; /* 設定迴圈控制變數的值初值 */
while(i<=10) /* while迴圈,當sum小於10則繼續累加 */
{
sum+=i;
i++; /* 重新設定迴圈控制變數的值 */
}
printf("Accumulate %2d\n",sum);
//printf("It must be added to the %d\n",i-1);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int deposit=0, num=0, money;
while(deposit<30000)
{
num=num+1;
printf("Please enter deposit amount:\n ");
scanf("%d",&money);
printf("number of deposits: %d\n",num);
deposit=deposit+money;
}
printf("accumulate %d\n",deposit);
system("pause");
return 0;
}
/* prog7_10, 巢狀while迴圈求9*9乘法表 */ //1325
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i=1, j=1; /* 設定迴圈控制變數的初值 */
while (i<=9) /* 外層迴圈 */
{
while (j<=9) /* 內層迴圈 */
{
printf("%d*%d=%2d ",i,j,i*j);
j++;
}
printf("\n");
i++;
j=1;
}
system("pause");
return 0;
}
/* prog7_11, 利用巢狀迴圈印出三角形 */ 11 //1339
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i,j,n=6; /* 設定迴圈初值 */
for (i=1;i<=n;i++) /* 外層迴圈決定哪一列要印星號 */
{
for (j=1;j<=i;j++) /* 內層迴圈印出*星號 */
printf("*");
printf("\n");
}
system("pause");
return 0;
}
/* prog7_111, 巢狀迴圈的應用 */ //1353
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char row, column;
for(row='A';row<='G';row++)
{
for(column=row;column<='G';column++)
printf("%2c ",column);
printf("\n");
}
system("pause"); return 0;
}
/* 11 prog4_18, 讀取到錯誤的字元 */ //1412
int main(void) {
int num;
char ch;
printf("Please input an integer: ");
scanf("%d",&num); /* 由鍵盤輸入整數,並指定給變數num */
//fflush(stdin); //可用來清除緩衝區的資料, stdin 標準輸入設備(鍵盤)
printf("Please input a character: ");
scanf("%c",&ch); /* 由鍵盤輸入字元,並指定給變數ch */
//fflush(stdin); //可用來清除緩衝區的資料, stdin 標準輸入設備(鍵盤)
printf("num=%d, ascii of ch=%d\n",num,ch); /* 印出num與ch的ascii碼 */
system("pause"); return 0;
}
```
## :blue_book: Class01 (2020.01.09)
- 單純的「複製 / 貼上」對程式設計的學習並沒有太大的幫助
- 重點部分的程式碼一定要自己輸入
否則對程式語法的記憶與程式撰寫能力的提升是有限的
- 觀念一:簡單的英文單字但卻抽象
C 語言的指令都是簡單的英文單字(如:while、for)
初學還是會覺得那些語法與規定很抽象
這樣的反應是正常的
- 觀念二:語法的規定就是聖旨
程式語法的規定除了遵守還是遵守
沒有可以自由創造的空間
不能加分號,就是不要加!
必須在宣告時給初始值,就是一定要給!
記熟並且依照規定去使用就對了
- 標頭檔的載入
#include <stdio.h>
載入 stdio.h (標準輸入與輸出函式庫定義檔)
因為使用了 printf 與 scanf
#include <stdlib.h>
載入 stdlib.h (標準函式庫定義檔)
因為使用了 system
- 內建函式與標頭檔的載入
必須先載入才能使用
內建函式的定義檔則是以 .h 為副檔名
載入使用 #include 指令,定義檔的名稱則必須寫在 <> 裡面
語法規範的要求
printf 與 scanf 就是 C 語言所提供的基本函式,又稱為內建函式
- 回傳值與函式引數
int main(void)
int 代表這個函式的回傳值
( ) 中的 void 代表不需要引數
回傳值:return 0 就是回傳 0
0 表示程式執行沒有問題
舉例來說
你請別人(函式)幫你做事情,你必須提供一些執行所需的東西(這些傳過去的東西就是引數) 當然也可以不給(void 就是表示不用給)
當然你會希望他在做完之後跟你回報(這個回報就是回傳值)
- 注意
main 的回傳值是 int 這是 ANSI C 的規範
必須使用 return 回傳一個值,0 表示程式執行沒有問題
可改成 void main(void),不需要回傳值(省略 return 0 )
但這並非 ANSI C 的標準
- system("pause")
讓「命令提示字元」處於暫停的狀態,視窗看到「請按任意鍵繼續 . . .」的字眼 ─ 才能看到程式執行的結果
按下鍵盤上任何一個按鍵,就能終止這個暫停的動作
- 字串
凡是以 “ 與 ” 所包含文字集合體,C 語言稱之為字串(string)
- 註解
介於 /* 與 */ 之間的都是註解,就算跨行也算
請養成寫註解的習慣!!!
掌握註解的幾個原則:
有特定用途的變數
在宣告時,就在它的後面用註解說明這個變數的使用目的
以程式區塊為單位
在程式區塊之前用簡單的幾行說明這個程式區塊所能達成的功能
在函式之前
利用註解說明這個函式功能以及每一個引數的作用與回傳值的意義 (第七章會介紹函式)
不斷的練習,寫、寫、寫
程式一定要寫才能熟練,除了背別人寫的程式還是自己多寫
學習他人的技巧
多觀摩他人寫的「好程式」是提升程式功力的好方法
背下解題技巧,未來遇到相關的問題時,就能很快的得到可參考的依據
清晰的數理邏輯
建立清晰的數理邏輯 ─ 多練習就能建立
將解決問題的策略寫下來,常看多思考,就能逐步的建立
熟練的電腦操作
快去摸熟你所要使用的程式開發環境
```c=
#include <stdlib.h> //載入標準函式庫, 載入使用 #include 指令, 定義檔的名稱則必須寫在 <>裡面
int main(void)
{
system("pause");
return 0;
}
/* ##13 prog1_1, 第一個C程式碼 1*/
#include <stdio.h> // 載入標準輸入輸出函式庫
#include <stdlib.h> //必須先載入才能使用, 定義檔則是以 .h 為副檔名( header)
int main(void) //程式進入點,程式就是從這邊開始執行,這是規定,一定要用 main()
{ //每個C程式必須定義一個屬於他自己的函式, 函式名稱為main()
printf("Hello C!\n"); /* 只要將要輸出的文字寫在 " 與 " 之間就行了, 印出Hello C! 字串 */
printf("Hello World!\n"); /* 印出Hello World! 字串 */
system("pause"); //這裡都必須有分號
return 0;
}
X /* prog2_4, 有錯誤的程式 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int num; /* 宣告了一個名叫做 i 的變數,它只能儲存整數(integer),並設值為2 */
num=2; /* 將num設值為2 */
printf("I have %d dogs. \n",num); //印出字串及變數內容
printf("You have %d dogs, too. \n",num); /* 印出字串及變數內容 */
system("pause");
return 0;
}
/*##18 prog3_1,變數的使用, C語言有大小寫之分int num; int Num; int NUM;代表三個變數喔 2*/
#include <stdio.h> //內容可隨時改變的數 0920
#include <stdlib.h>
int main(void) //等號右邊的運算結果儲存到等號左邊的變數中 右邊可以是運算式、數值或是變數
{ //等號的左邊只能是變數,而且只能有一個,不能是運算式
int num1=12400; /* 宣告num1為整數變數,它只能儲存整數(integer),並設值為12400 */
double num2=5.234; /* 宣告num2為倍精度浮點數變數,並設值為5.234 */
printf("%d is an integer\n",num1); /* 呼叫printf()函數 */
printf("%f is a double\n",num2); /* 呼叫printf()函數 */
system("pause");
return 0;
/* ##28 prog3_2, 短整數資料型態的溢位3 */ 0948
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
short sum,s=32767; /* 宣告短整數變數sum與s */
sum=s+1;
printf("s+1= %d\n",sum); /* 列印出sum的值 */
sum=s+2;
printf("s+2= %d\n",sum); /* 列印出sum的值 */
system("pause");
return 0;
}
/* ##31 prog3_3, 字元的列印4*/ 1025
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch='a'; /* 宣告字元變數ch,並設值為'a' */
printf("ch= %c\n",ch); /* 印出ch的值 */
printf("ASCII of ch= %d\n",ch); /* 印出ch的十進位值 */
printf("ASCII of ch= %x\n",ch); // ASCII用來制定電腦中符號對應的整數代碼 A 65 Z 90
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char ch='a';
printf("ch=%c\n",ch);
printf("ch=%d\n",ch);
system("pause");
return 0;
}
/* prog3_4, 利用ASCII碼來設定字元變數 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch=90; /* 將整數90設給字元變數ch */
printf("ch=%c\n",ch); /* 印出ch的值 */
system("pause");
return 0;
}
/* prog3_5, 數字字元與其相對應的ASCII碼 5*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch='2'; /* 宣告字元變數ch,並設值為'2' */
printf("ch=%c\n",ch); /* 印出字元變數ch */
printf("the ASCII of ch is %d\n",ch); /* 印出ch的ASCII碼 */
system("pause");
return 0;
}
/* prog3_8, 跳脫序列「\"」的列印6*/ 1045
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char ch='\"'; /* 宣告字元變數ch,並設值為'\"' */
printf("%cWe are the World%c\n",ch,ch); /* 印出字串 */
system("pause");
return 0;
}
/* ##34 prog3_10, float與double精度的比較 7*/
#include <stdio.h>
#include <stdlib.h>
int main(void) { //float 的運算速度較快
float num1=12345.6789012345F; /* 宣告num1為float,並設定初值 */
double num2=12345.6789012345; /* 宣告num2為double,並設定初值 */
printf("num1=%16.10f\n",num1); /* 列印出浮點數num1的值 */
printf("num2=%16.10f\n",num2); /* 列印出倍精度浮點數num2的值 */
system("pause");
return 0;
}
/* prog4_4, 印出特定格式 8 ##35*/ 1115
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int inum1=32, inum2=1024;
float fnum3=12.3478f;
printf("num1=%6d km\n",inum1); /* 以「%6d」格式印出num1 */
printf("num2=%-6d km\n",inum2); /* 以「%-6d」格式印出num2 */
printf("num3=%6.2f mile\n",fnum3); /* 以「%6.2f」格式印出num3 */
system("pause");
return 0;
}
/*##, 格式碼需與變數相對應 */ 1125
int main(void)
{
int x1,x2,x3,x4;
x1=4+2;
x2=4-2;
x3=4*2;
x4=4/2;
printf("x1=%d,\nx2=%d,\nx3=%d,\nx4=%d\n",x1,x2,x3,x4);
system("pause");
return 0;
}
/* prog3_12, 資料型態的轉換 */ 1135
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n1,n2;
float num1=3.03f,num2=3.98f;
n1=(int) num1; /* 將浮點數num1轉換成整數 */
n2=(int) num2; /* 將浮點數num2轉換成整數 */
printf("num1=%f, num2=%f\n",num1,num2); /* 印出浮點數的值 */
printf("n1=%d, n2=%d\n",n1,n2); /* 印出浮點數轉成整數後的值 */
system("pause");
return 0;
}
/*##36 prog4_9, 使用scanf()函數 9 */ 1142
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int inum;
printf("Please input an integer:"); //& 是一定要的喔, & 前的逗號也不能省喔!
scanf("%d",&inum); /* 由鍵盤輸入整數,並指定給num存放 */
printf("num=%d\n",inum); /* 印出num的內容 */
system("pause");
return 0;
}
/* prog1_7, 10*/ 1320
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int itea, icoffee, itotal;
printf("How many bottles of black tea do you want to buy:");
scanf("%d",&itea);
printf("How many bottles of coffee do you want to buy:");
scanf("%d",&icoffee);
itotal = itea*25 + icoffee * 35;
printf("Price:%d \n",itotal);
system("pause");
return(0);
}
#include <stdio.h> 1331
#include<stdio.h>
int main(void)
{
//char size='M';
int cups=8;
float discount=0.85f;
//printf("%d size cup of 35 dollars\n",size);// 給錯輸出格式
printf("Buy %d cups,total %d dollars\n",cups); //漏打了一個引數
printf("Buy %d cups and get a discount %4.2f \n",discount,cups+2); //引數不小心放錯順序
/*
printf("%c size cup of 35 dollars\n",cSize);// 給錯輸出格式
printf("Buy %d cups,total %d dollars\n",iCups,35*iCups); //漏打了一個引數
printf("Buy %d cups and get a discount %4.2f \n",iCups+2,fDiscount); //引數不小心放錯順序
*/
system("pause");
return 0;
}
}/* ##37 prog6_1, 選擇性結構if敘述 12 */ 1352
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
int num;
printf("Please input an integer:");
scanf("%d",&num);
if(num>0)
{
printf("You enter an integer greater than 0\n");
}
if(num<=0)
{
printf("You enter an integer less than or equal to 0\n");
}
printf("end");
system("pause");
return 0;
}
#include <stdio.h> 1402
#include <stdlib.h>
int main(void) {
int ix1 = (1 && 0); /* 0 - logical and */
int ix2 = (1 || 0); /* 1 - logical or */
printf("ix1=%d,i x2=%d \n", x1, x2);
system("pause");
return 0;
}
/* ##44 prog5_40, 關係運算子的練習 15*/
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int ix1 = (2 == 3); /* 0 - equal to */
int ix2 = (2 != 3); /* 1 - not equal to */
int ix3 = (2 > 3); /* 0 - greater than */
int ix4 = (2 < 3); /* 1 - less than */
int ix5 = (2 >= 3); /* 0 - greater than or equal to */
int ix6 = (2 <= 3); /* 1 - less than or equal to */
printf("ix1=%d, ix2=%d, ix3=%d, ix4=%d, ix5=%d, ix6=%d\n", ix1, ix2, ix3, ix4, ix5, ix6);
system("pause");
return 0;
}
/* ##45 prog5_6, 邏輯運算子的應用 17*/ 1440
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int iscore;
printf("Please input a score:");
scanf("%d",&iscore);
if ((iscore<0) || (iscore>100)) /* 若成績超出0到100之間 */
printf("input error!!\n");
if ((iscore<60) && (iscore>49)) /* 若成績介於50到59之間 */
printf("make up exams!!\n");
system("pause");
return 0;
}
/* ##41 prog5_2, 「!」運算的用法 14*/ 1448
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int ia=0;
int ib=6;
printf("a=%d, !a=%d\n",ia,!ia); /* 印出a及!a的值 */
printf("b=%d, !b=%d\n",ib,!ib); /* 印出b及!b的值 */
system("pause"); return 0;
}
#include<stdlib.h>
#include<stdio.h>
int main(void)
{
int num=5.0;
printf("num/2=%.2f\n",num/2);
printf("num/2=%2d\n",num/2);
printf("num/2=%.2f\n",(float)num/2);
system("pause");
return 0;
}
/* ##47 prog7_0, for迴圈的使用 18*/ 1517
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
for(i=1;i<=5;i++) /* 計算1+2+...+10的結果 */
printf("%d\n",i); /* 印出sum的值 */
system("pause");
return 0;
}
int main(void)
{
int i;
for(i=5;i>0;i--) /* 計算1+2+...+10的結果 */
printf("%d\n",i); /* 印出sum的值 */
system("pause");
return 0;
}
/* prog7_1, for迴圈的使用 19*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i,isum=0;
for(i=1;i<=5;i++) /* 計算1+2+...+10的結果 */
isum+=i;
printf("1+2+3+4+5=%d\n",isum); /* 印出sum的值 */
system("pause");
return 0;
}
/* prog7_2, for迴圈的使用 20*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i,j;
for(i=0,j=1;i+j<=5;i++,j++) /* 計算1+2+...+10的結果 */
printf("i=%d j=%d i+j=%d\n",i,j,i+j); /* 印出sum的值 */
system("pause");
return 0;
}
//stdio.h
int printf (const char *__format, ...)
{
register int __retval;
__builtin_va_list __local_argv; __builtin_va_start( __local_argv, __format );
__retval = __mingw_vprintf( __format, __local_argv );
__builtin_va_end( __local_argv );
return __retval;
}
```