2013/05/21
[C++]字串關鍵字搜尋範例
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
string Data = "";
string KeyWord = "";
int Counter = 0;
int Index = 0;
cout << "Please input message: ";
getline(cin,Data);
cout << "Please input keyword: ";
getline(cin,KeyWord);
while(Index < Data.length())
{
if(Data.find(KeyWord,Index) != std::string::npos)
{
cout << "Find " << "\"" << KeyWord << "\"" << " at [" << Data.find(KeyWord,Index) << "]."<< endl;
Counter++;
Index = Data.find(KeyWord,Index)+KeyWord.length() ;
}
else
{
break;
}
}
cout << "\"" << KeyWord << "\"" << " appears " << Counter << " times." << endl;
system(("PAUSE"));
return 0;
}
2013/05/19
[Arduino] LED 呼吸燈
int Bright = 0;
int Delta = 5;
void setup()
{
pinMode(3,OUTPUT);
}
void loop()
{
analogWrite(3,Bright);
Bright = Bright + Delta;
if((Bright <= 0)||(Bright >= 255)) //0:最暗,255:最亮。
{
Delta = -Delta;
//當到達最暗時,Delta的值變正,亮度漸增;反之,達到最亮時,Delta變負, 亮度漸減
//當到達最暗時,Delta的值變正,亮度漸增;反之,達到最亮時,Delta變負, 亮度漸減
}
delay(30);//控制呼吸燈的頻率
}
2013/05/17
[Arduino] LED 閃爍
void Flash() //所有LED閃爍的副程式
{
for(int i=2; i<=13; i++)
{
digitalWrite(i,HIGH);
}
delay(100);
for(int i=2; i<=13; i++)
{
digitalWrite(i,LOW);
}
delay(100);
}
void setup()
{
// put your setup code here, to run once:
for(int i=2; i<=13; i++)
{
pinMode(i,OUTPUT);
}
}
void loop()
{
// put your main code here, to run repeatedly:
for(int i=2; i<=13; i++)
{
digitalWrite(i,HIGH);
delay(50);
digitalWrite(i,LOW);
delay(50);
}
for(int i=13; i>=2; i--)
{
digitalWrite(i,HIGH);
delay(50);
digitalWrite(i,LOW);
delay(50);
}
for(int i=0; i<3; i++)
{
Flash();
}
}
2013/05/16
[Arduino] LED 跑馬燈效果
void setup()
{
// put your setup code here, to run once:
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
int Cnt = 1;
int N;
while(true)
{
N = (Cnt % 4) + 2;
digitalWrite(N,HIGH);
delay(100);
digitalWrite(N,LOW);
delay(100);
Cnt++;
}
}
2013/05/15
[ACM]Q10929 : You can say 11
#include <cstdlib>
#include <iostream>
#include <sstream>
using namespace std;
int CharToInt(char chr)
{
stringstream ss;
int N;
ss << chr;
ss >> N;
return N;
}
int main(int argc, char *argv[])
{
int Sum = 0;
string str;
while(true)
{
cout << "Please input a number: ";
getline(cin,str);
if(str == "0")
{
break;
}
for(int i=0; i<str.length(); i++)
{
if(i % 2 == 0)
{
Sum = Sum + CharToInt(str[i]);
}
else
{
Sum = Sum - CharToInt(str[i]);
}
}
if(Sum == 0)
{
cout << str << " is a multiple of 11..." << endl;
}
else
{
cout << str << " is not a multiple of 11..." << endl;
}
Sum = 0;
}
system("PAUSE");
return EXIT_SUCCESS;
}
訂閱:
文章 (Atom)