哎,最近好无聊。 找实习啊,找实习。 该找个怎么样的实习呢。 继续码作业去。 我这里都快成了课后答案博客了,哎。 谁叫我是一个分享达人呢。 碰我我,请叫我雷锋! 哈哈哈
专注游戏服务器开发
哎,最近好无聊。 找实习啊,找实习。 该找个怎么样的实习呢。 继续码作业去。 我这里都快成了课后答案博客了,哎。 谁叫我是一个分享达人呢。 碰我我,请叫我雷锋! 哈哈哈
4.1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c4._1
{
class Program
{
static void Main(string[] args)
{
Program test = new Program();
double [] height = new double[10]{156, 150, 167, 178, 180, 176, 173, 154, 155, 158 };
Console.WriteLine(“学生身高如下”);
test.show(height);
double max = 0.0;
double min = 0.0;//C#不允许未初始化的变量!
test.maxWithMin(height,ref max,ref min);//必须用引用传递
double avg=test.avg(height);
Console.Write(“平均身高={0},最高身高={1},最低身高={2}”, max, min, avg);
int n = test.heigher(height, avg);
Console.WriteLine(“\n高于平均身高的学生人数={0}”, n);
Console.ReadKey();
}
public void show(double[] arr)
{
foreach (double i in arr)
{
Console.Write(“ {0}”, i);
}
Console.WriteLine();
}
public void maxWithMin(double \[\]arr,ref double min,ref double max)
{
max=arr\[0\];
min=arr\[0\];
//int size = arr.Length;
for (int i = 0; i < arr.Length; i++)
{
if (max < arr\[i\])
max = arr\[i\];
if (min > arr\[i\])
min = arr\[i\];
}
}
public double avg(double\[\] arr)
{
double average;
double sum=0;
for(int i=0;i<arr.GetLength(0);i++)
sum+=arr\[i\];
average=sum\*1.0/arr.GetLength(0);
return average;
}
public int heigher(double\[\] arr, double avg)
{
int count = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr\[i\] > avg)
count++;
}
return count;
}
}
}
4.2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c4._2
{
class Program
{
static void Main(string[] args)
{
int []score = new int[]{ 80, 90, 67, 89, 78, 85, 45, 69, 77, 95 };
(new Program()).show(score);
Console.ReadKey();
}
public void show(int[] score)
{
int bad = 0;
int min = 0;
int good = 0;
int well=0;//90-100
foreach (int i in score)
{
if (i>=90)
well++;
else if(i>=80)
good++;
else if(i>=60)
min++;
else
bad++;
}
int size=score.Length;
Console.WriteLine(“优(90-100)分数段的学生人数={0},所占百分比={1}%”,well,well*100.0/size);
Console.WriteLine(“良(80-89)分数段的学生人数={0},所占百分比={1}%”,good,good*100.0/size);
Console.WriteLine(“中(60-79)分数段的学生人数={0},所占百分比={1}%”,min,min*100.0/size);
Console.WriteLine(“差(0-59)分数段的学生人数={0},所占百分比={1}%”,bad,bad*100.0/size);
}
}
}
4.3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c4._3
{
class Program
{
static void Main(string[] args)
{
int[] num = new int[10];
Program p=new Program();
Console.WriteLine(“原始数组:”);
p.randIt(num);
p.show(num);
p.sort(num);
Console.WriteLine();
Console.WriteLine(“降序数组”);
p.show(num);
Console.ReadKey();
}
public void randIt(int\[\] num)
{
Random randNum = new Random();
for (int i = 0; i < num.Length; i++)
{
num\[i\]= randNum.Next(101);//101??
}
}
public void sort(int\[\] arr)
{
for(int i=0;i<arr.Length;i++)
for (int j = 0; j < arr.Length-i-1; j++)//看清楚j的终止条件
{
if (arr\[j\]< arr\[j+1\])
{
int temp = arr\[j\];
arr\[j\] = arr\[j + 1\];
arr\[j +1\] = temp;
}
}
}
public void show(int\[\] arr)
{
foreach (int i in arr)
{
Console.Write("{0} ", i);
}
}
}
}
4.4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c4._4
{
class Program
{
static void Main(string[] args)
{
int[] num = new int[10];
Program p = new Program();
Console.WriteLine(“原始数组:”);
p.randIt(num);
p.show(num);
p.sort(num);
Console.WriteLine();
Console.WriteLine(“降序数组”);
p.show(num);
Console.ReadKey();
}
public void randIt(int[] num)
{
Random randNum = new Random();
for (int i = 0; i < num.Length; i++)
{
num\[i\] = randNum.Next(101);//101??
}
}
//这是选择排序的算法
public void sort(int\[\] arr)
{
for (int i = 0; i < arr.Length; i++)
for (int j = i; j < arr.Length; j++)//看清楚j的终止条件
{ //选择跟冒泡有点不同,是先排第一个,然后往后排
if (arr\[i\] < arr\[j\])
{
int temp = arr\[j\];
arr\[j\] = arr\[i\];
arr\[i\] = temp;
}
}
}
public void show(int\[\] arr)
{
foreach (int i in arr)
{
Console.Write("{0} ", i);
}
}
}
}
4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Array;
namespace c4._5
{
class Program
{
static void Main(string[] args)
{
int[,] A = new int[4,4];
int[,] B = new int[4, 4];
int[,] C = new int[4, 4];
int[,] D = new int[4, 4];
Program p = new Program();
p.RandIt(A, 50, 100);
p.RandIt(B, 0, 10);
Console.WriteLine("数组A的内容:");
p.show(A);
Console.WriteLine("数组B的内容:");
p.show(B);
Console.WriteLine("上三角显示数组A的内容:");
p.showTriangle(A,0);
Console.WriteLine("下三角显示数组B的内容:");
p.showTriangle(B,1);
p.add(A, B, C);
Console.WriteLine("数组A+B的结果:");
p.show(C);
Console.WriteLine("数组A-B的结果:");
p.dec(A, B, C);
p.show(C);
Console.ReadKey();
}
public void dec(int\[,\] a, int\[,\] b, int\[,\] c)
{
int ax = a.GetLength(0);
int ay = a.GetLength(1);
int bx = b.GetLength(0);
int by = b.GetLength(1);
int cx = c.GetLength(0);
int cy = c.GetLength(1);
if (ax == bx && ax == cx && ay == by && by == cx)
{
for (int i = 0; i < ax; i++)
for (int j = 0; j < bx; j++)
c\[i, j\] = a\[i, j\] - b\[i, j\];
}
else
Console.WriteLine("Can't add it !");
}
public void add(int\[,\] a, int\[,\] b, int\[,\] c)
{
int ax = a.GetLength(0);
int ay = a.GetLength(1);
int bx = b.GetLength(0);
int by = b.GetLength(1);
int cx = c.GetLength(0);
int cy = c.GetLength(1);
if (ax == bx && ax == cx && ay == by && by == cx)
{
for (int i = 0; i < ax; i++)
for (int j = 0; j < bx; j++)
c\[i, j\] = a\[i, j\] + b\[i, j\];
}
else
Console.WriteLine("Can't add it !");
}
public void RandIt(int\[,\] arr, int randNumMin,int randNumMax)
{
int sizeX = arr.GetLength(0);
int sizeY = arr.GetLength(1);
Random rand=new Random();
for (int i = 0; i < sizeX; i++)
{
for (int j = 0; j < sizeY; j++)
{
arr\[i,j\] = rand.Next(randNumMin, randNumMax);
}
}
}
public void showTriangle(int\[,\] arr, int flag)//flag=0,上三角,=1,下三角
{
int sizeX = arr.GetLength(0);
int sizeY = arr.GetLength(1);
string ch=" ";
for (int i = 0; i < sizeX; i++)
{
for (int j = 0; j <sizeY; j++)
{
if(flag==0)//上三角
{
if (i<=j)
Console.Write("{0,4}", arr\[i, j\]);
else {
//for (int f = 0; f < j \* 10;f++ )
Console.Write("{0,4}",ch);
}
}
else if(flag==1)//下三角
{
if (i>=j)
Console.Write("{0,4}", arr\[i, j\]);
else
Console.Write("{0,4}",ch);
}
}
Console.WriteLine();
}
}
public void show(int \[,\]num)
{
int sizeX = num.GetLength(0);
int sizeY = num.GetLength(1);
for (int i = 0; i < sizeX; i++)
{
for (int j = 0; j < sizeY; j++)
{
Console.Write(" {0}", num\[i,j\]);
}
Console.WriteLine();
}
Console.WriteLine();
}
}
}
4.6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c4._6
{
class Program
{
const int MAX= 10;
static void Main(string[] args)
{
Program.showTriangle(MAX);
Console.ReadKey();
}
public static void showTriangle(int LINE)
{
int [,]triangle=new int[LINE,LINE];
for (int i = 0; i < LINE; i++)//先令每一个元素都为1
for (int j = 0; j < LINE; j++)
triangle[i, j] = 1;
for (int i = 0; i < LINE; i++)//构造杨辉三角
for (int j = 1; j < i; j++)//每排第一个和最后一个不变
triangle[i, j] = triangle[i - 1, j] + triangle[i - 1, j - 1];
//其他的为上一层头顶和左边一个之和
for (int i = 0; i < LINE; i++)
{
for (int j = 0; j <= i; j++)
Console.Write(" {0,4}", triangle\[i, j\]);
Console.WriteLine();
}
}
}
}
4.7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c4._7
{
class Program
{
static void Main(string[] args)
{
int[] a = new[] { 80, 90, 67, 89, 78, 85, 45, 69, 77, 95 };
Console.WriteLine(“数组的维数:{0}”, a.Rank);
Console.WriteLine(“数组的长度(元素总数):{0}”, a.Length);
Program p = new Program();
Console.WriteLine(“排序前的内容:”);
p.show(a);
Console.WriteLine(“排序后的内容:”);
Array.Sort(a);
p.show(a);
Console.WriteLine(“反转后的内容:”);
Array.Reverse(a);//利用反向迭代器
p.show(a);
Console.ReadKey();
}
public void show(int\[\] a)
{
foreach(int i in a)
Console.Write("{0,4}",i);
Console.WriteLine();
}
}
}
4.8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c4._8
{
unsafe class Program
{
static void Main(string[] args)
{
int a, b, c;
int *pa,pb,pc;//三个都是指针?c#真是个神奇的东东啊
Console.Write(“请输入整数a:”);
String s=Console.ReadLine();
a = int.Parse(s);
Console.Write(“请输入整数b:”);
s = Console.ReadLine();
b = int.Parse(s);
Console.Write(“请输入整数c:”);
s = Console.ReadLine();
c = int.Parse(s);
Console.WriteLine(“原始数据:a={0},b={1},c={2}”, a, b, c);
pa = &a;
pb = &b;
pc = &c;
if (a > b) swap(pa,pb);
if (a > c) swap(pa, pc);
if (b > c) swap(pb, pc);
Console.WriteLine(“升序排序:a={0},b={1},c={2}”, a, b, c);
Console.ReadKey();
}
public static void swap(int\* a, int\* b)
{
int temp = \*a ;
\*a = \*b;
\*b = temp;
}
}
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
7.8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace c7._8
{
class Account {
private Object thisLock = new Object();
int balance;
Random r = new Random();
public Account(int i)
{
balance = i;
}
int Withdraw(int amount)
{
if (balance < 0)
{
throw new Exception(“账户余额不足(<=0)”);
}
lock (thisLock)//同步锁
{
if (balance >= amount)
{
Console.WriteLine(“{0} 正在工作”, Thread.CurrentThread.Name);
Console.WriteLine(“提取前账户总额:{0}”, balance);
Console.WriteLine(“取款额:{0}”, amount);
balance -= amount;
Console.WriteLine(“提取后账户总额:{0}”, balance);
Console.WriteLine();
return amount;//返回干嘛?
}
else
{
/*
Console.WriteLine(“{0} 正在工作”, Thread.CurrentThread.Name);
Console.WriteLine(“提取前账户总额:{0}”, balance);
Console.WriteLine(“取款额:{0},操作已被拒绝”, amount);
Console.WriteLine();
*/
return 0;//提款是否成功?
}
}
}
public void DoTransactions()
{
for (int i = 0; i < 100; i++)
Withdraw(r.Next(1, 100));
}
}
class Program
{
static void Main(string\[\] args)
{
Thread\[\] thread = new Thread\[10\];
Account acc = new Account(1000);
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(acc.DoTransactions));
t.Name = "线程" + i;
thread\[i\] = t;
}
for (int i = 0; i < 10; i++)
thread\[i\].Start();
Console.ReadLine();
}
}
}
7.7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace c7._7
{
public class Worker
{
public void DoWork()
{
while (!shouldStop)
{
Console.WriteLine(“工作线程…..”);
}
Console.WriteLine(“工作线程已经停止”);
}
public void Stop()
{
shouldStop = true;
}
//volatile
private volatile bool shouldStop;
}
class Program
{
static void Main(string\[\] args)
{
Worker wOject = new Worker();
Thread wThread = new Thread(wOject.DoWork);
wThread.Start();
while (!wThread.IsAlive) //未激活工作线程前,一直循环
;//这里有空语句
Thread.Sleep(1);
wOject.Stop();
wThread.Join();
Console.WriteLine("主线程:Worker thread has terminal.");
Console.ReadLine();
}
}
}
7.6
#define DEBUG
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c7._6
{
[System.AttributeUsage(System.AttributeTargets.Class
System.AttributeTargets.Struct,
AllowMultiple = true)
]
public class Author : System.Attribute
{
string name;
public double version;
public Author(string name)
{
this.name=name;
version=1.00;
}
public string GetName()
{
return name;
}
}
\[Author("H.Ackerman")\]//,Author("M.Knott",version=2.00)\]
class FirstClass {
}
class SecondClass {
}
\[Author("H.Ackerman"), Author("M.Knott", version = 2.00)\]
class ThirdClass {
}
class Program
{
static void Main()
{
printAuthorInfo(typeof(FirstClass));
printAuthorInfo(typeof(SecondClass));
printAuthorInfo(typeof(ThirdClass));
System.Console.ReadLine();
}
//这就是传说中的反射?
private static void printAuthorInfo(System.Type t)
{
System.Console.WriteLine("作者的信息:{0}",t);
System.Attribute \[\]attrs=System.Attribute.GetCustomAttributes(t);
foreach(System.Attribute attr in attrs)
{
if(attr is Author)
{
Author a=(Author)attr;
System.Console.WriteLine(" {0},版本{1:f}",a.GetName(),a.version);
}
}
System.Console.ReadLine();
}
}
}
7.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace c7._5
{
public class Trace
{
[Conditional(“DEBUG”)]
public static void Msg(string msg)
{
Console.WriteLine(msg);
}
[Conditional(“DEBGU”),Conditional(“TRACE”)]
public static void Method2()
{
Console.WriteLine(“DEBUG or TRACE is defined”);
}
}
class Program
{
static void Main(string\[\] args)
{
Trace.Msg("Now in main...");
Trace.Method2();
Console.WriteLine("Main done!");
Console.ReadKey();
}
}
}
7.4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c7._4
{
public class GenericList
{
protected Node head;
protected Node current = null;
protected class Node
{
public Node next;
private T data;
public Node(T t)
{
next = null;
data = t;
}
public Node Next
{
get { return next; }
set { next = value; }
}
public T Data
{
set { data = value; }
get { return data; }
}
}
public GenericList()
{
head = null;
}
//implement
public System.Collections.Generic.IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
}
//sort
public class SortedList<T> : GenericList<T> where T : System.IComparable<T>
{
public void AddHead(T t)//书本居然漏写了,坑啊
{
Node tmp = new Node(t);
tmp.next = head;
head = tmp;
}
public void BubbleSort()
{
if (null == head null == head.Next)
{
return;
}
bool swapped;
do
{
Node previous = null;
Node current = head;
swapped = false;
while (current.Next != null)//最好用Next而不是next
{
//交换两个值
if (current.Data.CompareTo(current.next.Data) > 0)
{
Node tmp = current.next;
current.next = current.next.next;
tmp.next = current;
if (previous == null)
{
head = tmp;
}
else
{
previous.next = tmp;
}
previous = tmp;
swapped = true;//只要进行了排序,就保证了下一次排序的进行
//即只要需要排序,就进行下一次排序
}
else
{
previous = current;
current = current.next;
}
}
} while (swapped);
}
}
public class Person : System.IComparable<Person>
{
string name;
int age;
public Person(string n, int i)
{
name = n;
age = i;
}
public int CompareTo(Person p)
{
return age - p.age;
}
public override string ToString()
{
return name + ":" + age;
//return base.ToString();
}
public bool Equals(Person p)
{
return age == p.age;
}
}
static class Program
{
static void Main()
{
SortedList<Person> list = new SortedList<Person>();
string\[\] name = new string\[\]{
"陈A",
"陈B",
"刘C",
"李四",
"王五",
"光哥"
};
int\[\] age = new int\[\] { 45, 19, 28, 23, 18, 79 };
for (int x = 0; x < 6; x++)
{
list.AddHead(new Person(name\[x\], age\[x\]));
}
Console.WriteLine("原始数列:");
foreach (Person p in list)
Console.WriteLine(p.ToString() + " ");
Console.WriteLine();
list.BubbleSort();
Console.WriteLine("排序后数列:");
foreach (Person p in list)
Console.WriteLine(p.ToString() + " ");
Console.ReadLine();
Console.ReadLine();
}
}
}
7.3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c7._3
{
//哎,感觉就好像是C++的加强版
public class mQueue
{
private class Node
{
public Node(T t)
{
next = null;
data = t;
}
private Node next;
public Node Next {
get {
return next;
}
set {
next= value;
}
}
private T data;
public T Data {
get { return data; }
set { data = value; }
}
}
private Node head;
public mQueue()
{
head=null;
}
public void Add(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
public IEnumerator<T> GetEnumerator()//for foreach
//public void Display()
{
Node pos = head;
while (pos != null)
{
yield return pos.Data;
pos = pos.Next;
}
}
}
class Program
{
static void Main(string\[\] args)
{
mQueue<int> list = new mQueue<int>();
Console.WriteLine("0-9 十个数字组成堆栈");
for (int x = 0; x < 10; x++)
list.Add(x);
Console.WriteLine("堆栈内容如下:");
foreach (int i in list)
Console.Write(i + " ");
Console.ReadKey();
}
}
}
7.2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c7._2
{
class Program
{
static void Swap
{
T temp;
temp=lhs;
lhs = rhs;
rhs = temp;
}
static void Main(string\[\] args)
{
Console.Write("请输入整数1:");
int a = int.Parse(Console.ReadLine());
Console.Write("请输入整数2:");
int b = int.Parse(Console.ReadLine());
Console.WriteLine("原始值:a={0},b={1}", a, b);
Swap<int>(ref a,ref b);
Console.WriteLine("交换后:a={0},b={1}", a, b);
Console.Write("请输入实数1:");
double c = double.Parse(Console.ReadLine());
Console.Write("请输入实数2:");
double d = double.Parse(Console.ReadLine());
Console.WriteLine("原始值:c={0},d={1}", c, d);
Swap<double>(ref c, ref d);
Console.WriteLine("交换后:c={0},d={1}", c, d);
Console.ReadKey();
}
}
}
7.1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace c7._1
{
class Program
{
static void Main(string[] args)
{
List
List
Console.WriteLine(“整型数组如下:”);
foreach (int i in arrInt)
Console.Write(“{0,-5}”, i);
Console.WriteLine(“\n整型字符数组如下:”);
foreach (string s in arrStr)
Console.Write(“{0,-5}”, s);
arrInt.Add(4);
arrStr.Remove("one");
Console.WriteLine("整型数组如下:");
foreach (int i in arrInt)
Console.Write("{0,-5}", i);
Console.WriteLine("\\n整型字符数组如下:");
foreach (string s in arrStr)
Console.Write("{0,-5}", s);
Console.ReadKey();
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
第一章没有习题 2.1
//p35 1
#include <iostream.h>
int main()
{
cout<<”我的名字啊,这是个什么啊”<<endl;
cout<<”我家住在黄土高坡\n”;
cin.get();
return 0;
}
2.2
// 2.2
#include <iostream.h>
int main()
{
cout<<”输入一个以码为单位的距离\n”;
int n;
cin>>n;
//cout<<n;
cout<<”转换之后啊,看看是多少啊:\n”<<n*220<<”码”<<endl;
cin.get();
cin.get();
return 0;
}
2.3
//2.3
#include <iostream.h>
void myFuction1();
void myFuction2() ;
int main()
{
using namespace std;
myFuction1();
myFuction1();
myFuction2();
myFuction2();
cin.get();
return 0;
}
void myFuction1()
{
cout<<”Three blind mice”<<endl;
cout<<”Three blind mice”<<endl;
}
void myFuction2()
{
cout<<”see how they run”<<endl;
cout<<”see how they run”<<endl;
}
2.4
//2.4
#include <iostream.h>
int main()
{
cout<<”请输入你的年龄”<<endl;
int age;
cin>>age;
cout<<”你的年龄是:”<<age<<endl;
cout<<”包含”<<age*12<<”个月”<<endl;
cin.get();
cin.get();
return 0;
}
2.5
//2.5
#include <iostream.h>
double change(double n);
int main()
{
cout<<”please enter a Celsius valus:”;
double n;
cin>>n;
cout<<”\nyour valus is :”<<n<<endl;
double now=change(n);
cout<<”now is “<<now<<endl<<change(5.2);
cin.get();
cin.get();
return 0;
}
double change(double n)
{
return n*10;//这里的公式是错的,应该是return 1.8*n+32.0 才对 –2015/01/28
}
不懂可以留言,这么简单应该都懂吧
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
2.6
#include <iostream.h>
double change(double y);
int main()
{
cout<<”Enter the number of light years:\n”;
double n;
cin>>n;
cout<<”Your number is”<<n<<endl;
cout<<”===”<<change(n);
cin.get();
cin.get();
return 0;
}
double change(double y)
{
return y\*62340;
}
2.7
//7
#include <iostream.h>
void see(int h,int m);
int main()
{
cout<<”Enter the number of hours:”;
int h;
cin>>h;
// cout<< h<<endl;
cout<<”Enter the number of minutes:”;
int m;
cin>>m;
//cout<<m<<endl;
see(h,m);
cin.get();
cin.get();
cin.get();
cin.get();
return 0;
}
void see(int h,int m)
{
cout<<”Time”<<h<<”:”<<m<<endl;
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
4.1
#include
void main41()
{
using namespace std;
cout<<”What is your first name?”;
char fname[20];
cin.getline(fname,20);
//int fsize=sizeof(fname);
//cout<<b;
//cout<<fname;
cout<<endl<<”What is your last name ?”;
char lname[20];
cin.getline(lname,20);
cout<<endl<<”what letter grade do you deserve?”;
char grade;
cin>>grade;
cout<<”\nwhat is your age?”;
int age;
cin>>age;
cout<<endl<<”Name:”<<lname<<” ,”<<fname<<endl;
cout<<”Grade:”<<char(grade+1);
cout<<endl<<”Age:”<<age;
cin.get();
}
4.2
#include
#include
using namespace std;
void main42()
{
string name;
cout<<”Enter your name:\n”;
//cin>>name;//一定要包含string头文件,否则会出错
getline(cin,name);
cout<<”Enter your favorite dessert:\n”;
string dessert;
getline(cin,dessert);
cout<<”I have some delicous “<<dessert;
cout<<” for you ,”<<name<<endl;
cin.get();
}
4.3
#include
using namespace std;
void main43()
{
const int ArrSize=20;
char name[ArrSize];
char xing[ArrSize/2];
cout<<”Enter your first name:”;
cin.getline(name,20);
cout<<endl<<”Enter your last name:”;
cin.get(xing,10);
cout<<endl<<”Here’s the information in a single string “
<<xing<<”,”<<name;
cin.get();
}
4.4
#include
#include
void main44()
{
using namespace std;
cout<<”Enter your First Name:”;
string fname;
getline(cin,fname);
cout<<”\nEnter your last name:”;
string lname;
getline(cin,lname);
cout<<endl<<”Here’s the information in a single string:”
<<lname<<”, “<<fname;
cin.get();
}
4.5
#include
#include
using namespace std;
struct CandyBar
{
string name;
double weight;
int kaluli;
};
void main45()
{
CandyBar snack={“OO CANDAY”,2.5,60};
cout<<”name:”<<snack.name;
cout<<”\nweight:”<<snack.weight<<” \n kaluli: “<<snack.kaluli;
cin.get();
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
4.6
#include
#include
using namespace std;
struct CandyBar
{
string name;
double weight;
int kaluli;
};
void show(CandyBar &c);//按值传递和引用传递均可
void main46()
{
for(int i=0;i<3;i++)
show(cb[i]);
}
void show(CandyBar &c)
{
cout<<”name=”<<c.kaluli<<” ,weight=”<<c.weight<<” ,kaluli=”<<c.kaluli<<endl;
}
4.7
#include
#include
using namespace std;
struct Pizza
{
string name;
double d;
double w;
};
Pizza get(Pizza &p);
void show(const Pizza &p);
void main47()
{
Pizza p;
for(int i=0;i<3;i++)
{
get(p);
show(p);
}
}
Pizza get(Pizza &p)//若不使用引用变量,返回时返回的是一个未知的变量
{
cout<<”Enter the Pizza name:”;
string name;
getline(cin,name);
p.name=name;
cout<<”\n Enter the d:”;
double d;
cin>>d;
p.d=d;
cout<<endl<<”Enter the weight:”;
double w;
cin>>w;
p.w=w;
return p;
}
void show(const Pizza &p)
{
cout<<”name=”<<p.name<<” ,d=”<<p.d<<” ,weight=”<<p.w<<endl
;
cin.get();
}
4.8
#include
#include
using namespace std;
struct Pizza
{
string name;
double d;
double w;
};
Pizza *get(Pizza *p);
void show(const Pizza *p);
void main48()
{
Pizza *p;
p=new Pizza();
for(int i=0;i<3;i++)
{
get(p);
show(p);
//delete p;
}
delete p;
//delete p;
}
Pizza *get(Pizza *p)//若不使用引用变量,返回时返回的是一个未知的变量
{
//p=new Pizza();
cout<<”Enter the Pizza name:”;
string name;
getline(cin,name);
p->name=name;
cout<<”\n Enter the d:”;
double d;
cin>>d;
p->d=d;
cout<<endl<<”Enter the weight:”;
double w;
cin>>w;
p->w=w;
return p;
}
void show(const Pizza *p)
{
cout<<”name=”<
;
cin.get();
}
4.9
#include
#include
using namespace std;
struct CandyBar
{
string name;
double weight;
int kaluli;
};
void show(const CandyBar *c);
void main49()
{
CandyBar *c=new CandyBar[3];//结构体数组定义方法
for(int i=0;i<3;i++,c++)
{
c->name=”String”+i;
c->weight=i;
c->kaluli=i*10;
show(c);
}
delete []c;
}
void show(const CandyBar *c)
{
cout<<”name=”<
}
4.10
#include
#include
using namespace std;
void main410()
{
array <int,3> arr;
int sum=0;
for(int i=0;i<3;i++)
{
cout<<”Enter the “<<i+1<<” time :”;
cin>>arr[i];
sum+=arr[i];
}
double pinjun;
cout<<”平均成绩为:”<<sum*1.0/3<<endl;
cin.get();
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
5.1
#include
#include
using namespace std;
void main51()
{
int min,max;
cout<<”Enter the min:”;
cin>>min;
cout<<endl<<”Enter the max:”;
cin>>max;
int sum=0;
for(int i=min;i<=max;i++)
sum+=i;
cout<<”\nsum=”<<sum<<endl;
cin.get();
// int ArrSize=max-min+1;
//array<int,ArrSize> arr;
}
5.2
#include
#include
using namespace std;
const int ArrSize=101;
void main52()
{
array<long double,ArrSize> arr;
arr[0]=arr[1]=1L;
for(int i=2;i<ArrSize;i++)
arr[i]=arr[i-1]*i;
for(int i=0;i<ArrSize;i++)
cout<<i<<”!=”<<arr[i]<<endl;
cin.get();
}
5.3
#include
void main53()
{
using namespace std;
cout<<”Plese Enter the number ,if is 0,for end:”;
double iput,sum=0;
cin>>iput;
while(iput)
{
sum+=iput;
cout<<”\nnow sum is “<<sum<<” \nEnter a another number:”;
cin>>iput;
}
cin.get();
}
5.4
#include
using namespace std;
void main54()
{
double Dsum=10;
double Csum=10;
int i=0;
while (Csum<=Dsum)
{
Dsum+=10;
Csum+=Csum*0.05;
i++;
}
cout<<”After “<<i<<” year, Csum is bigger than Dsum”<<endl;
cout<<”Dsum=”<<Dsum<<” Csum=”<<Csum;
cin.get();
}
5.5
#include
void main55()
{
using namespace std;
char arr[15]={‘1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’,’10’,’11’,’12’};
char *c=arr;
int num[15];
int sum=0;
for(int i=0;i<12;i++,c++)
{
//void *c1=c;
cout<<”The “<<*c<<” month number is “;
cin>>num[i];
sum+=num[i];
cout<<”\n now sum is “<<sum<<endl;
}
cout<<”This year num is “<<sum<<endl;
cin.get();
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
5.6
#include
#include
void main56()
{
using std::cout;
using std::cin;
using std::string;
string m[12]={“1”,”2”,”3”,”4”,”5”,”6”,”7”,”8”,”9”,”10”,”11”,”12”};
const string *s=m;
int arr[3][15];
int sum=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<12;j++,s++)
{
cout<<”Please enter the “<<i+1<<” years “<<*s<<” moth sales:”;
cin>>arr[i][j];
sum+=arr[i][j];
cout<<”Now sum is “<<sum<<”\n”;
}
s=m;//重新令s指向m的开头
}
cout<<”All sum is “<<sum<<”, THE END\n”;
cin.get();
}
5.7
#include
#include
using namespace std;
struct car{
string name;
int year;
};
void get(car *);
void show(const car const *);
void main57()
{
cout<<”How many cars do your wish to catalog?”;
int num;
cin>>num;
car *c=new car[num];
for(int i=0;i<num;i++,c++)
{
cout<<”Car #”<<i+1<<”:”<<endl;
get(c);
show(c);
}
cin.get();
}
void get(car *c)
{
cin.get();
cout<<”Please enter the make:”;
string name;
getline(cin,name);
cout<<”\nplease enter the years of make:”;
int y;
cin>>y;
c->name=name;
c->year=y;
}
void show(const car const *c)
{
cout<<”/nHere is your collection: “;
cout<
}
5.8有点BUG,详看5.9,我懒得改了
#include
#include
using namespace std;
void main58()
{
char test[20];
int count=0;
char ch;
int i=0;
cout<<”Enter words (to stop,type the word done):”;
//cin.get();
while(strcmp(test,”done”)!=0)
{
//cout<<”is in”<<endl;
//cin.get(ch)>>test[i];
cin.get(ch);
if(ch==’ ‘)
{
test[i]=’\0’;
count++;
//cout<<”i=0”<<test<<”\ncount=”<<count;
i=0;
}
else
{
test[i]=ch;
test[i+1]=’\0’;
//cout<<”i++”<<test<<endl;
i++;
}
}
cout<<”You entered a total of “<<count<<” words”;
cin.get();
}
5.9(有错误,看下一道正确的)
#include
#include
//要注意输入是这种情况 doneff ajgk done,这时候,要注意doneff的判断 增加flag量
using namespace std;
void main59()
{
string test=””;
string t=”done”;
//if(test==t)
char ch;
int count=0;
int flag=1;
cout<<”Enter words (to stop, type the word done):”<<endl;
//cin.get();
while(test!=t)
{
//cout<<”Test2=”<<test<<” Count=”<<count<<endl;
//cout<<”here”<<endl;
flag=1;
while(flag==1)
{
cin.get(ch);
if(ch==’\n’)//回车键的表示
break;
else if(ch!=’ ‘)
{
test=test+ch;
}
else
{
test=””;
count++;
flag=0;
}
//cout<<”test1=”<<test<<” count=”<<count<<endl;
//cout<<”in in”<<endl;
}
}
cout<<”You enter a total of “<<count<<” words”;
cin.get();
}
5.9(正确版)
#include
#include
//#include
using namespace std;
int main()
{
string test;
cout<<”Enter words (to stop ,type the word done)”<<endl;
int counts=0;
do{
cin>>test;
if(strcmp(test.c_str(),”done”)==0)
break;
else
counts++;
}while(true);
cout<<”You entered a total of “<<counts<<” words”<<endl;
}
运行截图: 感谢网友的指出! ——–2014.10.8 5.10
#include
void main510()
{
using namespace std;
int row;
cout<<”Enter number of rows:”;
cin>>row;
for(int i=0;i<row;i++)
{
for(int j=0;j<row;j++)
{
if(j<row-i-1)
cout<<”.”;
else
cout<<”*“;
}
cout<<endl;
}
cin.get();
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
bank.h
#ifndef bank_H_
#define bank_H_
#include
using namespace std;
class Bank{
private:
string name;
string account;
double money;
void setIn(double in){money+=in;};
void setOut(double out){money-=out;};
double getMoney(){return money;};
public:
Bank(string n,string a,double m);
~Bank();
//void show(const bank &b);
void in(double input);
void out(double output);
friend ostream&operator<<(ostream &os,const Bank &b);//友元函数
};
#endif
bank.cpp
#include
#include “bank.h”
Bank::Bank(string n,string a,double m){
name=n;//怎么第一次编译老是提示我构造函数不能有返回值?
account=a;
money=m;
std::cout<<”\nBankaccount success!”<<endl;
}
/*
Bank::Bank(string n,string a,double m)
{
name=n;
account=a;
money=m;
std::cout<<”Success!”<<endl;
}*/
Bank::~Bank()
{
std::cout<<”\nBankaccount recevory!”<<endl;
}
void Bank::in(double input)
{
Bank::setIn(input);
}
void Bank::out(double output)
{
if(Bank::getMoney()>0)
Bank::setOut(output);
else
std::cout<<”\nYour have not enough money!”<<endl;
}
std::ostream &operator<<(ostream &os,const Bank &b)//重载<<运算符
{
os<<”\nshow start!”<<endl;
os<<”name:”<<b.name<<” account:”<<b.account<<endl;
os<<”money:”<<b.money<<endl;
return os;
}
main101.cpp
#include
#include “bank.h”
using namespace std;
void main101()
{
{//将其加入代码块是为了更好地查看对象的销毁过程
Bank b1=Bank(“guang”,”a1”,100);
Bank b2=Bank(“jing”,”a2”,600.5);
cout<<b1<<endl;
cout<<b2<<endl;
//b1.money=200;
b1.in(100);
cout<<b1<<endl;
b1.out(5.5);
cout<<b1<<endl;
}
system(“pause”);
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
缺失模块。
1、请确保node版本大于6.2
2、在博客根目录(注意不是yilia-plus根目录)执行以下命令:
npm i hexo-generator-json-content --save
3、在根目录_config.yml里添加配置:
jsonContent: meta: false pages: false posts: title: true date: true path: true text: false raw: false content: false slug: false updated: false comments: false link: false permalink: false excerpt: false categories: false tags: true