7-2 說反話-增強版
給定一句英語,要求你編寫程序,將句中全部單詞的順序顛倒輸出。git
定義數組 a[500001]; 定義 flag=0,len 字符串長度i,j,ret,count=0 單詞長度 gets(a); len=strlen(a); for i=len-1 To 0 //倒序判斷 if a[i]!=' ' ret=1;count++; end if if a[i]=' '&&ret=1 //一個單詞結束 if flag=1 已輸出過單詞 先空格 end if for j=i+1 To i+count 逐個輸出字符 flag=1; end for 所有初始化 end if if i=0&&ret=1 //第一個單詞單獨判斷 同理輸出 end if end for
#include<stdio.h> #include<string.h> int main() { char a[100][100]; int i,j; int flag=0; char sentense[10000]; gets(sentense); int m=0,n=0; for(i=0;sentense[i];i++) { if(sentense[i]!=' ') { a[m][n]=sentense[i]; m++; //單詞長度累加 } if(m!=0&&sentense[i]==' ') { a[m][n]='\0'; n++;m=0; //有單詞累計,遇到空格 } } for(i=n;i>0;i--) { for(j=0;a[j][i];j++) { printf("%c",a[j][i]);flag=1; } if(flag==1) printf(" "); } for(j=0;a[j][0];j++) printf("%c",a[j][i]); return 0; }
int computerExp(char grade,int *gradeNum) 定義i,j=0,step,n //控制生成的隨即位數&計算步數 定義exp[30], op[5]={'+','-','*','/'}; //枚舉 switch(grade) A:一位兩步 B:兩位一步 C:三位一步 for j=0 To step-1 for j To (i+1)*n-1 exp[j]=rand()%10+'0'; //生成隨機數轉字符錄入 end for exp[j]=op[rand()%*gradeNum]; //對應加減/加減乘除 end for; exp[i]='\0'; 輸出表達式
定義 former latter i,j,pos; for j=0 To j<n-1 former=10*former+exp[pos]-'0'; //轉數字 pos++; end for; for i=0 To i<step-1 for j=0;j<n-1 latter=10*latter+exp[pos]-'0'; //轉數字 pos++; end for end for switch op //運算 case +: case -: case *: case /: if latter=0,跳過此題 return latter;
void Calculation(char x) { switch(x) { case 'A': while(correctNum<10)//若是想一直答下去,就把它修改爲while(1),而後輸入6666照樣能夠退出循環w { printf("當前挑戰等級:青銅\n"); printf("當前答對題數:%d\n",correctNum); printf("tips:輸入6666提早結束遊戲\n\n\n"); x = rand() % 10; /* generate 1-digit random number */ y = rand() % 10; /* generate another 1-digit random number */ kind=rand()%4; randomOperation(kind,x,y); } break; case 'B': while(correctNum<10) { printf("當前挑戰等級:鑽石\n"); printf("當前答對題數:%d\n",correctNum); printf("tips:輸入6666提早結束遊戲\n\n\n"); x = rand() % 90+10; /* generate 1-digit random number */ y = rand() % 90+10; /* generate another 1-digit random number */ kind=rand()%2; randomOperation(kind,x,y); } break; case 'C': while(correctNum<10) { printf("當前挑戰等級:王者\n"); printf("當前答對題數:%d\n",correctNum); printf("tips:輸入6666提早結束遊戲\n\n\n"); x = rand() % 900+100; /* generate 1-digit random number */ y = rand() % 900+100; /* generate another 1-digit random number */ kind=rand()%2;//decide to minus or plus randomOperation(kind,x,y); } break; } }
void Choice(char x,int *correctNum,int *incorrectNum,int *gradeNum)//選擇 { int answer,end; while(*correctNum<10)//若是想一直答下去,就把它修改爲while(1),而後添加一個跳出循環條件就好啦w { again: switch(x) { case 'A':printf("當前挑戰等級:青銅\n");*gradeNum=4;break; case 'B':printf("當前挑戰等級:鑽石\n");*gradeNum=2;break; case 'C':printf("當前挑戰等級:王者\n");*gradeNum=2;break; } printf("當前答對題數:%d\n",*correctNum); printf("tips:輸入任意大於6666可直接結束遊戲\n\n\n"); answer=computerExp(x,gradeNum); if(answer==5555)//除數爲0 { fflush(stdin);//清空輸入緩衝區 system("cls");//empty goto again; } end=Judgement(answer,correctNum,incorrectNum); if(end)break; Pause(); }
#include <stdio.h> #include <stdlib.h> #include <time.h> int kind;//choose to minus or plus etc. int flag=0;//控制是否輸出正確提示語 int END=0;//控制跳出循環 int Num=0;//sum char grade;//choose one to play int wrongTimes=0;//the number of wrong times int correctNum=0;//the number of correct times int incorrectNum=0;//the number of incorrect times int x,y; void Calculation(char x); void Menu(void) ; void plus(int x,int y); void divide(int x,int y); void minus(int x,int y); void multiply(int x,int y); void correctMessage( void ); void incorrectMessage( void ); int randomOperation(int kind,int x,int y); void Ending(void);//ending message
#include <stdio.h> #include <stdlib.h> #include <time.h> void Pause(void); char Menu(void) ; int Calculation(char exp[],int step,int n); int Judgement(int answer,int *correctNum,int *incorrectNum); int computerExp(char grade,int *gradeNum); void Ending(int *correctNum,int *incorrectNum);//ending message void correctMessage( int *correctNum ); void incorrectMessage(int *wrongTimes,int *incorrectNum,int answer); void Choice(char x,int *correctNum,int *incorrectNum,int *gradeNum);
int Calculation(char exp[],int step,int n) { int former=0,latter=0; int i,j,pos=0; for(j=0;j<n-1;j++) { former=10*former+exp[pos]-'0'; pos++; } pos++; //jump the op position for(i=0;i<step-1;i++) { for(j=0;j<n-1;j++) { latter=10*latter+exp[pos]-'0';//change into number pos++; } switch(exp[i*n+n-1]) { case '+':former+=latter;latter=0,pos++;break; case '-':former-=latter;latter=0;pos++;break; case '*':former*=latter;latter=0;pos++;break; case '/':if(latter==0) return former=5555; former/=latter;latter=0;pos++;break; } } return former; }
switch (grade)//等級 { case 'A':n=1;break; case 'B':n=2;break; case 'C':n=3;break; } for(i=0; i<3*n; i++)num[i]=rand()%10; //生成隨機數 for(i=0; i<n; i++)x=10*x+num[i]; //生成第一個隨機數 for(i=n; i<2*n; i++)y=10*y+num[i]; //生成第二個隨機數 for(i=2*n; i<3*n; i++)z=10*z+num[i]; //生成第三個隨機數 for(i=0;i<3*n+3;i++) //錄入字符數組 { if (i==n) { exp[i]=randomOp(); if(exp[i]=='/'&&y==0) { do { for(i=n;i<2*n;i++) { num[i]=rand()%10; y=10*y+num[i]; } }while(y); } else if(exp[i]=='*'||exp[i]=='/') { mark1=1; } } else if(i==2*n+1) { exp[i]=randomOp(); if(exp[i]=='/'&&z==0) { do { for(i=2*n;i<3*n;i++) { num[i]=rand()%10; z=10*z+num[i]; } }while(!z); } else if(exp[i]=='*'||exp[i]=='/') { mark2=1; } } else if (i==3*n+2)exp[i]='='; else { exp[i]=num[j]+'0'; j++; } } exp[i]='\0';//加上終止符
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { if (grade=='A')gradeNum=4;else gradeNum=2; int i,step,j=0,n; printf("你想挑戰的位數:");scanf("%d",&n);n++; printf("你想挑戰的步數:");scanf("%d",&step);step++; char exp[30]; char op[5]={'+','-','*','/'}; for(i=0;i<step;i++) { for(j;j<(i+1)*n-1;j++) exp[j]=rand()%10+'0'; if(i!=step-1)exp[j]=op[rand()%gradeNum]; else exp[j]='=';j++; } exp[j]='\0'; printf("%s",exp); }