C prime plus 里的例题代码

本文深入探讨了C++中使用cerr进行错误信息输出的技巧,通过实例展示了文件的打开、写入、关闭及内容读取过程。同时,介绍了如何利用switch-case语句进行选择结构编程,以及如何进行格式化输出,包括字符、宽度和精度的控制。

cerr 一般用来输出错误信息

然后打开一个文件 ,写入内容,关闭(保存) ,再打开输出

 


//命令行处理技术 p772
//以下命令打开程序未命名3 

#include<iostream>
#include<cstdlib>
#include<fstream>

int main(int argc,char *argv[])
{
using namespace std;

	if(argc == 1)
	{
		cerr << "Usage: " << argv[0] << " filename[s]\n";//一般cerr 输出错误信息警告使用者
		exit(EXIT_FAILURE);
	}
	
	ifstream fin;
	long count;
	long total = 0;
	char ch;
	for(int file = 1;file < argc; file++)
	{
		fin.open(argv[file]); //connect stream to argv[file]
		if(!fin.is_open())
		{
			cerr << "Could not open " << argv[file] << endl;
			fin.clear();
			continue; 
		}
		count = 0;
	while (fin.get(ch))
	count++;
	cout << count << " characters in " << argv[file] << endl; 
	total+= count;
	fin.clear();
	fin.close();
	} 
	cout << total << " characters in all files\n";
return 0;
}

 

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
	string filename;
	cout << "Enter name for new file: ";
	cin >> filename;
	
	//打开记事本输入内容 
	ofstream fout(filename.c_str());
	fout << "FOR your eyes only!\n";
	cout << "Enter your secret number: ";
	float secret;
	cin >> secret;
	fout << "your secret number is " << secret <<endl;
	fout.close();
	//关闭记事本,并保存内容 
	//再次打开记事本,读取记事本内容 
	ifstream fin(filename.c_str());
	cout << "Here are the contents of " << filename << ":\n";
	char ch;
	while(fin.get(ch))
	cout << ch;
	cout << "Done\n";
	fin.close();
	return 0;
}

  学会 怎么用switch 和 case

搞了好久

#include<iostream>
using namespace std;

int main()
{
    int k;
    char c;
    for(k=1,c='A'; c < 'F'; k++)
    {
        switch(++c)
        {
        case'A': k++; printf("%c %d\n",c,k);
		cout << "a" << endl;
		break;
        case'B': k *= 2; printf("%c %d\n",c,k);
		cout << "b" << endl; 
		break;
		  //跳出switch()执行其后的语句
        case'C': k--; printf("%c %d\n",c,k);
		cout << "c" << endl;           //不论条件为何值,继续执行下一条case判断(case'D':)后面的语句                            
        case'D': k %= 3; printf("%c %d\n",c,k);
		cout << "d" << endl;
		continue; //不执行switch块后面的语句,跳出“本次”循环直接到外层循环
        //所有条件不符合,执行default后面的语句
        }
        k++;
        cout << "*********************\n";
    }
    cout << k << endl;
    return 0;
}

 

 

最最最重要的好

 

#include<iostream>
#include<cmath>
#include<cstring>
#include<iomanip> // 
using namespace std;
int main()
{
   cout << fixed << right;  右对齐 一定不能掉了 
   cout << setw(6) << "N" << setw(14) << "square root" << setw(15) << "fourth root \n";
   double root;
   for(int n = 10; n <= 100; n += 10)
   {
   	root = sqrt(double(n));
   	cout << setw(6) << setfill('.') << n << setfill (' ')    setfill 是用啥填满  setw 是有多少格子  setprecision 是设置输出精度 
   	<< setw(12) << setprecision(3) << root << setw(14) << setprecision(4) <<sqrt(root) << endl;
   }
	return 0;
 } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值