###### tags: `資料結構`
# 資料結構
#### ch0-6 func template
```程式類型
```
#### ch0-5 func
```程式類型
#include <iostream>
using namespace std;
//float sum(float list[], int c);
//int add(int a, int b);
//float add(float a, float b);
//int sub(int a, int b);
//float sub(float a, float b);
//define
int add(int a, int b)
{
return a+b;
}
float add(float a, float b)
{
return a+b;
}
int sub(int a, int b)
{
return a-b;
}
float sub(float a, float b)
{
return a-b;
}
int main ()
{
int x=1;
int y=2.5;
// float x=1;
// float y=2.5;
cout << "The total is: " << add(x,y)<<endl;
// int a=50;
// int b=27;
float a=50.897;
float b=32.569;
cout << "The total is: " << sub(a,b)<<endl;
return 0;
}//end of main
```
#### ch0-4 if and loop 練習
```程式類型
#include <iostream>
using namespace std;
int main ()
{
int n = 10;
//while
while (n>0) {
if(n%2 == 1)
cout << n << ", ";
--n;
}
cout << " lift off!\n";
//dowhile
string str;
do {
cout << "Enter text: ";
getline (cin,str);
cout << "You entered: " << str << '\n';
} while (str != "the end");
//for
int sum=0;
for (int n=0; n<=10; n++)
{
if(n%2 == 0){
n==10? cout << n : cout << n<< "+" ;
sum+=n;
}
}
cout<<"="<<sum<<endl;
//switch case
int x;
cout << "please enter a number!";
cin>>x;
switch (x) {
case 1:
case 2:
case 3:
cout << "x is 1, 2 or 3";
break;
default:
cout << "x is not 1, 2 nor 3";
}
return 0;
}
```
#### ch0-3 char and string練習
```程式類型
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string mystring,myname;
char a=65;
//initailize
mystring = "This is a string";
string test_string ("This is a book!");
string test = mystring + " " + test_string;
cout << mystring <<endl;
cout << test_string <<endl;
cout<<"this is char:"<<a<<endl;
cout << test <<endl;
cout << "What's your name?";
cin>>myname;
cout << "your name is "<<myname<<endl;
string mystr;
cout << "What's your name? ";
getline (cin, mystr);//函式呼叫
cout << "Hello " << mystr << ".\n";
cout << "What's your name? ";
getline (cin, myname);
cout << "Hello " << myname << ".\n";
cout << "What is your favorite team? ";
getline (cin, myname);
cout << "I like " << myname << " too!\n";
int age,g;
string grade;
cout << "what's your name? how old are you? What grade?";
cin>>myname>>age>>grade;
stringstream(grade)>>g;
cout << "your name and age : " << myname<<"("<< age << ") and grade = "<< g*2 <<" !\n";
return 0;
}
```
#### ch0-2 四則運算練習
```程式類型
#include <iostream>
using namespace std;
int main ()
{
int i,a,b;
cout << "Please enter an integer value: 1.+ 2./ 3.* 4.-:";
cin >> i;
if(i==1){
cout << "First value ?"<<"\n";
cin >> a;
cout << "Second vlue?"<<"\n";
cin >> b;
cout << "a+b="<<a+b <<endl;
}
// if(i==2){
//
// }
return 0;
}
```
#### ch0-1 宣告和給直練習
```程式類型
#include <iostream>
int main()
{
int a,b,c,d;
float answer1,answer2,answer3,answer4;
a=5,b=4,c=3,d=2;
answer1 = a+b;
answer2 = a-b;
answer3 = c*d;
answer4 = (float) c/d;
std::cout << "a+b=" <<answer1<< std::endl;
std::cout << "a-b="<<answer2<< std::endl;
std::cout << "c*d="<<answer3<< std::endl;
std::cout << "c/d="<<answer4<< std::endl;
return 0;
}
```
#### ch0-0 c++練習
```程式類型
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
```