C++语言程序设计(西电版)第4章 数组及自定义数据类型 代码

       当时还没有学函数,所以头文件和main函数只写了一遍,而且循环所用的变量i,j预先定义成全局变量了,后来才看到这么做不是很合适,所以弄得有点乱,不过可能也可以用来参考吧~

       所有代码都包含在main里面分割开了,是每一段放到一起的,中间用*//*分割,需要请自取,每一段都可以独立运行

#include<iostream>
#include<cstring>
#include<iomanip> 
#include<ctime>
#include<stdlib.h>
using namespace std;
int i,j; //用作循环的参数 
int main()
{
    *//* 

{
	int N;
	cout<<"请输入顾客总数\n";
	cin>>N;
	int table[N] = {0};
	cout<<"请输入顾客号和购买额(输入-1结束)\n";
    int n,k = 1;  
    do
	{
    	cin>>n; 
		cin>>table[n]; 
	    k++;
	}
	while(n != -1 && k<=N);
	  
	for(int i = 0; i<N; i++)
	{
		cout<<i<<"\t"<<table[i]<<endl;
	}    
}    
	*//*    
	   
//统计数组所占字节数 
{
	int Array[ ] = {1,2,3};
	cout<<sizeof(Array)<<"\t"; 
	printf("Array=%d\n",sizeof(int));
	
	char chArray[ ] = "Hello,world!";
	cout<<sizeof(chArray)<<"\t";
	printf("chArray=%d",sizeof(int)); 
}

    *//* 

//输入十个学生的成绩,找出最高分以及相应序号(从0开始)
{
	cout<<"请依次输入成绩"<<endl;
	int score[10];
	for(i=1;i<=10;i++)
	{cin>>score[i-1];}
	
	int num=0;
	int max=score[0];
	for(i=0;i<=9;i++)
	{
		if(max<=score[i]) {max=score[i];num=i;}
		cout<<i<<"号 "<<score[i]<<"分  ";
	} 
	cout<<"\n"<<"成绩最高的是"<<num<<"号,分数为"<<max<<endl;
} 

    *//* 
    
//用C的库函数strcpy给字符数组赋值
{
//#include<cstring>
	char str1[ ]=" ",str2[ ]="Hello,";
	strcpy(str1,str2);
	cout<<str1;
	strcpy(str1,"world!");
	cout<<str1;
}

    *//* 
    
//打印矩阵,并转置矩阵 
{
	int Ch[4][4]={0};
	for(i=1;i<=4;i++)
	{for(j=1;j<=4;j++)
	{scanf("%d",&Ch[i-1][j-1]);}}
	for(i=1;i<=4;i++)
	{for(j=1;j<=4;j++)
	{cout<<Ch[j-1][i-1]<<"\t";} cout<<"\n";}} 

    *//*

//用二维数组画图形
{
    char tree[4][4];
    for(i=0;i<4;i++) {for(j=0;j<4;j++) cin>>tree[i][j];}		
    for(i=0;i<4;i++) {for(j=0;j<4;j++) cout<<tree[i][j];cout<<endl; }
}
    
    *//*
    
//用枚举变量表示一星期,并输入n天后是星期几
{
	enum weekday{sun,mon,tue,wed,thu,fri,sat};
	int m,n,p;
	cout<<"今天是星期几(0123456)?要求几天后是星期几?"<<endl;
	cin>>m>>n;
	p=(m+n)%7;
	cout<<n<<"天后是"<<weekday(p);	
} 

    *//* 
    
//结构的声明,变量的定义,初始化与赋值 
//#include<iomanip>
{
	struct student
	{long num; char name[20]; char sex; int age; float score; char addr[30];}; //加分号 
	student s1 = {20041118,"JHT",'M',18,100,"s3217"};
	cout<<setw(8)<<s1.num<<setw(5)<<s1.name<<setw(3)<<s1.sex<<setw(3)<<s1.age<<endl;
	cout<<"size of s1:"<<sizeof(s1)<<"bytes"<<endl; 
	student s2 = s1;
	cout<<s1.num; 
}

    *//*

//访问带有结构体类型的结构成员
{
	struct date{int year; int month; int day;};
	struct weather
	{
		date today;
		double temp;
		double wind;
	};
	weather To_weat = {2010,11,30,10.0,3.1};
	cout<<To_weat.today.year<<"年" 
	    <<To_weat.today.month<<"月" 
	    <<To_weat.today.day<<"日" ;
	cout<<" 温度为"<<To_weat.temp<<"度,风力为"<<To_weat.wind<<"级"<<endl;
 } 

    *//*

//班内成绩、5个班全部50个学生的成绩由高到底排序并输出、计算每个班的平均分和及格率,并按平均分由高到低排序并输出。  
  // int i,j; 
  { int k,score_rand,sum;                                //过程参数 
   struct student{int name;int clas; int num; int score;};  // 生成学生数据 
   student stu[5][10] = {0}; 
   student bridge= {0};
   for(i=0;i<=4;i++)
   {    for(j=0;j<=9;j++)
   	       {
			score_rand = rand()%100;
   	        stu[i][j].name = i*10+j+1;
   	    	stu[i][j].clas = i+1;
   	    	stu[i][j].num = j+1;
   	    	stu[i][j].score =score_rand;   }   }
   	    	
//计算班级平均分和及格率,并按平均分由高到低排序
    struct the_class{int classno;float everage;float percent;};
    the_class cLass[5] ={0}; 
    the_class bridge2 = {0};
    for(i=0;i<=4;i++) 
       {
	    sum = 0; k = 0;
	    for(j=0;j<=9;j++)
    	   {sum+=stu[i][j].score;
            if(stu[i][j].score>=60)k++;}
		cLass[i].classno=i+1;
		cLass[i].everage=sum/10.0;
		cLass[i].percent=k/10.0;
	   }
 
    for(k=0;k<=4;k++)
    {
    	for(i=4;i>0;i--)
    	{if(cLass[i].everage>cLass[i-1].everage)
    		{bridge2 = cLass[i]; 
			 cLass[i] = cLass[i-1];
			 cLass[i-1]= bridge2;}}
	}
    cout<<"班级"<<"\t"<<"平均分"<<"\t"<<"及格率"<<endl;
    for(i=0;i<=4;i++)
    cout<<cLass[i].classno<<"\t"
		<<cLass[i].everage<<"\t"
		<<cLass[i].percent<<endl;
	cout<<"\n"<<endl;
		
//分班级排名输出   	    	
   	for(i=0;i<=4;i++)
       {for(k=0;k<9;k++)
	       {for(j=9;j>0;j--)
   	           {if(stu[i][j].score>stu[i][j-1].score)
			       {bridge = stu[i][j] ; stu[i][j] = stu[i][j-1] ; stu[i][j-1] = bridge; }
			   }  
		   }  
	   }    	
    cout<<"姓名\t班级\t序号\t分数"<<endl; 
	for(i=0;i<=4;i++)
   {    for(j=0;j<=9;j++)
   	    {	k=i+j;
   	    	cout<<stu[i][j].name<<"号"<<"\t"
			    <<stu[i][j].clas<<"\t"
			    <<stu[i][j].num<<"\t"
				<<stu[i][j].score<<endl; 
		}
		cout<<"\n"; 
	}
	cout<<"\n"<<endl; 

//全部学生排号输出 
   	for(k=0;k<52;k++)
       {for(i=4;i>=0;i--)
	       {for(j=9;j>0;j--)
   	           {if(stu[i][j].score>stu[i][j-1].score)
			       {bridge = stu[i][j] ; stu[i][j] = stu[i][j-1] ; stu[i][j-1] = bridge; } }
			if(i-1==-1) break;
			if(stu[i][0].score>stu[i-1][9].score)
			       {bridge = stu[i][0] ; stu[i][0] = stu[i-1][9] ; stu[i-1][9] = bridge;   }  
	       }
	   }
	   	
    cout<<"姓名\t班级\t序号\t分数"<<endl; 
	for(i=0;i<=4;i++)
   {    for(j=0;j<=9;j++)
   	    {	k=i+j;
   	    	cout<<stu[i][j].name<<"号"<<"\t"
			    <<stu[i][j].clas<<"\t"
			    <<stu[i][j].num<<"\t"
				<<stu[i][j].score<<endl; 
		}
   } 
}

    *//*

//100担100马,大马一马三担,中马一马两担,小马两马一担,共多少种选择? 
{
	int lar,med,lit;
	for(lar=0;lar<=33;lar++)
	{
		for(med=0;med<=50;med++)
		{
			lit = (100 - 3 * lar - 2 * med)*2;
			if(lar+med+lit==100&&lit>=0)
			{ 
			cout<<"大马"<<lar<<"头"
			    <<"中马"<<med<<"头"
			    <<"小马"<<lit<<"头"<<endl;} 
		}
	} 
}    

    *//* 

//凯撒密码
{
	
	string character1 = "abcdefghijklmnopqrstuvwxyz",
	       character2 = "ABCDEFGHIGKLMNOPQRSTUVWXYZ"; 
	string s1, s2;
	int key; 
	cout<<"请输入原文字母"<<endl; cin >> s1;   
	cout<<"请输入密码"<<endl;     cin >> key; 
    for (i = 0; i < s1.length(); i++) 
    {
    	for (j = 0; j < 26; j++) 
		{
			if (s1[i] == character1[j])  	
            s2 += character1[(j + key)%26]; 
			if (s1[i] == character2[j])  	
            s2 += character2[(j + key)%26];   	
		}
    }
    cout << s2 << endl; 
} 

    *//*
    
//表示十天后是星期几 
	cout<<"今天是星期(请输入1-7)"<<endl;
	int n=0,m=0;
	cin>>n;
	if(n<0||n>6)
	cout<<"error"<<endl;
	string weekday[7]={"三","四","五","六","日","一","二"};
	cout<<"十天后是星期"<<weekday[n]<<endl; 

   *//*	 

//字符数组的赋值 
	 char array[0];                         //strcpy的头文件是 "#include<cstring>"
	 strcpy(array,"hello"); 
	 cout<<array<<endl;


   *//*	

//输入成绩,查找最高分以及相应序号
	int score[10];
	for(int i=0;i<10;i++)
	cin>>score[i];
	
	int num = 0;
	int max = score[0];
	for(int i=1;i<10;i++)
	 {
	    if(score[i]>max)
		{
		     max=score[i];
			 num=i;	
		}	
	 } 
	cout<<"max="<<max<<"  num="<<num+1<<endl;

    *//* 
	  
//联合体的简单应用
{
	struct person
	{
		int num;
		char name[10];
		char sex;
		char job;
		union
		{
			int classes;
			char position[10];
		};
	}p[2];
	
	
	for(i=0;i<2;i++)
	{
		cout<<"请依次输入编号、姓名、性别f/m、职业t/s:"<<endl;
		cin>>p[i].num>>p[i].name>>p[i].sex>>p[i].job;
		if      (p[i].job == 's')
		{
			cout<<"请输入班号"<<endl;
			cin>>p[i].classes;
		} 
		else if (p[i].job == 't')
		{
			cout<<"请输入职位"<<endl;
			cin>>p[i].position;
		} 
		 
		else   cout<<"input error!"<<endl;
	}
	
	for(i=0;i<2;i++)
	{
	    if     (p[i].job == 's')
		    cout<<p[i].name<<" is a student of Class"<<p[i].classes<<"."<<endl;
		else if(p[i].job == 't')
		    cout<<p[i].name<<" is a teacher of Class"<<p[i].classes<<"."<<endl;
	}
} 

    *//* 

//从键盘输入一行或多行字符串,用字符数组存储,并统计所输入字符串中各字母出现的个数
{
	const int Max_Size = 80;  
	char buffer[Max_Size];
	int counts[256] = {0};
	
	cout<<"enter a string:"<<endl;
    cin.getline(buffer, Max_Size);
    while(buffer[0]!='\0')
    {
    	for (i = 0; buffer[i]!='\0'; i++)   
            counts[buffer[i]]++; 
		cin.getline(buffer, Max_Size); 
	}
	
	cout<<"the statistics result:"<<endl;
	for(i = 0;i<256;i++)
	{
		if(counts[i]>0)
		{cout<<char(i)<<":  "<<counts[i]<<endl;} 
    }
	return 0;	 
} 

    *//*
	
//随机生成矩阵并转置  
{
	const int M = 5, N = 6;
	int mat[M][N];
	int t_mat[N][M];
	
	srand((unsigned int)time(NULL));
	cout<<"Matrix:"<<endl;
	for(i = 0;i<M;i++)
	{
		for(j=0;j<N;j++)
		{
			mat[i][j]=rand()%100;
			cout<<setw(4)<<mat[i][j]<<"\t";
		}
		cout<<endl;
	}
	
	cout<<"transpose of matrix:"<<endl;
		for(i = 0;i<N;i++)
	{
		for(j=0;j<M;j++)
		{
			t_mat[i][j]=mat[j][i];
			cout<<setw(4)<<t_mat[i][j]<<"\t";
		}
		cout<<endl;
	}
	return 0; 
} 

    *//*

//编程计算5阶矩阵求乘积功能
{
	const int N = 5;
	int a[N][N] = {0},b[N][N] = {0},c[N][N] = {0};
	
	srand((unsigned int)time(NULL));
	for(i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
		{
			a[i][j] = rand()%100;
			b[i][j] = rand()%100;
		}
	} 
	
	cout<<"原矩阵分别为:"<<endl;
	for(i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
		{
			cout<<setw(5)<<a[i][j];
		}
		cout<<endl;
	}
	cout<<endl;
	for(i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
		{
			cout<<setw(5)<<b[i][j];
		}
		cout<<endl;
	}
	
	for(i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
		{
			for(int k = 0;k<N;k++)
			c[i][j] += a[i][k] + b[k][j];
		}
	}
	
	cout<<"乘积为"<<endl;
	for(i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
		{
			cout<<setw(5)<<c[i][j];
		}
		cout<<endl;
	}
} 

    *//*
    
//优化冒泡排序
{
	//生成100以内的随机数并显示 
	int Size = 12;
	srand((unsigned int)time(NULL));
	int a[Size] ={0};
	for(i=0;i<Size;i++)
	{
		a[i] = rand()%100;
		cout<<setw(2)<<a[i]<<" ";
	}
	cout<<"\n"<<endl;
	//排序
	for(i=1;i<Size;i++)
	{
		for(j=0;j<Size-i;j++)
		{
			int t1,t2;
			t1=a[j]<=a[j+1]?a[j+1]:a[j];
			t2=a[j]<=a[j+1]?a[j]:a[j+1];
			a[j+1]=t1;
			a[j]=t2;
		}
	}
	//显示 
	for(i=0;i<Size;i++)
        cout<<setw(2)<<a[i]<<" ";
} 
 
    *//* 

//结构数组的输入与输出
{
	struct Employee{char name[20] ; unsigned long id ; float salary;};
	Employee allone[5]={ {"Wang",13916,4490.0},
                    	 {"Zhou",27519,3110.0},
	              	     {"Meng",42876,6230.0},
		              	 {"Yang",23987,4000.0},
	              	   	 {"chen",12335,5110.0} };
	for(i=1;i<5;i++)
	{
	    for(j=0;j<5-i;j++)
		{
			Employee t1,t2;
			t1 = allone[j].salary<allone[j+1].salary?allone[j+1]:allone[j];
			t2 = allone[j].salary<allone[j+1].salary?allone[j]:allone[j+1];
			allone[j+1]=t1;
			allone[j] = t2;
	    }
	}
	for(i=0;i<5;i++)
		cout<<allone[i].name<<"\t"<<allone[i].id<<"\t"<<allone[i].salary<<"\n";
} 

    */

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值