2014年12月7日 星期日

(綠色皮) 最新C++程式語言, 施威銘研究室 著, 旗標 第七章 變數 '參考' 答案

 ==============================================
/* 程式檔名:Test_C7-62-1.cpp */
/* 程式目的:將一維陣列中放入十個整數,並計算每個陣列元素的平方和*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141204 */
   
#include<iostream>
using namespace std;   
#define ab(x) x*x
int main()
{
     int a[10]={1,2,3,4,5,6,7,8,9,10};
     
     for(int i=0;i<10;i++)
     {
      cout << ab(a[i]) << endl;        
     }
     system("PAUSE");
     return 0;    

}
 ==============================================
/* 程式檔名:Test_C7-62-2.cpp */
/* 程式目的:在字元陣列中放入未按順序排列的26個英文字母,然後將字母依照a~z順序從螢幕排列*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141204 */
   
#include<iostream>
using namespace std;   
int main()
{
     char a[26]={'q','w','e','r','t','y','u','i',
                 'o','p','a','s','d','f','g','h',
                 'j','k','l','z','x','c','v','b','n','m'};
     char temp;
     for(int i=0;i<26;i++)
     {
      for(int j=i+1;j<26;j++)
      {
        if(a[i]>a[j])
        {
         temp=a[i];
         a[i]=a[j];
         a[j]=temp;                            
        }
      }        
     }
     for(int i=0;i<26;i++)
     {
       cout << a[i];
     }
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-3.cpp */
/* 程式目的:宣告2個整數型別的一維陣列,其中一個設定初值,將此陣列的值,複製到另外一個陣列*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141204 */
   
#include<iostream>
using namespace std;   
int main()
{
     int a[1]={10};
     int b[1];
     b[0]=a[0];
     cout << b[0] << endl;
     
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-4.cpp */
/* 程式目的:在陣列中存入6個字元密碼,使用者從鍵盤輸入密碼,檢查輸入是否正確,只有三次機會*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141204 */
   
#include<iostream>
using namespace std;   
int main()
{
     char a[]="aaa123";
     char b[6];
     
     int flag=0,i=1;
     do
     {
      cout << "請輸入六個字元密碼:";
      cin >> b;
      if(strcmp(a,b)==0)
      {
       cout << "輸入正確" << endl;
       flag=1;
      }
      else
      {
       cout << "密碼錯誤 " << i++ << " 次" << endl;
       cout << "請重新輸入" << endl;
       if(i>3)
       {
        cout << "密碼錯誤三次,程式結束 " << endl ;
        break;
       }
      }
     }while(flag!=1);
     
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-5.cpp */
/* 程式目的:利用指標把變數i=5的值從螢幕輸出*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;   
int main()
{
     int i=5;
     int *p=&i;
     cout << *p << endl;
     
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-6.cpp */
/* 程式目的:從螢幕輸出變數abcd的位址*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;   
int main()
{
     int a,b,c,d;
     cout << "a = " << &a << endl;
     cout << "b = " << &b << endl;
     cout << "c = " << &c << endl;
     cout << "d = " << &d << endl;
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-7.cpp */
/* 程式目的:將abcd的位址指定給指標*e*f*g*h並從螢幕輸出*e*f*g*h的內容以及位址*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;   
int main()
{
     int a,b,c,d;
     int *e = &a,*f = &b,*g = &c,*h = &d;
     //*f,*g,*h;
     cout << "內容 " << *e << "\t位址 " << e << endl;
     cout << "內容 " << *f << "\t位址 " << f << endl;
     cout << "內容 " << *g << "\t位址 " << g << endl;
     cout << "內容 " << *h << "\t位址 " << h << endl;
     
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-8.cpp */
/* 程式目的:利用指標從螢幕輸出陣列array[5]={2,3,4,5,6}的內容*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;   
int main()
{
     int *p;
     int array[5]={2,3,4,5,6};
     p=array;
     
     for(int i=0;i<5;i++)
     {
      cout << *(p+i);            
     }
     cout << endl; 
     system("PAUSE");
     return 0;    
}

 ==============================================
/* 程式檔名:Test_C7-62-9.cpp */
/* 程式目的:試寫一程式,包含三個函式,排序,總和,最大值三個功能*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;

void Sortingall(int allarray[10]);
int addall(int allarray[10]);
int maxall(int allarray[10]);
 
int main()
{
     int array[10]={45,65,24,49,68,78,45,12,32,40};
     
     int allsum=0,max=0,Sortall[10];
     
     Sortingall(array);
     allsum=addall(array);
     max=maxall(array);
     
     cout << "總和 : " << allsum << endl;
     cout << "最大值 : " << max << endl;
     system("PAUSE");
     return 0;    
}

void Sortingall(int allarray[10])
{
     int temp;
     for(int i=0;i<10;i++)
     {
      for(int j=i+1;j<10;j++)
      {
        if(allarray[i] > allarray[j])
        {
            temp=allarray[i];
            allarray[i]=allarray[j];
            allarray[j]=temp;
        }      
      }     
     }
     cout << "排序 : ";
     for(int i=0;i<10;i++)
     {
      cout << *(allarray+i) << " ";       
     }
     cout << endl;
}

int addall(int allarray[10])
{
     int sum=0;
     for(int i=0;i<10;i++)
     {
      sum=sum+allarray[i];        
     }
     return sum;
}


int maxall(int allarray[10])
{
     int maxall=0;
     for(int i=0;i<10;i++)
     {
      if(allarray[i] > maxall)
      {
       maxall=allarray[i];                      
      }    
     }
     return maxall;
}

 ==============================================
/* 程式檔名:Test_C7-62-10.cpp */
/* 程式目的:用陣列存放十名學生資料國英數三科,並計算出每個學生的總分及平均*/
/* 程式設計:蘇彥儒*/
/* 完成日期:20141205 */
   
#include<iostream>
using namespace std;

void sumstr(int stug[10],int stueng[10],int stumath[10]);
 
int main()
{
     int stug[10],stueng[10],stumath[10];
     
     for(int i=1;i<=10;i++)
     {
      cout << "請輸入第 " << i << "位同學成績" << endl;
      cout << "國文:";
      cin >> stug[i];    
      cout << "英文:";
      cin >> stueng[i]; 
      cout << "數學:";
      cin >> stumath[i];      
     }
     sumstr(stug,stueng,stumath);
     
     system("PAUSE");
     return 0;    
}

void sumstr(int stug[10],int stueng[10],int stumath[10])
{
     int sum=0;
     for(int i=1;i<=10;i++)
     {
      sum=stug[i]+stueng[i]+stumath[i];  
      cout << "第 "<< i << "名總成績:" << sum    
           << "總平均:" << sum/3.0 << endl;   
     }
}



 ==============================================

沒有留言:

張貼留言