中原951、952︰計算機概論︰上機程式範例
Last edited October 9, 2008
More by 小志 »
最近說的話
下列程式碼僅供參考,如果對於程式碼部分有問題,
或有更好的寫法還是有一些有趣或不懂的題目,
來信至ts101710@gmail.com分享,一同討論學習。
 
我只是有興趣寫而已,所以說程式碼只是我在做題目時,
當下所想出來的方式,可以完成指定的題目,不代表說是最好的寫法。
所以希望如果說有更好的方式寫出來麻煩請來信告訴我,
如果我認為寫得不錯,我也會貼在本版上面的。
  
相關連結:
952課程

 計概上課範例2007/06/07(待續......)
1.鍵盤控制
------------------------------------------------<程式起始>--------------------------------------
------------------------------------------------<程式結尾>--------------------------------------
準備4張圖片放到c槽下 
宣告  
int sp;
pictureBox1:  
於Form1連點兩下後輸入sp=10; 
然後回到設計畫面
將底下程式寫進Form1事件的KeyDown
if(e->KeyCode==::System::Windows::Forms::Keys::Add)
     {
      sp+=10;
      label2->Text=sp.ToString();
     }
     else if(e->KeyCode==::System::Windows::Forms::Keys::Subtract)
     {
      if(sp<=10)
      {
       sp=10;
       label2->Text="10";
      }
      else
      {
       sp-=10;
       label2->Text=sp.ToString();
      }      
     }
     if(e->KeyCode==::System::Windows::Forms::Keys::Down)
     {
      pictureBox1->Top+=sp;
      pictureBox1->Load(String::Concat("c:\\Down.jpg"));
     }
     else if(e->KeyCode==::System::Windows::Forms::Keys::Up)
     {
      pictureBox1->Top-=sp;
      pictureBox1->Load(String::Concat("c:\\Up.jpg"));
     }
     else if(e->KeyCode==::System::Windows::Forms::Keys::Left)
     {
      pictureBox1->Left-=sp;
      pictureBox1->Load(String::Concat("c:\\Left.jpg"));
     }
     else if(e->KeyCode==::System::Windows::Forms::Keys::Right)
     {
      pictureBox1->Left+=sp;
      pictureBox1->Load(String::Concat("c:\\Right.jpg"));
     }
計概上課範例2007/05/31(待續......)
1.程式繪圖練習:手繪直線
------------------------------------------------<程式起始>--------------------------------------
------------------------------------------------<程式結尾>--------------------------------------
宣告  
Graphics^ gf;
int a, b, c, d;
pictureBox1: 
MouseDown 
a=e->X;
b=e->Y;
MouseUp 
c=e->X;
d=e->Y;
gf=pictureBox1->CreatGraphics();
Pen^ myPen=gcnew Pen(Color::Red);
gf->DrawLine(myPen, a, b, c, d); 
 
Button1:
gf->Clear(pictureBox1->BackColor);
計概上課範例2007/05/17
1.事件練習(拖曳按鈕)
------------------------------------------------<程式起始>--------------------------------------
------------------------------------------------<程式結尾>--------------------------------------
宣告  
int btnx, btny, btnw, btnh;
 bool aa;
 
Button1:
MouseDown 
if(e->Button==::System::Windows::Forms::MouseButtons::Left)
      aa=true;
 
MouseUp 
if(e->Button==::System::Windows::Forms::MouseButtons::Left)
      aa=false;
 
MouseMove  
 if(aa)
     {
      btnw=button1->Width;
      btnh=button1->Height;
      btnx=e->X+button1->Left;
      button1->Left=btnx-btnw/2;
      btny=e->Y+button1->Top;
      button1->Top=btny-btnh/2;
      label2->Text=btnh.ToString();
      label3->Text=btnw.ToString();
      label4->Text=btnx.ToString();
      label5->Text=btny.ToString();
     }
計概上課範例2007/04/19(待續......)
1.自由創作(電子書)
------------------------------------------------<程式起始>--------------------------------------
------------------------------------------------<程式結尾>--------------------------------------
gcnew String   =  
 計概上課範例2007/04/13(待續......)
1.自由創作(電子書)
------------------------------------------------<程式起始>--------------------------------------
------------------------------------------------<程式結尾>--------------------------------------pictureBox1->Load(String::Concat(Application::StartupPath,"\\photo\\",j.ToString(),".jpg"));
 
 計概上課範例2007/03/29
1.鍵結串列:輸入5個字元後反向輸出(Win32)
------------------------------------------------<程式起始>--------------------------------------
#include "stdafx.h"
#include"iostream"
using namespace std;
struct DDD
{
    char ch;
    DDD* ptr;
};
void print(DDD* current){
    cout<<current->ch<<endl;
    if(current->ptr!=0){current=current->ptr;
    print(current);
 }
}
void main()
{
    DDD N1,N2,N3,N4,N5;
    DDD *head;
    head=&N5;
    cout<<"請輸入5個字元\n";
    cin>>N1.ch;
    cin>>N2.ch;
    cin>>N3.ch;
    cin>>N4.ch;
    cin>>N5.ch;
    cout<<endl;
    
    N1.ptr=0;
    N2.ptr=&N1;
    N3.ptr=&N2;
    N4.ptr=&N3;
    N5.ptr=&N4;
    print(head);
}
------------------------------------------------<程式結尾>--------------------------------------
 
2.圖片遊覽(Windows Form)
請先準備一個資料夾內含4張圖片檔名分別為1.2.3.4
拖入一個pictureBoxbutton
連點button後將下列程式碼輸入
------------------------------------------------<程式起始>--------------------------------------
static int j=1;
     if(j<=4)
      j++;
     if(j>4)
      j=1;
     pictureBox1->Load(String::Concat("c:\\photo\\",j.ToString(),".jpg"));
------------------------------------------------<程式結尾>--------------------------------------
附註: 
程式碼c:\\photo\\請改為你的資料夾路徑
計概上課範例2007/03/22
1.鍵結串列初探(Win32)
------------------------------------------------<程式起始>--------------------------------------
#include "stdafx.h"
#include"iostream"
using namespace std;
struct DDD
{
    char ch;
    DDD* ptr;
};
void print(DDD* current){
    cout<<current->ch<<endl;
    if(current->ptr!=0){current=current->ptr;
    print(current);
 }
}
void main()
{
    DDD N1,N2,N3;
    DDD *head;
    head=&N1;
    N1.ch='A';
    N2.ch='B';
    N3.ch='C';
    N1.ptr=&N2;
    N2.ptr=&N3;
    N3.ptr=0;
    print(head);
}
------------------------------------------------<程式結尾>--------------------------------------
計概上課範例2007/03/15(整理中......)
1.timer應用(Windows Form)
------------------------------------------------<程式起始>--------------------------------------
------------------------------------------------<程式結尾>--------------------------------------
timer
static int b=0;
     b++;
     label1->Text=b.ToString();
 
button
timer1->Enabled=true;
 
 label
計概上課範例2007/03/08
1.播放音樂(Windows Form)
用上次的作法將在Form1.h[設計]
於視窗畫面上連點兩下後開啟程式編輯頁面(Form1.h)
然後在將下列的程式碼輸入至游標定位點後
方法一:
------------------------------------------------<程式起始>--------------------------------------
String^ strMusic = gcnew String("C:\\WINDOWS\\Media\\ding.wav");
::System::Media::SoundPlayer sndPlayer(strMusic);
sndPlayer.Play();
------------------------------------------------<程式結尾>--------------------------------------
方法二:
------------------------------------------------<程式起始>--------------------------------------
::System::Media::SoundPlayer^ p = gcnew ::System::Media::SoundPlayer();
p->SoundLocation="C:\\WINDOWS\\Media\\ding.wav";
p->Play();
------------------------------------------------<程式結尾>--------------------------------------
方法三:
------------------------------------------------<程式起始>--------------------------------------
::System::Media::SoundPlayer sndPlayer("C:\\WINDOWS\\Media\\ding.wav");
sndPlayer.Play();
------------------------------------------------<程式結尾>--------------------------------------
 
 
2.開啟圖片並顯示(Windows Form)
依上次的作法將在Form1.h[設計]
從工具箱拖曳pictureBoxopenFileDialog還有Button到畫面上
然後連點Button兩下將下列的程式碼打到游標定位點後
於視窗畫面上連點兩下後開啟程式編輯頁面(Form1.h)
然後在將下列的程式碼輸入至游標定位點後
------------------------------------------------<程式起始>--------------------------------------
openFileDialog1->ShowDialog();
pictureBox1->Load(openFileDialog1->FileName);
------------------------------------------------<程式結尾>--------------------------------------
計概上課範例2007/03/01
1.按下按鈕於文字框顯示姓名學號(Windows Form)
首先安裝好Visual C++後新增使用專案
檔案>新增專案>CLR>Windows Form應用程式>(輸入名稱)>確定

然後開始編輯使用介面
我們會用到工具箱屬性
這兩樣會在檢視內可找到
工具箱Button(1個)和TextBox(2個)和Label(2個=>對應TextBox)
拖曳至Form1.h[設計]的介面上
稍微修飾後
連點Button兩下打開程式碼
將下列程式碼輸入 
------------------------------------------------<程式起始>--------------------------------------
textBox1->Text="(學號)"; 
textBox2->Text="(姓名)"; 
------------------------------------------------<程式結尾>--------------------------------------
附註:
Button是按鈕工具
TextBox是文字框
Label是文字
951課程

計概上課範例2007/01/04
1.
------------------------------------------------<程式起始>-------------------------------------- 
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
 if(argc==1)
        cout<<"AAA";
    else if(argc==2)
        cout<<"BBB";
    return 0;
}
------------------------------------------------<程式結尾>--------------------------------------
 
2.
------------------------------------------------<程式起始>-------------------------------------- 
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
 FILE *f;
 int var;
 f=fopen("text.txt","w");
    var=123;
 fprintf(f,"%d",var);
 fclose(f);
}
------------------------------------------------<程式結尾>--------------------------------------
計概上課範例2006/12/28
1.
------------------------------------------------<程式起始>-------------------------------------- 
#include<iostream>
#define m(x,y) x<y?x:y;
using namespace std;
void main()
{
 int a, b, c;
 cout<<"輸入兩值判斷其最小值";
 cin>>a>>b;
 c=m(a, b);
 cout<<c;
 cout<<endl;
}
------------------------------------------------<程式結尾>--------------------------------------
 
2.
------------------------------------------------<程式起始>-------------------------------------- 
#include<iostream>
#define m(x,y) x<y?x:y;
using namespace std;
void main()
{
 int a, b, c;
 cout<<"輸入兩值判斷其最小值";
 cin>>a>>b;
 c=m(a, b);
 cout<<c;
 cout<<endl;
}
------------------------------------------------<程式結尾>--------------------------------------
 
3.
------------------------------------------------<程式起始>-------------------------------------- 
static//重複迴圈時只定義一次
#include<iostream>
using namespace std;
void main()
{
 for(int i=0;i<5;i++)
 {
  static int a=0;
  cout<<a;
  a++;
 }
 cout<<endl;
}
------------------------------------------------<程式結尾>--------------------------------------
 
4.
------------------------------------------------<程式起始>-------------------------------------- 
#include<iostream>
using namespace std;
void main()
{
    int n;
    cin>>n;
    int *ptr=new int [n];
    for(int i=0;i<n;i++)
 {
        cin>>ptr[i];
 }
    cout<<endl;
    for(int i=0;i<n;i++)
 {
        cout<<ptr[i]<<endl;
 }
 delete [] ptr;
}
------------------------------------------------<程式結尾>--------------------------------------
計概上課範例2006/12/21
指標練習
------------------------------------------------<程式起始>-------------------------------------- 
#include<iostream>
using namespace std;
void main()
{
 int a[10]={0,1,2,3,4,5,6,7,8,9};
 int *pa=a;
 cout<<"a="<<a<<endl<<"pa="<<pa<<"\n\n";
 for(int i=0;i<10;i++)
 {
        cout<<"a["<<i<<"]="<<*pa<<endl;
  cout<<"pa["<<i<<"]="<<pa<<endl;
  pa++;
 }
}
------------------------------------------------<程式結尾>--------------------------------------
計概上課範例2006/12/14
指標練習 
1.讀取位置
------------------------------------------------<程式起始>-------------------------------------- 
#include<iostream>
using namespace std;
void main()
{
 double a=5, b[5]={1, 2, 3, 4, 5};
    cout<<a<<endl<<&a<<"\n\n";
 cout<<"element   address    value"<<endl;
 for(int i=0;i<5;i++)
 {
  cout<<" a["<<i<<"] =  "<<&b[i]<<"\t"<<b[i]<<endl;
 }
}
------------------------------------------------<程式結尾>--------------------------------------
 
2.Call by value 與 Call by address比較
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
int add1(int, int);
int add2(int *, int *);
void main()
{
 int i=876, j=678;
 cout<<"i="<<i<<"\tj="<<j<<endl;
 cout<<"i+j="<<add1(i, j)<<"\n\n";
 cout<<"i="<<i<<"\tj="<<j<<endl;
 cout<<"i+j="<<add2(&i, &j)<<endl;//這裡必換行,原因目前不知
 cout<<"i="<<i<<"\tj="<<j<<endl;
}
int add1(int x, int y)
{
 x=x+y;
 return x;
}
int add2(int *x, int*y)
{
 *x=*x+*y;
 *y=-(*y);
 return *x;
}
------------------------------------------------<程式結尾>--------------------------------------
 
3.用指標寫氣泡排序法
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
void add(int *, int *);
void main()
{
 int a[5]={5, 4, 3, 2, 1};
    for(int i=0;i<5;i++)
 {
        for(int j=0;j<5;j++)
  {
   add(&a[j], &a[j+1]);
  }
 }
 
    for(int k=0;k<5;k++)
 {
        cout<<a[k];
 }
 cout<<endl;
}
void add(int *x, int *y)
{
 int *t;
    if(x>y)
        *t=*x,*x=*y,*y=*t;//兩個數值交換用程式碼 
}
------------------------------------------------<程式結尾>--------------------------------------
計概上課範例2006/12/07
1.手動輸入十進位轉為某進位(2~9)
------------------------------------------------<程式起始>--------------------------------------
 
#include<iostream>
using namespace std;
void DECtoBIN(int, int);
int main()
{
 int a, b;
 cout<<"輸入十進位轉幾進位(2~9)";
 cin>>a;
 cout<<"輸入十進位整數數值";
 cin>>b;
  int t[8]={0};
     int i;
     for(i=0;i<8;i++)
  {
         t[i]=b%a;
         b=b/a;
  }
     for(i=7;i>=0;i--)
  {
         cout<<t[i];
  }
     cout<<endl;
}
------------------------------------------------<程式結尾>--------------------------------------
 
2.手動輸入十進位轉為某進位(2~9)(使用副程式)
------------------------------------------------<程式起始>--------------------------------------
 
#include<iostream>
using namespace std;
void DECtoBIN(int, int);
int main()
{
 int a, b;
 cout<<"輸入十進位轉幾進位(2~9)";
 cin>>a;
 cout<<"輸入十進位整數數值";
 cin>>b;
 DECtoBIN(a, b);
}
 void DECtoBIN(int x, int y)
 {
  int b[8]={0};
     int i;
     for(i=0;i<8;i++)
  {
         b[i]=y%x;
         y=y/x;
  }
     for(i=7;i>=0;i--)
  {
         cout<<b[i];
  }
     cout<<endl;
}
------------------------------------------------<程式結尾>--------------------------------------
 
3.氣泡排序(矩陣)
------------------------------------------------<程式起始>--------------------------------------
 
#include<iostream>
using namespace std;
void main()
{
 int a[4]={4, 1, 7, 6}, t;
 for(int i=0;i<4;i++)
 {
        for(int j=0;j<3;j++)
  {
            if(a[j]>a[j+1])
                t=a[j],a[j]=a[j+1],a[j+1]=t;//兩個數值交換用程式碼
  }       
 }
 for(int k=0;k<4;k++)
 {
        cout<<a[k];
 }
    cout<<endl;
}
------------------------------------------------<程式結尾>--------------------------------------
計概上機考範例2006/11/30
不管考不考得好,都還是要複習一下
1.手動輸入學號並在螢幕上輸出且輸出自己的名字。
------------------------------------------------<程式起始>-------------------------------------- 
#include<iostream>
using namespace std;
int main()
{
 int a;
 cout<<"請輸入學號";
 cin>>a;
 cout<<a<<"\t自己的名字\n";
 system("pause");
 return 0;
}
------------------------------------------------<程式結尾>--------------------------------------
 
2.手動輸入公比,然後輸出以首項為1且末項不大於1000的等比級數和。
(因不知道項數所以請勿用公式來計算)
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
 float x, a, t=0;
 cin>>x;
 cout<<endl;
 for(int i=0;;i++)
 {
  a=pow(x,i);
  if(a>1000)
   break;
  cout<<a<<endl;
  t+=a;
 }
 cout<<endl<<"答案為:"<<t<<endl;
 system("pause");
 return 0;
}
------------------------------------------------<程式結尾>--------------------------------------
 
助教教法
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
int main()
{
 int total=1;
 int r, n=1;
 cin>>r;
 while(total<1000)
 {
  n=n*r;
  total=total+n;
 }
 cout<<total<<endl;
}
------------------------------------------------<程式結尾>--------------------------------------
 
3.設計十進位轉二進位的程式
(a)限制條件:十進位範圍(0~255)、二進位範圍(00000000~11111111)
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
int main()
{
 int t[8], a, b;
 cin>>a;
 for(int i=0;i<8;i++)
 {
  t[i]=a%2;
  a/=2;
  b=i;
 }
 cout<<endl;
 for(int j=b;j>=0;j--)
 {
        cout<<t[j];
 }
 cout<<endl;
 return 0;
}
------------------------------------------------<程式結尾>--------------------------------------
 
助教教法
------------------------------------------------<程式起始>--------------------------------------
 #include<iostream>
using namespace std;
int main()
{
 int a;
 cin>>a;
  int b[8]={0};
     int i;
     for(i=0;i<8;i++)
  {
         b[i]=a%2;
         a=a/2;
  }
     for(i=7;i>=0;i--)
  {
         cout<<b[i];
  }
     cout<<endl;
 ------------------------------------------------<程式結尾>--------------------------------------
 
(b)使用副程式完成,副程式型態為DECtoBIN(int n)
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
void DECtoBIN(int);
int main()
{
 int x;
 cin>>x;
 DECtoBIN(x);
    return 0;
}
void DECtoBIN(int n)
{
    int t[8], a=n, b;
     for(int i=0;i<8;i++)
  {
         t[i]=a%2;
         a/=2;
         b=i;
  }
     cout<<endl;
     for(int j=b;j>=0;j--)
  {
         cout<<t[j];
  }
     cout<<endl;
}
------------------------------------------------<程式結尾>--------------------------------------
 
助教教法
------------------------------------------------<程式起始>--------------------------------------
 #include<iostream>
using namespace std;
void DECtoBIN(int);
int main()
{
 int a;
 cin>>a;
 DECtoBIN(a);
}
 void DECtoBIN(int n)
 {
  int b[8]={0};
     int i;
     for(i=0;i<8;i++)
  {
         b[i]=n%2;
         n=n/2;
  }
     for(i=7;i>=0;i--)
  {
         cout<<b[i];
  }
     cout<<endl;
 ------------------------------------------------<程式結尾>--------------------------------------
計概上課範例2006/11/23
1.矩陣、副程式混何應用
------------------------------------------------<程式起始>-------------------------------------- 
#include<iostream>
using namespace std;
void printarray(int [], int);
void main()

    int x[4]={1, 2, 3, 4}, y[5]={4, 3, 1, 2, 7};
 printarray(x,4);
 printarray(y,5);
}
void printarray(int a[], int b)

 for(int i=0;i<b;i++)
 {
        cout<<a[i]<<endl;
 }
 cout<<endl;
}
------------------------------------------------<程式結尾>--------------------------------------
計概期中考程式語言範例2006/11/13

1.顯示下列文字

------------------------------------------------<程式起始>--------------------------------------
 
#include<iostream>
using namespace std;
int main()
{
      cout<<"Merry Christmas &\nHappy New Year"<<endl;
      return 0; 
}
------------------------------------------------<程式結尾>--------------------------------------

2.輸入一個華氏溫度,輸出對應之攝氏溫度。請參閱上課範例2006/10/05
計概上課範例2006/11/09
本堂課的內容比較難,有蠻多同學還不了解如何使用矩陣(基本的可能也......),
所以要寫二維矩陣前請先翻閱前面的文章研究一維矩陣
 
1.二維矩陣轉置(4*3)
------------------------------------------------<程式起始>--------------------------------------
 
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
 int a, b, c, d;
 int s[4][3],e[3][4];
 
 for(a=0;a<4;a++){
  for(b=0;b<3;b++){
            cin>>s[a][b];
  }
 }
 
    cout<<endl;
    for(a=0;a<4;a++){
        for(b=0;b<3;b++){
            cout<<setw(3)<<s[a][b]<<" ";
  }
        cout<<endl;
 }

    cout<<endl;
 
 for(c=0;c<3;c++){
  for(d=0;d<4;d++){
   e[c][d]=s[d][c];
  }
 }
 
 for(a=0;a<3;a++){
        for(b=0;b<4;b++){
            cout<<setw(3)<<e[a][b]<<" ";
  }
        cout<<endl;
 }

    cout<<endl;

     return 0;
}
------------------------------------------------<程式結尾>--------------------------------------
附註:本程式雖然看起來很複雜,其實只是因為二維矩陣需要有兩個變數來控制,
   程式分四大部分,第一部分是輸入數值,第二部份是輸出你輸入的值,
   第三部份是轉置,第四部份是輸出轉置的值。
 
2.二維矩陣轉置(自訂長寬)
------------------------------------------------<程式起始>--------------------------------------
 
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
 int a, b, x, y;
    int s[4][3], e[3][4];
 cout<<"輸入列值";
 cin>>x;
 cout<<"輸入行值";
 cin>>y;
 s[x][y],e[y][x];
 cout<<endl<<"請輸入數值"<<endl;
 for(a=0;a<x;a++){
        for(b=0;b<y;b++){
            cin>>s[a][b];
  }
 }
    cout<<endl<<"輸入的矩陣為"<<endl;
 
    for(a=0;a<x;a++){
        for(b=0;b<y;b++){
            cout<<setw(3)<<s[a][b]<<" ";
  }
     //   if(b=y-1)
            cout<<endl;
 }
    cout<<endl<<"轉置後為"<<endl;
 for(a=0;a<y;a++){
  for(b=0;b<x;b++){
   e[a][b]=s[b][a];
  }
 }
 for(a=0;a<y;a++){
        for(b=0;b<x;b++){
            cout<<setw(3)<<e[a][b]<<" ";
  }
     //   if(b=x-1)
            cout<<endl;
 }
    cout<<endl;
 return 0;
}
------------------------------------------------<程式結尾>--------------------------------------
附註:本程式以上面的程式來修改,只是宣告兩個值作為矩陣的尺寸來自行輸入,
   其他部分沒有做任何太多的修改,請自行研究。
 
計概上課範例2006/11/07
物體從100M自由落體落下.顯示出每秒落下之速度及高度
------------------------------------------------<程式起始>-------------------------------------- 
#include<iostream>
#include<iomanip>//數值定位用的程式庫
using namespace std;
int main()
{
     float t, v, s;
//t為時間,v為速度s為高度
     for(t=1;t<10;t++){
      v=9.8*t;
      s=100-(0.5*9.8*t*t);
      if(s<0)//判斷是否已落地
           break;//跳出迴圈
      cout<<t<<"s => v="<<setw(4)<<v<<"\t s="<<setw(4)<<s<<endl;  
      }
    return 0;
    //system("pause");
}
------------------------------------------------<程式結尾>--------------------------------------
附註:也可以載入#include<cmath>t*t改成pow(t,2)表示t的平方,
寫法改為
s=100-(0.5*9.8*pow(t,2));
pow是次方的函數,可自訂次方次數。
計概上課範例2006/11/02
1.副程式應用
------------------------------------------------<程式起始>--------------------------------------
 
#include<iostream>
using namespace std;
int maximum(int, int);
int minimum(int, int);
int main()
{
 int x, y;
 cout<<"輸入兩個整數求最大值"<<endl;
 cin>>x;
 cin>>y;
 cout<<"最大值為"<<maximum(x, y)<<endl;
 cout<<"輸入兩個整數求最小值"<<endl;
 cin>>x;
 cin>>y;
 cout<<"最小值為"<<minimum(x, y)<<endl;
 return 0;
}
int maximum(int x, int y)
{
 if(x>y)
  return x;
 else
  return y;
}
int minimum(int x, int y)
{
 if(x<y)
  return x;
 else
  return y;
}
------------------------------------------------<程式結尾>--------------------------------------
 附註:輸入兩個值判斷其大小(2次)。
 
2.一維矩陣(陣列)應用
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>                    
using namespace std;
int main()
{
 int p[5], i ,j;
 cout<<"輸入5個整數值"<<endl;
 for(i=0;i<5;i++){
        cin>>p[i];
 }
 cout<<endl;
 for(j=0;j<5;j++){
  cout<<p[j]<<endl;
 }
 return 0;
------------------------------------------------<程式結尾>--------------------------------------
附註:這個程式是輸入五個值後再將這五個值輸出。
計概上課範例2006/10/26
1.Simple Simulator :3+5-2=?
------------------------------------------------<程式起始>-------------------------------------- 
第一行:10 10 11 11 52 01 32 61 14 12 55 24 35 62
第二行:03 05 FE
------------------------------------------------<程式結尾>--------------------------------------
附註:
運算流程是將3+5的值(8)再加上-2(FE)後將答案存至62這個位置,
如想了解詳細的執行方式請直接詢問。
 
1.輸入半徑求圓周長及面積(副程式應用)
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;

float circumference(float);//圓周長函數宣告
float area(float);//圓面積函數宣告

float pi=3.14;//圓周率宣告

int main()
{
     float x, y, z;
     cout<<"輸入半徑:";
     cin>>x;
     y=circumference(x);
     z=area(x);
     cout<<"圓周長為:"<<y<<endl<<"圓面積為:"<<z<<endl;
     return 0;
     //system("pause");
}

float circumference(float a)//圓周長函數
{
    a=pi*2*a;
    return a;
}

float area(float b)//圓面積函數
{
    b=pi*b*b;
    return b;
}
------------------------------------------------<程式結尾>--------------------------------------
內容補充:
一開始做三個宣告,前兩個是副程式的宣告,第三個是圓周率的數值,接下來進入主程式(main)
首先定義三個值,x為半徑、y為圓周長、z為圓面積,輸入完半徑後,
我們將半徑x值送至副程式[circumference(x)]及[area(x)],計算出來後再分別送回(return)x、y再輸出至畫面。
計概上課範例2006/10/19
1.if複習(混合應用)
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
int main()
{
     float x, y, z, a, b, c, i, j, k, l=0, sum, sum2;
     a=200;//物品A價格
     b=300;//物品B價格
     c=500;//物品C價格
     i=0.8;//3000元以上折價
     j=0.85;//2000元以上折價
     k=0.9;//1000元以上折價
     cout<<"物品A:"<<a<<"元"<<endl<<"物品B:"<<b<<"元"<<endl<<"物品C:"<<c<<"元"<<endl;
     cout<<"輸入買A物品的數量:";
     cin>>x;
     cout<<"輸入買B物品的數量:";
     cin>>y;
     cout<<"輸入買C物品的數量:";
     cin>>z;
 
     sum=x*a+y*b+z*c;
     if(sum>=3000)
        sum2=sum*i, l=i, l=(1-l)*100;
     else if(sum>=2000 && sum<3000)
        sum2=sum*j, l=j, l=(1-l)*100;
     else if(sum>=1000 && sum<2000)
        sum2=sum*k, l=k, l=(1-l)*100;
     else if(sum<1000)
        sum2=sum;
 
     cout<<"總價為:"<<sum<<endl<<"你的折扣為:"<<l<<"%"<<endl<<"你的折扣後的價位為:"<<sum2<<endl;
     //system("pause");
     return 0;
}
------------------------------------------------<程式結尾>--------------------------------------
 
2.while(混合應用之三次迴圈)
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
int main()
{
     float x, y, z, a, b, c, i, j, k, l=0, sum, sum2, time;
     a=200;//物品A價格
     b=300;//物品B價格
     c=500;//物品C價格
     i=0.8;//3000元以上折價
     j=0.85;//2000元以上折價
     k=0.9;//1000元以上折價
     cout<<"物品A:"<<a<<"元"<<endl<<"物品B:"<<b<<"元"<<endl<<"物品C:"<<c<<"元"<<endl;
 
     while(time<3){
        cout<<"輸入買A物品的數量:";
        cin>>x;
        cout<<"輸入買B物品的數量:";
        cin>>y;
        cout<<"輸入買C物品的數量:";
        cin>>z;
 
        sum=x*a+y*b+z*c;
        if(sum>=3000)
           sum2=sum*i, l=i, l=(1-l)*100;
        else if(sum>=2000 && sum<3000)
           sum2=sum*j, l=j, l=(1-l)*100;
        else if(sum>=1000 && sum<2000)
           sum2=sum*k, l=k, l=(1-l)*100;
        else if(sum<1000)
           sum2=sum;
 
        cout<<"總價為:"<<sum<<endl<<"你的折扣為:"<<l<<"%"<<endl<<"你的折扣後的價位為:"<<sum2<<endl;
        time++;//計算回圈次數用
        }

     //system("pause");
     return 0;
}
------------------------------------------------<程式結尾>--------------------------------------
 
3.[下次作業]求兩數之最大公因數(輾轉相除法)
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
int main()
{
     int x, y, z=1, a;
     cout<<"請輸入兩個數字求其最大公因數"<<endl<<"請輸入第一個數字";
     cin>>x,
     cout<<"請輸入第二個數字";
     cin>>y;
     cout<<"("<<x<<","<<y<<")=";
     if(x<y)
         a=x,x=y,y=a;
     while(z!=0){
         z=x%y;//求X除以y的餘數
         x=y;
         y=z;
     }
     cout<<x<<endl;
     //system("pause");
     return 0;
}
------------------------------------------------<程式結尾>--------------------------------------
計概上課範例2006/10/12
1.1+2+...+99+100=?
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
int main()
{
     float i, j;
     for(i = 1; i<=100; i++)
     {
            j=j+i;  
     }
     cout<<j<<endl;
     //system("pause");
     return 0;
}
------------------------------------------------<程式結尾>-------------------------------------- 
 
2.列出九九乘法表
------------------------------------------------<程式起始>-------------------------------------- 
#include<iostream>
using namespace std;
int main()
{
      int i, j;
      for(i = 1; i<=9; i++)
      {
       for(j=1; j<=9; j++)
            {
                      cout<<i<<" x "<<j<<" = "<<i*j<<endl;
            } 
       cout<<endl;
       }
       //system("pause");
       return 0;
}
------------------------------------------------<程式結尾>-------------------------------------- 
計概上課範例2006/10/05
1.溫度轉換公式(華氏換攝氏)
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
int main()
{
      float a, b;
      cout<<"請輸入華氏溫度:";
      cin>>a;
      b=(a-32)*5/9;
      cout<<b<<endl;
      return 0;
}
------------------------------------------------<程式結尾>-------------------------------------- 
 
 2.條件判斷(內含判斷程式)
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
int main()
{
      float a;
    cout<<"請輸入你的分數:\n";
    cin>>a;
    for(;a>100 || a<0;)
    {
        cout<<"你輸入錯誤,請重新輸入分數:";
        cin>>a;       
    }

    if(a>=60 && a<=100)
        cout<<"恭喜你及格了!"<<endl;
    else if(a<60)
        cout<<"不及格......"<<endl;
    //system("pause");
    return 0;
}

------------------------------------------------<程式結尾>-------------------------------------- 
 
3.進階條件判斷(內含判斷程式)
------------------------------------------------<程式起始>--------------------------------------
#include<iostream>
using namespace std;
int main()
{
    float a;
    cout<<"請輸入你的分數:\n";
    cin>>a;
    for(;a<0 || a>100;)
    {
        cout<<"你輸入錯誤,請重新輸入分數:";
        cin>>a;
    }

    if(a>=90 && a<=100)
        cout<<"A"<<endl;
    else if(a>=80 && a<90)
        cout<<"B"<<endl;
    else if(a>=70 && a<80)
        cout<<"C"<<endl;
    else if(a>=60 && a<70)
        cout<<"D"<<endl;
    else
        cout<<"E"<<endl;
    //system("pause");
    return 0;
}
------------------------------------------------<程式結尾>-------------------------------------- 
計概作業2006/10/03
1.做出一個波的所有值(Excel)
 

 

a

b

1

time

Volt

2

0

=10*sin(2*3.14159*a2)

3

=a2+1/360

=10*sin(2*3.14159*a2)

 
 
2.做出一個波的所有值(C++)
 ------------------------------------------------<程式起始>--------------------------------------
#include <iostream>
#include <cmath>//數學(三角函數)的程式庫
#include <iomanip>//數值定位用的程式庫
using namespace std;
int main()
{
    int i;
    float f, t, v;
    for(i=0; i<=359; i++)
       {
           t=i/360.0;
           v=10.0*sin(2*3.14159*t);
           cout<<setw(8)<<t<<" "<<v<<endl;
       }
       //system("pause");
       return 0;
}
------------------------------------------------<程式結尾>-------------------------------------- 
計概上課範例2006/09/28
1.做兩個圖片(箭頭.方框)
2.加減乘除計算(2數值)

(以上兩項合併為一個程式碼)


------------------------------------------------<程式起始>--------------------------------------
#include <iostream>
using namespace std;
int main()
{
    //箭頭程式碼
    cout << "   *\n";
    cout << "  ***\n";
    cout << " *****\n";
    cout << "*******\n";
    cout << "   *\n";
    cout << "   *\n";
    cout << "   *\n"<<endl;
    
    //方框程式碼
    cout << "*********\n";
    cout << "*\t"<<"*\n";
    cout << "*\t"<<"*\n";
    cout << "*\t"<<"*\n";
    cout << "*\t"<<"*\n";
    cout << "*********\n"<<endl;
    
    //計算程式碼
    double a, b;
    cout <<"輸入2個數字\n"<<"數字A:";
    cin >>a;
    cout <<"數字B:";
    cin >>b;
    cout <<"a+b="<<a+b<<endl<<"a-b="<<a-b<<endl<<"a*b="<<a*b<<endl<<"a/b="<<a/b<<endl;
    
    return 0;
}
------------------------------------------------<程式結尾>-------------------------------------- 
計概上課範例2006/09/21
1.製作加法程式(2數值加法)
------------------------------------------------<程式起始>--------------------------------------
#include <iostream>
using namespace std;
int main()
{
    short int a, b, c;
    cout << "這是加法程式:\n" <<"請輸入第一個數字\n";
    cin >> a ;
    cout << "請再輸入第二個數字\n";
    cin >>b;
    c = a + b;
    cout << "答案是:"<<c << endl;
    return 0;
}
------------------------------------------------<程式結尾>--------------------------------------
其他

輸入民國年月資料寫出月曆(C)

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int year, month, total, x, z=1, a, i=0, j=1, h=1;
    int day[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   
    printf("請輸入民國年份");   
    scanf("%d",&year);
    printf("請輸入月份");
    scanf("%d",&month);
   
    a=year%4;
    total=365*(year-1)+(year/4);
           
    if(a!=0)
       total+=1;
      
    if(a==1)
       day[1]=29;
              
    for(i=0;i<(month-1);i++)
    {
       total+=day[i];
    }
           
   
    total++;   
    x=total%7;
   
    printf("\n民國 %d 年 %d 月\n",year, month);
    printf(" Sum Mon Tue Wed Thu Fri Sat\n");
    printf("-------------------------------\n");
   
    int t=day[month-1]+1;//判斷天數
   
    for(h=1;h<=x;h++)
    {
       printf("    ");//每月開頭空多少天
    }
   
              
    for(j=1;j<=6&&z!=t;j++)//判斷每月幾週
    {
           for(;x<7&&z!=t;x++,z++)//判斷每週幾天
        {
          if(z<10)
            printf("   ");
          else
            printf("  ");
          printf("%d",z);         
        }
        x=0;
        printf("\n");
    }       
         
    system("PAUSE");
    return 0;
}
輸入民國年月資料寫出月曆(C++)

#include<stdio.h>
#include<stdlib.h>
#include<iomanip>
#include<iostream>
using namespace std;

int main()
{
    int year, month, total, x, z=1, a, i=0, j=1, h=1;
    int day[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   
    cout<<"請輸入民國年份";   
    cin>>year;
    cout<<"請輸入月份";
    cin>>month;
   
    a=year%4;
    total=365*(year-1)+(year/4);
           
    if(a!=0)
       total+1;
    if(a==1)
       day[1]=29;
              
    for(i=0;i<(month-1);i++)
    {
       total+=day[i];
    }
           
   
    total++;   
    x=total%7;
   
    cout<<"民國 "<<year<<" 年 "<<month<<" 月"<<endl;
    cout<<" Sum Mon Tue Wed Thu Fri Sat"<<endl;
    cout<<"-------------------------------"<<endl;
   
    int t=day[month-1]+1;//判斷天數
   
    for(h=1;h<=x;h++)
    {
       cout<<"    ";//每月開頭空多少天
    }
   
              
    for(j=1;j<=6&&z!=t;j++)//判斷每月幾週
    {
           for(;x<7&&z!=t;x++,z++)//判斷每週幾天
        {
          cout<<setw(4)<<z;         
        }
        x=0;
        cout<<endl;
    }       
         
    system("PAUSE");
    return 0;
}
The content on this page is provided by a Google Notebook user, and Google assumes no responsibility for this content.