锘??xml version="1.0" encoding="utf-8" standalone="yes"?> class CNumber void print()
cout.precision(4); //璁劇疆灝忔暟閮ㄥ垎鐨?浣嶆湁鏁堟暟瀛?img src ="http://www.shnenglu.com/zhenglinbo/aggbug/198145.html" width = "1" height = "1" />
]]>
]]>
妗屼笂鏈変竴鍙犵墝錛屼粠絎竴寮犵墝寮濮嬩粠涓婂線涓嬩緷嬈$紪鍙?~n銆傚綋鑷沖皯榪樺墿涓ゅ紶鐗屾椂榪涜濡備笅鎿嶄綔錛氭妸絎竴寮犵墝鎵旀帀錛岀劧鍚庢妸鏂扮殑絎竴寮犵墝鏀懼埌鏁村彔鐗岀殑鏈鍚庛傝緭鍏錛岃緭鍑烘瘡嬈℃墧鎺夌殑鐗岋紝浠ュ強鏈鍚庡墿涓嬬殑鐗屻?br />
鏍蜂緥杈撳叆錛?
鏍蜂緥杈撳嚭錛? 3 5 7 4 2 6
浠g爜濡備笅錛?br />
#include<queue>
using namespace std;
queue<int> q; //澹版槑闃熷垪
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++) q.push(i+1);
while(!q.empty())
{
cout<<q.front()<<" ";
q.pop();
if(!q.empty()) //姝ゅ闇瑕佸垽鏂鏃墮槦鍒楁槸鍚︿負絀?br /> {
q.push(q.front());
q.pop();
}
}
cout<<endl;
return 0;
}
]]>
class CArray
{
public:
CArray(int l)
{
length=l;
Buff=new char[length];
}
~CArray(){delete Buff;}
int GetLength(){return length;}
char& operator [](int i);
private:
int length;
char *Buff;
};
char & CArray::operator[](int i)
{
static char ch=0;
if(i<length && i>=0)
return Buff[i];
else
{
cout<<"\nIndex out of range.";
return ch;
}
}
void main()
{
int cnt;
CArray string1(6);
char *string2="string";
for(cnt=0;cnt<string1.GetLength();cnt++)
string1[cnt]=string2[cnt];
cout<<"\n";
for(cnt=0;cnt<string1.GetLength();cnt++)
cout<<string1[cnt];
cout<<"\n";
cout<<string1.GetLength()<<endl;
}
1錛夎鍑芥暟鍙甫涓涓弬鏁幫紝涓嶅彲甯﹀涓弬鏁般?br />2錛夊緱閲嶈澆涓哄弸鍏冨嚱鏁幫紝蹇呴』鏄潪static綾葷殑鎴愬憳鍑芥暟銆?br />
]]>
{
private:
int n;
public:
CNumber(int number)
{
n=number;
}
~CNumber(){ }
int isEven()
{
if(n%2==0)
return 1;
else
return 0;
}
int isOdd()
{
if(n%2!=0)
return 1;
else
return 0;
}
int isPrime()
{
int i;
if(n<1)
return 0;
for(i=2;i<=sqrt(n);i++)
if(n%i==0)
return 0;
else
return 1;
return 0;
}
bool isAPrime(int n)
{
for(int i=2;i<sqrt(n);i++)
if(n%i==0)
return false;
return true;
}
int isGoldBach()
{
int i;
int halfnum=n/2;
for(i=2;i<=halfnum;i++)
if(isAPrime(i)&&isAPrime(n-i))
return 1;
return 0;
}
{
if(isEven()) cout<<"This number is even."<<endl;
if(isOdd()) cout<<"This number is odd."<<endl;
if(isPrime()) cout<<"This number is prime."<<endl;
if(isGoldBach()) cout<<"This number is goldbach."<<endl;
}
};
涓誨嚱鏁幫細
void main()
{
int num;
cout<<"Please enter one number:"<<endl;
cin>>num;
CNumber numb(num);
numb.print();
}
]]>
鐜板湪鍋囪f(a)<0,f(b)>0,a<b 銆銆
①濡傛灉f[(a+b)/2]=0錛岃鐐瑰氨鏄浂鐐癸紝 銆銆濡傛灉f[(a+b)/2]<0,鍒欏湪鍖洪棿錛?a+b)/2錛宐)鍐呮湁闆剁偣錛?a+b)/2=>a錛屼粠①寮濮嬬戶緇嬌鐢?銆銆涓偣鍑芥暟鍊煎垽鏂?銆銆濡傛灉f[(a+b)/2]>0錛屽垯鍦ㄥ尯闂?a,(a+b)/2)鍐呮湁闆剁偣錛?a+b)/2<=b錛屼粠①寮濮嬬戶緇嬌鐢?銆銆涓偣鍑芥暟鍊煎垽鏂?銆銆榪欐牱灝卞彲浠ヤ笉鏂帴榪戦浂鐐廣?銆銆閫氳繃姣忔鎶奻(x)鐨勯浂鐐規墍鍦ㄥ皬鍖洪棿鏀剁緝涓鍗婄殑鏂規硶錛屼嬌鍖洪棿鐨勪袱涓鐐歸愭榪繎鍑芥暟鐨勯浂鐐癸紝浠ユ眰寰楅浂鐐圭殑榪戜技鍊鹼紝榪欑鏂規硶鍙仛浜屽垎娉曘?
澶存枃浠跺畾涔?br />class CEquation
{
private:
double solution; //鏂圭▼鐨勮繎浼艱В
double a, b; //榪戜技瑙g殑鍖洪棿
double (*p_fx)(double x); //p_fx鏄竴涓寚鍚戝嚱鏁扮殑鎸囬拡,鎸囧悜鏂圭▼寮忔眰鍊煎嚱鏁?br /> double (*p_solution)(double x, double y); //鎸囧悜鐢辮繎浼艱В鍖洪棿姹傝繎浼艱В鐨勫嚱鏁扮殑鎸囬拡
double delta; //姹傝В綺懼害
public:
CEquation(double av, double bv, double (*p1)(double), double (*p2)(double,double), double dv);
double biSection(); //浜屽垎娉曟眰鏂圭▼榪戜技瑙?br /> void printSolution() const;
};
綾葷殑瀹炵幇鍙婁富鍑芥暟瀹炵幇錛?
CEquation::CEquation(double a_val, double b_val, double (*p1)(double), double (*p2)(double, double), double delta_val)
{
a = a_val;
b = b_val;
p_fx = p1;
p_solution = p2;
solution = p_solution(a, b);
delta = delta_val;
}
double fx(double x) //鏂圭▼涓? e^x+4^x3-6^x2+3x-2=0
{
return exp(x)+4.0*x*x*x-6.0*x*x+3.0*x-2.0;
}
double middle(double x, double y) //涓?/span>
{
return 0.5*(x+y);
}
double CEquation::biSection()
{
double h;
while (fabs(a-b) > delta)
{
h = p_solution(a, b);
if (p_fx(a)*p_fx(h) > 0)
a = h;
else
b = h;
}
solution = p_solution(a, b);
return solution;
}
void CEquation::printSolution() const
{
cout << "Solution is: " << solution << endl;
}
void main ()
{
CEquation a(0.0, 1.0, fx, middle, 1e-6);
a.biSection();
a.printSolution();
}
]]>
]]>
涓嬪崍鐓ф牱鐪婥++how to program錛屽姞娌癸紝瑕佸埌鏁扮粍鍜屾寚閽堜簡錛屾帴涓嬫潵灝辨槸綾葷殑娣卞叆鍓栨瀽浜嗭紝榪欏嚑澶╁湪鍔犲己鐏姏鐚涙敾榪欏潡鍦版柟錛屽搱鍝堬紝榪樻槸緙栫▼鑿滈笩錛佷笉榪囨垜鑷繁鎰熻姣斿ぇ涓閭f椂鍊欑湡鐨勬湁鐐硅繘姝ヤ簡錛屾湁閭gcoding 鐨勬劅瑙変簡錛屽姫鍔涘涔犺繕鏄湁鏀惰幏鐨勩傞棽璇濅紤璇達紝鎶婁粖澶╀笅鍗堣嚜宸卞啓鐨勪唬鐮佸彂涓婃潵錛?
using namespace std;
int main()
{
int row,line;//瀹氫箟琛屻佸垪鏁扮殑鍙橀噺
for(row=1;row<=10;row++)
{
for(line=1;line<=row;line++)
cout<<"*";
cout<<endl;
}
cout<<"\n";//絎竴涓浘褰㈣嚦姝ゆ墦鍗板嚭鏉?/span>
for(row=10;row>=1;row--)
{
for(line=1;line<=row;line++)
cout<<"*";
cout<<endl;
}
cout<<"\n";//絎簩涓浘褰㈡墦鍗板嚭鏉?/span>
for(row=10;row>=1;row--)
{
for(line=1;line<=row;line++)
cout<<"*";
cout<<endl;
for(line=1;line<=(11-row);line++)
cout<<" ";
}
cout<<"\n";// 絎笁涓浘褰㈡墦鍗板嚭鏉?/span>
for(row=1;row<=10;row++)
{
for(line=1;line<=10-row;line++)
cout<<" ";
for(line=1;line<=row;line++)
cout<<"*";
cout<<endl;
} //鏈鍚庝竴涓浘褰紒
cout<<endl;
return 0;
}
紼嬪簭榪愯緇撴灉
錛?br />
]]>
璇風湅涓涓嬩竴渚嬶細
璇ョ▼搴忚姹傝緭鍏ラ暱搴︼紝鐒跺悗杈撳嚭涓涓洓鏉¤竟閮藉甫鐩稿悓鏁伴噺鏄熷彿鐨勭煩褰€?
#include<iostream>
using namespace std;
int main()
{
int side,rowPosition,size;
cout<<"input the square side: ";//杈撳叆鐭╁艦鐨勫搴?/span>
cin>>side;
size=side;//浣塊暱瀹界殑杈規墍甯︽槦鍙鋒暟閲忕浉鍚?/span>
while(side>0)//鍙岄噸寰幆杈撳嚭鐭╁艦
{
rowPosition=size;
while(rowPosition>0)
{
if(size==side||side==1||rowPosition==1||rowPosition==size)
cout<<'*'<<;
else
cout<<' ';
--rowPosition;
}
cout<<'\n';//鍦ㄨ繖閲岀瓑涓琛岃嚜鐒惰緭鍑哄悗錛屽湪鍒╃敤cout<<‘\n'鍥炶濺錛岃緭鍑轟笅涓琛?/span>
--side;
}
cout<<endl;//榪欓噷鎬葷殑寮哄埗杈撳嚭鎵鏈夎緭鍏ョ殑瀛楃
return 0;
}
紼嬪簭榪愯鏁堟灉濡備笅鍥撅紝杈撳叆8錛?br />
濡傛灉鍦ㄧ▼搴忕殑姣忔潯cout璇彞涓姞涓?lt;<endl; 閭d箞紼嬪簭榪愯鐨勬晥鏋滐紙鍥炬墍闄愶紝"end line": inserts a newline into the stream and calls flush.鏈夌渷鐣ヤ竴浜涳級濡備笅錛?br />
鍚庢敞錛氬垰鍒氬湪緇村熀鐧劇閲屾煡鍒皊td::endl鐨勫畾涔夛紝瀹冭錛?end line": inserts a newline into the stream and calls flush. 榪欏氨鏄endl鐨勫姛鑳藉氨鏄己鍒惰緭鍑哄拰鎹㈣錛岀幇鍦ㄦ噦浜嗭紝鎰熻阿鍗氬弸鐨勮鐪熸洿姝o紝瀛︿範浜嗐?)
]]>
Tonight I was studying the credit card codes, all of these was copied from my book,"C++ ——how to program".
This are the program demand:
1)Account number(an integer);
2)Balance at the begining of the month;
3)Total all items charged by this customer this month;
4)Total of all items applied to this customer's account this month;
5)Allowed credit limit.
The program should input each of these facts, calculate the new balance(=begining balance+charges-credits) and determine if the new balance exceeds the customer credit limit.
For those customers whose credit limit is exceeded, the program should display the customer's account number,credit limit,new balance and the message "Credit limit exceeded".
input -1 and the program will be ended.
#include<iostream>
using namespace std;
int main()
{
int x;
int y;
int z;
int result;
cout<<"input three numbers:\n";
cin>>x>>y>>z;
result=x*y*z;
cout<<"The product is "<<result<<endl;
return 0;
}