现在的位置: 首页 > 综合 > 正文

第5周作业

2014年11月11日 ⁄ 综合 ⁄ 共 7363字 ⁄ 字号 评论关闭

例4.1

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#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

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#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

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int a[10], i, big;
	cout<<"Please input 10 number:\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 = 0;i<10;i++)
		if(a[i]>big)
			big = a[i];
	cout<<"the big number is: "<<big<<endl;

	return 0;
}

 

例4.4

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int a[10];
	int i, j, t;
	cout<<"Please input 10 number:\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 = 0;i<10;i++)
		for(j = 0;j<9-i;j++)
			if(a[j]>a[j+1])
			{
				t = a[j], a[j] =  a[j+1], a[j+1] = t;
			}
	cout<<"the sorted numbers are: ";
	for(i = 0;i<10;i++)
		cout<<setw(4)<<a[i];
	cout<<endl;

	return 0;
}

 

例4.5

// 1111.cpp : Defines the entry point for the console application.
//

#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

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int i;
	int j;
	int 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

// 1111.cpp : Defines the entry point for the console application.
//

#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

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	char str[50];
	cout<<"Please input string: ";
	cin.get (str,50);
	cout<<"The string is: ";
	cout<<str<<endl;

	return 0;
}

 

例4.9

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	char str[100];
	cout<<"Please input string: ";
	cin.get (str,100);
	cout<<"字符串"<<str<<"的反向字符串为:";
	for(int i = strlen(str)-1;i>=0;i--)
		cout<<str[i];
	cout<<endl;

	return 0;
}

 

例4.10

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	char s[] = "The is C programming test.";
	int i = 0, pLen = 0, maxLen = 0, pSeat = 0;
	while(s[i]!='\0')
	{
		while(s[i]!=' '&&s[i]!='\0')
		{
			pLen++;
			i++;
		}
		if(pLen>maxLen)
		{
			pSeat = i - pLen;
			maxLen = pLen;
		}
		while(s[i] == ' ')
			i++;
		pLen = 0;
	}
	cout<<"最长的单词为: ";
	for(i = 0;i<maxLen;i++)
		cout<<s[pSeat+i];
	cout<<endl;

	return 0;
}

 

例4.11

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	char str[50];
	cout<<"Please input a string:";
	cin.get (str,50);
	cout<<"The length of sring"<<str<<"is"<<strlen(str)<<endl;

	return 0;
}

 

例4.12

// 1111.cpp : Defines the entry point for the console application.
//

#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

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	char str[50];
	int len = 0;
	cout<<"请输入一个字符串: "<<endl;
	cin.get (str,50);
	while(str[len]!='\0')
		len++;
	cout<<"字符串"<<str<<"的长度为:"<<len<<endl;
	
	return 0;
}

 

习题:

1.

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int a[5];
	int i,j,t;
	cout<<"请输入5个数字:";
	for(i=0;i<5;i++)
		cin>>a[i];
	for(i=0;i<4;i++)
	for(j=0;j<4-i;j++)
		if(a[j]>a[j+1])
		{
			t=a[j];
			a[j]=a[j+1];
			a[j+1]=t;
		}
			cout<<"这些数字的正确排序是:";
			for(i=0;i<5;i++)
				cout<<setw(3)<<a[i];
			cout<<endl;
			return 0;
}

 

2.

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int a[2][3];
	int i,j,big;
	cout<<"请输入2行3列的数字值:"<<endl;
	for(i=0;i<2;i++)
	for(j=0;j<3;j++)
	{
		cout<<"a["<<i<<"]"<<"["<<j<<"]=";
		cin>>a[i][j];
	}
	cout<<"这个二维数组是:"<<endl;
	for(i=0;i<2;i++)
	{
		for(j=0;j<3;j++)
	        cout<<setw(4)<<a[i][j];
		cout<<endl;
	}
    big=a[0][0];
	for(i=0;i<2;i++)
	{
		for(j=0;j<3;j++)
			if(a[i][j]>big)
				big=a[i][j];
	}
	for(i=0;i<2;i++)
    for(j=0;j<3;j++)
			if(a[i][j]==big)
	cout<<"该数组的最大值为:"<<"a["<<i<<"]"<<"["<<j<<"]="<<a[i][j]<<endl;
	return 0;
}

 

3.

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;
int function(int n);
int main()
{
    int i,count;
	count=0;
    for(i=1;i<=20;i++)
	{
		cout<<function(i)<<" ";
        cout<<endl;
		if(function(i)>=100&&function(i)<=999)
			count++;
    }
	 cout<<"Fibonacci数列前20个数中的三位数个数为:"<<count<<endl;
	 cout<<"该数列第16项数据是:"<<function(16)<<endl;
    
	 return 0;    
}
int function(int n)
{
   if(n==1||n==2)return 1;
   else return function(n-1)+function(n-2);
}

 

4.

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{	
	
	char a[100];
	int i,xiaoxue=0,shuzi=0,daxue=0,kongge=0,qita=0;
	cout<<"请输入一行字符小于100的文字:";
    cin.get(a,100);
    for(i=0;i<100;i++)
    {
	 if(a[i]>='a'&&a[i]<='z')
     xiaoxue++;
     else if
     (a[i]>='0'&&a[i]<='9')   //因为a被定义为char,,所以应该写成'0' '9'
     shuzi++;
     else if
     (a[i]>='A'&&a[i]<='Z')
     daxue++;
     else if
     (a[i]==' ')
     kongge++;
     else if
	 (a[i]=='\0')
	 qita=strlen(a)-(shuzi+xiaoxue+daxue+kongge);
	}
     cout<<"数字个数为:"<<shuzi<<endl;
     cout<<"小写字母个数为:"<<xiaoxue<<endl;
     cout<<"大写字母个数为:"<<daxue<<endl;
     cout<<"空格个数为:"<<kongge<<endl;
     cout<<"其他字符个数为:"<<qita<<endl;
     return 0;
}

 

5.

// 1111.cpp : Defines the entry point for the console application.
//

#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;
    cout<<"此字符串的长度为:"<<strlen(str)<<endl;
	return 0;
}

 

6.

// 1111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	char i,str[100],count=0;
	cout<<"请输入一个字符串:";
	cin.get(str,100);
	cout<<"删除数字后的字符串为:";
	for(i=0;i<100;i++)
	{
		if(str[i]=='\0')
			break;
		else if(str[i]>='0'&&str[i]<='9')
		{
			cout<<"\0";
		count++;
		}
			else cout<<str[i];
	}
	cout<<endl;
		cout<<"此字符串的长度为:"<<strlen(str)-count<<endl;
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

【上篇】
【下篇】

抱歉!评论已关闭.