基本數學運算:
需在標頭檔寫入:
#include <Cmath>
#include <math.h>
即可以使用C++內建的數學運算。常用數學函數如下:
(1) 指對數:
※ pow(a,b):回傳a的b次方之值。
※ sqrt(a):回傳a的開根號之值。
※ exp(a):回傳exp的a次方之值。
※ log(a):回傳以exp為底數、a為真數的對數之值。
(2) 三角函數:
※ cos(a):回傳cos(a)之值。其中a的單位為徑度。
※ sin(a):回傳sin(a)之值。其中a的單位為徑度。
※ tan(a):回傳tan(a)之值。其中a的單位為徑度。
(3) 四捨五入與進位:
※ round(a):回傳a之小數經四捨五入後之值。
※ floor(a):回傳a之小數無條件捨去後之值。
※ ceil(a):回傳a之小數無條件進位後之值。
(4) 絕對值:
※ fabs(a):回傳a之絕對值。
範例程式碼如:
cout<<cos(1.1)+sin(2.2)+tan(2); //顯示-0.922947
註:三角函數括號內的數字,單位是徑度。又如:
cout<<exp(1); //顯示2.71828
cout<<exp(2); //顯示7.38906 (exp的2次方)
cout<<sqrt(5); //顯示2.23607(5的開根號)
文章標籤
全站熱搜

老師好~~ 又來請教問題了... 目前仍卡在輸入問題。文字與整數的輸出入都能成功,但輸入整數輸出浮點數就有問題, 其實我是比較想直接輸入浮點數,直接抓浮點數來用。 目前程式碼的前四個輸出入沒問題,第5.6.7項似乎會互相干擾 1.第7項若輸入3位數則第6項就無法顯示,第7項若輸入4位數則第6項就顯示第7項的個位數 2.同樣的問題也發生在第6項與第5項的互相牽扯 因此第6項與第7項似乎只能輸入兩位數才能正常顯示,附上程式碼,請老師幫忙看一下問題到底是出在哪? 先說謝謝囉~~ #include
void dNumber2Char(TCHAR* szResult, double dNumber)
{
TCHAR szDot[] = {"."};
int iNumberhead = dNumber;
int iNumbertail = dNumber * 10 - iNumberhead * 10;
wsprintf(szResult, "%i %s %i" ,iNumberhead,szDot,iNumbertail);
}
/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
HMENU hMenu ;
PAINTSTRUCT ps ;
TCHAR szBuffer[6];
TCHAR szBufferOut1[6] = " ";
TCHAR szBufferOut2[6] = " ";
TCHAR szBufferOut3[6] = " ";
TCHAR szBuffer2[6] = " ";
TCHAR szBuffer3[6] = " ";
TCHAR szBuffer4[6] = " ";
TCHAR szBuffer5[6] = " ";
TCHAR szBuffer51[6] = " ";
TCHAR szBuffer6[6] = " ";
TCHAR szBuffer61[6] = " ";
static HWND hLHCH1;
static HWND hS2;
static HWND hS3;
static HWND ht4;
static HWND hZx5;
static HWND hZy6;
static HWND hkg7;
TCHAR szhLHCH1[6] = (""); // 先給空字串才不會一開始內涵亂碼
int dhS2 = 0;
int dhS3 = 0;
int dht4 = 0;
float dhZx5 = 0;
float dhZy6 = 0;
float dhkg7 = 0;
float dhZx50 = 0.0f;
float dhZy60 = 0.0f;
float dhkg70 = 0.0f;
switch(Message) {
case WM_CREATE:
hLHCH1=CreateWindow(TEXT("edit"),TEXT(" "), // 種類
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
90, 74, 30, 20,
hwnd, (HMENU) 1, NULL, NULL);
hS2=CreateWindow(TEXT("edit"),TEXT(" "), // 邊
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
120, 74, 30, 20,
hwnd, (HMENU) 1, NULL, NULL);
hS3=CreateWindow(TEXT("edit"),TEXT(" "),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, // 邊
150, 74, 30, 20,
hwnd, (HMENU) 1, NULL, NULL);
ht4=CreateWindow(TEXT("edit"),TEXT(" "),
WS_VISIBLE | WS_CHILD | WS_BORDER |ES_LEFT, // 厚度
180, 74, 30, 20,
hwnd, (HMENU) 1, NULL, NULL);
hZx5=CreateWindow(TEXT("edit"),TEXT(" "),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, // Zx
230, 74, 40, 20,
hwnd, (HMENU) 1, NULL, NULL);
hZy6=CreateWindow(TEXT("edit"),TEXT(" "),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, // Zy
320, 74, 40, 20,
hwnd, (HMENU) 1, NULL, NULL);
hkg7=CreateWindow(TEXT("edit"),TEXT(" "),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, // kg/M
410, 74, 40, 20,
hwnd, (HMENU) 1, NULL, NULL);
CreateWindow(TEXT("button"),TEXT("確認") /*("打完字請按我") */,
WS_VISIBLE | WS_CHILD,
490, 74, 40, 20,
hwnd, (HMENU) 1, NULL, NULL);
return 0 ;
case WM_COMMAND:
switch (HIWORD(wParam))
{
case BN_CLICKED:
GetWindowText(hLHCH1,szBuffer,4);
wsprintf(szhLHCH1,"%s", szBuffer);
GetWindowText(hS2,szBuffer,4);
if(0 == (dhS2 = atoi(szBuffer))) // 文字轉成數字失敗 "0 ==" 表失敗
MessageBox(NULL, "9999以內非0之整數" ,"請輸入數值", MB_OK);
dhS2 = atoi(szBuffer);
GetWindowText(hS3,szBuffer,4);
dhS3 = atoi(szBuffer);
GetWindowText(ht4,szBuffer,4);
dht4 = atoi(szBuffer);
GetWindowText(hZx5,szBuffer,5);
dhZx5 = atoi(szBuffer);
GetWindowText(hZy6,szBuffer,5);
dhZy6 = atoi(szBuffer);
GetWindowText(hkg7,szBuffer,5);
dhkg7 = atoi(szBuffer);
dhZx50 = dhZx5 / 10;
dhZy60 = dhZy6 / 10;
dhkg70 = dhkg7 / 10;
wsprintf(szBuffer2," %d", dhS2);
wsprintf(szBuffer3," %d", dhS3);
wsprintf(szBuffer4," %d", dht4);
dNumber2Char(szBufferOut1, dhZx50);
dNumber2Char(szBufferOut2, dhZy60);
dNumber2Char(szBufferOut3, dhkg70);
SendMessage(hwnd,WM_PAINT,0,0); // WM_PAINT 是無效的
InvalidateRect(hwnd, NULL, FALSE); // 觸發WM_PAINT 重繪畫面
}
// return 0 ; 這裡不可以 return 0 ; 會造成沒有回傳值
// break; 也不可以 break; 會造成沒有回傳值
case WM_PAINT:
// Paint the window
hdc = BeginPaint (hwnd, &ps) ;
TextOut(hdc,90,100, szhLHCH1,lstrlen(szhLHCH1));
TextOut(hdc,110,100, szBuffer2,lstrlen(szBuffer2));
TextOut(hdc,145,100, szBuffer3,lstrlen(szBuffer3));
TextOut(hdc,180,100, szBuffer4,lstrlen(szBuffer4));
TextOut(hdc,240,100, szBufferOut1,lstrlen(szBufferOut1));
TextOut(hdc,320,100, szBufferOut2,lstrlen(szBufferOut2));
TextOut(hdc,400,100, szBufferOut3,lstrlen(szBufferOut3));
EndPaint (hwnd, &ps) ;
return 0 ;
/* Upon destruction, tell the main thread to stop */
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
/* All other messages (a lot of them) are processed using default procedures */
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc; /* A properties struct of our window */
HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG msg; /* A temporary location for all messages */
/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
640, /* width */
480, /* height */
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
/*
This is the heart of our program where all input is processed and
sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
this loop will not produce unreasonably high CPU usage
*/
while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
TranslateMessage(&msg); /* Translate key codes to chars if present */
DispatchMessage(&msg); /* Send it to WndProc */
}
return msg.wParam;
}
GetWindowText(XXX,XXX,Y) 此函式最後的數字Y,決定了你能取得文字的數量。 也許你的問題和這個有關? 試試把他改成大一點的數字看看
謝謝老師 我試試看...