1.输入球的中心点和球上某一点的坐标,计算球半径和体积。
C++
#include <iostream>
#include <cmath>
using namespace std;
double const PI=3.14;
int main()
{
double a,b,c,x,y,z,r,v;
cout<<"input circle center coordinates:";
cin>>a>>b>>c;
cout<<"input coordinates of any point:";
cin>>x>>y>>z;
r=sqrt(pow(x-a,2)+pow(y-b,2)+pow(z-c,2));
v=4/3.0*PI*pow(r,3); //注意:4/3=1,所以要除以 3.0
cout<<"Radius is:"<<r<<endl;
cout<<"Volume is:"<<v<<endl;
return 0;
}
Go
package exam2003
import (
"fmt"
"math"
)
//Sphere is used to calculate the radius and volume of a certain sphere
func Sphere() {
var a, b, c, x, y, z, r, v float64
fmt.Print("input the circle center coordinates:")
fmt.Scan(&a, &b, &c)
fmt.Print("input the coordinates of any point:")
fmt.Scan(&x, &y, &z)
r = math.Sqrt(math.Pow(x-a, 2) + math.Pow(y-b, 2) + math.Pow(z-c, 2))
v = 4 / 3.0 * math.Pi * math.Pow(r, 3)
fmt.Printf("The radius is:%f\n", r)
fmt.Printf("The volume is:%f\n", v)
}
2.手工建立一个文件,文件种每行包括学号、姓名、性别和年龄。每一个属性使用空格分开。再根据输入的学号,查找文件,输出该学生的信息。文件如下:
01 李江 男 21
02 刘唐 男 23
03 张军 男 19
04 王娜 女 19
C++
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int num,age,stu,flag=0;
string name,gender;
cout<<"input students' info like:01 ML female 21 and ends with 00 00 00\n";
ofstream out("student.txt");
while(cin>>num>>name>>gender>>age)
{
if(num==0)
{
break;
}
out<<num<<" "<<name<<" "<<gender<<" "<<age<<endl;
}
cout<<"input the number of you want\n";
cin>>stu;
ifstream in("student.txt");
while(!in.eof())
{
in>>num>>name>>gender>>age;
if(num==stu)
{
flag=1;
cout<<num<<" "<<name<<" "<<gender<<" "<<age<<endl;
break;
}
}
if(flag==0)
{
cout<<"There is no such student here\n";
}
return 0;
}
Go
package exam2003
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
//Student is used to handle information of students
func Student() {
var num, age, flag int
var name, gender, Snum string
//create file and input information
file, err := os.OpenFile("D:/stu.txt", os.O_CREATE|os.O_WRONLY|os.O_RDONLY, 0)
if err != nil {
fmt.Println("Failed to open file")
return
}
defer file.Close()
fmt.Println("inout the Info of studetns:01 ml female 22 and ends by 0 0 0 0")
for {
fmt.Scan(&num, &name, &gender, &age)
if num == 00 {
break
}
fmt.Fprint(file, num, " ", name, " ", gender, " ", age)
fmt.Fprint(file, "\n")
}
fmt.Println("input the student number of you want:")
fmt.Scan(&Snum)
//Read out and select the students you want to query
f, err := ioutil.ReadFile("D:/stu.txt")
if err != nil {
fmt.Println("read fail", err)
}
strArr := strings.Split(string(f), "\n")
flag = 0
for _, s := range strArr {
y := strings.Split(string(s), " ")
if y[0] == Snum {
fmt.Printf("%s\n", y)
flag = 1
break
}
}
if flag == 0 {
fmt.Println("There is no such student here")
}
}
3.输入年月日,计算该天是本年的第几天。例如 1990 年 9 月 20 日是 1990 年的第 263 天,2000 年 5 月 1 日是 2000 年第 122 天。(闰年:能被 400 整除,或能被 4 整除但不能被 100 整除。每年 1、3、5、7、8、10 、12 为大月)
C++
#include <iostream>
using namespace std;
int main()
{
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int year,month,day,sum=0;
cout<<"input the date:1998 06 08"<<endl;
cin>>year>>month>>day;
if(year%400==0||(year%4==0&&year%100!=0))
{
a[2]=29;
}
for(int i=1;i<month;i++)
{
sum+=a[i];
}
sum+=day;
cout<<"today is "<<sum<<"th day"<<endl;
}
Go
package exam2003
//Day is used to calculate how many days today is
func Day(year, month, day int) int {
a := [13]int{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
a[2] = 29
}
sum := 0
for i := 1; i < month; i++ {
sum += a[i]
}
sum += day
return sum
}