ETH官方钱包

切換
舊版
前往
大廳
主題

C++上課筆記(5)

查理 | 2013-12-12 19:53:59 | 巴幣 0 | 人氣 250

#include <iostream>
using namespace std;
int main()
{
char c[20];
cout<<"input string:";
cin>>c;
cout<<c<<endl;

system("pause");
return 0;
}
------------------------------------------------------------------------------------------
字串長度
strlen(字串)

字串比較
strcmp(字串1,字串2)

#include <iostream>
using namespace std;
int main()
{
    char str1[]={'d','e','v','_','c','+','+','\0'};
    char str2[]="dEv_c++";

    cout<<str1<<endl;
    cout<<str2<<endl;
    
    int i;
    for(i=0;i<strlen(str1);i++)
       cout<<str1[i]<<endl;
    
    cout<<strcmp(str1,str2)<<endl;
    if( strcmp(str1,str2)==0)
       cout<<"相同"<<endl;
    else
       cout<<"不相同"<<endl;
        
//    if(str1==str2)

    system("pause");
    return 0;
}
-------------------------------------------------------------------------------------------------------
小寫轉大寫

#include <iostream>
using namespace std;

int main()
{
    char str[20];
    cout<<"input string";
    cin>>str;
    int i=0;
    while(str[i]!='\0')
    {
       if(str[i]>=97&&str[i]<=122)
          str[i]-=32;
       i++;
    }
    cout<<str<<endl;

system("pause");
return 0;
}
-------------------------------------------------------------------------------------------------
反向列印

#include <iostream>
using namespace std;

int main()
{
    char str[20];
    cout<<"input string";
    cin>>str;
    int i;
    for(i=strlen(str)-1;i>=0;i--)
       cout<<str[i];
    cout<<endl;

system("pause");
return 0;
}
-------------------------------------------------------------------------------------------------
輸入密碼  正確列印welcome 錯誤列印 error 有三次機會

#include <iostream>
using namespace std;

int main()
{
    char pwd[]="abc123";
    char pass[20];
    int i;
    for(i=1;i<=3;i++)
    {
    cout<<"輸入密碼:";
    cin>>pass;
    if(strcmp(pwd,pass)==0)
    {
      cout<<"welcome"<<endl;
       break;
    }
     else
    {
       cout<<"error"<<endl;
    }
    }
system("pause");
return 0;
}
--------------------------------------------------------------------------------------
二維陣列

#include <iostream>
using namespace std;
int main()
{
   char a[3][4];
   int i,j;
   for(i=0;i<3;i++)
   {
     for(j=0;j<4;j++)
     {
        a[i][j]=i+j;
        cout<<a[i][j]<<" ";
     }
     cout<<endl;
     
   }

   system("pause");
   return 0;
}
------------------------------------------------------------------------
二維陣列相加

#include <iostream>
using namespace std;
int main()
{
   int a[][3]={{2,6,1},{-1,-2,5},{3,5,4}};
   int b[3][3]={{-1,-3,3},{0,5,-1},{1,-3,-2}};  
   int c[3][3];   
   int i,j;
   for(i=0;i<3;i++)
   {
      for(j=0;j<3;j++)
      {
         c[i][j]=a[i][j]+b[i][j];
         cout<<c[i][j]<<" ";
      }
      cout<<endl;
   }            

   system("pause");
   return 0;
}
-------------------------------------------------------------
陣列購買課程

#include <iostream>
using namespace std;
int main()
{
    char acc[][20]={"andy","sen","charlie","tom};
    int point[4]={30,60,45,100}
    int course[4]={0,50,100,40};
    char user[20];
    cout<<"帳號:";
    cin>>user;
    
    int i,item;
    for(i=0;i<4;i++)
    {
       if(strcmp(user,acc[i])==0)
       {                         
         cout<<"1.Java50點"<<endl;
         cout<<"2.Android 100點"<<endl;
         cout<<"3.C++ 40點"<<endl;
         cout<<"請選擇:";
         cin>>item;
         if(point[i]>=course[item])
         {
            point[i]-=course[item];
            cout<<"成功,你還有"<<point[i]<<"點"<<endl;
         }
         else
            cout<<"點數不足,你只有"<<point[i]<<"點"<<endl;
         break;
        }      
     }
     if( i==4 )
       cout<<"找不到帳號"<<endl;

    system("pause");
    return 0;
}

創作回應

更多創作