C++中string用scanf的输入和~scanf()

1. cin

string s;
while(cin >> s){}
//可以一直执行

2. scanf
定义:

string s;
s.resize();	//设置长度

输入

scanf("%s",&s[0]);

输出

printf("%s\n",s.c_str());	//输出有效字符
cout << s << endl;			//剩余字符用空格补齐

while(~scanf())的判断

  1. 机器数为补码表示
  2. ~ 按位取反
  3. scanf() 返回正确读取到的个数,读取错误返回-1
  4. -1的补码为全1,取反后就全0,退出循环
  5. ~scanf() == (scanf() != EOF)
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
travel Algrithm. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #include <conio.h> struct Path { char pass[25]; //sign passed note. int min_distance; //minmax distance from concurrent note.// int nextcity; //next note from current note. struct Path *next; }; int C[25][25]; int Getmin(struct Path *G[25][25],int i,int num_S,char S[25]); // search G[i][n] linking-node. void Write(int i,int n,char S[25],struct Path *G[25][25],int num_S); // Calculate G[i][num_S] void Travel(int size,int i,int n,char S[25],struct Path *G[25][25],int num_S); // signed V-S node. void Trip(int n,char S[25],struct Path *G[25][25]); // Calculate and output the min_distance. void Printpath(char S[],int path[],struct Path *G[][25],int n); // print the path // S[] is the charater of the node. void main() { int n,i,j; int output[25]; char S[25]; struct Path *G[25][25]; printf("Please enter the number of cities:\n"); // Input the numbers of node scanf("%d",&n); printf("Please enter the matrix:\n"); // Input the maxtric of weight. for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&C[i][j]); printf("The number of cities is:%d\n",n); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%d\t",C[i][j]); printf("\n"); } for(i=0;i<n;i++) S[i]='0'; S[i]='\0'; for(i=0;i<n;i++) { G[i][0]=(struct Path *)malloc(sizeof(struct Path)); strcpy(G[i][0]->pass,S); G[i][0]->min_distance=C[i][0]; G[i][0]->next=NULL; } for(i=0;i<n;i++) for(j=1;j<n;j++) G[i][j]=NULL; Trip(n,S,G); Printpath(S,output,G,n); printf("For the demonstration route:\n"); for(i=0;i<n;i++) printf("%d->",output[i]+1); printf("1\n"); getch(); }
#define _CRT_SECURE_NO_WARNINGS 1 #ifndef __CONTACT_H_ #define __CONTACT_H_ #include<stdio.h> #include<stdlib.h> #include<string.h> #define PEO_MAX 30 #define STUID_MAX 10 #define NAME_MAX 20 #define SEX_MAX 10 #define TEL_MAX 15 typedef struct People { char id[STUID_MAX]; char name[NAME_MAX]; char sex[SEX_MAX]; char tel[TEL_MAX]; }*peo; typedef struct Contact { int count; struct People people[PEO_MAX]; }*pCon; void add_peo(pCon pcon); //添加联系人信息 void show_peo(pCon pcon); //显示指定联系人信息 void find_peo(pCon pcon); //查找联系人信息 void modify_peo(pCon pcon); //修改指定联系人信息 void clear_peo(pCon pcon); //清空联系人信息 void show_menu(); //菜单显示 #endif int search(pCon pcon, char *name) { int i = 0; for (i=0; i < pcon->count; i++) { if (strcmp(name, pcon->people[i].name) == 0) return i; } return -1; } void add_peo(pCon pcon) //添加联系人 { if (pcon->count == PEO_MAX) { printf("The contact has fullen."); return; } printf("please input studentID: "); scanf("%s",(pcon->people[pcon->count]).id); printf("please input name: "); scanf("%s", (pcon->people[pcon->count]).name); printf("please input sex : "); scanf("%s", (pcon->people[pcon->count]).sex); printf("please input tel: "); scanf("%s", (pcon->people[pcon->count]).tel); pcon->count++; } void show_peo(pCon pcon)//显示联系人 { int i = 0; for (; i < pcon->count; i++) { printf("studentID name sex tel \n"); printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } } void find_peo(pCon pcon) //查找联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to find:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } void modify_peo(pCon pcon) //修改指定联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to modify:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("please input studentID: "); scanf("%s", (pcon->people[i]).id); printf("please input name: "); scanf("%s", (pcon->people[i]).name); printf("please input sex : "); scanf("%s", (pcon->people[i]).sex); printf("please input tel: "); scanf("%s", (pcon->people[i]).tel); } void clear_peo(pCon pcon) //清空联系人 { pcon->count = 0; } void show_menu() //菜单显示 { printf("==========通讯录==========\n"); printf(" \n"); printf("*******[1]显示通讯录******\n"); printf("*******[2]查 询******\n"); printf("*******[3]修 改******\n"); printf("*******[4]结 束******\n"); } int main() { struct Contact my_contact; int input = 1; my_contact.count = 0; while (input) { show_menu(); printf("please input:"); scanf("%d", &input;); switch (input) { case 1: add_peo(&my;_contact); break; case 2: show_peo(&my;_contact); break; case 3: find_peo(&my;_contact); break; case 4: modify_peo(&my;_contact); break; case 5: clear_peo(&my;_contact); break; default: break; } } return 0; }
关于c语言和c++的课程成绩信息管理系统,共有将近6000行代码,建议使用vs2012或2010便于管理也可使用VC6.0++环境修改运行但查找麻烦,所有的语言没有脱离c和c++,主要采用模块思想,也可以转换成面向对象型的语言,只要将模块函数写进类。同时学c语言的也可以使用,除了使用cout,cin一些很容易上手的c++代码,相当于printf,scanf,主要为了方便输入输出,不用写%d%c... 详细细节也可以访问,百度文库网址 使用注意事项 有着强大的报错功能。 1 全部采用鼠标点击功能,可以看百度网址图片。 2 录用学生信息的细节选项,如果点击错误信息,再次点击将会取消。 3 附加功能的高级打印功能,如果想改变选项,只需要点击另一个即可,当前的状态就会消失。 4 输入学号为53120101--531215**(其不包括****00,例如53120700)。(可以设置) 5 所有成绩范围为0--99。(可以设置) 6 如果想去掉钢琴曲,直接删除MP3,或者改成其他名字即可。 7 打印直方图可以根据班级的不同,向后移动。 7 如果打印不规范,可能窗口较小,可通过调节窗口大小来打印排名...... 8 请包含student.txt默认文件(文件至少一名学生信息),否则将会程序在进行实质功能作用时意外退出(已在包)。 头文件student.h #ifndef _STUDENT_H_ #define _STUDENT_H_ #include #include HWND hWnd; //来自msdn #define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s \ on line %d\n", __FILE__, GetLastError(), api, __LINE__);} void cls( HANDLE hConsole ) { COORD coordScreen = { 0, 0 }; /* here's where we'll home thecursor */ BOOL bSuccess; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ DWORD dwConSize; /* number of character cells inthe current buffer *//* get the number of character cells in the current buffer */ bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi ); PERR( bSuccess, "GetConsoleScreenBufferInfo" ); dwConSize = csbi.dwSize.X * csbi.dwSize.Y;/* fill the entire screen with blanks */ bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ); PERR( bSuccess, "FillConsoleOutputCharacter" );/* get the current text attribute */ bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi ); PERR( bSuccess, "ConsoleScreenBufferInfo" );/* now set the buffer's attributes accordingly */ bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ); PERR( bSuccess, "FillConsoleOutputAttribute" );/* put the cursor at (0, 0) */ bSuccess = SetConsoleCursorPosition( hConsole, coordScreen ); PERR( bSuccess, "SetConsoleCursorPosition" ); return; } HANDLE hOut; HANDLE hIn; void enter(); void ReadFile(char*str="student.txt"); typedef enum grade{you=95,liang=85,zhong=75,pass=65,nopass=0 } Grade; Grade g1=you; Grade g2=liang; Grade g3=zhong; Grade g4=pass; Grade g5=nopass; void DelClass(); //学生类结构 typedef struct student{ int studentid; char name[20]; char sex[5]; char nation[20]; int biryear; int birmonth; char post[10]; int cyu; int cyushe; int cshe; int cdui; int cduishe; struct student* next; double ave; double wave; } Student; Student *stubegin=NULL; Student* stulast=NULL; int total=0; //课程类结构 typedef struct course{ char obj[30]; int time; int xuefen; int mark; Grade rank; } Course; Course c1; Course c2; Course c3; Course c4; Course c5; void InitCourse(); void AddData(Student*); void AltData(); void AddShuju(int *,Student*); void IntEro(int& ,int,int,int,int); //功能介绍 /*****************************************Loading页面*******************************************/ void input();//输入信息功能 void output();//输出信息功能 void addition();//附加功能 /*****************************************输入信息功能*******************************************/ void AddStudent(); void DelStudent(int); void AltStudent(int); void SaveMessage(); void readfile(); /*****************************************输出信息功能*******************************************/ void CalRankAve(int); void CalRankWave(int); void PrintStudent(int); void PrintCourse(int); void PrintWell(); /*****************************************打印总成绩*******************************************/ void PrintTotal(); void PrintStudentID(); void PrintAve(); void PrintWave(); /*****************************************打印科目成绩*******************************************/ void PrintObj(); void PrintCyu(); void PrintCyushe(); void PrintCshe(); void PrintCdui(); void PrintCuishe(); /*****************************************打印优秀职务单科成绩*******************************************/ void PrintObjYou(); void PrintCyuYou(); void PrintCyusheYou(); void PrintCsheYou(); void PrintCduiYou(); void PrintCuisheYou(); /*********************辅助函数**********************/ void printmark(Student *); void AddStudent1(); void SortAve(); void SortWave(); void SortId(); void SortCyu(); void SortCyushe(); void SortCshe(); void SortCdui(); void SortCduishe(); int GetInt(char* ); int Search(int); void PrintGrade(int); int GetXuefen(int,int); /****计算平均成绩和加权成绩**/ void CalAve(Student*); void CalWave(Student*); /*****************************************附加功能*******************************************/ void PrintHistogram(); void PrintTotalHistogram();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值