目前分類:c++ (34)

瀏覽方式: 標題列表 簡短摘要

陣列-加總及超出範圍_2121124-1陣列-加總及超出範圍_2121124-2    

 

#include<iostream>
using namespace std;
int main()
{
const int c=3;
int price[c],sum=0,i=0,n; //實際線上操作,才知道sum=0的預設值用意原來在此,不然加總一直總會-1
do
{
if(i==c)
{
cout<<"超出範圍"<<endl;
i++; //為何在這要i++???
break;
}
cout<<"請輸入第"<<i+1<<"件商品價格:";
cin>>price[i];
i++;
}while(price[i-1]>=0); //輸入負數表示結束
n=i-1; //實際的件數做加總,不會將輸入負數的數值也加總進去
for(i=0;i<n;i++)
{
sum=sum+price[i];
}
cout<<"總計"<<sum<<endl;
system("pause");
return 0;
}

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

陣列-最大最小值    

#include<iostream>
using namespace std;
int main()
{
const int c=4;                       //不是很懂為何要用const來宣告
int n[c],min,max;  
for(int i=0;i<c;i++)
{
cout<<"請輸入第"<<i+1<<"個數字:";
cin>>n[i];
}
cout<<"輸入數值分別為:";        //為何不能直接在這行顯示「cout<<n[i]<<" ";」,為何還需要跑個for迴圈??
for(int i=0;i<c;i++)
{
cout<<n[i]<<" ";
}
cout<<endl;
max=n[0];
for(int i=1;i<c;i++)
{
if(n[i]>max)
{
max=n[i];
}
}
cout<<"最大值為:"<<max<<endl;
for(int j=1;j<c;j++)
{
if(n[j]<min)
{
min=n[j];
}
}
cout<<"最小值為:"<<min<<endl;

system("pause");
return 0;
}

 

錯誤版//超級無敵怪,min在max的前面就正常,在後面就錯誤

陣列-最大最小值-test

#include<iostream>
using namespace std;
int main()
{
const int c=4; //不是很懂為何要用const來宣告
int n[c],max,min,i; //超級無敵怪,min在max的前面就正常,在後面就錯誤
for(i=0;i<c;i++)
{
cout<<"請輸入第"<<i+1<<"個數字:";
cin>>n[i];
}
cout<<"輸入數值分別為:";
for(i=0;i<c;i++)
{
cout<<n[i]<<" ";
}
cout<<endl;
max=n[0];

for(i=1;i<c;i++)
{
if(n[i]>max)
{
max=n[i];
}
}
cout<<"最大值為:"<<max<<endl;
for(i=1;i<c;i++)               // 正確版要自己再另宣告一個變數,不然出現數值會錯亂,但下面暫保留的程式變數一致就沒問題???
{
if(n[i]<min)
{
min=n[i];
}
}
cout<<"最小值為:"<<min<<endl;

system("pause");
return 0;
}

 

 

 

暫保留(正確版)

#include<iostream>
using namespace std;
int main()
{
const int c=4;
int n[c],min,max;
for(int i=0;i<c;i++)
{
cout << "請輸入第" << i+1 << "個數字:";
cin >> n[i];
}
min=n[0];
for(i=1;i<c;i++)
{
if(n[i]<min)
{
min=n[i];
}
}
cout << "最小值為:" <<min<<endl;
for(i=1;i<c;i++)
{
if(n[i]>max)
{
max=n[i];
}
}
cout << "最大值為:" <<max<<endl;
system("pause");
return 0;
}

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

陣列-學生學號_2121119-3  

該題使用陣列及for 

#include<iostream>
using namespace std;
int main()
{
int seat[50];
cout<<"本班學生學號:"<<endl;
for(int i=0;i<50;i++)
{
seat[i]=i+1; //set[0]為1號;seat[1]為2號...
cout<<"10100"<<seat[i]<<"\t";
}
cout<<endl;
system("pause");
return 0;
}

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

陣列是一群性質相同變數的集合
sizeof運算子可以取得整個陣列所佔記憶體的大小,也能取得每個陣列元素記憶體空間的大小


此例為一維陣列宣告
20121119-1

#include<iostream>
using namespace std;

int main()
{
double n[9];
cout<<"n[0]的大小"<<sizeof(n[0])<<endl;
cout<<"int n[5]的大小"<<sizeof(n)<<endl;
cout<<"陣列個數"<<sizeof(n)/sizeof(n[0])<<endl;
system("pause");
return 0;
}



一維陣列儲存資料
陣列-存檔輸入的名字_20121119-2    

#include<iostream>
using namespace std;
int main()
{
string name[2];
cout<<"請輸入第一個好友的名字:";
cin>>name[0];
cout<<"請輸入第二個好友的名字:";
cin>>name[1];
cout<<"請輸入第三個好友的名字:";
cin>>name[2];
cout<<"好友名字:"<<name[0]<<"  " <<name[1]<<"  "<<name[2]<<"  "<<endl;
system("pause");
return 0;
}

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

20121116-3

本題是利用continue跳過4樓
3秀出1 2 3
4秀出1 2 3
5秀出1 2 3 5
以此類推
continue是跳出if的條件值,再往下執行

#include
using namespace std;
int main()
{
int level;
do{
cout << "請輸入樓層數:";
cin >> level;
for(int i=1;i<=level;i++)
{
if(i==4)
{
continue;
}
cout << i <<" ";
}
cout <
}while(level!=1);
system("pause");
return 0;
}

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

20121116-2

#include
using namespace std;
int main()
{
int num;
cout << "請輸入九九乘法表要出現的最大數字:";
cin >> num ;
for(int i=1;i<=9;i++)
{
if(i>num) //已到輸入數值則跳出迴圈
{
break;
}
for(int j=1;j<=9;j++)
{
if(j>num) //已到輸入數值則跳出迴圈
{
break;
}
cout << i <<"*"<< j << "=" <<<" ";
}
cout << endl ;
}
system("pause");
return 0;
}

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

20121116-1

依輸入數值
印出該數值的高度#三角形
高度只到6

本題用到if來判斷"如果已經到了輸入的高度就離開迴圈"
並使用break強制跳出

#include
using namespace std;
int main()
{
int hight;
do{
cout << "請輸入三角形高度(2-6的高度):";
cin >> hight;
for(int i=1;i<=6;i++)
{
if(i>hight) //當輸入的高度大於限制的高度時則強制跳出
{
break;
}
for(int j=1;j<=i;j++)
{
cout << "#" ;
}
cout << endl ;
}
}while(hight!=1);
system("pause");
return 0;
}

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

是否繼續

#include
using namespace std;
int main()
{
int input;
char end;

do{
cout << "請輸入一個整數值:" ;
cin >> input;
if(input%5==0)
{
cout << input <<"為5的倍數"<<endl;
}
else
{
cout << input <<"不是5的倍數"< }
cout << "是否繼續?";
cin >> end;
}while(end!='0');
system("pause");
return 0;
}


但…
輸入2還是可以繼續
這不是我要的結果
我要的是只能輸入1才繼續
其他數值則是一直重覆出現"是否繼續"

經老公指點
做出來啦!
老公太強啦!


20121115-3-1   

#include
using namespace std;
int main()
{
int input;
char end;

do{
cout << "請輸入一個整數值:" ;
cin >> input;
if(input%5==0)
{
cout << input <<"為5的倍數"<<endl;
}
else
{
cout << input <<"不是5的倍數"< }
do{
cout << "是否繼續?";
cin >> end;
}while((end!='0')&&(end!='1')); //如果不是輸入0或1就要繼續跳出"是否繼續"
}while(end!='0'); //因在此加 end!='1',會產生輸入0及1統統跳出(即不再執行迴圈)
system("pause");
return 0;
}

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

do-while與while不同則在於它是先斬後奏
20121115-2

該程式使用到do-while
使用到字串

#include
using namespace std;
int main()
{
string pw; //因為是要輸入字串,所以使用string
do{
cout << "請輸入密碼:" ;
cin >> pw ;
}while(pw!="1234"); //密碼不正確,則再進入迴圈,切記得字串用""括起來
cout << "密碼正確" <
system("pause");
return 0;
}

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

倍數

while主要的用法簡而言之就是迴圈執行次數是未知的狀況下使用
1.沒有初始值
2.執行次數依判斷條件而定
3.在迴圈執行程式中加入遞增量程式

#include
using namespace std;
int main()
{
int i;
while(i<100)
{
if(i%15==0)
{
cout << i << " " ;
}

i++; //遇到第一個15可以整除的,再往下累加
}
cout << endl;
system("pause");
return 0;
}

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

本題為符號交錯印出
使用到if來做判斷

符號交錯印出

#include
using namespace std;
int main()
{
for(int i=1;i<=5;i++)
{
if(i%2==0)
{
for(int j=1;j<=i;j++)
{
cout << "#" ;
}
}
else
{
for(int k=1;k<=i;k++)
cout << "*" ;
}
cout << endl;
}
system("pause");
return 0;
}



本題為
依符號的不同
印出不同的方向

符號列印
#print

#include
using namespace std;
int main()
{
char input;
cout <<"請輸入你要秀出的符號(僅能輸入?及#):";
cin >> input;
if(input=='?')
{
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
cout << input ;
}
cout << endl;
}
}
else if(input=='#')
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
cout << input ;
}
cout << endl;
}
}
else
{
cout << "非設定字元" <
}
system("pause");
return 0;
}

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

for 99乘法表

#include
using namespace std;
int main()
{
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
cout << i <<"*"<< j << "=" << i*j <<" ";
}
cout << endl;
}
system("pause");
return 0;
}

但怎沒法對齊,待修

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

star

#include
using namespace std;
int main()
{
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
cout << "*" ;
}
cout << endl;
}
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
cout << "*" ;
}
cout << endl;
}
system("pause");
return 0;
}


$
#include

using namespace std;
int main()
{
char input;
cout <<"請輸入你要秀出的符號:";
cin >> input;
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
cout << input ;
}
cout << endl;
}
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
cout << input ;
}
cout << endl;
}
system("pause");
return 0;
}

注意:
若為for(;;)
則會形成無窮迴圈


延伸問題待補
印出
*****
++++
***
++
*

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

for add

#include
using namespace std;
int main()
{
int sum=0,money;
for(int i=1;i<=7;i++)
{
cout << "請輸入星期"<< i <<"的支出:";
cin >> money;
sum=sum+money;
}
cout << "本星期的支出為:"<<
system("pause");
return 0;
}

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

case

#include
using namespace std;
int main()
{
int a,b;
char input;
Start:
cout <<"請輸入第一個數值:";
cin >>a;
cout <<"請輸入第二個數值:";
cin >>b;
cout <<"請輸入運算符號";
cin >>input;
switch(input)
{
case '+':
cout << a << input << b << "=" << a+b << endl;
break;
case '-':
cout << a << input << b << "=" << a-b << endl;
break;
case '*':
cout << a << input << b << "=" << a*b << endl;
break;
case '/':
cout << a << input << b << "=" << a/b << endl;
break;
default: //注意default前面不用加case
cout << "無效輸入" << endl;
break;
}
goto Start;
return 0;
}

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

switch


#include
using namespace std;
int main()
{
char grade;
do
{
cout << "請輸入英文等地(A-D):";
cin >> grade;
switch(grade)
{
case 'A':
cout << "你輸入的英文等地為優:"<
break;
case 'B':
cout << "你輸入的英文等地為甲:"<
break;
case 'C':
cout << "你輸入的英文等地為乙:"<
break;
default:
cout << "你輸入的英文等地為丙:"<
break;
}
}while(grade!='0'); //??輸入0跳出丙耶?
system("pause");
return 0;

}

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

SALE0sale

#include
using namespace std;
int main()
{
int money;
do{
cout << "請輸入購買金額:" ;
cin >> money;
if(money>=10000){
cout << "打八折,共為:" << money-(money*0.2)<
else if(money<10000 && money>=5000){
cout << "打八五折,共為" << money-(money*0.15)<
else if(money<5000 && money>=3000){
cout << "打九折,共為" << money-(money*0.1)<
else if(money<3000 && money>=1000){
cout << "打九五折,共為" << money-(money*0.05)<
else if(money<1000){
cout << "無折扣"<
}while(money!=0);
system("pause");
return 0;
}

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

#include

#include

#include

using namespace std;



#define BUF_SIZE 128


int main(void)

{ double score=0, sum=0;

int count = 0;

char buf[BUF_SIZE];


do

{ cout << "請輸入成績:";

cin.getline(buf, BUF_SIZE);


if (buf[0] == 'q' && buf[1] == '\0' || strcmp(buf, "quit")==0)

break;

score = atof(buf);


if (score>=0. && 100.>=score && isdigit(buf[0]))

{ ++count; sum += score; }

} while (score != -1.);



if (count)

printf("%.2f\n", sum/count);



system("pause");


return 0;

}


說明:

大小不限、

可以直接比較兩 string 是否一樣。

但它的缺點太多,所以不建議使用!

因此我用了char 陣列。



char 陣列比較是否相同,要用 strcmp

如果要不管大小寫,用 stricmp

當然,那個 buf[0] == 'q' 也要改成 tolower(buf[0]) == 'q'

在 C,能直接 == 的,只有 primitive type (就是 char, short, int, float, double等)!

在C++,除了上述外,程式可以自己寫 operator overload。

1. include 是插入那個檔案到這個位置。
主要是用來引用別人(含compiler)幫你做好的宣告。
因此,compiler知道你要用的東西長什麼樣子;
不同的程式之間,可以有致的用法。

2.#define BUF_SIZE 128 是定義 BUF_SIZE 這個字為128,
以後,compiler看到這個字,就會幫你換成 128。
因此,萬一你要改成256或其它數值,
只要改一處,就全都改到了。
而且,將來看到這個程式,
不會想不起來:128是怎麼來的?
不會不知道要改哪些地方!
(可能有別的值也剛好是128!所以,不能用搜尋→置換!)
2010-04-17 18:54:07 補充
3. 同2.。這時BUF_SIZE 已被換成128。

4.cin只抓一個值。字串間有空白的話,會被視為二個字串!
 getline(buf, BUF_SIZE);則會整列讀進來。
 BUF_SIZE 這個字很清楚地告訴我們:那裡是暫存區的大小。
 多大? 2. 告訴我們:128
 也就是說:使用者輸入127字以內,都不會出問題。
 第128字起,會不見!

5. strcmp 是標準函式,宣告在string.h裡。
 我竟然漏 include了!
 少數compiler會自動幫你 include 有的沒的!
 其實這不好!
 如果裡面的兩串字一樣,會傳回0;
 如果不同,前大傳回正,小則負。

程式分享原網址:http://tw.knowledge.yahoo.com/question/question?qid=1510041608965

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

BMI

這程式主要學到
1.goto的用法(可做為重覆輸入的語法)
在此取的變數為Start
於是在一開始設定「Start:」結尾設定「goto Start;」

2.多項選擇
if(條件1)
else if(條件2)
else


#include
using namespace std;
main()
{
float kg,m,bmi;
Start:
cout << "請輸入身高(公分)" ;
cin >> m;
cout << "請輸入體重(公斤)" ;
cin >> kg;
m=m/100;
bmi=(int)(kg/(m*m));
cout <<"你的BMI值為"<< bmi <
if(bmi>24){
cout <<"你的體重過重" <
else if (bmi<18){
cout <<"你的體重過輕" <
else{
cout <<"正常範圍" <
goto Start;
system("pause");
return 0;
}

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

007

#include
using namespace std;
int main()
{
int kg,cm;
for( ; ;){
cout <<"請輸入體重(公斤):";
cin >> kg;
cout <<"請輸入身高(公分):";
cin >> cm;
if(kg>(cm-100))//當身高減100後仍小於體重,則為過重
{
cout <<"過重囉!"<
}
else
{
cout <<"標準"<
}
}
system("pause");
return 0;
}

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

1 2