Programming
C++學習歷程
2014年3月29日 星期六
2013年6月9日 星期日
多行寫檔
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
//----------------------------------
// C++檔案輸出範例
//----------------------------------
int main(int argc, char *argv[])
{
string Data; //儲存使用者輸入的資料
ofstream outfile("C:\\output.txt"); //宣告並指定輸出.txt檔的路徑
while(true)
{
cout << "請輸入一段訊息: (輸入*QUIT則結束程式)" << endl;
getline(cin,Data);
if(Data == "*QUIT")
{
break;
}
outfile << Data << endl;
Data = "";
}
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
#include <fstream>
using namespace std;
//----------------------------------
// C++檔案輸出範例
//----------------------------------
int main(int argc, char *argv[])
{
string Data; //儲存使用者輸入的資料
ofstream outfile("C:\\output.txt"); //宣告並指定輸出.txt檔的路徑
while(true)
{
cout << "請輸入一段訊息: (輸入*QUIT則結束程式)" << endl;
getline(cin,Data);
if(Data == "*QUIT")
{
break;
}
outfile << Data << endl;
Data = "";
}
system("PAUSE");
return EXIT_SUCCESS;
}
2013年6月7日 星期五
亂數產生
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, char *argv[])
{
bool Find = false;
int A,B;//表示範圍的起始與結束
int Change;
int Num;
int Temp;
int Index = 0;
srand(time(NULL));
cout << "請輸入範圍起始值:";
cin >> A;
cout << "請輸入範圍結束值:";
cin >> B;
if(A>B)
{
Change = A;
A = B;
B = Change;
}
cout << "請輸入亂數個數:";
cin >> Num;
int Box[Num];
for(int i=0; i<Num; i++)
{
Box[i] = 0;
}
Box[Num-1] = -1;
while(Box[Num-1] == -1)
{
Find = false;//此處旗標需重新設定為false!
Temp = rand()%B+A;
for(int j=0; j<Num; j++)
{
if(Box[j] == Temp)
{
Find = true;
break;
}
}
//
if(!Find)
{
Box[Index] = Temp;
Index++;
}
}
//輸出產生的亂數
cout << "產生的亂數:" << endl;
for(int k=0; k<Num; k++)
{
cout << Box[k] << " ";
}
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, char *argv[])
{
bool Find = false;
int A,B;//表示範圍的起始與結束
int Change;
int Num;
int Temp;
int Index = 0;
srand(time(NULL));
cout << "請輸入範圍起始值:";
cin >> A;
cout << "請輸入範圍結束值:";
cin >> B;
if(A>B)
{
Change = A;
A = B;
B = Change;
}
cout << "請輸入亂數個數:";
cin >> Num;
int Box[Num];
for(int i=0; i<Num; i++)
{
Box[i] = 0;
}
Box[Num-1] = -1;
while(Box[Num-1] == -1)
{
Find = false;//此處旗標需重新設定為false!
Temp = rand()%B+A;
for(int j=0; j<Num; j++)
{
if(Box[j] == Temp)
{
Find = true;
break;
}
}
//
if(!Find)
{
Box[Index] = Temp;
Index++;
}
}
//輸出產生的亂數
cout << "產生的亂數:" << endl;
for(int k=0; k<Num; k++)
{
cout << Box[k] << " ";
}
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
字串轉成數值
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string num_data; //首先宣告為string型別
cout << "Please input a number: ";
cin >> num_data; //這裡的num_data為string型別,不能作運算
int num = atoi(num_data.c_str()); //字串轉為數值,轉換後的num即可進行運算
cout << ++num;
//解析: c_str(),將string轉為傳統C字元陣列 ; atoi():傳統C字元陣列(即C字串)轉為數值
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
string num_data; //首先宣告為string型別
cout << "Please input a number: ";
cin >> num_data; //這裡的num_data為string型別,不能作運算
int num = atoi(num_data.c_str()); //字串轉為數值,轉換後的num即可進行運算
cout << ++num;
//解析: c_str(),將string轉為傳統C字元陣列 ; atoi():傳統C字元陣列(即C字串)轉為數值
system("PAUSE");
return EXIT_SUCCESS;
}
幾個程式語言的參考網站
良葛格學習筆記 http://caterpillar.onlyfun.net/Gossip/
C++學習筆記 http://caterpillar.onlyfun.net/Gossip/CppGossip/CppGossip.html
C語言學習筆記 http://caterpillar.onlyfun.net/Gossip/CGossip/CGossip.html
Java學習筆記(一) http://caterpillar.onlyfun.net/Gossip/JavaGossip-V1/JavaGossip.htm
Java學習筆記(二) http://caterpillar.onlyfun.net/Gossip/JavaGossip-V2/JavaGossip2.htm
VB/ VBA/ C#/ Java/ C++ 語言學習筆記 http://www.dotblogs.com.tw/yc421206/Default.aspx
VB/ VBA/ C#/ Java/ C++ 語言學習筆記(備份) http://yc421206.pixnet.net/blog
C++學習筆記 http://caterpillar.onlyfun.net/Gossip/CppGossip/CppGossip.html
C語言學習筆記 http://caterpillar.onlyfun.net/Gossip/CGossip/CGossip.html
Java學習筆記(一) http://caterpillar.onlyfun.net/Gossip/JavaGossip-V1/JavaGossip.htm
Java學習筆記(二) http://caterpillar.onlyfun.net/Gossip/JavaGossip-V2/JavaGossip2.htm
VB/ VBA/ C#/ Java/ C++ 語言學習筆記 http://www.dotblogs.com.tw/yc421206/Default.aspx
VB/ VBA/ C#/ Java/ C++ 語言學習筆記(備份) http://yc421206.pixnet.net/blog
指定路徑讀檔
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
string Loc;
string LineData = "";
cout << "請輸入檔案路徑:";
getline(cin,Loc);
ifstream infile(Loc.c_str(),ios::in);
if(infile)
{
cout << "Succeeded..." << endl;
while(!infile.eof())
{
getline(infile,LineData);
cout << LineData << endl;
LineData = "";
}
}
else
{
cout << "Failed..." << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
最大公因數、最小公倍數(非遞迴版)
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int max,min,tmpm,tmpn,temp;
cout << "請輸入第一個數:";
cin >> max;
cout << "請輸入第二個數:";
cin >> min;
if(max < min)
{
temp = max;
max = min;
min = temp;
}
tmpm = max;
tmpn = min;
if(max % min == 0)
{
cout << "GCD = " << min << endl;
cout << "LCM = " << max << endl;
}
//
else
{
while(min > 0)
{
max = max % min;
min = min % max;
if(min == 0)
{
cout << "GCD = " << max << endl;
cout << "LCM = " << max*(tmpm/max)*(tmpn/max) << endl;
}
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int max,min,tmpm,tmpn,temp;
cout << "請輸入第一個數:";
cin >> max;
cout << "請輸入第二個數:";
cin >> min;
if(max < min)
{
temp = max;
max = min;
min = temp;
}
tmpm = max;
tmpn = min;
if(max % min == 0)
{
cout << "GCD = " << min << endl;
cout << "LCM = " << max << endl;
}
//
else
{
while(min > 0)
{
max = max % min;
min = min % max;
if(min == 0)
{
cout << "GCD = " << max << endl;
cout << "LCM = " << max*(tmpm/max)*(tmpn/max) << endl;
}
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
訂閱:
文章 (Atom)