計算機程式語言實習課

Download Report

Transcript 計算機程式語言實習課

計算機程式語言實習課
分數運算
計算分數加減乘除,需用到最大公因數通分,
要將最大公因數用function來寫
例:
運算式= 8/15 + 2/15
ans = 2/3
again(y/n)? y
運算式= 5/3 + 5/6
ans = 2+1/2
again(y/n)? y
運算式= 8/5 + 12/5
ans = 4
again(y/n)? y
運算式= 8/5 * 5/12
ans = 2/3
……
分數運算




while語法
計算器進階
最大公因數
函式的應用
分數運算
計算分數加減乘除,需用到最大公因數通分,
要將最大公因數用function來寫
例:
運算式= 8/15 + 2/15
ans = 2/3
again(y/n)? y
運算式= 5/3 + 5/6
ans = 2+1/2
again(y/n)? y
運算式= 8/5 + 12/5
ans = 4
again(y/n)? y
運算式= 8/5 * 5/12
ans = 2/3
……
intx,int
a_up,a_down;
int gcd(int
y);
int b_up,b_down;
void mian()
char op;
{
int c,c_up,c_dwon;
int gcd(int
a_up,a_down;
int
x,int y);
int b_up,b_down;
char op;
int c,c_up,c_dwon;
}
分數運算
計算分數加減乘除,需用到最大公因數通分,
要將最大公因數用function來寫
例:
運算式= 8/15 + 2/15
ans = 2/3
again(y/n)? y
運算式= 5/3 + 5/6
ans = 2+1/2
again(y/n)? y
運算式= 8/5 + 12/5
ans = 4
again(y/n)? y
運算式= 8/5 * 5/12
ans = 2/3
……
int gcd(int x,int y);
void mian()
{
int ...;
cin >> a_up >> a_down
>> op
>> b_up >> b_down;
switch(op)
{
case ‘+’:
... break;
case ‘-’:
}
;}
分數運算
 如何作到多項式運算
 如何知道使用者輸入了幾個算式?
cin >> a >> op;
while(op != ‘=‘)
{
cin >> b;
switch(op){…};
cin >> op;
if (op!=‘=‘)
a=ans;
};
先乘除後加減?
24點
 抽4張牌
 運用+-*/()配合4張牌點數使答案為24
 例:
 1 2 3 4 => 1*2*3*4 => (1+3)*(2+4)
 6 9 2 8 => 2*8*9/6
How about …22,23,25,26… ?
老鼠走迷宮
 迷宮產生器
 單一路徑
=>
 可有circle =>
 檔案讀取迷宮
搜尋路徑
最短路徑
 老鼠走迷宮
 搜尋路徑
 最短路徑
(一路往終點走)
(一路靠北…邊走…)
=>
=>
遞迴(堆疊)
廣域搜尋
老鼠走迷宮
1. bool findway(int step,int news,int x,int y,int ex,int ey)
2. {
3.
if (m[x][y]==0)//可以走
4.
{
5.
Sleep(200);
6.
gotoxy(x*2+1,y+1); cout << "〥";
7.
}
8.
9.
if (x==ex && y==ey)
10.
return true;
//到終點
11.
else if (m[x][y]==1)
12.
return false;
//撞牆
13.
if(news!=反方向 && findway(step+1,0,x+1,y,ex,ey)){
14.
m[x][y]=step;
15.
return true;
16.
}
17.
…
18.
Sleep(200);
19.
gotoxy(x*2+1,y+1); cout << " ";
20.
return false;
21. }
函數的預設值
1. #include <iostream>
using namespace
1.2. #include
<iostream> std;
2.3. using namespace std;
3.4. void hello(char* a="阿發")
4.5. void
{ hello(char* a="阿發")
5. {
6.
cout << a << ":hello";
6.
cout << a << ":hello";
7.7. } }
voidmain()
main()
8.8. void
9.9. { {
10.
hello();
10.
hello();
11.
hello("isfun");
11.
hello("isfun");
12.
system("pause");
12.
system("pause");
13. }
13. }
C字串的宣告






char s[] = “Hello”;
char s[6];
s[0]=‘H’; s[1]=‘e’; s[2]=‘l’;s[3]=‘l’;s[4]=‘o’; s[5]=‘\0’;
char s2[6] = {‘H’,’e’ ,’l’ ,’l’ ,’o’ ,’\0’};
char s3[] = {‘H’,’e’ ,’l’ ,’l’ ,’o’ ,’\0’};
char *s4 = {‘H’,’e’ ,’l’ ,’l’ ,’o’ ,’\0’};
C字串的宣告
 請問下列有什麼不一樣?
 char name1[10] = “Isfun”;
 char name2[] = “Isfun”
C字串

1.
2.
3.
4.
5.
6.
7.
8.
9.
以下程式輸出為何?
#include <iostream>
using namespace std;
void main()
{
char name[] = “Tseng yen hsiang”;
naem[5]=‘\0’;
cout << name;
}
C字串

計算字串長度
1.
2.
3.
4.
5.
#include <cstring>
char s[]=“Hello”;
int len=strlen(s);
for (int i=0;i<len;i++)
cout << s[i];
C字串

字串複製
1.
2.
3.
4.
5.
6.
#include <cstring>
char name1[]=“Many”;
char name2[]=“John”;
strcpy(name1,name2);
cout << name2;
C字串

字串局部複製
1.
2.
3.
4.
5.
6.
#include <cstring>
char name1[]=“Many”;
char name2[]=“John”;
strncpy(name1,name2,3);
cout << name2;
C字串

字串比較
1.
2.
3.
4.
5.
#include <cstring>
char name1[]=“Many”;
char name2[]=“John”;
strcmp(name1,name2);
//
0一樣
C字串

字串附加
1.
2.
3.
4.
5.
#include <cstring>
char name1[]=“Many”;
char name2[]=“John”;
strcat(name1,name2);
cout << name1 //ManyJohn