目前分類:c++ (34)

瀏覽方式: 標題列表 簡短摘要
自己想的題目
主要在練習if-else的用法

借書逾期費用

#include
using namespace std;
int main()
{
int later,money=10;
cout << "請輸入借書逾期天數(以3天為限,超過一天累加10元):" ;
cin >> later ;
if (later<3)
{
cout << "你的罰款為 0 元!" ;
}
else
{
cout << "你的罰款為" << (later-3)*money << "元!" <
}
system("pause");
return 0;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

奇、偶數

此題學到do-while的連續輸入方式
if-else及以?:來表示

第一次秋的寫法
#include
using namespace std;
int main()
{
int input;
cout << "請輸入一個數值:" ;
cin >>input;
if(input%2==0)
cout << "你輸入的數值為偶數" << endl;
else
cout << "你輸入的數值為奇數" << endl;
system("pause");
return 0;
}



老公指導的寫法
#include
using namespace std;
int main()
{
int input;
string s;
do{
cout << "請輸入一個正整數值:" ;
cin >> input;
if(input<0){
cout << "你輸入的數值非正整數,請重新輸入!" <
}
else{
if(input%2==0){ //s= input%2==0?偶數:奇數;
s="偶數";
}
else {
s="奇數";
}
cout << "你輸入的正整數值為:" << s <
cout << "繼續輸入一個正整數 或 按 0 結束程式" << endl;
cout << endl;
}
}while(input!=0);
system("pause");
return 0;
}


最後精簡的寫法
#include
using namespace std;
int main()
{
int input;
string s;
do{
cout << "請輸入一個正整數值:" ;
cin >> input;
if(input<0){
cout << "你輸入的數值非正整數,請重新輸入!" <
}
else{
s=(input%2==0)?"偶數":"奇數";
cout << "你輸入的正整數值為:" << s <
cout << "繼續輸入一個正整數 或 按 0 結束程式" << endl;
cout << endl;
}
}while(input!=0);
system("pause");
return 0;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

圓面積

#include
using namespace std;
int main()
{
int num; //課本上是寫的型態是float
const float pi = 3.1416;
cout << "請輸入圓的半徑" ;
cin >> num ;
cout <<"該圓的圓面積為:"<< pi*num*num <
system("pause");
return 0;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

秋寫的,看似對,但沒做到型別強制轉換

#include
using namespace std;
int main()
{
int day1,day2,day3;
cout << "第一天支出" ;
cin >> day1 ;
cout << "第二天支出" ;
cin >> day2 ;
cout << "第三天支出" ;
cin >> day3 ;
cout << "總支出為:" << day1+day2+day3 << ",其每日平均花費" << (day1+day2+day3)/3 << endl;
system("pause");
return 0;
}


修正後

型別強制轉換
#include
using namespace std;
int main()
{
short day1,day2,day3; //short佔的記憶體大小為2bytes、int則是4bytes
cout << "第一天支出" ;
cin >> day1 ;
cout << "第二天支出" ;
cin >> day2 ;
cout << "第三天支出" ;
cin >> day3 ;
int sum = day1+day2+day3; //小轉大資料不會遺失,大轉小才會
float avg = (float)sum/3; //sum前面沒加(float)則不會有小數點的平均
cout << "總支出為:" << sum << ",其每日平均花費" << avg << endl;
system("pause");
return 0;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

cin&cout

#include
using namespace std;
int main()
{
string name; //注意,因為一開始輸入為char
int score;
cout << "請輸入學生姓名:" ;
cin >> name ;
cout << "請輸入學生成績:" ;
cin >> score ;
cout << name <<"同學成績為"<< score << "分" << endl;
system("pause");
return 0;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

#include //使程式能有cout及cin的功能,加#則是在編訪其他程式碼之前就會被讀入
using namespace std; //沒加這段則每次cout及cin前都要加std::
int main() //C++規定main()傳回值必須為整數,通常用return 0表結束
{
cout << "小秋的分數" << 95 << "分" << endl;
cout << "小平的分數" << 100 << "分" << endl;
system("pause"); //暫停指令的語法
return 0;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

for為已知的範圍
如:
for(i=1;i<=10;i+=2)

while為未知的範圍
如:
while(input!=0)
{
cout<<"請輸入數值”<
cin>>input>>endl;
}
cout<<"你所輸入的數值為0,結束"<

hfnkiki 發表在 痞客邦 留言(0) 人氣()

#include
#include

using namespace std;

int main(int argc, char *argv[])
{
int input=0,sum=0,count=0;
cout<<"Enter Score"<
cin>>input;
while(input!=-1)
{
count++;
sum+=input;
cout<<"Again Enter Score"<
cin>>input;
}
cout<<"your enter -1 => ending, average="<<
system("PAUSE");
return EXIT_SUCCESS;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

#include
#include

using namespace std;

int main(int argc, char *argv[])
{
int input=0,sum=0;
cout<<"Enter Number"<
cin>>input;
while(input!=0)
{
sum+=input;
cout<<"Again Enter Number"<
cin>>input;
}
cout<<"your enter 0,count="<
system("PAUSE");
return EXIT_SUCCESS;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

#include
#include

using namespace std;

int main(int argc, char *argv[])
{
double i,j,sum;
char ch;
cout<<"Enter first Number"<
cin>>i;
cout<<"Enter second Number"<
cin>>j;
cout<<"select +-*/"<
cin>>ch;
switch(ch){
case '+':
cout<<"+"<<"="<<
break;
case '-':
cout<<"-"<<"="<
break;
case '*':
cout<<"*"<<"="<<
break;
case '/':
cout<<"/"<<"="<<
break;
default:
return 0;
}
system("PAUSE");
return EXIT_SUCCESS;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

#include
#include

using namespace std;

int main(int argc, char *argv[])
{
int sum=0;
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++)
cout<<"*"<<"="<<
system("PAUSE");
return EXIT_SUCCESS;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

#include
#include

using namespace std;

int main(int argc, char *argv[])
{
int sum=0;
for(int i=1;i<=10;i++){
sum=sum+i;
cout<<"目前的加總值="<
}
system("PAUSE");
return EXIT_SUCCESS;
}

註:cout包含在for迴圈內及迴圈外的差別

hfnkiki 發表在 痞客邦 留言(0) 人氣()

#include
#include
using namespace std;

int main(int argc, char *argv[])
{
int sum=0;
for(int i=1;i<=10;i++){
sum=sum+i;
}
cout<<"1至10的加總值"<
system("PAUSE");
return EXIT_SUCCESS;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

#include
#include

using namespace std;

int main(int argc, char *argv[])
{
int input=0;
cout<<"Enter Number"<
cin>>input;
if(input>=90)
cout<<"為A級"<
else if(input>=80&&input<90)
cout<<"為B級"<
else if(input>=70&&input<80)
cout<<"為C級"<
else if(input>=60&&input<70)
cout<<"為D級"<
else
cout<<"為E級"<
system("PAUSE");
return EXIT_SUCCESS;
}

hfnkiki 發表在 痞客邦 留言(0) 人氣()

«12