//--《C++捷徑教程》讀書筆記--Chapter 5--數組和字串(第一部分)
//--Chapter 5--數組和字串
//--11/10/2005 Thurs.
//--Computer Lab
//--Liwei
//--程式#1 數組
#include <iostream>
using namespace std;
int main()
{
int sample[10];
int t;
for(t=0; t<10; t++)
sample[t]=t;
for(t=0; t<10; t++)
cout<<sample[t]<<" ";
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#2 數組 rand()使用
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i,min_value,max_value;
int list[10];
for(i=0;i<10;i++)
list[i]=rand();
cout<<endl<<"======================="<<endl;
for(i=0;i<10;i++)
cout<<list[i]<<' ';
cout<<endl;
min_value=list[0];
for(i=1;i<10;i++)
if(min_value>list[i])
min_value=list[i];
cout<<"minimum value: "<<min_value<<endl;
max_value=list[0];
for(i=1;i<10;i++)
if(max_value<list[i])
max_value=list[i];
cout<<"maximum value: "<<max_value<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#3 一個不正確的程式
#include <iostream>
using namespace std;
int main()
{
int crash[10],i;
for(i=0; i<150; i++)
crash[i]=i;
for(i=0; i<150; i++)
cout<<crash[i]<<' ';
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#4 用冒泡排序法對數組排序
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int nums[100];
int a,b,t;
int size;
size=100;
for(t=0; t<size; t++)
nums[t]=rand();
cout<<"Original array is: /n";
for(t=0; t<size; t++)
cout<<nums[t]<<' ';
cout<<endl<<"======================="<<endl;
for(a=1; a<size; a++)//遍曆size-1次
for(b=size-1; b>=a; b--)//每次找最小值的遍曆次數
{
if(nums[b-1]>nums[b])
{
t=nums[b-1];
nums[b-1]=nums[b];
nums[b]=t;
}
}
cout<<"Sorted array is: /n";
for(t=0; t<size; t++)
cout<<nums[t]<<' ';
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#5 使用cin從鍵盤讀入字串
#include <iostream>
using namespace std;
int main()
{
char str[80];
cout<<"Enter a string: ";
cin>>str;
cout<<"Here is your string: ";
cout<<str;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#6 使用gets()從鍵盤讀入字串
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
cout<<"Enter a string: ";
gets(str);
cout<<"Here is your string: ";
cout<<str;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#7 strcpy()
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[80];
strcpy(str,"hello!");
cout<<str;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#8 strcat()
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[20],s2[10];
strcpy(s1,"hello");
strcpy(s2,"there");
strcat(s1,s2);
cout<<s1<<" "<<s2;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#9 strcmp()
#include <iostream>
#include <cstring>
using namespace std;
bool password();
int main()
{
if(password())
cout<<"Logged on./n";
else
cout<<"Access denied./n";
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
bool password()
{
char s[80];
cout<<"Enter password: ";
gets(s);
if(strcmp(s,"password"))
{
cout<<"Invalid password./n";
return false;
}
return true;
}
//--程式#10 strcmp()
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
char s[80];
for(;;)
{
cout<<"Enter a string: ";
gets(s);
if(!strcmp("quit",s)) break;
}
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#11 strlen()
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
cout<<"Enter a string: ";
gets(str);
cout<<"Length is: "<<strlen(str);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#12 反向輸字串
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
int i;
cout<<"Enter a string: ";
gets(str);
for(i=strlen(str)-1; i>=0; i--)
cout<<str[i];
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#13 4個字串處理函數
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
char s1[80],s2[80];
cout<<"Enter two strings: ";
gets(s1);
gets(s2);
cout<<"lengths: "<<strlen(s1)<<' '<<strlen(s2)<<endl;
if(!strcmp(s1,s2))
cout<<"The strings are equal./n";
else
cout<<"not equal./n";
strcat(s1,s2);
cout<<"s1: "<<s1<<' '<<"s2: "<<s2<<endl;
strcpy(s1,s2);
cout<<s1<<" and "<<s2<<' '<<"are now the same./n";
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#14 將一個字串轉換為大寫字元
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
char str[80];
int i;
strcpy(str,"this is a test.abcdefghijk....");
for(i=0; str[i]; i++)
str[i]=toupper(str[i]);
cout<<str;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#15 二維數組
#include <iostream>
using namespace std;
int main()
{
int t,i,num[3][4];
for(t=0;t<3;t++)
{
for(i=0;i<4;i++)
{
num[t][i]=t*4+i+1;
cout<<num[t][i]<<' ';
}
cout<<endl;
}
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#16 二維數組
#include <iostream>
using namespace std;
int sqrs[10][2]={
{1,1},
{2,4},
{3,9},
{4,16},
{5,25},
{6,36},
{7,49},
{8,64},
{9,81},
{10,100},
};
int main()
{
int i,j;
cout<<"Enter a number between 1 to 10: ";
cin>>i;
for(j=0;j<10;j++)
{
if(sqrs[j][0]==i)
{cout<<"The square of "<<i<<" is "<< sqrs[j][1];
break;
}
}
cout<<"/nThe square of "<<i<<" is "<< sqrs[j][1];
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}