用C++程序编写

作者&投稿:祖印 (若有异议请与网页底部的电邮联系)
C/C++程序编写~

这是c的:
#include
#include
void main()
{
char str[50],*pstr=str;
int i;
printf("请输入字符串:
");
scanf("%s",str);
for(i=0;i<strlen(str);i++)
if(*(pstr+i)>='A'&&*(pstr+i)<='Z')
*(pstr+i)+=32;
printf("转换后:
%s
",pstr);
}
这是C++的:
#include
#include
void main()
{
char str[50];
int i;
cout<<"请输入字符串:
";
cin>>str;
for(i=0;i<strlen(str);i++)
if(str[i]>='A'&&str[i]<='Z')
str[i]=str[i]+32;
cout<<"转换后:
"<<str<<endl;
}

跟编写万年历类似,主要是计算从2000年1月1日到输入的时间有多少天(需要判断闰年),之后就是天数%5,余数1、2、3打渔,4、0晒网

以前用过的一个程序,大致功能差不多,自己做点局部的修改就可以用:

main() 完成各种提示与主操作输入输出

History:

<author> <time> <version > <desc>

*****************************************************/
#include <iostream>
#include <assert.h>
#include <string>
#include <cstring>
#include <fstream>
#include <iomanip>
using namespace std;
class telist;
class Node
{
private:
char name[30];
char workplace[50];
char hometel[50];
char mobiletel[50];
Node *next;
public:
friend class telist;
friend void load(telist&);
};
class telist
{
private:
Node *head;
Node *tail;
public:
telist()
{
head=tail=NULL;
}
telist(Node* h,Node *t):tail(t),head(h){}
void add();
void del();
void show();
Node* search();
void change();
void store();
Node* find1();
Node* find2();
friend void load(telist&);
};
void operate(string ,telist &);
void load(telist &);
int main()
{
system("cls");
cout<<"欢迎使用电话簿管理系统:\n";
telist tele;
load(tele);
string strord;
do
{
cout<<"输入你的操作:\n";
cout<<"1.加入新的电话记录。\n";
cout<<"2.显示输出所有人的移动电话\n";
cout<<"3.按姓名或单位查询家庭电话\n";
cout<<"4.通过姓名和单位确定个人,修改其电话信息并存盘.\n";
cout<<"5.删除一个人信息并存盘。\n";
cout<<"0.退出程序。\n";
cin>>strord;
operate(strord,tele);
system("cls");
}while(strord!="0") ;
system("pause");
system("cls");
return 0;
}
void operate(string str,telist &t)
{
if(str=="1")
t.add();
else if(str=="2")
t.show();
else if(str=="3")
t.search();
else if(str=="4")
t.change();
else if(str=="5")
t.del();
else if(str=="0")
t.store();
else cout<<"输入错误!\n";
system("pause");
}

void load(telist &t)
{
ifstream infile("电话簿.dat",ios::in|ios::binary);
if(infile == NULL)
{
cerr<<"打开电话簿失败!!\n";
t.head=t.tail=NULL;
return;
}
if(!infile.eof())
{
cout<<"没有数据\n";
t.head=t.tail=NULL;
return ;
}
Node *temp=new Node;
t.head=temp;
Node *temp2;
infile.read ((char*)temp,sizeof(Node));
while(!infile.eof()||temp->next==NULL) //
{
temp->next=new Node;
temp2=temp;
temp=temp->next;
infile.read ((char*)temp,sizeof(*temp));
t.tail=temp;
}
temp->next=NULL;
delete(temp);
t.tail=temp2;
t.tail->next=NULL;
// delete temp;
assert(t.tail->next==NULL);
infile.close();
// delete infile;
}

void telist::add()
{
if(head==tail&&tail==NULL)
{
Node * newnode;
newnode=head=tail=new Node;
if(!newnode)
{
cout<<"警告:内存分配失败!\n";
return;
}//if
cout<<"姓名:\n";
cin>>newnode->name;
cout<<"工作单位:\n";
cin>>newnode->workplace;
cout<<"家庭电话:\n";
cin>>newnode->hometel;
cout<<"移动电话:\n";
cin>>newnode->mobiletel;
newnode->next=NULL;
}//if
else{

Node *temp;
temp=new Node;
tail->next = temp;
if(!temp)
{
cout<<"警告:内存分配失败!\n";
return;
}
cout<<"姓名:\n";
cin>>temp->name;
cout<<"工作单位:\n";
cin>>temp->workplace;
cout<<"家庭电话:\n";
cin>>temp->hometel;
cout<<"移动电话:\n";
cin>>temp->mobiletel;
temp->next=NULL;
tail=temp;
}//else
}
void telist::del()
{
cout<<"先确定你要删除的人:\n";
Node *temp=search();
Node *p=head;
if(temp==NULL)
{
cout<<"没找到要找的人!\n";
return ;
}
if(p==temp)
{
cout<<"确定删除?(y或n)";
char ord1;
cin>>ord1;
if(ord1=='N'||ord1=='n') return;
if(ord1=='Y'||ord1=='y')
{
head=head->next;
if(p==tail)
tail=NULL;//wenti zaizheli
}
}
else
{
for(;(p->next)!=temp;)
{
p=p->next;
}
cout<<"确定删除?(y或n)";
char ord;
cin>>ord;
if(ord=='N'||ord=='n')return;
if(ord=='Y'||ord=='y')//if1
{
if(temp==tail)//if2
{
delete temp;
tail=p;

}//if2
else
{
p->next=temp->next;
delete temp;
}//else
}//if1
}//else
store();
}//del
void telist::show()
{
Node *temp=head;
for(;temp!=NULL;)
{
cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace;
cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl;
temp=temp->next;
}
return;
}
Node* telist::search()
{
// char sname[30];
// char sworkplace[50];
int ord;
cout<<"按照什么查询?\n";
cout<<"1.名字。\n";
cout<<"2.工作单位。\n";
cin>>ord;
for(;!cin;)
{
cout<<"error!";
cin>>ord;
}
switch(ord)
{
case 1:return(find1());break;
case 2:return(find2());break;
default:cout<<"警告:操作错误!\n";return 0;
}
}
Node* telist::find1()
{
char sn[30];
cout<<"请输入姓名:\n";
cin>>sn;
cout<<endl;
Node *temp=head;
for(;temp;temp=temp->next)
{
if(!strcmp(sn,temp->name))
{
cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace;
cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl;
return temp;
}
if(temp->next==NULL)
{
cout<<"NO FIND!\n";
return NULL;
}
else return NULL;
}
}
Node* telist::find2()
{
char sw[50];
cout<<"请输入工作单位:\n";
cin>>sw;
cout<<endl;
Node *temp=head;
for(;temp;temp=temp->next)
{
if(!strcmp(sw,temp->workplace))
{
cout<<setw(15)<<temp->name<<setw(15)<<temp->workplace;
cout<<setw(15)<<temp->hometel<<setw(15)<<temp->mobiletel<<endl;
return temp;
break;
}
if(temp->next==NULL)
{
cout<<"NO FIND!\n";
return NULL;
}
else return NULL;
}
}

void telist::store()
{
Node * temp=head;
ofstream outfile("电话簿.dat",ios::out|ios::binary);
if(!outfile)
{
cout<<"打开文件失败!\n";
return ;
}
for(;temp;)
{
outfile.write ((char *)temp,sizeof(*temp));
temp=temp->next ;
}
outfile.close();

}

void telist::change()
{
Node *temp=search();
cout<<"请输入新的记录:\n";
cout<<"姓名:\n";
cin>>temp->name;
cout<<"工作单位:\n";
cin>>temp->workplace;
cout<<"家庭电话:\n";
cin>>temp->hometel;
cout<<"移动电话:\n";
cin>>temp->mobiletel;
store();
}


C语言编写程序的方法是什么?
C语言编写程序的方法:visual c++6.0 报错比较准确,但比较难用。是微软推出的一款编译器,是一个功能强大的可视化软件开发工具。Turbo C 2.0 是dos环境下的,比较好用,但不支持复制,粘贴等功能,比较不好用,要记住常用的几个快捷键。win-tc 窗口下的tc,比较好用,界面简洁,美观。适合编一些...

c语言编程怎样运行程序?
运行程序步骤:1.编辑:输入源程序并存盘(.C)2.编译:将源程序翻译为目标文件(.OBJ)3.链接:将目标文件生成可执行文件( .EXE)4.运行:执行.EXE文件,得到运行结果。上机1 C语言简单程序的编写和调试

用C语言编写程序必须经过哪些步骤
对 C)编写C语言程序一般应该经历下面的几个基本步骤:确定程序要完成的目标和功能,进行分析和设计;编写程序;编辑、编译、链接程序;运行\/执行、测试和调试程序;程序的维护(修改程序中的Bug、完善和扩充程序的功能等)。对 D)C语言有很多种开发环境,如:BC、GCC、VC等。其中的Visual C++提供一个集...

如何编写C语言程序?
2.快捷键“CTRL+N”建立新源代码。3.输入源代码,下面给出最简单的Hello,world源代码:include <stdio.h> int main( ){ printf("Hello,World\\n");return 0;} 4.按下F11编译并且运行源代码,得到运行结果:5.点击任意键返回源代码编辑界面可以继续进行开发,接下来就是C语言语法的学习了。

用c语言编程:编写一个程序,输入4个数,求其平均值
程序代码如下:直接编译,程序输出结果中任意输入4个数字,程序执行结果如下图所示:

c语言简单程序编写?
include<stdio.h> int main(){ double num1, num2, result, re;char sign, euq;printf("Enter an expression: ");while(scanf("%lf%c%lf%c%lf", &num1,&sign,&num2,&euq,&result) == 5){ switch(sign){ case '+':re = num1 + num2;break;case '-':re = num1 - num...

c语言 编写程序
链表创建使用creat函数,每调用一次创建一个节点。第一次调用函数会自己创建头节点内存。打印输出部分,只要打印第一个节点,只要stuHead->next就可以了。我这里写循环里,是演示遍历链表,你把break去掉就是打印整个链表。include<stdio.h> include<malloc.h> typedef struct stuInfo { int id;\/\/学号 ...

应用c语言编写程序
c语言编译程序属于系统软件。编译程序(Compiler,compilingprogram)也称为编译器,是指把用高级程序设计语言书写的源程序,翻译成等价的机器语言格式目标程序的翻译程序。这里的编译程序是一种动作,是根据编译原理技术,由高级程序设计语言编译器翻译成机器语言二进制代码行为,因此它是系统软件。

C语言是什么? 要怎么编 ? 用什么编?
C语言的模块化程序结构用函数来实现,即将复杂的C程序分为若干模块,每个模块都编写成一个C函数,然后通过主函数调用函数及函数调用函数来实现一大型问题的C程序编写,因此常说:C程序=主函数+子函数。 因此,对函数的定义、调用、值的返回等中要尤其注重理解和应用,并通过上机调试加以巩固。三.掌握一些简单的算法编程...

C语言:下面要求的C程序怎么编?
先定义一个字符型数组,把这一串字符串都进去,以后撤出他的串长,然后对这个字符串以字符为单位进行一次冒泡排序就可以了。具体的程序代码和运行情况见图片。程序代码文本:include<stdio.h> int main(){ int i,j,n;char s[100],t;scanf("%s",s);for(n=0; s[n]; n++);for(i=0; i<...

呼图壁县19379449865: 用C++ 编写程序 -
濯申伊可: 一:#includevoid main(){ int a[5]; int temp = 0; for(int i=0;ia[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } for(int i=0;iint BigSum(int n){ int sum = 0; int big = 1; for(int i=1;iclass student{public: double phy; double math; char * name;}void main(){ student stu[5]; for(int i=0;i

呼图壁县19379449865: 用C++语言编写一个程序 -
濯申伊可: #include using namespace std; template T COUNT(char op,T a,T b) { switch(op) {case '+':return (a+b);break; case '-':return (a-b);break; case '*':return (a*b);break; case '/':return (a/b);break; default:cout } } int main() { int a,b;//数据类型可以自己选定...

呼图壁县19379449865: 用C++语言编写程序 +
濯申伊可: #include<bits/stdc++.h>using namespace std;int main(){ cout<<"* * * * * * * * * * "<<endl; cout<<" * * * * * * * * * *"<<endl; cout<<"* * * * * * * * * * "<<endl; cout<<" * * * * * * * * * *"<<endl; cout<<"* * * * * * * * * * "<<endl; cout<<" * * * * * * * * * *"<<endl; cout<<"* * * * * * * * * * "<<endl; return 0;}

呼图壁县19379449865: 用c++语言编写一个程序
濯申伊可: #include<iostream> #include<string> using namespace std; int twosqu(int x) { int result=1; if(x!=0) for(i=0;i<x;i++) result=result*2; return result; } int main() { int sum=0; string bnum; cin>>bnum; int j=0; for(j=0;j<8;j++) sum+=(bnum[j+1]-30)*twosqu(j); ...

呼图壁县19379449865: 用c++编写程序 -
濯申伊可: #include<stdio.h> int n; int a[11]; int main(){ void dfs(int t,int sum); printf("输入n: "); scanf("%d",&n); a[0]=n-1; dfs(1,n); return 0; } void dfs(int t,int sum){ int i; if(sum==0){ for(i=1;i<t;i++) printf("%d ",a[i]); printf("\n"); return ; } for(i=a[t-1];i>0;i--) if(sum>=i){ a[t]=i; dfs(t+1,sum-i); } }

呼图壁县19379449865: 用C++编写程序
濯申伊可: #include<iostream>using namespace std; void main() { int y,m,d; int x[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//定义每个月的天数 cout<<"输入年 月 日,用空格分开:"; cin>>y>>m>>d; if(y%4==0&&y%100!=0||y%400==0)x[2]=29;//闰...

呼图壁县19379449865: 用C++编写一个程序 -
濯申伊可: #include using namespace std; void check(char input, double &a, double &y) { int op;char err; if(input=='+') op=1; else if(input=='-') op=2; else if(input=='*') op=3; else if(input=='/'&&y!=0) op=4; else if(input=='/'&&y==0) op=-2; else op=-3; if(op==1) ...

呼图壁县19379449865: 用C++编写一套程序 -
濯申伊可: template <class T> double aver(T *a, int n) { int i; double r = 0; for(i = 0; i < n; i ++) r += a[i]; r/=n; return r; }

呼图壁县19379449865: 用C++编写程序 -
濯申伊可: #include using namespace std;int main(){for(int x=100; x<200; x++){if(x%3==0 && x%5==0 && x%7==0){cout<<<endl;}}return 0;}这样行不行 直接写的 没测试...

呼图壁县19379449865: c++编写一程序
濯申伊可: #include<stdio.h> #include<string.h> void main() { char string1[200]; //用于存放输入的字符串 char string2[200]; //用于存放倒序后的字符串 int invertion(char *ch1, char *ch2); //声明函数 printf("Please input a sentences:\n"); gets(string1); ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网