1
例4.1
#include<iostream>
using namespace std;
int main()
{
int a[10];
int i;
for(i=0;i<10;i++)
a[i]=i*2+2;
for(i=0;i<10;i++)
{
cout<<a[i]<<'\t';
if((i+1)%5==0)
cout<<endl;
}
return 0;
}
例4.2
#include<iostream>
using namespace std;
int main()
{
int i,math[40],n;
float aver=0.0;
int unpassedcount=0;
int highscorecount=0;
cout<<"请输入学生人数:";
cin>>n;
cout<<"请输入成绩:";
for(i=0;i<n;i++)
{
cin>>math[i];
aver+=math[i];}
aver/=n;
for(i=0;i<n;i++)
{if(math[i]<60)unpassedcount++;
if(math[i]>=90)highscorecount++;
}
cout<<"平均分为:"<<aver<<endl;
cout<<"90分以上的人数为:"<<highscorecount<<endl;
cout<<"不及格的人数为:"<<unpassedcount<<endl;
return 0;
}
4.3
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[10],i,big;
cout<<"please input 10numbers:\n";
for(i=0;i<10;i++)
cin>>a[i];
cout<<"the numbers are:";
for(i=0;i<10;i++)
cout<<setw(4)<<a[i];
cout<<endl;
big=a[0];
for(i=1;i<10;i++)
if(a[i]>big)
big=a[i];
cout<<"the bigmumber is :"<<big<<endl;
return 0;
}
4.4
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[10];
int i,j,t;
cout<<"please input 10numbers:\n";
for(i=0;i<10;i++)
cin>>a[i];
cout<<"the numbers are:";
for(i=0;i<10;i++)
cout<<setw(4)<<a[i];
cout<<endl;
for(i=1;i<9;i++)
for(i=1;j<9-i;j++)
if(a[i]>a[j+1])
{t=a[j];a[j]=a[+1];a[j+1]=t;}
cout<<"the sorted numbers are:";
cout<<endl;
return 0;
}
4.5
#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i;
int f[40]={1,1};
for(i=2;i<40;i++)
f[i]=f[i-2]+f[i-1];
for(i=0;i<40;i++)
{
if(i%4==0)
cout<<endl;
cout<<setw(12)<<f[i];
}cout<<endl;
return 0;
}
4.6
#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j,a[5][5];
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(i%2==0)
a[i][j]=i*5+j+1;
else
a[i][4-j]=i*5+j+1;
for(i=0;i<5;i++)
{ for(j=0;j<5;j++)
cout<<setw(4)<<a[i][j];
cout<<endl;
}
return 0;
}
4.7
#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i, j, t, n, m;
int a[2][3];
cout<<"请输入二维数组的元素值:"<<endl;
for(i = 0;i<2;i++)
{
for(j = 0;j<3;j++)
{
cout<<"a"<<"["<<i<<"]"<<"["<<j<<"] = ";
cin>>a[i][j];
}
}
cout<<"该二维数组为:"<<endl;
t = a[0][0];
for(i = 0;i<2;i++)
{
for(j = 0;j<3;j++)
{
cout<<setw(4)<<a[i][j];
if(a[i][j]>t)
{
t = a[i][j];
n = i, m = j;
}
}
cout<<endl;
}
cout<<"该数组中最大的元素值为:"<<"a"<<"["<<n<<"]"<<"["<<m<<"] = "<<t<<endl;
return 0;
}
4.8
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
char str[50];
cout<<"Please input strings:";
cin.get(str,50);
cout<<"The strings is:";
cout<<str<<endl;
return 0;
}
4.9
#include "stdafx.h"
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[100];
cout<<"请输入一个字符串:";
cin.get(str,100);
cout<<"字符串"<<str<<"的反向字符串为:";
for(int i=strlen(str)-1;i>=0;i--)
cout<<str[i];
cout<<endl;
return 0;
}
4.10
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
char s[]="This is C programming test.";
int i=0,pLen=0,maxpLen = 0, pSeat = 0;
while(s[i]!='\0')
{
while(s[i]!=' '&&s[i]!='\0')
{
pLen++;
i++;
}
if(pLen>maxpLen)
{
pSeat=i-pLen;
maxpLen=pLen;
}
while(s[i]==' ')
i++;
pLen=0;
}
cout <<"最长的单词 :";
for(i=0;i<maxpLen;i++)
cout << s[pSeat+i];
cout <<endl;
return 0;
}
4.11
#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
char str[50];
cout<<"Please input a string:";
cin.get(str,50);
cout<<"The length of string "<<str<<" is "<<strlen(str)<<endl;
return 0;
}
4.12
#include "stdafx.h"
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[10];
cout<<"请输入字符串,直到输入 hello 后程序结束:"<<endl;
do{
cin>>str;
}while(strcmp(str,"hello")!=0);
return 0;
}
4.13
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
char str[50];
int len=0;
cout<<"请输入一个字符串:";
cin.get(str,50);
while(str[len] !='\0')
len++;
cout<<"字符串"<<str<<"的长度"<<len<<endl;
return 0;
}
2.习题
1.
#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n[5],i,j,temp;
cout<<"请输入5个数:\n";
for(i=0;i<5;i++)
cin>>n[i];
for(i=0;i<4;i++)
for(j=0;j<4-1;j++)
if(n[j]<n[j+1])
{temp=n[j];n[j]=n[j+1];n[j+1]=temp;}
cout<<"从大到小分别为: ";
for(i=0;i<5;i++)
cout<<setw(4)<<n[i];
cout<<endl;
return 0;
}
2.
#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n[2][3],j,i;
cout<<"请输入6个数组元素:\n";
for(i=0;i<2;i++)
for(j=0;j<3;j++){
cin>>n[i][j];
}
int row=0,col=0,max=n[0][0];
for(i=0;i<2;i++)
for(j=0;j<3;j++)
if(max<n[i][j]){max=n[i][j];row=i;col=j;}
cout<<"最大的数组元素为: "
<<n[row][col]<<endl;
return 0;
}
3.
#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,sixteenth,count=0,fib[20]={1,1};
for(i=2;i<20;i++){fib[i]=fib[i-2]+fib[i-1];
if(fib[i]>=100 && fib[i]<=999)
count++;}
sixteenth=fib[15];
for(i=0;i<20;i++){if(i>0 && i % 4==0)cout<<endl;
cout<<setw(8)<<fib[i];}
cout<<endl<<endl
<<"fibonacci数列前20个数中3位数的个数为:"<<count<<endl
<<"fibonacci数列中第16项数据为:"<<sixteenth<<endl;
return 0;
}
4.
#include "stdafx.h"
#include<iostream>
#include <iostream>
using namespace std;
int main()
{
int i,j, n, m, k, t;
j = n = m = k = t = 0;
char str[100];
cout<<"请输入一行文字:";
cin.get(str,100);
for(i = 0;i<100&&str[i]!='\0';i++)
{
if(str[i] >= 'A'&&str[i] <= 'Z')
j++;
else if(str[i] >= 'a'&&str[i] <='z')
n++;
else if(str[i] >='0'&&str[i] <= '9')
m++;
else if(str[i] == ' ')
k++;
else
t++; }
cout<<"大写字母有 "<<j<<" 个。"<<endl;
cout<<"小写字母有 "<<n<<" 个。"<<endl;
cout<<"空格有 "<<k<<" 个。"<<endl;
cout<<"数字有 "<<m<<" 个。"<<endl;
cout<<"其他字符有 "<<t<<" 个。"<<endl;
return 0;
}
5.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int i;
char str[100];
cout<<"请输入一个字符串:";
cin.get(str,100);
cout<<"这个字符串"<<str<<"的反向字符串为:";
for(i = strlen(str)-1;i>=0;i--)
{
cout<<str[i];
}
cout<<endl;
cout<<"这个字符串的长度为:"<<strlen(str)<<endl;
return 0;
}
6.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int i, j =0;
char str1[100], str2[100];
cout<<"请输入字符串:";
cin.get(str1,100);
for(i = 0;i<100&&str1[i]!='\0';i++)
{
if(!(str1[i]>='0'&&str1[i]<='9'))
{
str2[j] = str1[i];
j++;
}
}
str2[j+1] = '\0';
cout<<"原字符串:"<<str1<<endl;
cout<<"改动后的字符串:"<<str2<<endl;
cout<<"改动后字符串的长度:"<<j<<endl;
return 0;
}
作业3.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[4][5], b[5][3], c[4][3];
int i, j, k, t = 0;
cout<<"请输入A矩阵[4][5]:"<<endl;
for(i = 0;i<4;i++)
{
for(j = 0;j<5;j++)
{
cout<<"a["<<i<<"]"<<"["<<j<<"] = ";
cin>>a[i][j];
}
}
cout<<"请输入B矩阵[5][3]:"<<endl;
for(i = 0;i<5;i++)
{
for(j = 0;j<3;j++)
{
cout<<"b["<<i<<"]"<<"["<<j<<"] = ";
cin>>b[i][j];
}
}
for(i = 0;i<5;i++)
for(j = 0;j<5;j++)
{
for(k = 0;k<5;k++)
{
t += a[i][k] * b[k][j];
}
c[i][j] = t;
}
cout<<"A矩阵与B矩阵的积为:"<<endl;
for(i = 0;i<4;i++)
{
for(j = 0;j<3;j++)
{
cout<<setw(4)<<c[i][j];
}
cout<<endl;
}
return 0;
}