//--《C++捷径教程》读书笔记--Chapter 5--数组和字符串(第二部分)
//--Chapter 5--数组和字符串
//--11/11/2005 Friday
//--Computer Lab
//--Liwei
//--程序#17 二维数组
#include <iostream>
using namespace std;
void f1();
int main()
{
f1();
f1();
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void f1()
{
char s[80]="This is a test./n";
cout<<s;
strcpy(s,"CHANGED./n");
cout<<s;
}
//--程序#18 字符串数组
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int t,i;
char text[100][80];
for(t=0;t<100;t++)
{
//int len;
cout<<t<<": ";
gets(text[t]); //如果没输入而按下enter键时,返回长度为0的字符串(第一个字符是
'/0')
//len=gets(text[t]);
//cout<<"len: "<<len<<endl;
if(!text[t][0])
{
cout<<"empty char is: "<<text[t]<<endl;
break;
}
}
for(i=0;i<t;i++)
cout<<i<<": "<<text[i]<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程序#19 一个简单的雇员数据库程序
#include <iostream>
using namespace std;
char name[10][80];
char phone[10][20];
float hours[10];
float wage[10];
int menu();
void enter(),report();//这么声明函数
int main()
{
int choice;
do{
choice=menu();
switch(choice){
case 0: break;
case 1: enter();
break;
case 2: report();
break;
default:cout<<"Try again./n/n";
}
}while(choice!=0);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
int menu()
{
int choice;
cout<<"0. Quit./n";
cout<<"1. Enter information./n";
cout<<"2. Report information./n";
cout<<"/nChoose one: ";
cin>>choice;
return choice;
}
void enter()
{
int i;
for(i=0;i<10;i++){
cout<<"Enter last name: ";
cin>>name[i];
cout<<"Enter phone number: ";
cin>>phone[i];
cout<<"Enter number of hours worked: ";
cin>>hours[i];
cout<<"Enter wage: ";
cin>>wage[i];
}//for
}
void report()
{
int i;
for(i=0;i<10;i++){
cout<<name[i]<<' '<<phone[i]<<endl;
cout<<"Pay for the week: "<<wage[i]*hours[i]<<endl;
}//for
}