六道题okokok

//根据折扣discount,计算并输出应收金额income,找零change. 
//price商品单价,pay收款金额,total商品总价,discount得到折扣,income应收金额,change找零,count购买数量 
#include <stdio.h>
int main(){
	float price,pay,total,discount,income,change;
	int count; 
	printf("请输入商品单价,购买数量,收款金额\n");
	scanf("%f%d%f",&price,&count,&pay);
	total=price*count;
	if(total<1000){
		discount=0; 
	}else if(total<3000){
		discount=0.05;
	}else if(total<6000){
		discount=0.1;
	}else if(total<8000){
		discount=0.15;
	}else{
		discount=0.2;
	}
	income=total*(1-discount);
	change=pay-income;
	printf("应收金额%f,找零%f",income,change); 
}
#include <stdio.h>
int main(){
	int s;
	scanf("%d",&s);
	if(s<=100&&s>=0){
		if(s>=90){
			printf("优"); 
		}else if(s>=80){
			printf("良");
		}else if(s>=70){
			printf("中");
		}else if(s>=60){
			printf("及格");
		}else{
			printf("不及格");
		}
	}else{
		printf("输入不合格"); 
	}
}
#include <stdio.h>
int main(){
	int i,j;
	for(i=1;i<=9;i++){
		for(j=1;j<=i;j++){
			printf("%d*%d=%d ",i,j,i*j);
		}
		printf("\n");
	}
}
#include <stdio.h>
int main(){
	int a,b,c,min;
	scanf("%d%d%d",&a,&b,&c);
	if(a>b){
		min=a;
		a=b;
		b=min;
	}
	if(a>c){
		min=a;
		a=c;
		c=min;
	}
	if(b>c){
		min=b;
		b=c;
		c=min;
	}
	printf("从小到大为%d%d%d",a,b,c);
}  
#include <stdio.h>
int main(){
	int i,j,k;
	for(i=1;i<=9;i++){
		for(j=0;j<=9;j++){
			for(k=0;k<=9;k++){
				if(i*i*i+j*j*j+k*k*k==i*100+j*10+k*1){
					printf("%d\t",i*100+j*10+k*1);
				}
			}
		}
	}
} 
#include <stdio.h>
int main()
{
	int year;
	scanf("%d",&year);
	if(year%4==0||year%100!=0&&year%400==0){
		printf("the year is leapyyear") ;
	} else{
		printf("the year is not leaoyear");
	}
}

#include <stdio.h>
int main()
{
	int year;
	scanf("%d",&year);
	if(year%4==0||year%100!=0&&year%400==0){
		printf("the year is leapyyear") ;
	} else{
		printf("the year is not leaoyear");
	}
}

//根据折扣discount,计算并输出应收金额income,找零change. 
//price商品单价,pay收款金额,total商品总价,discount得到折扣,income应收金额,change找零,count购买数量 
#include <stdio.h>
int main(){
	float price,pay,total,discount,income,change;
	int count; 
	printf("请输入商品单价,购买数量,收款金额\n");
	scanf("%f%d%f",&price,&count,&pay);
	total=price*count;
	if(total<1000){
		discount=0; 
	}else if(total<3000){
		discount=0.05;
	}else if(total<6000){
		discount=0.1;
	}else if(total<8000){
		discount=0.15;
	}else{
		discount=0.2;
	}
	income=total*(1-discount);
	change=pay-income;
	printf("应收金额%f,找零%f",income,change); 
}

#include <stdio.h>
int main(){
	int s;
	scanf("%d",&s);
	if(s<=100&&s>=0){
		if(s>=90){
			printf("优"); 
		}else if(s>=80){
			printf("良");
		}else if(s>=70){
			printf("中");
		}else if(s>=60){
			printf("及格");
		}else{
			printf("不及格");
		}
	}else{
		printf("输入不合格"); 
	}
}

#include <stdio.h>
int main(){
	int i,j;
	for(i=1;i<=9;i++){
		for(j=1;j<=i;j++){
			printf("%d*%d=%d ",i,j,i*j);
		}
		printf("\n");
	}
}

#include <stdio.h>
int main(){
	int a,b,c,min;
	scanf("%d%d%d",&a,&b,&c);
	if(a>b){
		min=a;
		a=b;
		b=min;
	}
	if(a>c){
		min=a;
		a=c;
		c=min;
	}
	if(b>c){
		min=b;
		b=c;
		c=min;
	}
	printf("从小到大为%d%d%d",a,b,c);
} 

 

#include <stdio.h>
int main(){
	int i,j,k;
	for(i=1;i<=9;i++){
		for(j=0;j<=9;j++){
			for(k=0;k<=9;k++){
				if(i*i*i+j*j*j+k*k*k==i*100+j*10+k*1){
					printf("%d\t",i*100+j*10+k*1);
				}
			}
		}
	}
} 

### 自立一 **目重述** 构造第七章自立四所定义实现的二维方阵类 `matrix`,通过重载运算符`<<`来取代原有的`display`函数,以实现矩阵的输出运算。 **详解** 1. 定义一个名为`matrix`的类。 2. 在`matrix`类中重载运算符`<<`。 3. 实现`operator<<`函数,使得可以通过`cout << matrix_object`的方式输出矩阵。 4. 在`main`函数中创建`matrix`类的对象并测试输出。 ```cpp class matrix { public: friend std::ostream& operator<<(std::ostream &out, const matrix &m); // 其他成员函数和变量 }; std::ostream& operator<<(std::ostream &out, const matrix &m) { for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) out << m.data[i][j] << " "; out << std::endl; } return out; } ``` ### 自立二 **目重述** 设有说明:`int i=77`;`double d=8765.56789`;编程序,使用控制输出格式的函数或格式控制符,将整数`i`按照三种不同进制、将实数`d`按照如下四种不同的格式输出到`fout`对象即对应于“f.txt”的text型磁盘文件中。 **详解** 1. 打开文件`f.txt`。 2. 设置输出格式,使用控制符和函数输出`i`的不同进制和`d`的不同格式。 3. 关闭文件。 ```cpp #include <iomanip> #include <fstream> int main() { int i = 77; double d = 8765.56789; std::ofstream fout("f.txt"); fout << "77(10) = " << std::oct << i << "(8) = " << std::hex << i << "(16)" << std::endl; fout << std::fixed << std::setprecision(1) << std::setw(10) << std::left << d << std::endl; fout << std::fixed << std::setprecision(1) << std::setw(10) << std::right << d << std::endl; fout << std::fixed << std::setprecision(5) << d << std::endl; fout << std::scientific << std::setprecision(9) << d << std::endl; fout.close(); return 0; } ``` ### 自立三 **目重述** 利用`get`与`put`成员函数完成文件的拷贝工作。 **详解** 1. 打开源文件和目标文件。 2. 使用`get`函数逐字符读取源文件内容。 3. 使用`put`函数逐字符写入目标文件。 4. 关闭文件。 ```cpp #include <fstream> void main(int argc, char* argv[]) { if (argc != 3) { std::cerr << "Usage: program source_file destination_file" << std::endl; return; } std::ifstream fin(argv[1]); std::ofstream fout(argv[2]); char ch; while (fin.get(ch)) { fout.put(ch); } fin.close(); fout.close(); } ``` ### 自立四 **目重述** 编程序,从键盘输入某个C++源程序文件名,使用`getline`依次读入该文件中的各行,并统计显示出该源程序文件中出现的C++关键字及各关键字出现的次数。 **详解** 1. 定义一个包含关键字的数组。 2. 读取文件并逐行处理。 3. 分割每行内容并与关键字对比。 4. 统计并输出关键字出现次数。 ```cpp #include <fstream> #include <string> #include <map> #include <vector> const std::vector<std::string> keywords = {"int", "double", "if", "else", "while", /* add more */}; int main() { std::string filename; std::cout << "Enter the C++ source file name: "; std::cin >> filename; std::ifstream fin(filename); std::string line; std::map<std::string, int> keyword_count; while (std::getline(fin, line)) { std::istringstream iss(line); std::string word; while (iss >> word) { if (std::find(keywords.begin(), keywords.end(), word) != keywords.end()) { keyword_count[word]++; } } } for (const auto &pair : keyword_count) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; } ``` ### 自立五 **目重述** 键盘输入k个实数,先将它们写入到一个binary型文件`f.bin`中,再使用随机访问方式从后往前读取每一个数据并显示在屏幕上。 **详解** 1. 输入实数并写入`f.bin`。 2. 使用`seekg`定位到文件末尾。 3. 逐步向前读取数据并显示。 ```cpp #include <fstream> #include <vector> int main() { int k; std::cout << "Enter number of real numbers: "; std::cin >> k; std::vector<double> numbers(k); for (int i = 0; i < k; ++i) { std::cin >> numbers[i]; } std::ofstream fout("f.bin", std::ios::binary); for (const auto &num : numbers) { fout.write(reinterpret_cast<const char*>(&num), sizeof(double)); } fout.close(); std::ifstream fin("f.bin", std::ios::binary); fin.seekg(-sizeof(double), std::ios::end); for (int i = 0; i < k; ++i) { double num; fin.read(reinterpret_cast<char*>(&num), sizeof(double)); std::cout << num << " "; if (i != k - 1) fin.seekg(-2 * sizeof(double), std::ios::cur); } return 0; } ``` ### 自立六 **目重述** 设计一个类`CStudent`,类中包含学生的基本数据,并实现对学生数据的随机读写处理等功能。 **详解** 1. 定义`CStudent`类。 2. 实现成员函数:输入、写入文件、随机读取、顺序读取等。 3. 测试类的功能。 ```cpp #include <fstream> #include <string> class CStudent { private: int num; std::string name; char gender; int age; float mathScore, compScore, engScore; public: void input(); void save(std::ofstream &); void load(std::ifstream &); void display() const; }; void CStudent::input() { std::cout << "Enter student info (num name gender age mathScore compScore engScore): "; std::cin >> num >> name >> gender >> age >> mathScore >> compScore >> engScore; } void CStudent::save(std::ofstream &fout) { fout.write(reinterpret_cast<const char*>(this), sizeof(CStudent)); } void CStudent::load(std::ifstream &fin) { fin.read(reinterpret_cast<char*>(this), sizeof(CStudent)); } void CStudent::display() const { std::cout << num << " " << name << " " << gender << " " << age << " " << mathScore << " " << compScore << " " << engScore << std::endl; } int main() { CStudent s; s.input(); std::ofstream fout("students.bin", std::ios::binary | std::ios::app); s.save(fout); fout.close(); std::ifstream fin("students.bin", std::ios::binary); fin.seekg(sizeof(CStudent) * (s.num - 1)); // 定位到特定位置 s.load(fin); s.display(); fin.close(); return 0; } ``` ### 知识点 1. **文件流操作**:包括文件的打开、关闭、读写等基本操作,涉及`<fstream>`库。 2. **格式化输出**:使用`<iomanip>`库设置输出格式。 3. **二进制文件处理**:通过`seekg`、`seekp`进行文件指针定位,`read`、`write`进行二进制读写。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值