2019年8月6日 星期二

1080806程式設計



三不政策
不知道 不清楚 不要問我


long sum(long num1, long num2);




數學函數http://www.cplusplus.com/reference/cmath/
有範例


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* printf example */
#include <stdio.h>

int main()
{
   printf ("Characters: %c %c \n", 'a', 65);
   printf ("Decimals: %d %ld\n", 1977, 650000L);
   printf ("Preceding with blanks: %10d \n", 1977);
   printf ("Preceding with zeros: %010d \n", 1977);
   printf ("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
   printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
   printf ("Width trick: %*d \n", 5, 10);
   printf ("%s \n", "A string");
   return 0;
}


Output:

Characters: a A
Decimals: 1977 650000
Preceding with blanks:       1977
Preceding with zeros: 0000001977
Some different radices: 100 64 144 0x64 0144
floats: 3.14 +3e+000 3.141600E+000
Width trick:    10
A string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* scanf example */
#include <stdio.h>

int main ()
{
  char str [80];
  int i;

  printf ("Enter your family name: ");
  scanf ("%79s",str);  
  printf ("Enter your age: ");
  scanf ("%d",&i);
  printf ("Mr. %s , %d years old.\n",str,i);
  printf ("Enter a hexadecimal number: ");
  scanf ("%x",&i);
  printf ("You have entered %#x (%d).\n",i,i);
  
  return 0;
}


This example demonstrates some of the types that can be read with scanf:
Enter your family name: Soulie
Enter your age: 29
Mr. Soulie , 29 years old.
Enter a hexadecimal number: ff
You have entered 0xff (255).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// setprecision example
#include <iostream>     // std::cout, std::fixed
#include <iomanip>      // std::setprecision

int main () {
  double f =3.14159;
  std::cout << std::setprecision(5) << f << '\n';
  std::cout << std::setprecision(9) << f << '\n';
  std::cout << std::fixed;
  std::cout << std::setprecision(5) << f << '\n';
  std::cout << std::setprecision(9) << f << '\n';
  return 0;
}


Output:
3.1416
3.14159
3.14159
3.141590000

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// bad_alloc example
#include <iostream>     // std::cout
#include <new>          // std::bad_alloc

int main () {
  try
  {
    int* myarray= new int[10000];
  }
  catch (std::bad_alloc& ba)
  {
    std::cerr << "bad_alloc caught: " << ba.what() << '\n';
  }
  return 0;
}


Possible output:

bad_alloc caught: bad allocation





Example

1
2
3
4
5
6
7
8
9
10
11
// minmax example
#include <iostream>     // std::cout
#include <algorithm>    // std::minmax

int main () {
  auto result = std::minmax({1,2,3,4,5});

  std::cout << "minmax({1,2,3,4,5}): ";
  std::cout << result.first << ' ' << result.second << '\n';
  return 0;
}


Output:
minmax({1,2,3,4,5}): 1 5






Example

1
2
3
4
5
6
7
8
9
10
11
// min example
#include <iostream>     // std::cout
#include <algorithm>    // std::min

int main () {
  std::cout << "min(1,2)==" << std::min(1,2) << '\n';
  std::cout << "min(2,1)==" << std::min(2,1) << '\n';
  std::cout << "min('a','z')==" << std::min('a','z') << '\n';
  std::cout << "min(3.14,2.72)==" << std::min(3.14,2.72) << '\n';
  return 0;
}


Output:
min(1,2)==1
min(2,1)==1
min('a','z')==a
min(3.14,2.72)==2.72




double最多308位數
位數超過1000


三角形判別

// ConsoleApplication7.cpp : 定義主控台應用程式的進入點。
//

//#include "stdafx.h"
/* printf example */
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int a[3];
int tmp;

cout << "enter 3 numbers";
cin >> a[0] >> a[1] >> a[2];


         sort(a,a+3);
//int a(a[0]),b(a[1]),c(a[2]);   

cout << a[0] << " " << a[1] << " " << a[2] << endl;
if (a[0] + a[1] <= a[2])
{
cout << "No" << endl;
}

else if (a[0] * a[0] + a[1] * a[1] < a[2] * a[2])
{
cout << "Obtuse" << endl;
}

else if (a[0] * a[0] + a[1] * a[1] == a[2] * a[2])
{
cout << "Right" << endl;
}
else if (a[0] * a[0] + a[1] * a[1] < a[2] * a[2])
{
cout << "Acute" << endl;
}

return 0;
}


----------------------------------------------------


// ConsoleApplication7.cpp : 定義主控台應用程式的進入點。

//

#include "stdafx.h"
/* printf example */
#include <stdio.h>
#include <iostream>
using namespace std;

int main() {
int a[3];
int tmp;

cout << "enter 3 numbers";
cin >> a[0] >> a[1] >> a[2];

for (int i = 3; i > 0; i--)

for (int i = 3; i > 0; i--)
{                                //氣泡排序
for (int j = 0; j < i - 1; j++) {
if (a[j] > a[j + 1]) {
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}


}




cout << a[0] << " " << a[1] << " " << a[2] << endl;
if (a[0] + a[1] <= a[2])
{
cout << "No" << endl;
}

else if (a[0] * a[0] + a[1] * a[1] < a[2] * a[2])
{
cout << "Obtuse" << endl;
}

else if (a[0] * a[0] + a[1] * a[1] == a[2] * a[2])
{
cout << "Right" << endl;
}
else if (a[0] * a[0] + a[1] * a[1] < a[2] * a[2])
{
cout << "Acute" << endl;
}

return 0;
}


-------------------------------------------
// ConsoleApplication7.cpp : 定義主控台應用程式的進入點。
//

//#include "stdafx.h"
/* printf example */
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int s[3];
int tmp;

cout << "enter 3 numbers";
cin >> s[0] >> s[1] >> s[2];


         sort(s,s+3);

int a(s[0]),b(s[1]),c(s[2]);        //a有同名題

cout << a << " " << b << " " << c << endl;
if (a+ b <= c)
{
cout << "No" << endl;
}

else if (a * a + b * b < c * c)
{
cout << "Obtuse" << endl;
}

else if (a * a + b * b == c * c)
{
cout << "Right" << endl;
}
else if (a * a+ b * b < c * c)
{
cout << "Acute" << endl;
}

return 0;
}



---------------------------------

https://zh.wikipedia.org/wiki/ASCII


Funcition Overloading
變數不可同名
函數可以(靠資料格式)

int max(int x[],int len);
long max(long x[],int len)


沒有留言:

張貼留言