1. 程式人生 > >C++Primer學習筆記

C++Primer學習筆記

//1.9
#include<iostream>
using namespace std;
int main()
{
	int val = 50,sum = 0;
	while (val <= 100)
	{
		sum += val;
		val++;
	}
	cout << "50+51+..+100=" << sum << endl;
}
//1.10
#include<iostream>
using namespace std;
int main()
{
	int i = 10;
	while (i>=0)
	{
		cout << i << endl;
		i--;
	}
}
//1.11
#include<iostream>
using namespace std;
int main()
{
	int val1,val2;
	cin >> val1 >> val2;
	if (val1 < val2)
	{
		while (val1 <= val2)
		{
			cout << val1<< endl;
			val1++;
		}
	}
	else
	{
		while (val1 >= val2)
		{
			cout << val1 << endl;
			val1--;
		}
	}
}
//1.12
#include<iostream>
using namespace std;
int main()
{
	int i = 10;
	while (i>=0)
	{
		cout << i << endl;
		i--;
	}
}
//1.13.1
#include<iostream>
using namespace std;
int main()
{
	int i,sum = 0;
	for (i = 50;i <= 100;i++)
	{
		sum += i;
	}
	cout << sum << endl;
}
//1.13.2
#include<iostream>
using namespace std;
int main()
{
	int i;
	for (i = 10;i >= 0;i--)
	{
		cout << i << endl;
	}
	
}
//1.13.3
#include<iostream>
using namespace std;
int main()
{
	int val1,val2,i;
	cin >> val1 >> val2;
	if (val1 <= val2)
	{
		for (i = val1;i <= val2;i++)
		{
			cout << i << endl;
		}
	}
	else
	{
		for (i = val2;i <= val1;i++)
		{
			cout << i << endl;
		}
	}
}

1.14 for迴圈適應於迴圈次數已知的情況下,while迴圈適用於迴圈次數無法預知的情況。

//1.16
#include<iostream>
using namespace std;
int main()
{
	int i;
	int sum = 0;
	while (cin >> i)
	{
		sum += i;
	}
	cout << sum << endl;
}
//1.18
#include<iostream>
using namespace std;
int main()
{
	int cv, v;
	if (cin >> cv)
	{
		int count = 1;
		while (cin >> v)
		{
			if (cv == v)
				count++;
			else
			{
				cout << cv << "occurs" << count << "times" << endl;
				cv = v;
				count = 1;
			}
		}
		cout << cv << "occurs" << count << "times" << endl;
	}
	return 0;
}
//1.20
#include<iostream>
#include "test.h"
using namespace std;
int main()
{
	Sales_item book;
	cin >> book;
	cout << "ISBN、售出本數、總銷售額以及平均售價\n" << book << endl;
}
//1.21
#include<iostream>
#include "test.h"
using namespace std;
int main()
{
	Sales_item s1,s2;
	cout << "輸入s1,s2的ISBN、售出本數、平均售價"<<endl;
	cin >> s1>>s2;
	if (compareIsbn(s1, s2))
	{
		cout << "s1,s2的和: " << s1+s2 << endl;
	}
	else
	{
		cout << "ISBN不同" << endl;
	}

}
//1.22
/*這種情況是輸入物件的ISBN必須相等*/
#include<iostream>
#include "test.h"
using namespace std;
int main()
{
	Sales_item s1,s2;
	if (cin >> s1)
	{
		while (cin >> s2)
		{
			if (compareIsbn(s1, s2))
			{
				s1 += s2;
			}
			else
			{
				cout << "ISBN diffrent" << endl;
				return -1;
			}
		}
		cout << "相同ISBN的銷售記錄之和" <<s1<< endl;
	}
	else
	{
		cout << "沒有輸入資料" << endl;
	}

}
//1.23,1.24 每條相同的ISBN要連在一起才正確
#include<iostream>
#include "test.h"
using namespace std;
int main()
{
	Sales_item s1,s2;
	int num = 1;
	cout << "請輸入若干銷售記錄" << endl;
	if (cin >> s1)
	{
		while (cin >> s2)
		{
			if (compareIsbn(s1, s2))
			{
				num++;
			}
			else
			{
				cout << s1.isbn() << "共有" << num << "條銷售記錄" << endl;
				s1 = s2;
				num = 1;
			}
		}
			cout << s1.isbn() << "共有" << num << "條銷售記錄" << endl;
		
	}
	else
	{
		cout << "沒有輸入資料" << endl;
		return -1;
	}
	return 0;

}
//1.25
#include<iostream>
#include "test.h"
using namespace std;
int main()
{
	Sales_item s,t;
	cout << "請輸入ISBN、數量、售價: " << endl;
	if (cin >> t)
	{
		while (cin >> s)
		{
			if (t.isbn()==s.isbn())
			{
				t += s;
			}
			else
			{
				cout << "相同ISBN書的和" << t << endl;
				t = s;
			}
		}
		cout << "相同ISBN書和" << t << endl;
		
	}
	else
	{
		cerr << "沒有輸入資料" << endl;
		return -1;
	}
	return 0;

}