C语言:编写一个程序,它从标准输入(终端)读取C源代码,并验证所有的花括号都正确的成对出现

#include <stdio.h>

int main()

{

    char ch;

    int count=0;

    while((ch=getchar())!=EOF)

    {

     if(ch==‘{‘)

     {

      count++;

     } 

     else if(ch==‘}‘)

     {

      if(count==0)

      {

       printf("不成功匹配\n"); 

      }

      else

      {

       count--; 

      }

     } 

    }

    if(count==0)

    {

     printf("成功匹配!\n");

    }

    else

     {

     printf("不成功匹配!\n");

    }

    return 0;

}
时间: 2024-08-05 09:46:01

C语言:编写一个程序,它从标准输入(终端)读取C源代码,并验证所有的花括号都正确的成对出现的相关文章

编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现

(三种if的情况考虑完整,注意解决问题的思想) #include <stdio.h>int main(){   int count=0; char ch; while((ch=getchar())!=EOF)  //直到出现文件结束标志,不再进入循环 {     if(ch=='{')     {          count++;     }     if(ch=='}'&&count==0)    //前无'{',而后有'}',即不匹配     {        print

编写一个程序,它从标准输入(终端)读取C源代码,并验证所有的花括号都正确的成对出现。

#include<stdio.h> int main() { int cou=0; char ch; while((ch=getchar())!='\n') { if(ch=='{') cou++; else if(ch=='}') { if(cou==0) printf("匹配不成功!"); cou--; } } if(cou==0) printf("匹配成功!"); else printf("匹配不成功!"); return 0;

c语言 验证所有的花括号都正确的成对出现。

#include<stdio.h> int main() {   int i,j=0;  char s[100];  printf("输入字符:");  scanf("%s",&s);    for(i=0;s[i];i++)  {      if(s[i]=='}') { if(j==0) { printf("匹配不成功");return 0;} else j++; } else j--;  }  if(j==0)  pri

从标准输入读取C源代码,并验证所有的花括号都正确的成对出现

#include<stdio.h> #include<process.h> int main() { char c = 0; int count = 0; while ((c = getchar()) != EOF) { if (c == '{') { count++; } else if (c == '}'&&count == 0) { printf("花括号不匹配!"); return 0; } else if (c == '}'&&

编写一个程序,从标准输入中读取若干string对象并查找连续重复出现的单词。所谓连续重复出现的意思是:一个单词后面紧跟着这个单词本身。要求记录连续重复出现的最大次数以及对应的单词

#include<iostream> #include<string> #include<vector> using namespace std; int main() { string maxStr,Str1,Str2; int maxNum,Num1,Num2; if(cin>>Str1) Num1=1; maxNum=0; while(cin>>Str2) { Num2=1; if(Str2==Str1) Num2=++Num1; if(N

编写一个程序,从标准输入读取几行输入。每行输入都要打印到标准输出上,前面加上行号。

编写一个程序,从标准输入读取几行输入.每行输入都要打印到标准输出上,前面加上行号. 在编写这个程序的时候要使这个程序能够处理的输入行的长度没有限制. #include <stdio.h> #include <stdlib.h> int main() { char ch = '0'; int n = 1; int flag = 1; while (1) { printf("please input the line: "); do { scanf("%c

《用C++语言编写一个程序,求PI的值》

1 //编写一个C++程序求PI的值 2 /* 3 PI=16arctan(1/5)-4arctan(1/239) 4 其中arctan用如下形式的极数计算: 5 arctan=x-(x^3/3)+(x^5/7)-(x^7/7)+... 6 */ 7 #include<iostream> 8 using namespace std; 9 double arctan(double x){ 10 double sqr = x*x; 11 double e = x; 12 double r = 0;

编写一个程序,从标准输入读取几行输入,设置行号

#include<stdio.h> int main() {     int ch;     int i=1,j=1;     printf("请输入:\n");     while((ch=getchar()) != EOF)     {         if(j==1)         {             printf("%d.",i);             i++;             j++;         }         

C语言编写一个&#39;*&#39;金字塔的程序

olj3xg踩系凭珊氏菲<http://weibo.com/LXzpRp/230927982968498303012864> 8e8pxe栏胸俾侔善胶<http://weibo.com/20180414pp/230927983255722881978368> e46952阉友河痹敲呕<http://weibo.com/keMXfnmp/230927983173935224852480> 8fpln0计孪甘摆谱匕<http://weibo.com/20180414p