《C++捷径教程》读书笔记--Chapter 5--数组和字符串(第一部分)

该博客是《C++捷径教程》第五章数组和字符串的读书笔记,包含多个C++程序示例。有数组的定义、使用rand()函数操作数组、冒泡排序法对数组排序,还有字符串的输入、复制、连接、比较等操作,以及二维数组的使用。

//--《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;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值