程序设计基础实验报告

更新时间:2023-08-19 06:02:01 阅读量: 高中教育 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

《程序设计基础课程设计》实验报告

班级:1303011

姓名:洪礼翔

学号:13030110019

所选题目:1_1, 1_2,2_4,3_2,3_4,4_2,5_1

程序设计语言:c++

程序设计平台:visual c++ 6.0

日期:2014-4-5

签名:

第1_1题

算法描述:

比较两个文本文件并打印出它们第一个不相同的行(文件每行字符数不多于80)。

源程序: no1_1.cpp

#include<iostream.h> #include<fstream.h> #include<stdlib.h> #include<string.h> void main() { fstream infile1,infile2; infile1.open("f1.txt",ios::in); infile2.open("f2.txt",ios::in); if(!infile1) { cout<<"f1.txt can't open"; abort(); } if(!infile2) { cout<<"f2.txt can't open"; abort(); } char s1[80],s2[80]; while(!infile1.eof()&&!infile2.eof()) { infile1.getline(s1,sizeof(s1)); infile2.getline(s2,sizeof(s2)); if(strcmp(s1,s2)) { cout<<s1<<endl; cout<<s2<<endl; } } infile1.close(); infile2.close(); }

测试数据(输入、输出):

第1_2题

算法描述:

文本文件num1.txt和num2.txt中各有一组用空格分隔的整数,将num1.txt和num2.txt联合排序,并将结果保存在num3.txt中 源程序: no1_2.cpp

#include<fstream.h> #include<iostream.h> #include<stdlib.h> #include<strstrea.h>

int cmp(const void *m,const void *n); void main() { fstream inf1,inf2,outf; inf1.open("num1.txt",ios::in); if(!inf1) { cout<<"num1.txt can't open.\n"; abort(); } inf2.open("num2.txt",ios::in); if(!inf2) { cout<<"num2.txt can't open.\n"; abort(); } outf.open("num3.txt",ios::out); if(!outf) { cout<<"num3.txt can't open.\n"; abort(); } int array[40]={0}; int i=0; while(!inf1.eof()) { inf1>>array[i++]; } while(!inf2.eof()) { inf2>>array[i++]; }

qsort(array,i,sizeof(int),cmp); int j(0); while(i) { outf<<array[j++]; if(i!=1) outf<<' ';

i--; } cout<<"Sort correctlly!\n"; }

int cmp(const void *m,const void *n) { return *(int *)m-*(int *)n; }

测试数据(输入、输出):

第2_4题

算法描述:

编写程序XMLtoTXT自动将XML文件email.xml转换为文本文件email.txt。命令行格式:XMLtoTXT email.xml email.txt。 源程序: No2_4.cpp

#include<iostream.h> #include<fstream.h> #include<string.h> #include<stdlib.h> void main() { fstream infile,outfile; infile.open("email.xml",ios::in); if(!infile) { cout<<"email.xml can't open.\n"; abort(); } outfile.open("email.txt",ios::out); if(!outfile) { cout<<"email.txt can't open.\n"; abort(); } char ch=0; int state=0; cout<<"Progressing......"<<endl; while((ch=infile.get())!=EOF) { char str[10]={0}; if(ch=='<') { for(int i(0);infile.peek()!='>';i++) { str[i]=infile.get(); } if(!strcmp(str,"from")) state=-1; if(state==-1&&!strcmp(str,"address")) state=1; if(!strcmp(str,"to")) state=-2; if(state==-2&&!strcmp(str,"address")) state=2; if(!strcmp(str,"subject")) state=3; if(!strcmp(str,"body")) state=4; if(state>0) { switch(state) { case 1:outfile<<"from:";break;

case 2:outfile<<"to:";break; case 3:outfile<<"subject:";break; case 4:outfile<<"body:";break; default:cout<<"error!\n";exit(0); } state=0; infile.get(); while(infile.peek()!='<') { infile.get(ch); outfile.put(ch); } outfile<<endl; } } } cout<<"Convert correctly!"<<endl; }

测试数据(输入、输出)

第3_2题

算法描述:

设计一个复数类型,输入实部和虚部生成一个复数,可进行两个复数求和、两个复数求差、两个复数求积运算。 源程序: No3_2.cpp

#include<iostream.h> class complex {

public: complex(){set(0.0,0.0);} complex(double real, double imag){set(real,imag);} void set(double r, double i){real=r;imag=i;} friend complex operator +(const complex &c1,const complex &c2); friend complex operator -(const complex &c1,const complex &c2); friend complex operator *(const complex &c1,const complex &c2); void print(); private: double real, imag; };

void complex::print() { if(imag<0) cout<<real<<imag<<"i"<<endl; else cout<<real<<"+"<<imag<<"i"<<endl; }

complex operator +(const complex &c1,const complex &c2) { return complex(c1.real+c2.real,c1.imag+c2.imag); }

complex operator -(const complex &c1,const complex &c2) { return complex(c1.real-c2.real,c1.imag-c2.imag); }

complex operator *(const complex &c1,const complex &c2) { return complex(c1.real*c2.real-c1.imag*c2.imag, c1.real*c2.imag+c1.imag*c2.real); }

void main() { complex a,b,c; double r,i; cout<<"Complex a:\n";

cout<<"Input real part:"; cin>>r; cout<<"Input imaginary part:"; cin>>i; a.set(r,i); cout<<"a="; a.print(); cout<<"Complex b:\n"; cout<<"Input real part:"; cin>>r; cout<<"Input imaginary part:"; cin>>i; b.set(r,i); cout<<"b="; b.print(); int s; cout<<"Function:\n\n"; cout<<"1.c=a+b 2.c=a-b 3.c=a*b\n"<<"Please enter a number:"; Input:cin>>s; switch(s) { case 1: c=a+b;break; case 2: c=a-b;break; case 3: c=a*b;break; default:cout<<"Please enter '1','2'or'3'!\n";goto Input; } cout<<"\nc="; c.print(); }

测试数据(输入、输出)

第3_4题

算法描述:

根据输入的数字N,计算N以内(包括N)数据链并统计数据链末尾数字是1的数据个数。例如N=44,则数字链为:44->32->13->10->1,其规则为:4*4+4*4=32,3*3+2*2=13,1*1+3*3=10,1*1+0*0=1。 源程序: No3_4.cpp

#include<iostream.h>

#define size 80

int count;

class T {

public: T(int n); void next(); int repeat(); void print(); int check(); private: int array[size]; int *pt; };

T::T(int n) { array[0]=n; for(int i(1);i<size;i++) array[i]=0; pt=array; }

void T::next() { int sum=0,d=0,n=*pt; do { d=n%10; sum+=d*d; }while(n=n/10); pt++; *pt=sum; }

int T::repeat() { int i(0); while(&array[i]!=pt)

{ if(array[i]==*pt) return 1; i++; } return 0; }

void T::print() { cout<<"->"<<*pt; }

int T::check() { if(*pt==1) return 1; else return 0; }

void main() { int N; cout<<"Please input an integer larger than zero!\n"; cout<<"Start:"; cin>>N; while(N) { T d(N); cout<<N; do { d.next(); if(!d.repeat()) d.print(); else { if(d.check()) count++; break; } }while(1); cout<<endl; N--; } cout<<"The number of digit-string ending with 1 is: "<<count<<endl; }

测试数据(输入、输出)

第4_2题

算法描述:

模拟图书馆管理系统。每名读者只能借一本书,读者可借书或还书。管理员可对图书和读者信息进行录入、修改和删除。图书信息至少应包括:编号、书名、数量,读者信息至少应包括:编号、姓名、所借图书。可根据图书名称或编号进行图书信息查询,可查询某本书现在被哪些读者借走。 源程序: No4_2.cpp

#include<iostream.h> #include<fstream.h> #include<iomanip.h> #include<stdio.h> #include<string.h> #include<stdlib.h> #include"book.h" #include"reader.h"

char ini[10]={0}; char key[10]={0};

void menu();

void Administrator(); void Reader(); void M_book(); void M_reader();

void Menu() { cout<<"\n\n\n图书馆管理系统主菜单 "<<endl; cout<<"\n\n\n1.管理员 "<<endl; cout<<"\n2.读者 "<<endl; cout<<"\n3.退出系统 "<<endl; cout<<"\n\n请选择(1/2/3)并按回车键确认:"; int choose; cin>>choose; switch(choose) { case 1: Administrator();break; case 2: Reader();break; case 3: exit(0); default: { system("cls");

cout<<"\n\n\n输入错误!请选择(1/2/3)并按回车键确认:"<<endl; cin.clear(); cin.ignore(80,'\n'); Menu(); } } }

void Reader() { system("cls"); cout<<"\n您好,读者!\n\n"; char flag='Y'; do { User::find_t(); cout<<"\n\n要继续查询吗(y/n)"; cin>>flag; system("cls"); }while(toupper(flag)=='Y'); Menu(); }

void Administrator() { if(strcmp(key,ini)==0) { system("cls"); char temp1[10],temp2[10]; cout<<"\n第一次登陆,请设置您的密码(密码长度小于10位):"; cin>>temp1; cout<<"\n请再次输入:"; cin>>temp2; if(strcmp(temp1,temp2)==0) { cout<<"\n密码设置成功!\n"<<endl; strcpy(key,temp1); fstream fkey; fkey.open("key.bin",ios::out|ios::binary); fkey.write(key,sizeof(key)); fkey.close(); system("pause"); Administrator(); } else { cout<<"输入不一致!"<<endl; Administrator();

} } else { system("cls"); cout<<" \n\n------------------------------\n"; cout<<" 管理员登陆\n"; cout<<" ------------------------------\n"; char temp[10]; cout<<"\n请输入您的密码:"; cin>>temp; if(strcmp(key,temp)!=0) { cout<<"密码输入错误!"<<endl; system("pause"); Administrator(); } else { cout<<"登陆成功!"<<endl; reg: cout<<"\n\n\n1.图书管理系统"<<endl; cout<<"2.读者管理系统"<<endl; cout<<"\n\n请选择(1/2)并按回车键确认:"; int select; cin>>select; switch(select) { case 1: M_book();break; case 2: M_reader();break; default: { cout<<"请选择(1/2)并按回车键确认:"; cin.clear(); cin.ignore(80,'\n'); goto reg; } } } } }

void M_book() { cout<<"\n\n欢迎使用图书管理系统!"<<endl; do {

cout<<"\n\n\n1.添加图书信息 "<<endl; cout<<"2.输出图书信息 "<<endl; cout<<"3.查找图书信息(按编号索引) "<<endl; cout<<"4.查找图书信息(按书名索引) "<<endl; cout<<"5.修改图书信息(按编号索引) "<<endl; cout<<"6.修改图书信息(按书名索引) "<<endl; cout<<"7.删除图书信息(按编号索引) "<<endl; cout<<"8.删除图书信息(按书名索引) "<<endl; cout<<"9.图书信息排序(按编号排序) "<<endl; cout<<"10.保存数据 "<<endl; cout<<"11.读出数据 "<<endl; cout<<"12.跳转至读者管理系统"<<endl; cout<<"13.返回主菜单 "<<endl; cout<<"14.退出系统 "<<endl; cout<<"\n\n请选择(1~14)并按回车键确认:"; int choose; cin>>choose; switch(choose) { case 1: Book::add();break; case 2: Book::output();break; case 3: Book::find_n();break; case 4: Book::find_t();break; case 5: Book::modify_n();break; case 6: Book::modify_t();break; case 7: Book::delete_n();break; case 8: Book::delete_t();break; case 9: Book::sort();break; case 10: Book::save();break; case 11: Book::load();break; case 12: system("cls");M_reader();break; case 13: Menu();break; case 14: exit(0); } system("cls"); }while(1); }

void M_reader() { cout<<"\n\n欢迎使用读者管理系统!"<<endl; do { cout<<"\n\n\n1.添加读者信息 "<<endl; cout<<"2.输出读者信息 "<<endl; cout<<"3.查找读者信息(按编号索引) "<<endl; cout<<"4.查找读者信息(按书名索引) "<<endl; cout<<"5.修改读者信息(按编号索引) "<<endl; cout<<"6.修改读者信息(按书名索引) "<<endl;

cout<<"7.删除读者信息(按编号索引) "<<endl; cout<<"8.删除读者信息(按书名索引) "<<endl; cout<<"9.读者信息排序(按编号排序) "<<endl; cout<<"10.保存数据 "<<endl; cout<<"11.读出数据 "<<endl; cout<<"12.跳转至图书管理系统 "<<endl; cout<<"13.返回主菜单"<<endl; cout<<"14.退出系统 "<<endl; cout<<"\n\n请选择(1~14)并按回车键确认:"; int choose; cin>>choose; switch(choose) { case 1: User::add();break; case 2: User::output();break; case 3: User::find_n();break; case 4: User::find_t();break; case 5: User::modify_n();break; case 6: User::modify_t();break; case 7: User::delete_n();break; case 8: User::delete_t();break; case 9: User::sort();break; case 10: User::save();break; case 11: User::load();break; case 12: system("cls");M_book();break; case 13: Menu();break; case 14: exit(0); } system("cls"); }while(1); }

int main() { cout<<"LOADING... \n"; fstream fkey; fkey.open("key.bin",ios::in|ios::binary); fkey.read(key,sizeof(key)); fkey.close(); Book::load(); User::load(); system("pause"); system("cls"); Menu(); return EXIT_SUCCESS; }

Book.h

#ifndef BOOK_H

#define BOOK_H

class Book {

public: Book(); static void add(); static void output(); static void find_n(); static void find_t(); static void modify_n(); static void modify_t(); static void delete_n(); static void delete_t(); static void sort(); static void save(); static void load (); Book & operator=(Book & Temp); friend istream & operator >> (istream & scin, Book & Temp); friend ostream & operator << (ostream & scout, Book & Temp); private: char number[8]; char title[20]; int quantity; static int NumBook; };

Book b[100];

int Book::NumBook=0;

void Book::add() { char flag='Y'; while(toupper(flag)=='Y') { cin>>b[NumBook]; NumBook++; cout<<"要继续添加吗(y/n)"; cin>>flag; if(toupper(flag)!='Y') { if(toupper(flag)=='N') { break; } else {

cout<<"错误,请重输:"; cin>>flag; } } } }

void Book::output() { if(NumBook!=0) { cout<<"--------------------------------\n"; cout<<"number title quantity\n"; cout<<"--------------------------------\n"; for(int i=0;i<NumBook;i++) { cout<<b[i]; } } getchar(); }

void Book::find_n() { bool flag=0; char n[8]; Book temp; cout<<"输入你要查找的图书编号: "; cin>>n; for(int j=0;j<NumBook;j++) { if(strcmp(b[j].number,n)==0) { temp=b[j]; flag=1; } } if(flag==0) cout<<"对不起,没有你要查找的图书"<<endl; else { cout<<"--------------------------------\n"; cout<<"number title quantity\n"; cout<<"--------------------------------\n"; cout<<temp; } getchar(); }

本文来源:https://www.bwwdw.com/article/4agj.html

Top