程序设计与算法三第二单元测验

第一题(函数知识点,代码的复用性):

001:编程填空:学生信息处理程序

总时间限制: 

1000毫秒

内存限制: 

1024KB

// 在此处补充你的代码

描述

实现一个学生信息处理程序,计算一个学生的四年平均成绩。

要求实现一个代表学生的类,类并且所有中成员变量都是【私有的】。

补充下列程序中的学生类以实现上述功能。

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;

class Student {
};

int main() {
	Student student;        // 定义类的对象
	student.input();        // 输入数据
	student.calculate();    // 计算平均成绩
	student.output();       // 输出数据
}

输入

输入数据为一行,包括:
。姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,学年第四平均成绩
其中姓名为由字母状语从句:空格组成的字符串(输入保证姓名不超过20个字符,并且空格不会出现在字符串两端),年龄,学号和学年平均成绩均为非负整数。信息之间用逗号隔开。

输出

输出一行数据,包括:
姓名,年龄,学号,四年平均成绩。
信息之间用逗号隔开。

样例输入

Tom Hanks,18,7817,80,80,90,70

样例输出

Tom Hanks,18,7817,80

提示

必须用类实现,其中所有成员变量都是私有的。
输出结果中,四年平均成绩不一定为整数。

//Dev C++
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;

class Student {
// 在此处补充你的代码
	static const int year=4;//const int只是声明一个常量,
                //static表示该常量是静态变量,只要程序不结束,该变量从声明的地方开始,一直在内存中存在不释放
                //这里year应该始终存在,因此应该是static const
	char name[20];
	int age;
	int num;
	int score[year];//增强程序的可修改性,不要用用s1,s2,s3 
	float ave=0.0;
public:
	Student input(){
		char all[100];
		cin.getline(all,100);
		char *p=strtok(all,",");
		strcpy(name,p);
		p=strtok(NULL,",");
		age=atoi(p);
		p=strtok(NULL,",");
		num=atoi(p);
		for(int i=0;i<4;i++){
			p=strtok(NULL,",");
			score[i]=atoi(p);
		}		
	}
	Student calculate(){
		for(int i=0;i<4;i++){			
			ave+=score[i];
		}
		ave=ave/year;
	}
	Student output(){
		cout<<name<<","<<age<<","<<num<<","<<ave<<endl;
	}
};

int main() {
	Student student;        // 定义类的对象
	student.input();        // 输入数据
	student.calculate();    // 计算平均成绩
	student.output();       // 输出数据
}
//VS2015中strtok,strcpy会报错,解决方案:https://www.cnblogs.com/dmego/p/6065144.html
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;

class Student {
	// 在此处补充你的代码
	static const int year = 4;
	char name[20];
	int age;
	int num;
	int score[year];//增强程序的可修改性,不要用用s1,s2,s3 
	float ave = 0.0;
public:
	void input() {
		char all[100];
		cin.getline(all, 100);
		rsize_t allmax = sizeof all;
		const char *delim = ",";
		char *nextp=NULL;
		char *p = strtok(all,delim);
		strcpy(name, p);
		p = strtok(NULL, delim);
		age = atoi(p);
		p = strtok(NULL, delim);
		num = atoi(p);
		for (int i = 0; i<4; i++) {
			p = strtok(NULL, delim);
			score[i] = atoi(p);
		}
	}
	void calculate() {
		for (int i = 0; i<4; i++) {
			ave += score[i];
		}
		ave = ave / year;
	}
	void output() {
		cout << name << "," << age << "," << num << "," << ave << endl;
	}
};

int main() {
	Student student;        // 定义类的对象
	student.input();        // 输入数据
	student.calculate();    // 计算平均成绩
	student.output();       // 输出数据
	//while (1);
}

知识点:

1,Strtok()函数详解:来自:https://www.cnblogs.com/Bob-tong/p/6610806.html

  该函数包含在"string.h"头文件中 
函数原型:

  1. char* strtok (char* str,constchar* delimiters );

函数功能: 
  切割字符串,将str切分成一个个子串 
函数参数: 
  str:在第一次被调用的时间str是传入需要被切割字符串的首地址;在后面调用的时间传入NULL。 
  delimiters:表示切割字符串(字符串中每个字符都会 当作分割符)。 
函数返回值: 
  当s中的字符查找到末尾时,返回NULL; 
  如果查不到delimiter所标示的字符,则返回当前strtok的字符串的指针。


使用strtok()函数:

#include<stdio.h>
#include<string.h>
int main(void)
{
    char buf[]="hello@boy@this@is@heima";
    char*temp = strtok(buf,"@");
    while(temp)
    {
        printf("%s ",temp);
        temp = strtok(NULL,"@");
    }
    return0;
}

预计输出结果:

  "hello boy this is heima "

实际运行结果: 

得到的结论: 
  strtok在切割字符串的时间,实际上就是将分割符的字符delimiter替换为'\0'并且返回首地址。

2,int atoi(const char *str):把参数 str 所指向的字符串转换为一个整数(类型为 int 型)

 该函数包含在<stdlib.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
   int val;
   char str[20];
   
   strcpy(str, "98993489");
   val = atoi(str);
   printf("字符串值 = %s, 整型值 = %d\n", str, val);

   strcpy(str, "runoob.com");
   val = atoi(str);
   printf("字符串值 = %s, 整型值 = %d\n", str, val);

   return(0);
}

运行结果为:

字符串值 = 98993489, 整型值 = 98993489
字符串值 = runoob.com, 整型值 = 0

3.cin.get()和cin.getline()详解:https://blog.csdn.net/a1015553840/article/details/50579396

(1)cin.getline(arrayname,size)与cin.get(arrayname,size)的区别

用法:cin.get(arrayname,size)  把字符输入到arrayname中,长度不超过size

cin.get(arrayname,size)当遇到[enter]时会结束目前输入,他不会删除缓冲区中的[enter]

cin.getline(arrayname,size)当遇到[enter]时会结束当前输入,但是会删除缓冲区中的[enter]//相对而言安全一些

#include<iostream>
using namespace std;
 
int main()
{
	char a[10];char b;
        cin.get(a,10);
	cin.get(b);
	cout<<a<<endl<<int(b);
}

输入:12345[enter]

输出:12345 【换行】 10

说明:cin.get(a,10)把12345到a中,遇到[enter]结束,但是不把[enter]删除,则把[enter]输入到b中,(enter的ASCALL码为10)

#include<iostream>
using namespace std;
 
int main()
{
	char a[10];char b;
        cin.getline(a,10);
	cin.get(b);
	cout<<a<<endl<<int(b);
}

输入:12345[enter]a[enter]

输出:12345【换行】97

说明:cin.getline(a,10)吧12345输入到a中,遇到{enter}结束,并把缓冲区中[enter]删除,把接下来的a输入到b中

(2)cin.getline(arrayname,size,s)与cin.gei(arrayname,size,s)的区别

用法:cin.get(arrayname,size,s)  把数据输入到arrayname字符数组中,当到达长度size时结束或者遇到字符s时结束

注释:a必须是字符数组,即char a[ ]类型,不可为string类型;size为最大的输入长度;s为控制,遇到s则当前输入结束缓存区里的s将被舍弃

cin.get(arrayname,size,s)当遇到s时会结束输入,但不会删除缓冲区中的s

cin.getline(arrayname,size,s)当遇到s时会结束输入,并把s从缓冲区中删除//相对而言安全一些

#include<iostream>
using namespace std;
 
int main()
{
	char a[10];char b;
        cin.get(a,10,',');
	cin.get(b);
	cout<<a<<endl<<b;
}

输入:12345,[enter]

输出:12345【换行】,

说明:cin,get不会删除缓冲区的

#include<iostream>
using namespace std;
 
int main()
{
	char a[10];char b;
        cin.getline(a,10,',');
	cin.get(b);
	cout<<a<<endl<<int(b);
}

输入:12345,[enter]

输出:12345【换行】10

说明:cin.getline吧12345输入到一个中,遇到 '' 结束,并删除缓冲区的 '',后面的[输入]输入到b中

4.char a[ ]和char *的区别:https://blog.csdn.net/w417950004/article/details/78614455

char* s1=”abc”;s1是一个指针,s1所指向的地址的内容是不可改变的,但是s1可以指向其他地址。s1是指向字符串常量的,它存储在里不可被修改。 
如下:

char* s1="abcd";
s1[2]='z';    //错误:编译时能通过运行的时候会报错
s1="xyz";     //可以将指针指向其他内容
cout<<s1[2]<<endl;

char s2[] =”cdef”;是一个数组,s2指向第一个元素所在的位置,一经分配就不能更改。 它的空间是则栈里分配的,可以被重新修改,s2中内容可以修改,但是s2不能够再指向其他空间。 
如下:

char s2[]="efgh";
s2="xyz";        //出错:s2不可以再指向其他内容
cout<<s2[2]<<endl;  // s2中的元素是可以被修改的

若是将指针指向一个数组,那么这个数组即可以被改变元素值又可以被指向其他字符串。如下:

char *p=s2;
p[0]='x';   //可以改变元素值
p="rty";    //可以指向其他字符串
cout<<p;

这种既能改变元素值又能重新指向其他字符串的方式与string定义的字符串功能很相似。

第二题核心:

Sample b = a;会调用复制构造函数

第三题:

003:超简单的复数类

描述

下面程序的输出是:

3 + 4I 
5+6i

请补足复杂类的成员函数。不能加成员变量。

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex {
private:
    double r,i;
public:
    void Print() {
        cout << r << "+" << i << "i" << endl;
    }
// 在此处补充你的代码
};
int main() {
    Complex a;
    a = "3+4i"; a.Print();
    a = "5+6i"; a.Print();
    return 0;
}

输入

输出

3 + 4i
5 + 6i

样例输入

样例输出

3+4i
5+6i
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex {
private:
	double r, i;
public:
	void Print() {
		cout << r << "+" << i << "i" << endl;
	}
	Complex(const char a[10]) {//一定要有const不然编译出错
		char b[10];
		strcpy(b, a);
		char *p=strtok(b,"+");
		r = atof(p);//atof() 用于将字符串转换为双精度浮点数(double)
		p = strtok(NULL, "i");
		i = atof(p);
	}
	Complex(){}
};
int main() {
	Complex a;
	a = "3+4i"; a.Print();
	a = "5+6i"; a.Print();
	//getchar();
	return 0;
}

的atoi:将字符串转换为整数(INT)

ATOF:将字符串转换为双精度浮点数(双)

蒂:将字符串转换成长(长整型)

第四题:要点为析构函数

猜你喜欢

转载自blog.csdn.net/qq_31647835/article/details/81178303